blob: d80e4a17a16ec134a5577feb5b1774a0ab22ca0a [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 Mautner05d62722013-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 Mautner05d62722013-02-11 09:39:27 -080031import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070032import android.app.IActivityManager;
Craig Mautnercae015f2013-02-08 14:31:27 -080033import android.app.IThumbnailReceiver;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070034import android.app.IThumbnailRetriever;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070035import android.app.IApplicationThread;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070036import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080037import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import android.app.IActivityManager.WaitResult;
39import android.content.ComponentName;
40import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070043import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080045import android.content.res.Resources;
46import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080047import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080048import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070050import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.os.Handler;
52import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090053import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.os.Message;
55import android.os.PowerManager;
56import android.os.RemoteException;
57import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070058import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070061import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062
Craig Mautnercae015f2013-02-08 14:31:27 -080063import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080064import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import java.lang.ref.WeakReference;
66import java.util.ArrayList;
67import java.util.Iterator;
68import java.util.List;
69
70/**
71 * State and management of a single stack of activities.
72 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070073final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070075 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
77 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
Craig Mautner29219d92013-04-16 20:19:12 -070078 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
80 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
81 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
82 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
83 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070084 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautnera9a3fb12013-04-18 10:01:00 -070085 static final boolean DEBUG_STACK = ActivityManagerService.DEBUG_STACK;
Craig Mautner9658b312013-02-28 10:55:59 -080086
Craig Mautner2420ead2013-04-01 17:13:20 -070087 static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES;
88 static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE;
89 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
90 static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070091
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094 // How long we wait until giving up on the last activity telling us it
95 // is idle.
96 static final int IDLE_TIMEOUT = 10*1000;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070097
98 // Ticks during which we check progress while waiting for an app to launch.
99 static final int LAUNCH_TICK = 500;
100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700101 // How long we wait until giving up on the last activity to pause. This
102 // is short because it directly impacts the responsiveness of starting the
103 // next activity.
104 static final int PAUSE_TIMEOUT = 500;
105
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700106 // How long we wait for the activity to tell us it has stopped before
107 // giving up. This is a good amount of time because we really need this
108 // from the application in order to get its saved state.
109 static final int STOP_TIMEOUT = 10*1000;
110
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800111 // How long we can hold the sleep wake lock before giving up.
112 static final int SLEEP_TIMEOUT = 5*1000;
113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700114 // How long we can hold the launch wake lock before giving up.
115 static final int LAUNCH_TIMEOUT = 10*1000;
116
117 // How long we wait until giving up on an activity telling us it has
118 // finished destroying itself.
119 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800122 // disabled.
123 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700125 // How long between activity launches that we consider safe to not warn
126 // the user about an unexpected activity being launched on top.
127 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 // Set to false to disable the preview that is shown while a new activity
130 // is being started.
131 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133 enum ActivityState {
134 INITIALIZING,
135 RESUMED,
136 PAUSING,
137 PAUSED,
138 STOPPING,
139 STOPPED,
140 FINISHING,
141 DESTROYING,
142 DESTROYED
143 }
144
145 final ActivityManagerService mService;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800146
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700147 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 /**
150 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151 * running) activities. It contains #TaskRecord objects.
152 */
153 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
154
155 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156 * Used for validating app tokens with window manager.
157 */
Craig Mautnerb1fd65c2013-02-05 13:34:57 -0800158 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 /**
161 * List of running activities, sorted by recent usage.
162 * The first entry in the list is the least recently used.
163 * It contains HistoryRecord objects.
164 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700165 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800168 * List of activities that are in the process of going to sleep.
169 */
170 final ArrayList<ActivityRecord> mGoingToSleepActivities
171 = new ArrayList<ActivityRecord>();
172
173 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 * Animations that for the current transition have requested not to
175 * be considered for the transition animation.
176 */
177 final ArrayList<ActivityRecord> mNoAnimActivities
178 = new ArrayList<ActivityRecord>();
179
180 /**
181 * List of activities that are ready to be finished, but waiting
182 * for the previous activity to settle down before doing so. It contains
183 * HistoryRecord objects.
184 */
185 final ArrayList<ActivityRecord> mFinishingActivities
186 = new ArrayList<ActivityRecord>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800187
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700188 /**
189 * List of people waiting to find out about the next launched activity.
190 */
191 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
192 = new ArrayList<IActivityManager.WaitResult>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 /**
195 * List of people waiting to find out about the next visible activity.
196 */
197 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
198 = new ArrayList<IActivityManager.WaitResult>();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700199
200 final ArrayList<UserStartedState> mStartingUsers
201 = new ArrayList<UserStartedState>();
202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 /**
204 * Set when the system is going to sleep, until we have
205 * successfully paused the current activity and released our wake lock.
206 * At that point the system is allowed to actually sleep.
207 */
208 final PowerManager.WakeLock mGoingToSleep;
209
210 /**
211 * We don't want to allow the device to go to sleep while in the process
212 * of launching an activity. This is primarily to allow alarm intent
213 * receivers to launch an activity and get that to run before the device
214 * goes back to sleep.
215 */
216 final PowerManager.WakeLock mLaunchingActivity;
217
218 /**
219 * When we are in the process of pausing an activity, before starting the
220 * next one, this variable holds the activity that is currently being paused.
221 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800222 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700223
224 /**
225 * This is the last activity that we put into the paused state. This is
226 * used to determine if we need to do an activity transition while sleeping,
227 * when we normally hold the top activity paused.
228 */
229 ActivityRecord mLastPausedActivity = null;
230
231 /**
232 * Current activity that is resumed, or null if there is none.
233 */
234 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700236 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700237 * This is the last activity that has been started. It is only used to
238 * identify when multiple activities are started at once so that the user
239 * can be warned they may not be in the activity they think they are.
240 */
241 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800242
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700243 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700244 * Set when we know we are going to be calling updateConfiguration()
245 * soon, so want to skip intermediate config checks.
246 */
247 boolean mConfigWillChange;
248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800250
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800251 /**
252 * Set when we have taken too long waiting to go to sleep.
253 */
254 boolean mSleepTimeout = false;
255
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700256 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800257 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
258 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
259 */
260 private ActivityRecord mLastScreenshotActivity = null;
261 private Bitmap mLastScreenshotBitmap = null;
262
Craig Mautnercae015f2013-02-08 14:31:27 -0800263 /**
264 * List of ActivityRecord objects that have been finished and must
265 * still report back to a pending thumbnail receiver.
266 */
267 private final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>();
268
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800269 int mThumbnailWidth = -1;
270 int mThumbnailHeight = -1;
271
Amith Yamasani742a6712011-05-04 14:49:28 -0700272 private int mCurrentUser;
273
Craig Mautnerc00204b2013-03-05 15:02:14 -0800274 final int mStackId;
275
Craig Mautner27084302013-03-25 08:05:25 -0700276 /** Run all ActivityStacks through this */
277 final ActivityStackSupervisor mStackSupervisor;
278
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800279 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
280 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
281 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
282 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
283 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
284 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
285 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700286 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700287 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700288 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
289
290 static class ScheduleDestroyArgs {
291 final ProcessRecord mOwner;
292 final boolean mOomAdj;
293 final String mReason;
294 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
295 mOwner = owner;
296 mOomAdj = oomAdj;
297 mReason = reason;
298 }
299 }
300
Zoran Marcetaf958b322012-08-09 20:27:12 +0900301 final Handler mHandler;
302
303 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 //public Handler() {
305 // if (localLOGV) Slog.v(TAG, "Handler started!");
306 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900307 public ActivityStackHandler(Looper looper) {
308 super(looper);
309 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700310
Zoran Marcetaf958b322012-08-09 20:27:12 +0900311 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700312 public void handleMessage(Message msg) {
313 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800314 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700315 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700316 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700317 Slog.w(TAG, "Sleep timeout! Sleeping now.");
318 mSleepTimeout = true;
319 checkReadyForSleepLocked();
320 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800321 }
322 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700323 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800324 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700325 // We don't at this point know if the activity is fullscreen,
326 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800327 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700328 synchronized (mService) {
329 if (r.app != null) {
330 mService.logAppTooSlow(r.app, r.pauseTime,
331 "pausing " + r);
332 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700333
Craig Mautnerd2328952013-03-05 12:46:26 -0800334 activityPausedLocked(r != null ? r.appToken : null, true);
335 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 } break;
337 case IDLE_TIMEOUT_MSG: {
338 if (mService.mDidDexOpt) {
339 mService.mDidDexOpt = false;
340 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
341 nmsg.obj = msg.obj;
342 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
343 return;
344 }
345 // We don't at this point know if the activity is fullscreen,
346 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800347 ActivityRecord r = (ActivityRecord)msg.obj;
348 Slog.w(TAG, "Activity idle timeout for " + r);
Craig Mautner6170f732013-04-02 13:05:23 -0700349 synchronized (mService) {
Craig Mautnere79d4262013-04-01 19:01:53 -0700350 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700352 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700353 case LAUNCH_TICK_MSG: {
354 ActivityRecord r = (ActivityRecord)msg.obj;
355 synchronized (mService) {
356 if (r.continueLaunchTickingLocked()) {
357 mService.logAppTooSlow(r.app, r.launchTickTime,
358 "launching " + r);
359 }
360 }
361 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800363 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700364 // We don't at this point know if the activity is fullscreen,
365 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800366 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800367 synchronized (mService) {
368 activityDestroyedLocked(r != null ? r.appToken : null);
369 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700370 } break;
371 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800372 ActivityRecord r = (ActivityRecord)msg.obj;
Craig Mautnere79d4262013-04-01 19:01:53 -0700373 synchronized (mService) {
374 activityIdleInternalLocked(r != null ? r.appToken : null, false, null);
375 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 } break;
377 case LAUNCH_TIMEOUT_MSG: {
378 if (mService.mDidDexOpt) {
379 mService.mDidDexOpt = false;
380 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
381 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
382 return;
383 }
384 synchronized (mService) {
385 if (mLaunchingActivity.isHeld()) {
386 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
387 mLaunchingActivity.release();
388 }
389 }
390 } break;
391 case RESUME_TOP_ACTIVITY_MSG: {
392 synchronized (mService) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700393 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 }
395 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700396 case STOP_TIMEOUT_MSG: {
397 ActivityRecord r = (ActivityRecord)msg.obj;
398 // We don't at this point know if the activity is fullscreen,
399 // so we need to be conservative and assume it isn't.
400 Slog.w(TAG, "Activity stop timeout for " + r);
401 synchronized (mService) {
402 if (r.isInHistory()) {
403 activityStoppedLocked(r, null, null, null);
404 }
405 }
406 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700407 case DESTROY_ACTIVITIES_MSG: {
408 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
409 synchronized (mService) {
410 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
411 }
412 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700413 }
414 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800415 }
416
Craig Mautner000f0022013-02-26 15:04:29 -0800417 private int numActivities() {
418 int count = 0;
419 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
420 count += mTaskHistory.get(taskNdx).mActivities.size();
421 }
422 return count;
423 }
424
Craig Mautner2219a1b2013-03-25 09:44:30 -0700425 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId,
Craig Mautner2420ead2013-04-01 17:13:20 -0700426 ActivityStackSupervisor supervisor, int userId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900427 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 mService = service;
429 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700430 PowerManager pm =
431 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
432 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
433 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
434 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800435 mStackId = stackId;
Craig Mautner27084302013-03-25 08:05:25 -0700436 mStackSupervisor = supervisor;
Craig Mautner2420ead2013-04-01 17:13:20 -0700437 mCurrentUser = userId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 }
Craig Mautner5962b122012-10-05 14:45:52 -0700439
440 private boolean okToShow(ActivityRecord r) {
441 return r.userId == mCurrentUser
442 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
443 }
444
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700445 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800446 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
447 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800448 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800449 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
450 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 if (!r.finishing && r != notTop && okToShow(r)) {
452 return r;
453 }
454 }
455 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 return null;
457 }
458
459 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800460 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
461 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800462 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800463 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
464 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800465 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
466 return r;
467 }
468 }
469 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700470 return null;
471 }
472
473 /**
474 * This is a simplified version of topRunningActivityLocked that provides a number of
475 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800476 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700477 * @param token If non-null, any history records matching this token will be skipped.
478 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800479 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700480 * @return Returns the HistoryRecord of the next activity on the stack.
481 */
482 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800483 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
484 TaskRecord task = mTaskHistory.get(taskNdx);
485 if (task.taskId == taskId) {
486 continue;
487 }
488 ArrayList<ActivityRecord> activities = task.mActivities;
489 for (int i = activities.size() - 1; i >= 0; --i) {
490 final ActivityRecord r = activities.get(i);
491 // Note: the taskId check depends on real taskId fields being non-zero
492 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
493 return r;
494 }
495 }
496 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 return null;
498 }
499
Craig Mautner8849a5e2013-04-02 16:41:03 -0700500 final ActivityRecord topActivity() {
501 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700502 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700503 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
504 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
505 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700506 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700507 }
508 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700509 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700510 }
511
Craig Mautnerd2328952013-03-05 12:46:26 -0800512 TaskRecord taskForIdLocked(int id) {
513 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
514 final TaskRecord task = mTaskHistory.get(taskNdx);
515 if (task.taskId == id) {
516 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800517 }
518 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700519 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700520 }
521
Craig Mautnerd2328952013-03-05 12:46:26 -0800522 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800523 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800524 if (r != null) {
525 final TaskRecord task = r.task;
526 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
527 if (task.stack != this) Slog.w(TAG,
528 "Illegal state! task does not point to stack it is in.");
529 return r;
530 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800531 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800532 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800533 }
534
Craig Mautner2420ead2013-04-01 17:13:20 -0700535 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700536 final boolean hadit = mLRUActivities.remove(r);
537 mLRUActivities.add(r);
538 return hadit;
539 }
540
Craig Mautnerde4ef022013-04-07 19:01:33 -0700541 final boolean isHomeStack() {
542 return mStackId == HOME_STACK_ID;
543 }
544
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700545 /**
546 * Returns the top activity in any existing task matching the given
547 * Intent. Returns null if no such task is found.
548 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700549 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700550 ComponentName cls = intent.getComponent();
551 if (info.targetActivity != null) {
552 cls = new ComponentName(info.packageName, info.targetActivity);
553 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700554 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800555
556 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
557 final TaskRecord task = mTaskHistory.get(taskNdx);
558 final ActivityRecord r = task.getTopActivity();
559 if (r == null || r.finishing || r.userId != userId ||
560 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
561 continue;
562 }
563
564 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
565 // + "/aff=" + r.task.affinity + " to new cls="
566 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
567 if (task.affinity != null) {
568 if (task.affinity.equals(info.taskAffinity)) {
569 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700570 return r;
571 }
Craig Mautner000f0022013-02-26 15:04:29 -0800572 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
573 //Slog.i(TAG, "Found matching class!");
574 //dump();
575 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
576 return r;
577 } else if (task.affinityIntent != null
578 && task.affinityIntent.getComponent().equals(cls)) {
579 //Slog.i(TAG, "Found matching class!");
580 //dump();
581 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
582 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700583 }
584 }
585
586 return null;
587 }
588
589 /**
590 * Returns the first activity (starting from the top of the stack) that
591 * is the same as the given activity. Returns null if no such activity
592 * is found.
593 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700594 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700595 ComponentName cls = intent.getComponent();
596 if (info.targetActivity != null) {
597 cls = new ComponentName(info.packageName, info.targetActivity);
598 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700599 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700600
Craig Mautner000f0022013-02-26 15:04:29 -0800601 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
602 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
603 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
604 ActivityRecord r = activities.get(activityNdx);
605 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700606 //Slog.i(TAG, "Found matching class!");
607 //dump();
608 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
609 return r;
610 }
611 }
612 }
613
614 return null;
615 }
616
Amith Yamasani742a6712011-05-04 14:49:28 -0700617 /*
618 * Move the activities around in the stack to bring a user to the foreground.
619 * @return whether there are any activities for the specified user.
620 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700621 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800622 if (VALIDATE_TOKENS) {
623 validateAppTokensLocked();
624 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700625 mStartingUsers.add(uss);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800626 if (mCurrentUser == userId) {
627 return true;
628 }
629 mCurrentUser = userId;
630
631 // Move userId's tasks to the top.
632 boolean haveActivities = false;
633 TaskRecord task = null;
634 int index = mTaskHistory.size();
635 for (int i = 0; i < index; ++i) {
636 task = mTaskHistory.get(i);
637 if (task.userId == userId) {
638 haveActivities = true;
639 mTaskHistory.remove(i);
640 mTaskHistory.add(task);
641 --index;
642 }
643 }
644
645 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
646 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800647 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700648 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700649 }
650
Craig Mautner2420ead2013-04-01 17:13:20 -0700651 void minimalResumeActivityLocked(ActivityRecord r) {
652 r.state = ActivityState.RESUMED;
653 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
654 + " (starting new instance)");
655 r.stopped = false;
656 mResumedActivity = r;
657 r.task.touchActiveTime();
658 mService.addRecentTaskLocked(r.task);
659 completeResumeLocked(r);
660 checkReadyForSleepLocked();
661 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700662 }
663
Craig Mautnere79d4262013-04-01 19:01:53 -0700664 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700665 if (r.launchTime == 0) {
666 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700667 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700668 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700669 }
670 } else if (mInitialStartTime == 0) {
671 mInitialStartTime = SystemClock.uptimeMillis();
672 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800674
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800675 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700676 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 if (!mGoingToSleep.isHeld()) {
678 mGoingToSleep.acquire();
679 if (mLaunchingActivity.isHeld()) {
680 mLaunchingActivity.release();
681 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
682 }
683 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800684 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
685 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
686 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
687 checkReadyForSleepLocked();
688 }
689 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700690
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800691 void awakeFromSleepingLocked() {
692 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
693 mSleepTimeout = false;
694 if (mGoingToSleep.isHeld()) {
695 mGoingToSleep.release();
696 }
697 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800698 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
699 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
700 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
701 activities.get(activityNdx).setSleeping(false);
702 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800703 }
704 mGoingToSleepActivities.clear();
705 }
706
707 void activitySleptLocked(ActivityRecord r) {
708 mGoingToSleepActivities.remove(r);
709 checkReadyForSleepLocked();
710 }
711
Craig Mautnere79d4262013-04-01 19:01:53 -0700712 // Checked.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800713 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700714 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800715 // Do not care.
716 return;
717 }
718
719 if (!mSleepTimeout) {
720 if (mResumedActivity != null) {
721 // Still have something resumed; can't sleep until it is paused.
722 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700723 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
724 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800725 return;
726 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800727 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800728 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800729 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800730 return;
731 }
732
Craig Mautnerde4ef022013-04-07 19:01:33 -0700733 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800734 // Still need to tell some activities to stop; can't sleep yet.
735 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700736 + mStackSupervisor.mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700737 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800738 return;
739 }
740
741 ensureActivitiesVisibleLocked(null, 0);
742
743 // Make sure any stopped but visible activities are now sleeping.
744 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800745 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
746 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
747 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
748 final ActivityRecord r = activities.get(activityNdx);
749 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
750 r.setSleeping(true);
751 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800752 }
753 }
754
755 if (mGoingToSleepActivities.size() > 0) {
756 // Still need to tell some activities to sleep; can't sleep yet.
757 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
758 + mGoingToSleepActivities.size() + " activities");
759 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700760 }
761 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800762
763 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
764
765 if (mGoingToSleep.isHeld()) {
766 mGoingToSleep.release();
767 }
768 if (mService.mShuttingDown) {
769 mService.notifyAll();
770 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700771 }
Craig Mautner59c00972012-07-30 12:10:24 -0700772
Dianne Hackbornd2835932010-12-13 16:28:46 -0800773 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800774 if (who.noDisplay) {
775 return null;
776 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800777
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800778 Resources res = mService.mContext.getResources();
779 int w = mThumbnailWidth;
780 int h = mThumbnailHeight;
781 if (w < 0) {
782 mThumbnailWidth = w =
783 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
784 mThumbnailHeight = h =
785 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
786 }
787
788 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800789 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
790 || mLastScreenshotBitmap.getWidth() != w
791 || mLastScreenshotBitmap.getHeight() != h) {
792 mLastScreenshotActivity = who;
793 mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications(
794 who.appToken, Display.DEFAULT_DISPLAY, w, h);
795 }
796 if (mLastScreenshotBitmap != null) {
797 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
798 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800799 }
800 return null;
801 }
802
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700803 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800804 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800805 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700806 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800807 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 ActivityRecord prev = mResumedActivity;
809 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700810 Slog.e(TAG, "Trying to pause when nothing is resumed",
811 new RuntimeException("here").fillInStackTrace());
Craig Mautnerde4ef022013-04-07 19:01:33 -0700812 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700813 return;
814 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700815 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
816 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800818 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 mLastPausedActivity = prev;
820 prev.state = ActivityState.PAUSING;
821 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700822 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823
824 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800825
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 if (prev.app != null && prev.app.thread != null) {
827 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
828 try {
829 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700830 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800832 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
833 userLeaving, prev.configChangeFlags);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700834 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 mService.updateUsageStats(prev, false);
836 }
837 } catch (Exception e) {
838 // Ignore exception, if process died other code will cleanup.
839 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800840 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700841 mLastPausedActivity = null;
842 }
843 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800844 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 mLastPausedActivity = null;
846 }
847
848 // If we are not going to sleep, we want to ensure the device is
849 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700850 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 mLaunchingActivity.acquire();
852 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
853 // To be safe, don't allow the wake lock to be held for too long.
854 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
855 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
856 }
857 }
858
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800859 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 // Have the window manager pause its key dispatching until the new
861 // activity has started. If we're pausing the activity just because
862 // the screen is being turned off and the UI is sleeping, don't interrupt
863 // key dispatch; the same activity will pick it up again on wakeup.
864 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 } else {
867 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
868 }
869
870 // Schedule a pause timeout in case the app doesn't respond.
871 // We don't give it much time because this directly impacts the
872 // responsiveness seen by the user.
873 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
874 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700875 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700876 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
877 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
878 } else {
879 // This activity failed to schedule the
880 // pause, so just treat it as being paused now.
881 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700882 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 }
884 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700885
Craig Mautnerd2328952013-03-05 12:46:26 -0800886 final void activityResumedLocked(IBinder token) {
887 final ActivityRecord r = ActivityRecord.forToken(token);
888 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
889 r.icicle = null;
890 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700891 }
892
Craig Mautnerd2328952013-03-05 12:46:26 -0800893 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700894 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800895 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700896
Craig Mautnerd2328952013-03-05 12:46:26 -0800897 final ActivityRecord r = isInStackLocked(token);
898 if (r != null) {
899 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
900 if (mPausingActivity == r) {
901 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
902 + (timeout ? " (due to timeout)" : " (pause complete)"));
903 r.state = ActivityState.PAUSED;
904 completePauseLocked();
905 } else {
906 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
907 r.userId, System.identityHashCode(r), r.shortComponentName,
908 mPausingActivity != null
909 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700910 }
911 }
912 }
913
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700914 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
915 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700916 if (r.state != ActivityState.STOPPING) {
917 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
918 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
919 return;
920 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700921 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700922 if (icicle != null) {
923 // If icicle is null, this is happening due to a timeout, so we
924 // haven't really saved the state.
925 r.icicle = icicle;
926 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800927 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700928 r.updateThumbnail(thumbnail, description);
929 }
930 if (!r.stopped) {
931 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
932 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
933 r.stopped = true;
934 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700935 if (r.finishing) {
936 r.clearOptionsLocked();
937 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700938 if (r.configDestroy) {
939 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700940 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700941 } else {
942 // Now that this process has stopped, we may want to consider
943 // it to be the previous app to try to keep around in case
944 // the user wants to return to it.
945 ProcessRecord fgApp = null;
946 if (mResumedActivity != null) {
947 fgApp = mResumedActivity.app;
948 } else if (mPausingActivity != null) {
949 fgApp = mPausingActivity.app;
950 }
951 if (r.app != null && fgApp != null && r.app != fgApp
952 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
953 && r.app != mService.mHomeProcess) {
954 mService.mPreviousProcess = r.app;
955 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
956 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800957 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700958 }
959 }
960 }
961
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800962 private final void completePauseLocked() {
963 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700964 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800965
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800966 if (prev != null) {
967 if (prev.finishing) {
968 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700969 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800970 } else if (prev.app != null) {
971 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
972 if (prev.waitingVisible) {
973 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700974 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800975 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
976 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800977 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 if (prev.configDestroy) {
979 // The previous is being paused because the configuration
980 // is changing, which means it is actually stopping...
981 // To juggle the fact that we are also starting a new
982 // instance right now, we need to first completely stop
983 // the current instance before starting the new one.
984 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
985 destroyActivityLocked(prev, true, false, "pause-config");
986 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700988 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
989 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800990 // If we already have a few activities waiting to stop,
991 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700992 // them out. Or if r is the last of activity of the last task the stack
993 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800994 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
995 scheduleIdleLocked();
996 } else {
997 checkReadyForSleepLocked();
998 }
999 }
1000 } else {
1001 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1002 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001003 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001004 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001005 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001006
Craig Mautnerde4ef022013-04-07 19:01:33 -07001007 final ActivityStack topStack = mStackSupervisor.getTopStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001008 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 topStack.resumeTopActivityLocked(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001010 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001011 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001013 if (top == null || (prev != null && top != prev)) {
1014 // If there are no more activities available to run,
1015 // do resume anyway to start something. Also if the top
1016 // activity on the stack is not the just paused activity,
1017 // we need to go ahead and resume it to ensure we complete
1018 // an in-flight app switch.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001019 topStack.resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001020 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001022
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 if (prev != null) {
1024 prev.resumeKeyDispatchingLocked();
1025 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026
1027 if (prev.app != null && prev.cpuTimeAtResume > 0
1028 && mService.mBatteryStatsService.isOnBattery()) {
1029 long diff = 0;
1030 synchronized (mService.mProcessStatsThread) {
1031 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1032 - prev.cpuTimeAtResume;
1033 }
1034 if (diff > 0) {
1035 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1036 synchronized (bsi) {
1037 BatteryStatsImpl.Uid.Proc ps =
1038 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1039 prev.info.packageName);
1040 if (ps != null) {
1041 ps.addForegroundTimeLocked(diff);
1042 }
1043 }
1044 }
1045 }
1046 prev.cpuTimeAtResume = 0; // reset it
1047 }
1048
Craig Mautnere79d4262013-04-01 19:01:53 -07001049 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050 /**
1051 * Once we know that we have asked an application to put an activity in
1052 * the resumed state (either by launching it or explicitly telling it),
1053 * this function updates the rest of our state to match that fact.
1054 */
1055 private final void completeResumeLocked(ActivityRecord next) {
1056 next.idle = false;
1057 next.results = null;
1058 next.newIntents = null;
1059
1060 // schedule an idle timeout in case the app doesn't do it for us.
1061 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1062 msg.obj = next;
1063 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1064
1065 if (false) {
1066 // The activity was never told to pause, so just keep
1067 // things going as-is. To maintain our own state,
1068 // we need to emulate it coming back and saying it is
1069 // idle.
1070 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1071 msg.obj = next;
1072 mHandler.sendMessage(msg);
1073 }
1074
Craig Mautnerde4ef022013-04-07 19:01:33 -07001075 if (mStackSupervisor.isFrontStack(this)) {
1076 // TODO: Should this be done for all stacks, not just mMainStack?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001077 mService.reportResumedActivityLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 mService.setFocusedActivityLocked(next);
1079 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001080 if (mStackSupervisor.allResumedActivitiesComplete()) {
1081 next.resumeKeyDispatchingLocked();
1082 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
1083 mService.mWindowManager.executeAppTransition();
1084 mNoAnimActivities.clear();
1085 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001086
1087 // Mark the point when the activity is resuming
1088 // TODO: To be more accurate, the mark should be before the onCreate,
1089 // not after the onResume. But for subsequent starts, onResume is fine.
1090 if (next.app != null) {
1091 synchronized (mService.mProcessStatsThread) {
1092 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1093 }
1094 } else {
1095 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1096 }
1097 }
1098
Craig Mautnere79d4262013-04-01 19:01:53 -07001099 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001100 /**
1101 * Make sure that all activities that need to be visible (that is, they
1102 * currently can be seen by the user) actually are.
1103 */
1104 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1105 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1106 if (DEBUG_VISBILITY) Slog.v(
1107 TAG, "ensureActivitiesVisible behind " + top
1108 + " configChanges=0x" + Integer.toHexString(configChanges));
1109
1110 // If the top activity is not fullscreen, then we need to
1111 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001112 boolean aboveTop = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001113 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this);
1114 int taskNdx;
1115 for (taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001116 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1117 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1118 final ActivityRecord r = activities.get(activityNdx);
1119 if (r.finishing) {
1120 continue;
1121 }
1122 if (aboveTop && r != top) {
1123 continue;
1124 }
1125 aboveTop = false;
1126 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001128 TAG, "Make visible? " + r + " finishing=" + r.finishing
1129 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001130
Craig Mautnerd44711d2013-02-23 11:24:36 -08001131 final boolean doThisProcess = onlyThisProcess == null
1132 || onlyThisProcess.equals(r.processName);
1133
1134 // First: if this is not the current activity being started, make
1135 // sure it matches the current configuration.
1136 if (r != starting && doThisProcess) {
1137 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001139
1140 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001141 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001142 // This activity needs to be visible, but isn't even
1143 // running... get it started, but don't resume it
1144 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001145 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001146 if (r != starting) {
1147 r.startFreezingScreenLocked(r.app, configChanges);
1148 }
1149 if (!r.visible) {
1150 if (DEBUG_VISBILITY) Slog.v(
1151 TAG, "Starting and making visible: " + r);
1152 mService.mWindowManager.setAppVisibility(r.appToken, true);
1153 }
1154 if (r != starting) {
Craig Mautnere79d4262013-04-01 19:01:53 -07001155 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001156 }
1157 }
1158
1159 } else if (r.visible) {
1160 // If this activity is already visible, then there is nothing
1161 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001162 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001163 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001164
1165 } else if (onlyThisProcess == null) {
1166 // This activity is not currently visible, but is running.
1167 // Tell it to become visible.
1168 r.visible = true;
1169 if (r.state != ActivityState.RESUMED && r != starting) {
1170 // If this activity is paused, tell it
1171 // to now show its window.
1172 if (DEBUG_VISBILITY) Slog.v(
1173 TAG, "Making visible and scheduling visibility: " + r);
1174 try {
1175 mService.mWindowManager.setAppVisibility(r.appToken, true);
1176 r.sleeping = false;
1177 r.app.pendingUiClean = true;
1178 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1179 r.stopFreezingScreenLocked(false);
1180 } catch (Exception e) {
1181 // Just skip on any failure; we'll make it
1182 // visible when it next restarts.
1183 Slog.w(TAG, "Exception thrown making visibile: "
1184 + r.intent.getComponent(), e);
1185 }
1186 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001188
Craig Mautnerd44711d2013-02-23 11:24:36 -08001189 // Aggregate current change flags.
1190 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191
Craig Mautnerd44711d2013-02-23 11:24:36 -08001192 if (r.fullscreen) {
1193 // At this point, nothing else needs to be shown
1194 if (DEBUG_VISBILITY) Slog.v(
1195 TAG, "Stopping: fullscreen at " + r);
1196 behindFullscreen = true;
1197 }
1198 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001199 if (DEBUG_VISBILITY) Slog.v(
1200 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1201 + " state=" + r.state
1202 + " behindFullscreen=" + behindFullscreen);
1203 // Now for any activities that aren't visible to the user, make
1204 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001206 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 r.visible = false;
1208 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001209 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001210 if ((r.state == ActivityState.STOPPING
1211 || r.state == ActivityState.STOPPED)
1212 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001213 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001214 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 }
1216 } catch (Exception e) {
1217 // Just skip on any failure; we'll make it
1218 // visible when it next restarts.
1219 Slog.w(TAG, "Exception thrown making hidden: "
1220 + r.intent.getComponent(), e);
1221 }
1222 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001223 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001224 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001225 }
1226 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001227 }
1228 }
1229
Craig Mautnere79d4262013-04-01 19:01:53 -07001230 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 /**
1232 * Version of ensureActivitiesVisible that can easily be called anywhere.
1233 */
1234 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1235 int configChanges) {
1236 ActivityRecord r = topRunningActivityLocked(null);
1237 if (r != null) {
1238 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1239 }
1240 }
Craig Mautner58547802013-03-05 08:23:53 -08001241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001242 /**
1243 * Ensure that the top activity in the stack is resumed.
1244 *
1245 * @param prev The previously resumed activity, for when in the process
1246 * of pausing; can be null to call from elsewhere.
1247 *
1248 * @return Returns true if something is being resumed, or false if
1249 * nothing happened.
1250 */
1251 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001252 return resumeTopActivityLocked(prev, null);
1253 }
1254
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07001255 // Checked.
Dianne Hackborn84375872012-06-01 19:03:50 -07001256 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001257 // Find the first activity that is not finishing.
1258 ActivityRecord next = topRunningActivityLocked(null);
1259
1260 // Remember how we'll process this pause/resume situation, and ensure
1261 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001262 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1263 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001264
1265 if (next == null) {
1266 // There are no more activities! Let's just start up the
1267 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001268 ActivityOptions.abort(options);
Craig Mautner69ada552013-04-18 13:51:51 -07001269 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001270 }
1271
1272 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001273
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001274 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001275 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1276 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001277 // Make sure we have executed any pending transitions, since there
1278 // should be nothing left to do at this point.
1279 mService.mWindowManager.executeAppTransition();
1280 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001281 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001282 return false;
1283 }
1284
Craig Mautnerde4ef022013-04-07 19:01:33 -07001285 if (prev != null && prev.mLaunchHomeTaskNext && prev.finishing &&
1286 prev.task.getTopActivity() == null) {
Craig Mautner69ada552013-04-18 13:51:51 -07001287 return mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001288 }
1289
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001290 // If we are sleeping, and there is no resumed activity, and the top
1291 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001292 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001293 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001294 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001295 // Make sure we have executed any pending transitions, since there
1296 // should be nothing left to do at this point.
1297 mService.mWindowManager.executeAppTransition();
1298 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001299 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001300 return false;
1301 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001302
1303 // Make sure that the user who owns this activity is started. If not,
1304 // we will just leave it as is because someone should be bringing
1305 // another user's activities to the top of the stack.
1306 if (mService.mStartedUsers.get(next.userId) == null) {
1307 Slog.w(TAG, "Skipping resume of top activity " + next
1308 + ": user " + next.userId + " is stopped");
1309 return false;
1310 }
1311
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 // The activity may be waiting for stop, but that is no longer
1313 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001314 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001315 mGoingToSleepActivities.remove(next);
1316 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001317 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001318
Dianne Hackborn84375872012-06-01 19:03:50 -07001319 next.updateOptionsLocked(options);
1320
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001321 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1322
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001323 // If we are currently pausing an activity, then don't do anything
1324 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001325 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1326 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001327 return false;
1328 }
1329
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001330 // Okay we are now going to start a switch, to 'next'. We may first
1331 // have to pause the current activity, but this is an important point
1332 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a2010-09-20 22:24:38 -07001333 // XXX "App Redirected" dialog is getting too many false positives
1334 // at this point, so turn off for now.
1335 if (false) {
1336 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1337 long now = SystemClock.uptimeMillis();
1338 final boolean inTime = mLastStartedActivity.startTime != 0
1339 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1340 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1341 final int nextUid = next.info.applicationInfo.uid;
1342 if (inTime && lastUid != nextUid
1343 && lastUid != next.launchedFromUid
1344 && mService.checkPermission(
1345 android.Manifest.permission.STOP_APP_SWITCHES,
1346 -1, next.launchedFromUid)
1347 != PackageManager.PERMISSION_GRANTED) {
1348 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1349 } else {
1350 next.startTime = now;
1351 mLastStartedActivity = next;
1352 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001353 } else {
Dianne Hackborn034093a2010-09-20 22:24:38 -07001354 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001355 mLastStartedActivity = next;
1356 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001357 }
Craig Mautner58547802013-03-05 08:23:53 -08001358
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001359 // We need to start pausing the current activity so the top one
1360 // can be resumed...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001361 final ActivityStack lastStack = mStackSupervisor.getLastStack();
Craig Mautner967212c2013-04-13 21:10:58 -07001362 if ((isHomeStack() ^ lastStack.isHomeStack()) && lastStack.mResumedActivity != null) {
Craig Mautner69ada552013-04-18 13:51:51 -07001363 // TODO: Don't pause when launching to the sibling task.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001365 // At this point we want to put the upcoming activity's process
1366 // at the top of the LRU list, since we know we will be needing it
1367 // very soon and it would be a waste to let it get killed if it
1368 // happens to be sitting towards the end.
1369 if (next.app != null && next.app.thread != null) {
1370 // No reason to do full oom adj update here; we'll let that
1371 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001372 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001373 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001374 lastStack.startPausingLocked(userLeaving, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001375 return true;
1376 }
1377
Christopher Tated3f175c2012-06-14 14:16:54 -07001378 // If the most recent activity was noHistory but was only stopped rather
1379 // than stopped+finished because the device went to sleep, we need to make
1380 // sure to finish it as we're making a new activity topmost.
1381 final ActivityRecord last = mLastPausedActivity;
1382 if (mService.mSleeping && last != null && !last.finishing) {
1383 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1384 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1385 if (DEBUG_STATES) {
1386 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1387 }
1388 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001389 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001390 }
1391 }
1392
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001393 if (prev != null && prev != next) {
1394 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1395 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001396 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001397 if (DEBUG_SWITCH) Slog.v(
1398 TAG, "Resuming top, waiting visible to hide: " + prev);
1399 } else {
1400 // The next activity is already visible, so hide the previous
1401 // activity's windows right now so we can show the new one ASAP.
1402 // We only do this if the previous is finishing, which should mean
1403 // it is on top of the one being resumed so hiding it quickly
1404 // is good. Otherwise, we want to do the normal route of allowing
1405 // the resumed activity to be shown so we can decide if the
1406 // previous should actually be hidden depending on whether the
1407 // new one is found to be full-screen or not.
1408 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001409 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001410 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1411 + prev + ", waitingVisible="
1412 + (prev != null ? prev.waitingVisible : null)
1413 + ", nowVisible=" + next.nowVisible);
1414 } else {
1415 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1416 + prev + ", waitingVisible="
1417 + (prev != null ? prev.waitingVisible : null)
1418 + ", nowVisible=" + next.nowVisible);
1419 }
1420 }
1421 }
1422
Dianne Hackborne7f97212011-02-24 14:40:20 -08001423 // Launching this app's activity, make sure the app is no longer
1424 // considered stopped.
1425 try {
1426 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001427 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001428 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001429 } catch (IllegalArgumentException e) {
1430 Slog.w(TAG, "Failed trying to unstop package "
1431 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001432 }
1433
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001434 // We are starting up the next activity, so tell the window manager
1435 // that the previous one will be hidden soon. This way it can know
1436 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001437 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438 if (prev != null) {
1439 if (prev.finishing) {
1440 if (DEBUG_TRANSITION) Slog.v(TAG,
1441 "Prepare close transition: prev=" + prev);
1442 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001443 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001444 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445 } else {
1446 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001447 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1448 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001450 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1451 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001452 } else {
1453 if (DEBUG_TRANSITION) Slog.v(TAG,
1454 "Prepare open transition: prev=" + prev);
1455 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001456 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001457 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001458 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 } else {
1460 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001461 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1462 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 }
1464 }
1465 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001466 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1467 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 }
Craig Mautner967212c2013-04-13 21:10:58 -07001469 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 if (DEBUG_TRANSITION) Slog.v(TAG,
1471 "Prepare open transition: no previous");
1472 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001473 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001474 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001475 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001477 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001478 AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 }
1480 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001481 if (!noAnim) {
1482 next.applyOptionsLocked();
1483 } else {
1484 next.clearOptionsLocked();
1485 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486
1487 if (next.app != null && next.app.thread != null) {
1488 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1489
1490 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001491 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001492
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001493 // schedule launch ticks to collect information about slow apps.
1494 next.startLaunchTickingLocked();
1495
Craig Mautnerde4ef022013-04-07 19:01:33 -07001496 ActivityRecord lastResumedActivity = lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 ActivityState lastState = next.state;
1498
1499 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001500
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001501 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502 next.state = ActivityState.RESUMED;
1503 mResumedActivity = next;
1504 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001505 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001506 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 updateLRUListLocked(next);
1508
1509 // Have the window manager re-evaluate the orientation of
1510 // the screen based on the new activity order.
1511 boolean updated = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001512 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001513 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1514 mService.mConfiguration,
1515 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1516 if (config != null) {
1517 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001518 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001519 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001521
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522 if (!updated) {
1523 // The configuration update wasn't able to keep the existing
1524 // instance of the activity, and instead started a new one.
1525 // We should be all done, but let's just make sure our activity
1526 // is still at the top and schedule another run if something
1527 // weird happened.
1528 ActivityRecord nextNext = topRunningActivityLocked(null);
1529 if (DEBUG_SWITCH) Slog.i(TAG,
1530 "Activity config changed during resume: " + next
1531 + ", new next: " + nextNext);
1532 if (nextNext != next) {
1533 // Do over!
1534 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1535 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001536 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537 mService.setFocusedActivityLocked(next);
1538 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001539 if (mStackSupervisor.allResumedActivitiesComplete()) {
1540 ensureActivitiesVisibleLocked(null, 0);
1541 mService.mWindowManager.executeAppTransition();
1542 mNoAnimActivities.clear();
1543 return true;
1544 }
1545 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 }
Craig Mautner58547802013-03-05 08:23:53 -08001547
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001548 try {
1549 // Deliver all pending results.
Craig Mautner05d62722013-02-11 09:39:27 -08001550 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 if (a != null) {
1552 final int N = a.size();
1553 if (!next.finishing && N > 0) {
1554 if (DEBUG_RESULTS) Slog.v(
1555 TAG, "Delivering results to " + next
1556 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001557 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 }
1559 }
1560
1561 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001562 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 }
1564
1565 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001566 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001568
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001569 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001570 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001571 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001572 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001574
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001575 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576
1577 } catch (Exception e) {
1578 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001579 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1580 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 next.state = lastState;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001582 lastStack.mResumedActivity = lastResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 Slog.i(TAG, "Restarting because process died: " + next);
1584 if (!next.hasBeenLaunched) {
1585 next.hasBeenLaunched = true;
1586 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001587 if (SHOW_APP_STARTING_PREVIEW && mStackSupervisor.isFrontStack(lastStack)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001589 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001590 mService.compatibilityInfoForPackageLocked(
1591 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001592 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001593 next.labelRes, next.icon, next.windowFlags,
1594 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 }
1596 }
Craig Mautnere79d4262013-04-01 19:01:53 -07001597 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 return true;
1599 }
1600
1601 // From this point on, if something goes wrong there is no way
1602 // to recover the activity.
1603 try {
1604 next.visible = true;
1605 completeResumeLocked(next);
1606 } catch (Exception e) {
1607 // If any exception gets thrown, toss away this
1608 // activity and try the next one.
1609 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001610 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001611 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 return true;
1613 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 next.stopped = false;
1615
1616 } else {
1617 // Whoops, need to restart this activity!
1618 if (!next.hasBeenLaunched) {
1619 next.hasBeenLaunched = true;
1620 } else {
1621 if (SHOW_APP_STARTING_PREVIEW) {
1622 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001623 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001624 mService.compatibilityInfoForPackageLocked(
1625 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001627 next.labelRes, next.icon, next.windowFlags,
1628 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001629 }
1630 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1631 }
Craig Mautnere79d4262013-04-01 19:01:53 -07001632 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 }
1634
1635 return true;
1636 }
1637
Craig Mautner11bf9a52013-02-19 14:08:51 -08001638
Craig Mautner8849a5e2013-04-02 16:41:03 -07001639 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001640 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001641 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001642 TaskRecord rTask = r.task;
1643 final int taskId = rTask.taskId;
1644 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001645 // Last activity in task had been removed or ActivityManagerService is reusing task.
1646 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001647 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001648 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001649 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001650 mTaskHistory.add(rTask);
1651 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001652 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 if (!newTask) {
1654 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001655 boolean startIt = true;
1656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1657 task = mTaskHistory.get(taskNdx);
1658 if (task == r.task) {
1659 // Here it is! Now, if this is not yet visible to the
1660 // user, then just add it without starting; it will
1661 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001662 if (!startIt) {
1663 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1664 + task, new RuntimeException("here").fillInStackTrace());
1665 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001666 r.putInHistory();
Craig Mautner70a86932013-02-28 22:37:44 -08001667 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001668 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation,
1669 r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001670 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001672 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001674 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 return;
1676 }
1677 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001678 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 startIt = false;
1680 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001681 }
1682 }
1683
1684 // Place a new activity at top of stack, so it is next to interact
1685 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001686
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 // If we are not placing the new activity frontmost, we do not want
1688 // to deliver the onUserLeaving callback to the actual frontmost
1689 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001690 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001691 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001692 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1693 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 }
Craig Mautner70a86932013-02-28 22:37:44 -08001695
1696 task = r.task;
1697
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001699 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001700 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001701 task.addActivityToTop(r);
1702
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001703 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001705 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001706 // We want to show the starting preview window if we are
1707 // switching to a new task, or the next activity's process is
1708 // not currently running.
1709 boolean showStartingIcon = newTask;
1710 ProcessRecord proc = r.app;
1711 if (proc == null) {
1712 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1713 }
1714 if (proc == null || proc.thread == null) {
1715 showStartingIcon = true;
1716 }
1717 if (DEBUG_TRANSITION) Slog.v(TAG,
1718 "Prepare open transition: starting " + r);
1719 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001720 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001721 AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 } else {
1724 mService.mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001725 ? AppTransition.TRANSIT_TASK_OPEN
1726 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 mNoAnimActivities.remove(r);
1728 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001729 r.updateOptionsLocked(options);
Craig Mautner70a86932013-02-28 22:37:44 -08001730 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001731 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001732 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 boolean doShow = true;
1734 if (newTask) {
1735 // Even though this activity is starting fresh, we still need
1736 // to reset it to make sure we apply affinities to move any
1737 // existing activities from other tasks in to it.
1738 // If the caller has requested that the target task be
1739 // reset, then do so.
1740 if ((r.intent.getFlags()
1741 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1742 resetTaskIfNeededLocked(r, r);
1743 doShow = topRunningNonDelayedActivityLocked(null) == r;
1744 }
1745 }
1746 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1747 // Figure out if we are transitioning from another activity that is
1748 // "has the same starting icon" as the next one. This allows the
1749 // window manager to keep the previous window it had previously
1750 // created, if it still had one.
1751 ActivityRecord prev = mResumedActivity;
1752 if (prev != null) {
1753 // We don't want to reuse the previous starting preview if:
1754 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001755 if (prev.task != r.task) {
1756 prev = null;
1757 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001759 else if (prev.nowVisible) {
1760 prev = null;
1761 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762 }
1763 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001764 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001765 mService.compatibilityInfoForPackageLocked(
1766 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001767 r.labelRes, r.icon, r.windowFlags,
1768 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 }
1770 } else {
1771 // If this is the first activity, don't do any fancy animations,
1772 // because there is nothing for it to animate on top of.
Craig Mautner70a86932013-02-28 22:37:44 -08001773 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001774 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001775 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001776 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 }
1778 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001779 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 }
1781
1782 if (doResume) {
Craig Mautner29219d92013-04-16 20:19:12 -07001783 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 }
1785 }
1786
Dianne Hackbornbe707852011-11-11 14:32:10 -08001787 final void validateAppTokensLocked() {
1788 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001789 mValidateAppTokens.ensureCapacity(numActivities());
1790 final int numTasks = mTaskHistory.size();
1791 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1792 TaskRecord task = mTaskHistory.get(taskNdx);
1793 final ArrayList<ActivityRecord> activities = task.mActivities;
1794 if (activities.size() == 0) {
1795 continue;
Craig Mautnerb1fd65c2013-02-05 13:34:57 -08001796 }
Craig Mautner000f0022013-02-26 15:04:29 -08001797 TaskGroup group = new TaskGroup();
1798 group.taskId = task.taskId;
1799 mValidateAppTokens.add(group);
1800 final int numActivities = activities.size();
1801 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1802 final ActivityRecord r = activities.get(activityNdx);
1803 group.tokens.add(r.appToken);
1804 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001805 }
Craig Mautner00af9fe2013-03-25 09:13:41 -07001806 mService.mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001807 }
1808
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 /**
1810 * Perform a reset of the given task, if needed as part of launching it.
1811 * Returns the new HistoryRecord at the top of the task.
1812 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001813 /**
1814 * Helper method for #resetTaskIfNeededLocked.
1815 * We are inside of the task being reset... we'll either finish this activity, push it out
1816 * for another task, or leave it as-is.
1817 * @param task The task containing the Activity (taskTop) that might be reset.
1818 * @param forceReset
1819 * @return An ActivityOptions that needs to be processed.
1820 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07001821 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task,
Craig Mautnere3a74d52013-02-22 14:14:58 -08001822 boolean forceReset) {
1823 ActivityOptions topOptions = null;
1824
1825 int replyChainEnd = -1;
1826 boolean canMoveOptions = true;
1827
1828 // We only do this for activities that are not the root of the task (since if we finish
1829 // the root, we may no longer have the task!).
1830 final ArrayList<ActivityRecord> activities = task.mActivities;
1831 final int numActivities = activities.size();
1832 for (int i = numActivities - 1; i > 0; --i ) {
1833 ActivityRecord target = activities.get(i);
1834
1835 final int flags = target.info.flags;
1836 final boolean finishOnTaskLaunch =
1837 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1838 final boolean allowTaskReparenting =
1839 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1840 final boolean clearWhenTaskReset =
1841 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1842
1843 if (!finishOnTaskLaunch
1844 && !clearWhenTaskReset
1845 && target.resultTo != null) {
1846 // If this activity is sending a reply to a previous
1847 // activity, we can't do anything with it now until
1848 // we reach the start of the reply chain.
1849 // XXX note that we are assuming the result is always
1850 // to the previous activity, which is almost always
1851 // the case but we really shouldn't count on.
1852 if (replyChainEnd < 0) {
1853 replyChainEnd = i;
1854 }
1855 } else if (!finishOnTaskLaunch
1856 && !clearWhenTaskReset
1857 && allowTaskReparenting
1858 && target.taskAffinity != null
1859 && !target.taskAffinity.equals(task.affinity)) {
1860 // If this activity has an affinity for another
1861 // task, then we need to move it out of here. We will
1862 // move it as far out of the way as possible, to the
1863 // bottom of the activity stack. This also keeps it
1864 // correctly ordered with any activities we previously
1865 // moved.
1866 TaskRecord bottomTask = mTaskHistory.get(0);
1867 ActivityRecord p = bottomTask.mActivities.get(0);
1868 if (target.taskAffinity != null
1869 && target.taskAffinity.equals(p.task.affinity)) {
1870 // If the activity currently at the bottom has the
1871 // same task affinity as the one we are moving,
1872 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001873 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001874 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1875 + " out to bottom task " + p.task);
1876 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001877 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001878 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001879 target.task.affinityIntent = target.intent;
1880 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1881 + " out to new task " + target.task);
1882 }
1883
1884 final TaskRecord targetTask = target.task;
1885 final int targetTaskId = targetTask.taskId;
1886 mService.mWindowManager.setAppGroupId(target.appToken, targetTaskId);
1887
1888 ThumbnailHolder curThumbHolder = target.thumbHolder;
1889 boolean gotOptions = !canMoveOptions;
1890
1891 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1892 for (int srcPos = start; srcPos >= i; --srcPos) {
1893 p = activities.get(srcPos);
1894 if (p.finishing) {
1895 continue;
1896 }
1897
1898 curThumbHolder = p.thumbHolder;
1899 canMoveOptions = false;
1900 if (!gotOptions && topOptions == null) {
1901 topOptions = p.takeOptionsLocked();
1902 if (topOptions != null) {
1903 gotOptions = true;
1904 }
1905 }
1906 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1907 + task + " adding to task=" + targetTask,
1908 new RuntimeException("here").fillInStackTrace());
1909 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1910 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001911 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001912 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001913
Craig Mautnere3a74d52013-02-22 14:14:58 -08001914 mService.mWindowManager.setAppGroupId(p.appToken, targetTaskId);
1915 }
1916
1917 mService.mWindowManager.moveTaskToBottom(targetTaskId);
1918 if (VALIDATE_TOKENS) {
1919 validateAppTokensLocked();
1920 }
1921
1922 replyChainEnd = -1;
1923 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1924 // If the activity should just be removed -- either
1925 // because it asks for it, or the task should be
1926 // cleared -- then finish it and anything that is
1927 // part of its reply chain.
1928 int end;
1929 if (clearWhenTaskReset) {
1930 // In this case, we want to finish this activity
1931 // and everything above it, so be sneaky and pretend
1932 // like these are all in the reply chain.
1933 end = numActivities - 1;
1934 } else if (replyChainEnd < 0) {
1935 end = i;
1936 } else {
1937 end = replyChainEnd;
1938 }
1939 ActivityRecord p = null;
1940 boolean gotOptions = !canMoveOptions;
1941 for (int srcPos = i; srcPos <= end; srcPos++) {
1942 p = activities.get(srcPos);
1943 if (p.finishing) {
1944 continue;
1945 }
1946 canMoveOptions = false;
1947 if (!gotOptions && topOptions == null) {
1948 topOptions = p.takeOptionsLocked();
1949 if (topOptions != null) {
1950 gotOptions = true;
1951 }
1952 }
Craig Mautner58547802013-03-05 08:23:53 -08001953 if (DEBUG_TASKS) Slog.w(TAG,
1954 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001955 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001956 end--;
1957 srcPos--;
1958 }
1959 }
1960 replyChainEnd = -1;
1961 } else {
1962 // If we were in the middle of a chain, well the
1963 // activity that started it all doesn't want anything
1964 // special, so leave it all as-is.
1965 replyChainEnd = -1;
1966 }
1967 }
1968
1969 return topOptions;
1970 }
1971
1972 /**
1973 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1974 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1975 * @param affinityTask The task we are looking for an affinity to.
1976 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1977 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1978 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1979 */
Craig Mautner77878772013-03-04 19:46:24 -08001980 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1981 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001982 int replyChainEnd = -1;
1983 final int taskId = task.taskId;
1984 final String taskAffinity = task.affinity;
1985
1986 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1987 final int numActivities = activities.size();
1988 // Do not operate on the root Activity.
1989 for (int i = numActivities - 1; i > 0; --i) {
1990 ActivityRecord target = activities.get(i);
1991
1992 final int flags = target.info.flags;
1993 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1994 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1995
1996 if (target.resultTo != null) {
1997 // If this activity is sending a reply to a previous
1998 // activity, we can't do anything with it now until
1999 // we reach the start of the reply chain.
2000 // XXX note that we are assuming the result is always
2001 // to the previous activity, which is almost always
2002 // the case but we really shouldn't count on.
2003 if (replyChainEnd < 0) {
2004 replyChainEnd = i;
2005 }
2006 } else if (topTaskIsHigher
2007 && allowTaskReparenting
2008 && taskAffinity != null
2009 && taskAffinity.equals(target.taskAffinity)) {
2010 // This activity has an affinity for our task. Either remove it if we are
2011 // clearing or move it over to our task. Note that
2012 // we currently punt on the case where we are resetting a
2013 // task that is not at the top but who has activities above
2014 // with an affinity to it... this is really not a normal
2015 // case, and we will need to later pull that task to the front
2016 // and usually at that point we will do the reset and pick
2017 // up those remaining activities. (This only happens if
2018 // someone starts an activity in a new task from an activity
2019 // in a task that is not currently on top.)
2020 if (forceReset || finishOnTaskLaunch) {
2021 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2022 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2023 for (int srcPos = start; srcPos >= i; --srcPos) {
2024 final ActivityRecord p = activities.get(srcPos);
2025 if (p.finishing) {
2026 continue;
2027 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002028 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002029 }
2030 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002031 if (taskInsertionPoint < 0) {
2032 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002033
Craig Mautner77878772013-03-04 19:46:24 -08002034 }
Craig Mautner77878772013-03-04 19:46:24 -08002035
2036 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2037 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2038 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2039 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002040 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002041 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002042 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002043
Craig Mautnere3a74d52013-02-22 14:14:58 -08002044 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2045 + " to stack at " + task,
2046 new RuntimeException("here").fillInStackTrace());
2047 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2048 + " in to resetting task " + task);
2049 mService.mWindowManager.setAppGroupId(p.appToken, taskId);
2050 }
2051 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002052 if (VALIDATE_TOKENS) {
2053 validateAppTokensLocked();
2054 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002055
2056 // Now we've moved it in to place... but what if this is
2057 // a singleTop activity and we have put it on top of another
2058 // instance of the same activity? Then we drop the instance
2059 // below so it remains singleTop.
2060 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2061 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2062 boolean found = false;
2063 int targetNdx = taskActivities.indexOf(target);
2064 if (targetNdx > 0) {
2065 ActivityRecord p = taskActivities.get(targetNdx - 1);
2066 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002067 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2068 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002069 }
2070 }
2071 }
2072 }
2073
2074 replyChainEnd = -1;
2075 }
2076 }
Craig Mautner77878772013-03-04 19:46:24 -08002077 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002078 }
2079
Craig Mautner8849a5e2013-04-02 16:41:03 -07002080 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002081 ActivityRecord newActivity) {
2082 boolean forceReset =
2083 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2084 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2085 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2086 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2087 forceReset = true;
2088 }
2089 }
2090
2091 final TaskRecord task = taskTop.task;
2092
2093 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2094 * for remaining tasks. Used for later tasks to reparent to task. */
2095 boolean taskFound = false;
2096
2097 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2098 ActivityOptions topOptions = null;
2099
Craig Mautner77878772013-03-04 19:46:24 -08002100 // Preserve the location for reparenting in the new task.
2101 int reparentInsertionPoint = -1;
2102
Craig Mautnere3a74d52013-02-22 14:14:58 -08002103 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2104 final TaskRecord targetTask = mTaskHistory.get(i);
2105
2106 if (targetTask == task) {
2107 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2108 taskFound = true;
2109 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002110 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2111 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002112 }
2113 }
2114
Craig Mautner70a86932013-02-28 22:37:44 -08002115 int taskNdx = mTaskHistory.indexOf(task);
2116 do {
2117 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2118 } while (taskTop == null && taskNdx >= 0);
2119
Craig Mautnere3a74d52013-02-22 14:14:58 -08002120 if (topOptions != null) {
2121 // If we got some ActivityOptions from an activity on top that
2122 // was removed from the task, propagate them to the new real top.
2123 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002124 taskTop.updateOptionsLocked(topOptions);
2125 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002126 topOptions.abort();
2127 }
2128 }
2129
2130 return taskTop;
2131 }
2132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 * Find the activity in the history stack within the given task. Returns
2135 * the index within the history at which it's found, or < 0 if not found.
2136 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07002137 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002138 final ComponentName realActivity = r.realActivity;
2139 ArrayList<ActivityRecord> activities = task.mActivities;
2140 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2141 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002142 if (candidate.finishing) {
2143 continue;
2144 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002145 if (candidate.realActivity.equals(realActivity)) {
2146 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147 }
2148 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002149 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150 }
2151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
2153 long thisTime, long totalTime) {
2154 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
2155 WaitResult w = mWaitingActivityLaunched.get(i);
2156 w.timeout = timeout;
2157 if (r != null) {
2158 w.who = new ComponentName(r.info.packageName, r.info.name);
2159 }
2160 w.thisTime = thisTime;
2161 w.totalTime = totalTime;
2162 }
2163 mService.notifyAll();
2164 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002165
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002166 void reportActivityVisibleLocked(ActivityRecord r) {
2167 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
2168 WaitResult w = mWaitingActivityVisible.get(i);
2169 w.timeout = false;
2170 if (r != null) {
2171 w.who = new ComponentName(r.info.packageName, r.info.name);
2172 }
2173 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
2174 w.thisTime = w.totalTime;
2175 }
2176 mService.notifyAll();
Craig Mautner27084302013-03-25 08:05:25 -07002177 mStackSupervisor.dismissKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002178 }
2179
2180 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2181 String resultWho, int requestCode, int resultCode, Intent data) {
2182
2183 if (callingUid > 0) {
2184 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002185 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186 }
2187
2188 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2189 + " : who=" + resultWho + " req=" + requestCode
2190 + " res=" + resultCode + " data=" + data);
2191 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2192 try {
2193 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2194 list.add(new ResultInfo(resultWho, requestCode,
2195 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002196 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 return;
2198 } catch (Exception e) {
2199 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2200 }
2201 }
2202
2203 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2204 }
2205
2206 private final void stopActivityLocked(ActivityRecord r) {
2207 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2208 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2209 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2210 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002211 if (!mService.mSleeping) {
2212 if (DEBUG_STATES) {
2213 Slog.d(TAG, "no-history finish of " + r);
2214 }
2215 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002216 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002217 } else {
2218 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2219 + " on stop because we're just sleeping");
2220 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002222 }
2223
2224 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002225 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 if (mService.mFocusedActivity == r) {
2227 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2228 }
2229 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002230 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 try {
2232 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002233 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2234 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002235 r.state = ActivityState.STOPPING;
2236 if (DEBUG_VISBILITY) Slog.v(
2237 TAG, "Stopping visible=" + r.visible + " for " + r);
2238 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002239 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002240 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002241 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002242 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002243 r.setSleeping(true);
2244 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002245 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2246 msg.obj = r;
2247 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248 } catch (Exception e) {
2249 // Maybe just ignore exceptions here... if the process
2250 // has crashed, our death notification will clean things
2251 // up.
2252 Slog.w(TAG, "Exception thrown during pause", e);
2253 // Just in case, assume it to be stopped.
2254 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002255 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002256 r.state = ActivityState.STOPPED;
2257 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002258 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002259 }
2260 }
2261 }
2262 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002263
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002264 final void scheduleIdleLocked() {
2265 Message msg = Message.obtain();
2266 msg.what = IDLE_NOW_MSG;
2267 mHandler.sendMessage(msg);
2268 }
2269
Craig Mautnere79d4262013-04-01 19:01:53 -07002270 // Checked.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002271 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 Configuration config) {
2273 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2274
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002275 ActivityRecord res = null;
2276
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 ArrayList<ActivityRecord> stops = null;
2278 ArrayList<ActivityRecord> finishes = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002279 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280 int NS = 0;
2281 int NF = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 IApplicationThread sendThumbnail = null;
2283 boolean booting = false;
2284 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002285 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002286
Craig Mautnere79d4262013-04-01 19:01:53 -07002287 ActivityRecord r = ActivityRecord.forToken(token);
2288 if (r != null) {
2289 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2290 r.finishLaunchTickingLocked();
2291 }
2292
2293 // Get the activity record.
2294 if (isInStackLocked(token) != null) {
2295 res = r;
2296
2297 if (fromTimeout) {
2298 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002299 }
2300
Craig Mautnere79d4262013-04-01 19:01:53 -07002301 // This is a hack to semi-deal with a race condition
2302 // in the client where it can be constructed with a
2303 // newer configuration from when we asked it to launch.
2304 // We'll update with whatever configuration it now says
2305 // it used to launch.
2306 if (config != null) {
2307 r.configuration = config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002308 }
2309
Craig Mautnere79d4262013-04-01 19:01:53 -07002310 // No longer need to keep the device awake.
2311 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
2312 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2313 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 }
2315
Craig Mautnere79d4262013-04-01 19:01:53 -07002316 // We are now idle. If someone is waiting for a thumbnail from
2317 // us, we can now deliver.
2318 r.idle = true;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002319 if (mStackSupervisor.allResumedActivitiesIdle()) {
2320 mService.scheduleAppGcsLocked();
2321 }
Craig Mautnere79d4262013-04-01 19:01:53 -07002322 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
2323 sendThumbnail = r.app.thread;
2324 r.thumbnailNeeded = false;
2325 }
2326
2327 // If this activity is fullscreen, set up to hide those under it.
2328
2329 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
2330 ensureActivitiesVisibleLocked(null, 0);
2331
2332 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002333 if (!mService.mBooted && mStackSupervisor.isFrontStack(this)) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002334 mService.mBooted = true;
2335 enableScreen = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 }
Craig Mautnere79d4262013-04-01 19:01:53 -07002337 } else if (fromTimeout) {
2338 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
2339 }
2340
2341 // Atomically retrieve all of the other things to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002342 stops = mStackSupervisor.processStoppingActivitiesLocked(true);
Craig Mautnere79d4262013-04-01 19:01:53 -07002343 NS = stops != null ? stops.size() : 0;
2344 if ((NF=mFinishingActivities.size()) > 0) {
2345 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2346 mFinishingActivities.clear();
2347 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002348
2349 final ArrayList<ActivityRecord> thumbnails;
2350 final int NT = mCancelledThumbnails.size();
2351 if (NT > 0) {
Craig Mautnere79d4262013-04-01 19:01:53 -07002352 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
2353 mCancelledThumbnails.clear();
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002354 } else {
2355 thumbnails = null;
Craig Mautnere79d4262013-04-01 19:01:53 -07002356 }
2357
Craig Mautnerde4ef022013-04-07 19:01:33 -07002358 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnere79d4262013-04-01 19:01:53 -07002359 booting = mService.mBooting;
2360 mService.mBooting = false;
2361 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002362
Craig Mautnere79d4262013-04-01 19:01:53 -07002363 if (mStartingUsers.size() > 0) {
2364 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2365 mStartingUsers.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366 }
2367
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002368 // Perform the following actions from unsynchronized state.
2369 final IApplicationThread thumbnailThread = sendThumbnail;
2370 mHandler.post(new Runnable() {
2371 @Override
2372 public void run() {
2373 if (thumbnailThread != null) {
2374 try {
2375 thumbnailThread.requestThumbnail(token);
2376 } catch (Exception e) {
2377 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
2378 mService.sendPendingThumbnail(null, token, null, null, true);
2379 }
2380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002382 // Report back to any thumbnail receivers.
2383 for (int i = 0; i < NT; i++) {
2384 ActivityRecord r = thumbnails.get(i);
2385 mService.sendPendingThumbnail(r, null, null, null, true);
2386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002388 });
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389
2390 // Stop any activities that are scheduled to do so but have been
2391 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002392 for (int i = 0; i < NS; i++) {
Craig Mautnere79d4262013-04-01 19:01:53 -07002393 r = stops.get(i);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002394 if (r.finishing) {
2395 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2396 } else {
2397 stopActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 }
2399 }
2400
2401 // Finish any activities that are scheduled to do so but have been
2402 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002403 for (int i = 0; i < NF; i++) {
Craig Mautnere79d4262013-04-01 19:01:53 -07002404 r = finishes.get(i);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002405 activityRemoved |= destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002406 }
2407
2408 if (booting) {
2409 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002410 } else if (startingUsers != null) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002411 for (int i = 0; i < startingUsers.size(); i++) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002412 mService.finishUserSwitch(startingUsers.get(i));
2413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002414 }
2415
2416 mService.trimApplications();
2417 //dump();
2418 //mWindowManager.dump();
2419
2420 if (enableScreen) {
2421 mService.enableScreenAfterBoot();
2422 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002423
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002424 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002425 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002426 }
2427
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002428 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 }
2430
2431 /**
2432 * @return Returns true if the activity is being finished, false if for
2433 * some reason it is being left as-is.
2434 */
2435 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002436 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002437 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002438 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002439 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002440 + ", result=" + resultCode + ", data=" + resultData
2441 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002442 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002443 return false;
2444 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002445
Craig Mautnerd44711d2013-02-23 11:24:36 -08002446 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002447 return true;
2448 }
2449
Craig Mautnerd2328952013-03-05 12:46:26 -08002450 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002451 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2452 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2453 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2454 ActivityRecord r = activities.get(activityNdx);
2455 if (r.resultTo == self && r.requestCode == requestCode) {
2456 if ((r.resultWho == null && resultWho == null) ||
2457 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2458 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2459 false);
2460 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002461 }
2462 }
2463 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002464 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002465 }
2466
Craig Mautner05d62722013-02-11 09:39:27 -08002467 final void finishTopRunningActivityLocked(ProcessRecord app) {
2468 ActivityRecord r = topRunningActivityLocked(null);
2469 if (r != null && r.app == app) {
2470 // If the top running activity is from this crashing
2471 // process, then terminate it to avoid getting in a loop.
2472 Slog.w(TAG, " Force finishing activity "
2473 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002474 int taskNdx = mTaskHistory.indexOf(r.task);
2475 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002476 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d62722013-02-11 09:39:27 -08002477 // Also terminate any activities below it that aren't yet
2478 // stopped, to avoid a situation where one will get
2479 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002480 --activityNdx;
2481 if (activityNdx < 0) {
2482 do {
2483 --taskNdx;
2484 if (taskNdx < 0) {
2485 break;
2486 }
2487 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2488 } while (activityNdx < 0);
2489 }
2490 if (activityNdx >= 0) {
2491 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d62722013-02-11 09:39:27 -08002492 if (r.state == ActivityState.RESUMED
2493 || r.state == ActivityState.PAUSING
2494 || r.state == ActivityState.PAUSED) {
2495 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2496 Slog.w(TAG, " Force finishing activity "
2497 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002498 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d62722013-02-11 09:39:27 -08002499 }
2500 }
2501 }
2502 }
2503 }
2504
Craig Mautnerd2328952013-03-05 12:46:26 -08002505 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002506 ArrayList<ActivityRecord> activities = r.task.mActivities;
2507 for (int index = activities.indexOf(r); index >= 0; --index) {
2508 ActivityRecord cur = activities.get(index);
2509 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002510 break;
2511 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002512 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002513 }
2514 return true;
2515 }
2516
Dianne Hackborn5c607432012-02-28 14:44:19 -08002517 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2518 // send the result
2519 ActivityRecord resultTo = r.resultTo;
2520 if (resultTo != null) {
2521 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2522 + " who=" + r.resultWho + " req=" + r.requestCode
2523 + " res=" + resultCode + " data=" + resultData);
2524 if (r.info.applicationInfo.uid > 0) {
2525 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2526 resultTo.packageName, resultData,
2527 resultTo.getUriPermissionsLocked());
2528 }
2529 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2530 resultData);
2531 r.resultTo = null;
2532 }
2533 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2534
2535 // Make sure this HistoryRecord is not holding on to other resources,
2536 // because clients have remote IPC references to this object so we
2537 // can't assume that will go away and want to avoid circular IPC refs.
2538 r.results = null;
2539 r.pendingResults = null;
2540 r.newIntents = null;
2541 r.icicle = null;
2542 }
2543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 /**
2545 * @return Returns true if this activity has been removed from the history
2546 * list, or false if it is still in the list and will be removed later.
2547 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08002548 final boolean finishActivityLocked(ActivityRecord r, int resultCode,
2549 Intent resultData, String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 if (r.finishing) {
2551 Slog.w(TAG, "Duplicate finish request for " + r);
2552 return false;
2553 }
2554
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002555 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002556 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002557 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002559 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2560 final int index = activities.indexOf(r);
2561 if (index < (activities.size() - 1)) {
2562 ActivityRecord next = activities.get(index+1);
2563 if (r.frontOfTask) {
2564 // The next activity is now the front of the task.
2565 next.frontOfTask = true;
2566 }
2567 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2568 // If the caller asked that this activity (and all above it)
2569 // be cleared when the task is reset, don't lose that information,
2570 // but propagate it up to the next activity.
2571 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002572 }
2573 }
2574
2575 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002576 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002577 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002578 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579 }
2580 }
2581
Dianne Hackborn5c607432012-02-28 14:44:19 -08002582 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002583
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002584 if (mService.mPendingThumbnails.size() > 0) {
2585 // There are clients waiting to receive thumbnails so, in case
2586 // this is an activity that someone is waiting for, add it
2587 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002588 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002589 }
2590
Craig Mautnerde4ef022013-04-07 19:01:33 -07002591 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002592 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 if (DEBUG_TRANSITION) Slog.v(TAG,
2594 "Prepare close transition: finishing " + r);
2595 mService.mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002596 ? AppTransition.TRANSIT_TASK_CLOSE
2597 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002598
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08002600 mService.mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002601
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002602 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002603 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2604 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2605 startPausingLocked(false, false);
2606 }
2607
2608 } else if (r.state != ActivityState.PAUSING) {
2609 // If the activity is PAUSING, we will complete the finish once
2610 // it is done pausing; else we can just directly finish it here.
2611 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002612 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002613 } else {
2614 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2615 }
2616
2617 return false;
2618 }
2619
2620 private static final int FINISH_IMMEDIATELY = 0;
2621 private static final int FINISH_AFTER_PAUSE = 1;
2622 private static final int FINISH_AFTER_VISIBLE = 2;
2623
2624 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002625 int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002626 // First things first: if this activity is currently visible,
2627 // and the resumed activity is not yet visible, then hold off on
2628 // finishing until the resumed one becomes visible.
2629 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002630 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2631 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002632 if (mStackSupervisor.mStoppingActivities.size() > 3
2633 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002634 // If we already have a few activities waiting to stop,
2635 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002636 // them out. Or if r is the last of activity of the last task the stack
2637 // will be empty and must be cleared immediately.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002638 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002639 } else {
2640 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 }
2642 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002643 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2644 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002645 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002646 if (oomAdj) {
2647 mService.updateOomAdjLocked();
2648 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002649 return r;
2650 }
2651
2652 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002653 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002654 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002655 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 if (mResumedActivity == r) {
2657 mResumedActivity = null;
2658 }
2659 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002660 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 r.state = ActivityState.FINISHING;
2662
2663 if (mode == FINISH_IMMEDIATELY
2664 || prevState == ActivityState.STOPPED
2665 || prevState == ActivityState.INITIALIZING) {
2666 // If this activity is already stopped, we can just finish
2667 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002668 boolean activityRemoved = destroyActivityLocked(r, true,
2669 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002670 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002671 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002672 }
2673 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 }
Craig Mautner05d62722013-02-11 09:39:27 -08002675
2676 // Need to go through the full pause cycle to get this
2677 // activity into the stopped state and then finish it.
2678 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
2679 mFinishingActivities.add(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002680 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002681 return r;
2682 }
2683
Craig Mautnerd2328952013-03-05 12:46:26 -08002684 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d62722013-02-11 09:39:27 -08002685 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002686 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002687 final TaskRecord task = srec.task;
2688 final ArrayList<ActivityRecord> activities = task.mActivities;
2689 final int start = activities.indexOf(srec);
2690 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d62722013-02-11 09:39:27 -08002691 return false;
2692 }
2693 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002694 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d62722013-02-11 09:39:27 -08002695 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002696 final ComponentName dest = destIntent.getComponent();
2697 if (start > 0 && dest != null) {
2698 for (int i = finishTo; i >= 0; i--) {
2699 ActivityRecord r = activities.get(i);
2700 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d62722013-02-11 09:39:27 -08002701 r.info.name.equals(dest.getClassName())) {
2702 finishTo = i;
2703 parent = r;
2704 foundParentInTask = true;
2705 break;
2706 }
2707 }
2708 }
2709
2710 IActivityController controller = mService.mController;
2711 if (controller != null) {
2712 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2713 if (next != null) {
2714 // ask watcher if this is allowed
2715 boolean resumeOK = true;
2716 try {
2717 resumeOK = controller.activityResuming(next.packageName);
2718 } catch (RemoteException e) {
2719 mService.mController = null;
2720 }
2721
2722 if (!resumeOK) {
2723 return false;
2724 }
2725 }
2726 }
2727 final long origId = Binder.clearCallingIdentity();
2728 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002729 ActivityRecord r = activities.get(i);
2730 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d62722013-02-11 09:39:27 -08002731 // Only return the supplied result for the first activity finished
2732 resultCode = Activity.RESULT_CANCELED;
2733 resultData = null;
2734 }
2735
2736 if (parent != null && foundParentInTask) {
2737 final int parentLaunchMode = parent.info.launchMode;
2738 final int destIntentFlags = destIntent.getFlags();
2739 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2740 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2741 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2742 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2743 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2744 } else {
2745 try {
2746 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2747 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002748 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d62722013-02-11 09:39:27 -08002749 null, aInfo, parent.appToken, null,
2750 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2751 0, null, true, null);
2752 foundParentInTask = res == ActivityManager.START_SUCCESS;
2753 } catch (RemoteException e) {
2754 foundParentInTask = false;
2755 }
2756 requestFinishActivityLocked(parent.appToken, resultCode,
2757 resultData, "navigate-up", true);
2758 }
2759 }
2760 Binder.restoreCallingIdentity(origId);
2761 return foundParentInTask;
2762 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 /**
2764 * Perform the common clean-up of an activity record. This is called both
2765 * as part of destroyActivityLocked() (when destroying the client-side
2766 * representation) and cleaning things up as a result of its hosting
2767 * processing going away, in which case there is no remaining client-side
2768 * state to destroy so only the cleanup here is needed.
2769 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002770 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2771 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 if (mResumedActivity == r) {
2773 mResumedActivity = null;
2774 }
2775 if (mService.mFocusedActivity == r) {
2776 mService.mFocusedActivity = null;
2777 }
2778
2779 r.configDestroy = false;
2780 r.frozenBeforeDestroy = false;
2781
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002782 if (setState) {
2783 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2784 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002785 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002786 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002787 }
2788
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 // Make sure this record is no longer in the pending finishes list.
2790 // This could happen, for example, if we are trimming activities
2791 // down to the max limit while they are still waiting to finish.
2792 mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002793 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002794
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 // Remove any pending results.
2796 if (r.finishing && r.pendingResults != null) {
2797 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2798 PendingIntentRecord rec = apr.get();
2799 if (rec != null) {
2800 mService.cancelIntentSenderLocked(rec, false);
2801 }
2802 }
2803 r.pendingResults = null;
2804 }
2805
2806 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002807 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002808 }
2809
2810 if (mService.mPendingThumbnails.size() > 0) {
2811 // There are clients waiting to receive thumbnails so, in case
2812 // this is an activity that someone is waiting for, add it
2813 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002814 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002815 }
2816
2817 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002818 removeTimeoutsForActivityLocked(r);
2819 }
2820
2821 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002822 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002823 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002825 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002826 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 }
2828
Dianne Hackborn5c607432012-02-28 14:44:19 -08002829 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002830 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2831 r.makeFinishing();
2832 if (DEBUG_ADD_REMOVE) {
2833 RuntimeException here = new RuntimeException("here");
2834 here.fillInStackTrace();
2835 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002836 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002837 final TaskRecord task = r.task;
2838 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002839 if (DEBUG_STACK) Slog.i(TAG,
2840 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002841 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002842 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002843 r.takeFromHistory();
2844 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002845 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002846 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002847 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002848 r.app = null;
2849 mService.mWindowManager.removeAppToken(r.appToken);
2850 if (VALIDATE_TOKENS) {
2851 validateAppTokensLocked();
2852 }
2853 cleanUpActivityServicesLocked(r);
2854 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002855 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002856
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857 /**
2858 * Perform clean-up of service connections in an activity record.
2859 */
2860 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2861 // Throw away any services that have been bound by this activity.
2862 if (r.connections != null) {
2863 Iterator<ConnectionRecord> it = r.connections.iterator();
2864 while (it.hasNext()) {
2865 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002866 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 }
2868 r.connections = null;
2869 }
2870 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002871
2872 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2873 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2874 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2875 mHandler.sendMessage(msg);
2876 }
2877
Dianne Hackborn28695e02011-11-02 21:59:51 -07002878 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002879 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002880 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002881 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2882 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2883 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2884 final ActivityRecord r = activities.get(activityNdx);
2885 if (r.finishing) {
2886 continue;
2887 }
2888 if (r.fullscreen) {
2889 lastIsOpaque = true;
2890 }
2891 if (owner != null && r.app != owner) {
2892 continue;
2893 }
2894 if (!lastIsOpaque) {
2895 continue;
2896 }
2897 // We can destroy this one if we have its icicle saved and
2898 // it is not in the process of pausing/stopping/finishing.
2899 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2900 && r.haveState && !r.visible && r.stopped
2901 && r.state != ActivityState.DESTROYING
2902 && r.state != ActivityState.DESTROYED) {
2903 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2904 + " resumed=" + mResumedActivity
2905 + " pausing=" + mPausingActivity);
2906 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2907 activityRemoved = true;
2908 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002909 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002910 }
2911 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002912 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002913 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002914 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002915 }
2916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 /**
2918 * Destroy the current CLIENT SIDE instance of an activity. This may be
2919 * called both when actually finishing an activity, or when performing
2920 * a configuration switch where we destroy the current client-side object
2921 * but then create a new client-side object for this same HistoryRecord.
2922 */
2923 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002924 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002925 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002926 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2928 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002929 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002930 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931
2932 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002933
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002934 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935
2936 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002937
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 if (hadApp) {
2939 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002940 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2942 mService.mHeavyWeightProcess = null;
2943 mService.mHandler.sendEmptyMessage(
2944 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2945 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002946 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002947 // No longer have activities, so update oom adj.
2948 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 }
2950 }
2951
2952 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002953
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002954 try {
2955 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002956 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 r.configChangeFlags);
2958 } catch (Exception e) {
2959 // We can just ignore exceptions here... if the process
2960 // has crashed, our death notification will clean things
2961 // up.
2962 //Slog.w(TAG, "Exception thrown during finish", e);
2963 if (r.finishing) {
2964 removeActivityFromHistoryLocked(r);
2965 removedFromHistory = true;
2966 skipDestroy = true;
2967 }
2968 }
2969
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002971
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002972 // If the activity is finishing, we need to wait on removing it
2973 // from the list to give it a chance to do its cleanup. During
2974 // that time it may make calls back with its token so we need to
2975 // be able to find it on the list and so we don't want to remove
2976 // it from the list yet. Otherwise, we can just immediately put
2977 // it in the destroyed state since we are not removing it from the
2978 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002980 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2981 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 r.state = ActivityState.DESTROYING;
2983 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
2984 msg.obj = r;
2985 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2986 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002987 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2988 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002990 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002991 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 }
2993 } else {
2994 // remove this record from the history.
2995 if (r.finishing) {
2996 removeActivityFromHistoryLocked(r);
2997 removedFromHistory = true;
2998 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002999 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3000 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003001 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003002 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003003 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003004 }
3005 }
3006
3007 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003008
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 if (!mLRUActivities.remove(r) && hadApp) {
3010 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3011 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003012
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 return removedFromHistory;
3014 }
3015
Craig Mautnerd2328952013-03-05 12:46:26 -08003016 final void activityDestroyedLocked(IBinder token) {
3017 final long origId = Binder.clearCallingIdentity();
3018 try {
3019 ActivityRecord r = ActivityRecord.forToken(token);
3020 if (r != null) {
3021 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003023
3024 if (isInStackLocked(token) != null) {
3025 if (r.state == ActivityState.DESTROYING) {
3026 cleanUpActivityLocked(r, true, false);
3027 removeActivityFromHistoryLocked(r);
3028 }
3029 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003030 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Craig Mautnerd2328952013-03-05 12:46:26 -08003031 } finally {
3032 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 }
3034 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003035
Craig Mautner05d62722013-02-11 09:39:27 -08003036 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3037 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003039 if (DEBUG_CLEANUP) Slog.v(
3040 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003041 + " with " + i + " entries");
3042 while (i > 0) {
3043 i--;
Craig Mautner05d62722013-02-11 09:39:27 -08003044 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003045 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003046 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003047 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003049 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 }
3051 }
3052 }
3053
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003054 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3055 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003056 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3057 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003058 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003059 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003060 "mWaitingVisibleActivities");
3061 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
3062
3063 boolean hasVisibleActivities = false;
3064
3065 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003066 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003067 if (DEBUG_CLEANUP) Slog.v(
3068 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003069 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3070 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3071 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3072 final ActivityRecord r = activities.get(activityNdx);
3073 --i;
3074 if (DEBUG_CLEANUP) Slog.v(
3075 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3076 if (r.app == app) {
3077 boolean remove;
3078 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3079 // Don't currently have state for the activity, or
3080 // it is finishing -- always remove it.
3081 remove = true;
3082 } else if (r.launchCount > 2 &&
3083 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3084 // We have launched this activity too many times since it was
3085 // able to run, so give up and remove it.
3086 remove = true;
3087 } else {
3088 // The process may be gone, but the activity lives on!
3089 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003090 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003091 if (remove) {
3092 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
3093 RuntimeException here = new RuntimeException("here");
3094 here.fillInStackTrace();
3095 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3096 + ": haveState=" + r.haveState
3097 + " stateNotNeeded=" + r.stateNotNeeded
3098 + " finishing=" + r.finishing
3099 + " state=" + r.state, here);
3100 }
3101 if (!r.finishing) {
3102 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3103 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3104 r.userId, System.identityHashCode(r),
3105 r.task.taskId, r.shortComponentName,
3106 "proc died without state saved");
3107 }
3108 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003109
Craig Mautner0247fc82013-02-28 14:32:06 -08003110 } else {
3111 // We have the current state for this activity, so
3112 // it can be restarted later when needed.
3113 if (localLOGV) Slog.v(
3114 TAG, "Keeping entry, setting app to null");
3115 if (r.visible) {
3116 hasVisibleActivities = true;
3117 }
3118 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3119 + r);
3120 r.app = null;
3121 r.nowVisible = false;
3122 if (!r.haveState) {
3123 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
3124 "App died, clearing saved state of " + r);
3125 r.icicle = null;
3126 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003127 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003128
Craig Mautnerd2328952013-03-05 12:46:26 -08003129 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003130 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003131 }
3132 }
3133
3134 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003136
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003137 final void updateTransitLocked(int transit, Bundle options) {
3138 if (options != null) {
3139 ActivityRecord r = topRunningActivityLocked(null);
3140 if (r != null && r.state != ActivityState.RESUMED) {
3141 r.updateOptionsLocked(options);
3142 } else {
3143 ActivityOptions.abort(options);
3144 }
3145 }
3146 mService.mWindowManager.prepareAppTransition(transit, false);
3147 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003148
Craig Mautnercae015f2013-02-08 14:31:27 -08003149 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003150 final TaskRecord task = taskForIdLocked(taskId);
3151 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003152 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003153 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003154 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003155 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3156 // Caller wants the home activity moved with it. To accomplish this,
3157 // we'll just move the home task to the top first.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003158 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003159 }
3160 moveTaskToFrontLocked(task, null, options);
3161 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003162 }
3163 return false;
3164 }
3165
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003166 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003167 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168
Craig Mautner11bf9a52013-02-19 14:08:51 -08003169 final int numTasks = mTaskHistory.size();
3170 final int index = mTaskHistory.indexOf(tr);
3171 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
3172 // nothing to do!
3173 if (reason != null &&
3174 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3175 ActivityOptions.abort(options);
3176 } else {
3177 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3178 }
3179 return;
3180 }
3181
3182 // Shift all activities with this task up to the top
3183 // of the stack, keeping them in the same internal order.
3184 mTaskHistory.remove(tr);
3185 mTaskHistory.add(tr);
3186
3187 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 if (reason != null &&
3189 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003190 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 ActivityRecord r = topRunningActivityLocked(null);
3192 if (r != null) {
3193 mNoAnimActivities.add(r);
3194 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003195 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003197 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003199
Craig Mautner58547802013-03-05 08:23:53 -08003200 mService.mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201
Craig Mautnerde4ef022013-04-07 19:01:33 -07003202 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Craig Mautner58547802013-03-05 08:23:53 -08003203 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003204
3205 if (VALIDATE_TOKENS) {
3206 validateAppTokensLocked();
3207 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 }
3209
3210 /**
3211 * Worker method for rearranging history stack. Implements the function of moving all
3212 * activities for a specific task (gathering them if disjoint) into a single group at the
3213 * bottom of the stack.
3214 *
3215 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3216 * to premeptively cancel the move.
3217 *
3218 * @param task The taskId to collect and move to the bottom.
3219 * @return Returns true if the move completed, false if not.
3220 */
3221 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3222 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003223
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 // If we have a watcher, preflight the move before committing to it. First check
3225 // for *other* available tasks, but if none are available, then try again allowing the
3226 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003227 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 ActivityRecord next = topRunningActivityLocked(null, task);
3229 if (next == null) {
3230 next = topRunningActivityLocked(null, 0);
3231 }
3232 if (next != null) {
3233 // ask watcher if this is allowed
3234 boolean moveOK = true;
3235 try {
3236 moveOK = mService.mController.activityResuming(next.packageName);
3237 } catch (RemoteException e) {
3238 mService.mController = null;
3239 }
3240 if (!moveOK) {
3241 return false;
3242 }
3243 }
3244 }
3245
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003246 if (DEBUG_TRANSITION) Slog.v(TAG,
3247 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248
Craig Mautnerd2328952013-03-05 12:46:26 -08003249 final TaskRecord tr = taskForIdLocked(task);
3250 if (tr == null) {
3251 return false;
3252 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003253 mTaskHistory.remove(tr);
3254 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003255
3256 if (reason != null &&
3257 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003258 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003259 ActivityRecord r = topRunningActivityLocked(null);
3260 if (r != null) {
3261 mNoAnimActivities.add(r);
3262 }
3263 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08003264 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003265 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003267 mService.mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003269 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003270 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003271 }
3272
Craig Mautnerde4ef022013-04-07 19:01:33 -07003273 if (mResumedActivity != null && mResumedActivity.task == tr &&
3274 mResumedActivity.mLaunchHomeTaskNext) {
3275 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003276 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003277 }
3278
3279 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003280 return true;
3281 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003282
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003283 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003284 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003285 if (mResumedActivity != null && mResumedActivity.thumbHolder == tr) {
3286 info.mainThumbnail = screenshotActivities(mResumedActivity);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003287 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003288 if (info.mainThumbnail == null) {
3289 info.mainThumbnail = tr.lastThumbnail;
3290 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003291 return info;
3292 }
3293
Dianne Hackborn15491c62012-09-19 10:59:14 -07003294 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003295 if (mResumedActivity != null && mResumedActivity.task == tr) {
Dianne Hackborn15491c62012-09-19 10:59:14 -07003296 // This task is the current resumed task, we just need to take
3297 // a screenshot of it and return that.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003298 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003299 }
3300 // Return the information about the task, to figure out the top
3301 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003302 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003303 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003304 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003305 }
Craig Mautner05d62722013-02-11 09:39:27 -08003306 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003307 }
3308
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003309 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3310 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003311 final TaskRecord task = taskForIdLocked(taskId);
3312 if (task == null) {
3313 return null;
3314 }
Craig Mautner16030772013-02-26 16:05:47 -08003315 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003316 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003317 if (taskRequired) {
3318 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3319 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003320 return null;
3321 }
3322
3323 if (subTaskIndex < 0) {
3324 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003325 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003326 return info.root;
3327 }
3328
3329 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003330 if (taskRequired) {
3331 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3332 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003333 return null;
3334 }
3335
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003336 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003337 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003338 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003339 return subtask.activity;
3340 }
3341
Craig Mautner16030772013-02-26 16:05:47 -08003342 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003343 final TaskAccessInfo thumbs = new TaskAccessInfo();
3344 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003345 final ArrayList<ActivityRecord> activities = task.mActivities;
3346 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003347 int j = 0;
3348 ThumbnailHolder holder = null;
3349 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003350 ActivityRecord ar = activities.get(j);
3351 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003352 thumbs.root = ar;
3353 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003354 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003355 if (holder != null) {
3356 thumbs.mainThumbnail = holder.lastThumbnail;
3357 }
3358 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003359 break;
3360 }
3361 j++;
3362 }
3363
3364 if (j >= NA) {
3365 return thumbs;
3366 }
3367
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003368 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
3369 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003370 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003371 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003372 j++;
3373 if (ar.finishing) {
3374 continue;
3375 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003376 if (ar.thumbHolder != holder && holder != null) {
3377 thumbs.numSubThumbbails++;
3378 holder = ar.thumbHolder;
3379 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07003380 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003381 sub.activity = ar;
3382 sub.index = j-1;
3383 subtasks.add(sub);
3384 }
3385 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003386 if (thumbs.numSubThumbbails > 0) {
3387 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d62722013-02-11 09:39:27 -08003388 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003389 public Bitmap getThumbnail(int index) {
3390 if (index < 0 || index >= thumbs.subtasks.size()) {
3391 return null;
3392 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003393 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003394 if (mResumedActivity != null && mResumedActivity.thumbHolder == sub.holder) {
3395 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003396 }
3397 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003398 }
3399 };
3400 }
3401 return thumbs;
3402 }
3403
Craig Mautner8849a5e2013-04-02 16:41:03 -07003404 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003405 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003406 final Uri data = r.intent.getData();
3407 final String strData = data != null ? data.toSafeString() : null;
3408
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003409 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003410 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003411 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003412 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003413 }
3414
3415 /**
3416 * Make sure the given activity matches the current configuration. Returns
3417 * false if the activity had to be destroyed. Returns true if the
3418 * configuration is the same, or the activity will remain running as-is
3419 * for whatever reason. Ensures the HistoryRecord is updated with the
3420 * correct configuration and all other bookkeeping is handled.
3421 */
3422 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3423 int globalChanges) {
3424 if (mConfigWillChange) {
3425 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3426 "Skipping config check (will change): " + r);
3427 return true;
3428 }
3429
3430 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3431 "Ensuring correct configuration: " + r);
3432
3433 // Short circuit: if the two configurations are the exact same
3434 // object (the common case), then there is nothing to do.
3435 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003436 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003437 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3438 "Configuration unchanged in " + r);
3439 return true;
3440 }
3441
3442 // We don't worry about activities that are finishing.
3443 if (r.finishing) {
3444 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3445 "Configuration doesn't matter in finishing " + r);
3446 r.stopFreezingScreenLocked(false);
3447 return true;
3448 }
3449
3450 // Okay we now are going to make this activity have the new config.
3451 // But then we need to figure out how it needs to deal with that.
3452 Configuration oldConfig = r.configuration;
3453 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003454
3455 // Determine what has changed. May be nothing, if this is a config
3456 // that has come back from the app after going idle. In that case
3457 // we just want to leave the official config object now in the
3458 // activity and do nothing else.
3459 final int changes = oldConfig.diff(newConfig);
3460 if (changes == 0 && !r.forceNewConfig) {
3461 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3462 "Configuration no differences in " + r);
3463 return true;
3464 }
3465
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003466 // If the activity isn't currently running, just leave the new
3467 // configuration and it will pick that up next time it starts.
3468 if (r.app == null || r.app.thread == null) {
3469 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3470 "Configuration doesn't matter not running " + r);
3471 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003472 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 return true;
3474 }
3475
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003476 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003477 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3478 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3479 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003480 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003481 + ", newConfig=" + newConfig);
3482 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003483 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003484 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3485 r.configChangeFlags |= changes;
3486 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003487 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003488 if (r.app == null || r.app.thread == null) {
3489 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003490 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003491 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003492 } else if (r.state == ActivityState.PAUSING) {
3493 // A little annoying: we are waiting for this activity to
3494 // finish pausing. Let's not do anything now, but just
3495 // flag that it needs to be restarted when done pausing.
3496 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003497 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003498 r.configDestroy = true;
3499 return true;
3500 } else if (r.state == ActivityState.RESUMED) {
3501 // Try to optimize this case: the configuration is changing
3502 // and we need to restart the top, resumed activity.
3503 // Instead of doing the normal handshaking, just say
3504 // "restart!".
3505 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003506 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003507 relaunchActivityLocked(r, r.configChangeFlags, true);
3508 r.configChangeFlags = 0;
3509 } else {
3510 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003511 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003512 relaunchActivityLocked(r, r.configChangeFlags, false);
3513 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003514 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003515
3516 // All done... tell the caller we weren't able to keep this
3517 // activity around.
3518 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003519 }
3520
3521 // Default case: the activity can handle this new configuration, so
3522 // hand it over. Note that we don't need to give it the new
3523 // configuration, since we always send configuration changes to all
3524 // process when they happen so it can just use whatever configuration
3525 // it last got.
3526 if (r.app != null && r.app.thread != null) {
3527 try {
3528 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003529 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 } catch (RemoteException e) {
3531 // If process died, whatever.
3532 }
3533 }
3534 r.stopFreezingScreenLocked(false);
3535
3536 return true;
3537 }
3538
3539 private final boolean relaunchActivityLocked(ActivityRecord r,
3540 int changes, boolean andResume) {
3541 List<ResultInfo> results = null;
3542 List<Intent> newIntents = null;
3543 if (andResume) {
3544 results = r.results;
3545 newIntents = r.newIntents;
3546 }
3547 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3548 + " with results=" + results + " newIntents=" + newIntents
3549 + " andResume=" + andResume);
3550 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003551 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003552 r.task.taskId, r.shortComponentName);
3553
3554 r.startFreezingScreenLocked(r.app, 0);
3555
3556 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003557 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3558 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3559 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003560 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003561 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a2011-11-14 17:45:19 -08003562 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003563 // Note: don't need to call pauseIfSleepingLocked() here, because
3564 // the caller will only pass in 'andResume' if this activity is
3565 // currently resumed, which implies we aren't sleeping.
3566 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003567 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 }
3569
3570 if (andResume) {
3571 r.results = null;
3572 r.newIntents = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003573 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 mService.reportResumedActivityLocked(r);
3575 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003576 r.state = ActivityState.RESUMED;
3577 } else {
3578 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3579 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003580 }
3581
3582 return true;
3583 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003584
3585 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003586 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3587 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3588 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3589 final ActivityRecord r = activities.get(activityNdx);
3590 if (r.appToken == token) {
3591 return true;
3592 }
3593 if (r.fullscreen && !r.finishing) {
3594 return false;
3595 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003596 }
3597 }
3598 return true;
3599 }
3600
3601 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003602 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3603 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3604 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3605 final ActivityRecord r = activities.get(activityNdx);
3606 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003607 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003608 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003609 }
3610 }
3611 }
3612
3613 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3614 boolean didSomething = false;
3615 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003616 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3617 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3618 int numActivities = activities.size();
3619 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3620 ActivityRecord r = activities.get(activityNdx);
3621 final boolean samePackage = r.packageName.equals(name)
3622 || (name == null && r.userId == userId);
3623 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3624 && (samePackage || r.task == lastTask)
3625 && (r.app == null || evenPersistent || !r.app.persistent)) {
3626 if (!doit) {
3627 if (r.finishing) {
3628 // If this activity is just finishing, then it is not
3629 // interesting as far as something to stop.
3630 continue;
3631 }
3632 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003633 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003634 didSomething = true;
3635 Slog.i(TAG, " Force finishing activity " + r);
3636 if (samePackage) {
3637 if (r.app != null) {
3638 r.app.removed = true;
3639 }
3640 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003641 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003642 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08003643 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08003644 }
3645 }
3646 }
3647 return didSomething;
3648 }
3649
3650 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003651 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003652 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003653 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3654 --maxNum, --taskNdx) {
3655 final TaskRecord task = mTaskHistory.get(taskNdx);
3656 ActivityRecord r = null;
3657 ActivityRecord top = null;
3658 int numActivities = 0;
3659 int numRunning = 0;
3660 final ArrayList<ActivityRecord> activities = task.mActivities;
3661 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3662 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003663
Craig Mautneraab647e2013-02-28 16:31:36 -08003664 // Initialize state for next task if needed.
3665 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3666 top = r;
3667 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003668 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003669
3670 // Add 'r' into the current task.
3671 numActivities++;
3672 if (r.app != null && r.app.thread != null) {
3673 numRunning++;
3674 }
3675
3676 if (localLOGV) Slog.v(
3677 TAG, r.intent.getComponent().flattenToShortString()
3678 + ": task=" + r.task);
3679 }
3680
3681 RunningTaskInfo ci = new RunningTaskInfo();
3682 ci.id = task.taskId;
3683 ci.baseActivity = r.intent.getComponent();
3684 ci.topActivity = top.intent.getComponent();
3685 if (top.thumbHolder != null) {
3686 ci.description = top.thumbHolder.lastDescription;
3687 }
3688 ci.numActivities = numActivities;
3689 ci.numRunning = numRunning;
3690 //System.out.println(
3691 // "#" + maxNum + ": " + " descr=" + ci.description);
3692 if (receiver != null) {
3693 if (localLOGV) Slog.v(
3694 TAG, "State=" + top.state + "Idle=" + top.idle
3695 + " app=" + top.app
3696 + " thr=" + (top.app != null ? top.app.thread : null));
3697 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3698 if (top.idle && top.app != null && top.app.thread != null) {
3699 topRecord = top;
3700 } else {
3701 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003702 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003703 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003704 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003705 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003706 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003707 }
3708 return topRecord;
3709 }
3710
3711 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003712 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003713 if (DEBUG_SWITCH) Slog.d(
3714 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003715 if (top >= 0) {
3716 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3717 int activityTop = activities.size() - 1;
3718 if (activityTop > 0) {
3719 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3720 "unhandled-back", true);
3721 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003722 }
3723 }
3724
Craig Mautnere79d4262013-04-01 19:01:53 -07003725 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
3726 if (mPausingActivity != null && mPausingActivity.app == app) {
3727 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3728 "App died while pausing: " + mPausingActivity);
3729 mPausingActivity = null;
3730 }
3731 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3732 mLastPausedActivity = null;
3733 }
3734
3735 // Remove this application's activities from active lists.
3736 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3737
3738 if (!restarting) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003739 if (!mStackSupervisor.getTopStack().resumeTopActivityLocked(null)) {
Craig Mautnere79d4262013-04-01 19:01:53 -07003740 // If there was nothing to resume, and we are not already
3741 // restarting this process, but there is a visible activity that
3742 // is hosted by the process... then make sure all visible
3743 // activities are running, taking care of restarting this
3744 // process.
3745 if (hasVisibleActivities) {
3746 ensureActivitiesVisibleLocked(null, 0);
3747 }
3748 }
3749 }
3750 }
3751
Craig Mautnercae015f2013-02-08 14:31:27 -08003752 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003753 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3754 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3755 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3756 final ActivityRecord r = activities.get(activityNdx);
3757 if (r.app == app) {
3758 Slog.w(TAG, " Force finishing activity "
3759 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003760 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003761 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003762 }
3763 }
3764 }
3765
3766 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3767 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003768 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3769 final TaskRecord task = mTaskHistory.get(taskNdx);
3770 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003771 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003772 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3773 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003774 }
3775
3776 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3777 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3778
3779 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003780 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3781 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003782 }
3783 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003784 final int top = mTaskHistory.size() - 1;
3785 if (top >= 0) {
3786 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3787 int listTop = list.size() - 1;
3788 if (listTop >= 0) {
3789 activities.add(list.get(listTop));
3790 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003791 }
3792 } else {
3793 ItemMatcher matcher = new ItemMatcher();
3794 matcher.build(name);
3795
Craig Mautneraab647e2013-02-28 16:31:36 -08003796 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3797 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3798 if (matcher.match(r1, r1.intent.getComponent())) {
3799 activities.add(r1);
3800 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003801 }
3802 }
3803 }
3804
3805 return activities;
3806 }
3807
3808 ActivityRecord restartPackage(String packageName) {
3809 ActivityRecord starting = topRunningActivityLocked(null);
3810
3811 // All activities that came from the package must be
3812 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003813 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3814 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3815 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3816 final ActivityRecord a = activities.get(activityNdx);
3817 if (a.info.packageName.equals(packageName)) {
3818 a.forceNewConfig = true;
3819 if (starting != null && a == starting && a.visible) {
3820 a.startFreezingScreenLocked(starting.app,
3821 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3822 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003823 }
3824 }
3825 }
3826
3827 return starting;
3828 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003829
Craig Mautnerde4ef022013-04-07 19:01:33 -07003830 boolean removeTask(TaskRecord task) {
3831 mTaskHistory.remove(task);
3832 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003833 }
3834
Craig Mautnerde4ef022013-04-07 19:01:33 -07003835 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003836 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003837 if (toTop) {
3838 mTaskHistory.add(task);
3839 } else {
3840 mTaskHistory.add(0, task);
3841 }
3842 return task;
3843 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003844
3845 ArrayList<TaskRecord> getAllTasks() {
3846 return new ArrayList<TaskRecord>(mTaskHistory);
3847 }
3848
3849 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003850 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003851 if (task == null) {
3852 return;
3853 }
3854 task.stack.mTaskHistory.remove(task);
3855 task.stack = this;
3856 if (toTop) {
3857 mTaskHistory.add(task);
3858 } else {
3859 mTaskHistory.add(0, task);
3860 }
3861 }
3862
3863 public int getStackId() {
3864 return mStackId;
3865 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003866
3867 @Override
3868 public String toString() {
3869 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3870 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003871}