blob: 4c7767c40155fa3c442b97f9415a74b7c8791272 [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 Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
34import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
35import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
36import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070037import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
38
Dianne Hackborncee04b52013-07-03 17:01:28 -070039import android.os.Trace;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080041import com.android.internal.util.Objects;
Kenny Rootadd58212013-05-07 09:47:34 -070042import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080043import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070050import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080052import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080053import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
57import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.content.res.Resources;
63import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080064import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080065import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070067import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Handler;
69import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090070import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Message;
72import android.os.PowerManager;
73import android.os.RemoteException;
74import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070075import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
86
87/**
88 * State and management of a single stack of activities.
89 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070090final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080091
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070092 // Ticks during which we check progress while waiting for an app to launch.
93 static final int LAUNCH_TICK = 500;
94
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095 // How long we wait until giving up on the last activity to pause. This
96 // is short because it directly impacts the responsiveness of starting the
97 // next activity.
98 static final int PAUSE_TIMEOUT = 500;
99
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700100 // How long we wait for the activity to tell us it has stopped before
101 // giving up. This is a good amount of time because we really need this
102 // from the application in order to get its saved state.
103 static final int STOP_TIMEOUT = 10*1000;
104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105 // How long we wait until giving up on an activity telling us it has
106 // finished destroying itself.
107 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800110 // disabled.
111 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800112
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700113 // How long between activity launches that we consider safe to not warn
114 // the user about an unexpected activity being launched on top.
115 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // Set to false to disable the preview that is shown while a new activity
118 // is being started.
119 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800120
Craig Mautner5eda9b32013-07-02 11:58:16 -0700121 // How long to wait for all background Activities to redraw following a call to
122 // convertToTranslucent().
123 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 enum ActivityState {
126 INITIALIZING,
127 RESUMED,
128 PAUSING,
129 PAUSED,
130 STOPPING,
131 STOPPED,
132 FINISHING,
133 DESTROYING,
134 DESTROYED
135 }
136
137 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 /**
143 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144 * running) activities. It contains #TaskRecord objects.
145 */
146 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
147
148 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800149 * Used for validating app tokens with window manager.
150 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800151 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700153 /**
154 * List of running activities, sorted by recent usage.
155 * The first entry in the list is the least recently used.
156 * It contains HistoryRecord objects.
157 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700158 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159
160 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 * Animations that for the current transition have requested not to
162 * be considered for the transition animation.
163 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700164 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167 * When we are in the process of pausing an activity, before starting the
168 * next one, this variable holds the activity that is currently being paused.
169 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800170 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
173 * This is the last activity that we put into the paused state. This is
174 * used to determine if we need to do an activity transition while sleeping,
175 * when we normally hold the top activity paused.
176 */
177 ActivityRecord mLastPausedActivity = null;
178
179 /**
180 * Current activity that is resumed, or null if there is none.
181 */
182 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700185 * This is the last activity that has been started. It is only used to
186 * identify when multiple activities are started at once so that the user
187 * can be warned they may not be in the activity they think they are.
188 */
189 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800190
Craig Mautner5eda9b32013-07-02 11:58:16 -0700191 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
192 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
193 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
194 // Activity in mTranslucentActivityWaiting is notified via
195 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
196 // background activity being drawn then the same call will be made with a true value.
197 ActivityRecord mTranslucentActivityWaiting = null;
198 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
199 new ArrayList<ActivityRecord>();
200
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700201 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202 * Set when we know we are going to be calling updateConfiguration()
203 * soon, so want to skip intermediate config checks.
204 */
205 boolean mConfigWillChange;
206
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700207 long mLaunchStartTime = 0;
208 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800209
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800210 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800211 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
212 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
213 */
214 private ActivityRecord mLastScreenshotActivity = null;
215 private Bitmap mLastScreenshotBitmap = null;
216
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800217 int mThumbnailWidth = -1;
218 int mThumbnailHeight = -1;
219
Craig Mautner858d8a62013-04-23 17:08:34 -0700220 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700221
Craig Mautnerc00204b2013-03-05 15:02:14 -0800222 final int mStackId;
223
Craig Mautner27084302013-03-25 08:05:25 -0700224 /** Run all ActivityStacks through this */
225 final ActivityStackSupervisor mStackSupervisor;
226
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700227 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700228 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
229 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
230 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
231 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700232 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700233
234 static class ScheduleDestroyArgs {
235 final ProcessRecord mOwner;
236 final boolean mOomAdj;
237 final String mReason;
238 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
239 mOwner = owner;
240 mOomAdj = oomAdj;
241 mReason = reason;
242 }
243 }
244
Zoran Marcetaf958b322012-08-09 20:27:12 +0900245 final Handler mHandler;
246
247 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700248 //public Handler() {
249 // if (localLOGV) Slog.v(TAG, "Handler started!");
250 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900251 public ActivityStackHandler(Looper looper) {
252 super(looper);
253 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700254
Zoran Marcetaf958b322012-08-09 20:27:12 +0900255 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256 public void handleMessage(Message msg) {
257 switch (msg.what) {
258 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800259 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700260 // We don't at this point know if the activity is fullscreen,
261 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800262 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700263 synchronized (mService) {
264 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700265 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700266 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700267 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800268 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700270 case LAUNCH_TICK_MSG: {
271 ActivityRecord r = (ActivityRecord)msg.obj;
272 synchronized (mService) {
273 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700274 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700275 }
276 }
277 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280 // We don't at this point know if the activity is fullscreen,
281 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800282 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800283 synchronized (mService) {
284 activityDestroyedLocked(r != null ? r.appToken : null);
285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700287 case STOP_TIMEOUT_MSG: {
288 ActivityRecord r = (ActivityRecord)msg.obj;
289 // We don't at this point know if the activity is fullscreen,
290 // so we need to be conservative and assume it isn't.
291 Slog.w(TAG, "Activity stop timeout for " + r);
292 synchronized (mService) {
293 if (r.isInHistory()) {
294 activityStoppedLocked(r, null, null, null);
295 }
296 }
297 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700298 case DESTROY_ACTIVITIES_MSG: {
299 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
300 synchronized (mService) {
301 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
302 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700303 } break;
304 case TRANSLUCENT_TIMEOUT_MSG: {
305 synchronized (mService) {
306 notifyActivityDrawnLocked(null);
307 }
308 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 }
310 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800311 }
312
Craig Mautner000f0022013-02-26 15:04:29 -0800313 private int numActivities() {
314 int count = 0;
315 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
316 count += mTaskHistory.get(taskNdx).mActivities.size();
317 }
318 return count;
319 }
320
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900322 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700323 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 mWindowManager = service.mWindowManager;
325 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 mContext = context;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700327 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800328 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700330 }
Craig Mautner5962b122012-10-05 14:45:52 -0700331
332 private boolean okToShow(ActivityRecord r) {
333 return r.userId == mCurrentUser
334 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
335 }
336
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700337 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800338 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
339 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800340 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800341 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
342 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800343 if (!r.finishing && r != notTop && okToShow(r)) {
344 return r;
345 }
346 }
347 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700348 return null;
349 }
350
351 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800352 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
353 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800354 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800355 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
356 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800357 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
358 return r;
359 }
360 }
361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362 return null;
363 }
364
365 /**
366 * This is a simplified version of topRunningActivityLocked that provides a number of
367 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800368 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700369 * @param token If non-null, any history records matching this token will be skipped.
370 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800371 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 * @return Returns the HistoryRecord of the next activity on the stack.
373 */
374 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800375 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
376 TaskRecord task = mTaskHistory.get(taskNdx);
377 if (task.taskId == taskId) {
378 continue;
379 }
380 ArrayList<ActivityRecord> activities = task.mActivities;
381 for (int i = activities.size() - 1; i >= 0; --i) {
382 final ActivityRecord r = activities.get(i);
383 // Note: the taskId check depends on real taskId fields being non-zero
384 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
385 return r;
386 }
387 }
388 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700389 return null;
390 }
391
Craig Mautner8849a5e2013-04-02 16:41:03 -0700392 final ActivityRecord topActivity() {
393 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700394 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700395 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
396 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
397 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700398 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700399 }
400 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700401 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700402 }
403
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700404 final TaskRecord topTask() {
405 final int size = mTaskHistory.size();
406 if (size > 0) {
407 return mTaskHistory.get(size - 1);
408 }
409 return null;
410 }
411
Craig Mautnerd2328952013-03-05 12:46:26 -0800412 TaskRecord taskForIdLocked(int id) {
413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
414 final TaskRecord task = mTaskHistory.get(taskNdx);
415 if (task.taskId == id) {
416 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800417 }
418 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700419 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700420 }
421
Craig Mautnerd2328952013-03-05 12:46:26 -0800422 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800423 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800424 if (r != null) {
425 final TaskRecord task = r.task;
426 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
427 if (task.stack != this) Slog.w(TAG,
428 "Illegal state! task does not point to stack it is in.");
429 return r;
430 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800431 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800432 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800433 }
434
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700435 boolean containsApp(ProcessRecord app) {
436 if (app == null) {
437 return false;
438 }
439 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
440 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
441 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
442 final ActivityRecord r = activities.get(activityNdx);
443 if (r.finishing) {
444 continue;
445 }
446 if (r.app == app) {
447 return true;
448 }
449 }
450 }
451 return false;
452 }
453
Craig Mautner2420ead2013-04-01 17:13:20 -0700454 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 final boolean hadit = mLRUActivities.remove(r);
456 mLRUActivities.add(r);
457 return hadit;
458 }
459
Craig Mautnerde4ef022013-04-07 19:01:33 -0700460 final boolean isHomeStack() {
461 return mStackId == HOME_STACK_ID;
462 }
463
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700464 /**
465 * Returns the top activity in any existing task matching the given
466 * Intent. Returns null if no such task is found.
467 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700468 ActivityRecord findTaskLocked(ActivityRecord target) {
469 Intent intent = target.intent;
470 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700471 ComponentName cls = intent.getComponent();
472 if (info.targetActivity != null) {
473 cls = new ComponentName(info.packageName, info.targetActivity);
474 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700475 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800476
477 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
478 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700479 if (task.userId != userId) {
480 // Looking for a different task.
481 continue;
482 }
Craig Mautner000f0022013-02-26 15:04:29 -0800483 final ActivityRecord r = task.getTopActivity();
484 if (r == null || r.finishing || r.userId != userId ||
485 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
486 continue;
487 }
488
489 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
490 // + "/aff=" + r.task.affinity + " to new cls="
491 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
492 if (task.affinity != null) {
493 if (task.affinity.equals(info.taskAffinity)) {
494 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700495 return r;
496 }
Craig Mautner000f0022013-02-26 15:04:29 -0800497 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
498 //Slog.i(TAG, "Found matching class!");
499 //dump();
500 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
501 return r;
502 } else if (task.affinityIntent != null
503 && task.affinityIntent.getComponent().equals(cls)) {
504 //Slog.i(TAG, "Found matching class!");
505 //dump();
506 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
507 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700508 }
509 }
510
511 return null;
512 }
513
514 /**
515 * Returns the first activity (starting from the top of the stack) that
516 * is the same as the given activity. Returns null if no such activity
517 * is found.
518 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700519 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700520 ComponentName cls = intent.getComponent();
521 if (info.targetActivity != null) {
522 cls = new ComponentName(info.packageName, info.targetActivity);
523 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700524 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700525
Craig Mautner000f0022013-02-26 15:04:29 -0800526 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700527 TaskRecord task = mTaskHistory.get(taskNdx);
528 if (task.userId != mCurrentUser) {
529 return null;
530 }
531 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800532 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
533 ActivityRecord r = activities.get(activityNdx);
534 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700535 //Slog.i(TAG, "Found matching class!");
536 //dump();
537 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
538 return r;
539 }
540 }
541 }
542
543 return null;
544 }
545
Amith Yamasani742a6712011-05-04 14:49:28 -0700546 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700547 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700548 * @return whether there are any activities for the specified user.
549 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700550 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800551 if (VALIDATE_TOKENS) {
552 validateAppTokensLocked();
553 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800554 if (mCurrentUser == userId) {
555 return true;
556 }
557 mCurrentUser = userId;
558
559 // Move userId's tasks to the top.
560 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800561 int index = mTaskHistory.size();
562 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700563 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800564 if (task.userId == userId) {
565 haveActivities = true;
566 mTaskHistory.remove(i);
567 mTaskHistory.add(task);
568 --index;
569 }
570 }
571
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700572 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700573 }
574
Craig Mautner2420ead2013-04-01 17:13:20 -0700575 void minimalResumeActivityLocked(ActivityRecord r) {
576 r.state = ActivityState.RESUMED;
577 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
578 + " (starting new instance)");
579 r.stopped = false;
580 mResumedActivity = r;
581 r.task.touchActiveTime();
582 mService.addRecentTaskLocked(r.task);
583 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700584 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700585 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700586 }
587
Dianne Hackborncee04b52013-07-03 17:01:28 -0700588 private void startLaunchTraces() {
589 if (mFullyDrawnStartTime != 0) {
590 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
591 }
592 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
593 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
594 }
595
596 private void stopFullyDrawnTraceIfNeeded() {
597 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
598 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
599 mFullyDrawnStartTime = 0;
600 }
601 }
602
Craig Mautnere79d42682013-04-01 19:01:53 -0700603 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700604 if (r.displayStartTime == 0) {
605 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
606 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700607 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700608 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700609 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700610 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700611 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700612 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700613 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700614 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800615
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700616 void clearLaunchTime(ActivityRecord r) {
617 r.displayStartTime = r.fullyDrawnStartTime = 0;
618 }
619
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800620 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800621 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800622 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
623 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
624 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
625 activities.get(activityNdx).setSleeping(false);
626 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800627 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800628 }
629
Craig Mautner0eea92c2013-05-16 13:35:39 -0700630 /**
631 * @return true if something must be done before going to sleep.
632 */
633 boolean checkReadyForSleepLocked() {
634 if (mResumedActivity != null) {
635 // Still have something resumed; can't sleep until it is paused.
636 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
637 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
638 startPausingLocked(false, true);
639 return true;
640 }
641 if (mPausingActivity != null) {
642 // Still waiting for something to pause; can't sleep yet.
643 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
644 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800645 }
646
Craig Mautner0eea92c2013-05-16 13:35:39 -0700647 return false;
648 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800649
Craig Mautner0eea92c2013-05-16 13:35:39 -0700650 void goToSleep() {
651 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652
Craig Mautner0eea92c2013-05-16 13:35:39 -0700653 // Make sure any stopped but visible activities are now sleeping.
654 // This ensures that the activity's onStop() is called.
655 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
656 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
657 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
658 final ActivityRecord r = activities.get(activityNdx);
659 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
660 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800661 }
662 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800663 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700664 }
Craig Mautner59c00972012-07-30 12:10:24 -0700665
Dianne Hackbornd2835932010-12-13 16:28:46 -0800666 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800667 if (who.noDisplay) {
668 return null;
669 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800670
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800671 Resources res = mService.mContext.getResources();
672 int w = mThumbnailWidth;
673 int h = mThumbnailHeight;
674 if (w < 0) {
675 mThumbnailWidth = w =
676 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
677 mThumbnailHeight = h =
678 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
679 }
680
681 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800682 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
683 || mLastScreenshotBitmap.getWidth() != w
684 || mLastScreenshotBitmap.getHeight() != h) {
685 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700686 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800687 who.appToken, Display.DEFAULT_DISPLAY, w, h);
688 }
689 if (mLastScreenshotBitmap != null) {
690 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
691 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800692 }
693 return null;
694 }
695
Craig Mautnercf910b02013-04-23 11:23:27 -0700696 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800697 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800698 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700699 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800700 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700701 ActivityRecord prev = mResumedActivity;
702 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700703 Slog.e(TAG, "Trying to pause when nothing is resumed",
704 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700705 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700706 return;
707 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700708 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
709 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700710 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800711 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700712 mLastPausedActivity = prev;
713 prev.state = ActivityState.PAUSING;
714 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700715 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700716 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700717 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700718
719 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800720
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700721 if (prev.app != null && prev.app.thread != null) {
722 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
723 try {
724 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700725 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700726 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700727 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800728 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
729 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700730 } catch (Exception e) {
731 // Ignore exception, if process died other code will cleanup.
732 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800733 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734 mLastPausedActivity = null;
735 }
736 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800737 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700738 mLastPausedActivity = null;
739 }
740
741 // If we are not going to sleep, we want to ensure the device is
742 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700743 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700744 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700745 }
746
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800747 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700748 // Have the window manager pause its key dispatching until the new
749 // activity has started. If we're pausing the activity just because
750 // the screen is being turned off and the UI is sleeping, don't interrupt
751 // key dispatch; the same activity will pick it up again on wakeup.
752 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800753 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700754 } else {
755 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
756 }
757
758 // Schedule a pause timeout in case the app doesn't respond.
759 // We don't give it much time because this directly impacts the
760 // responsiveness seen by the user.
761 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
762 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700763 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700764 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
765 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
766 } else {
767 // This activity failed to schedule the
768 // pause, so just treat it as being paused now.
769 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700770 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700771 }
772 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700773
Craig Mautnerd2328952013-03-05 12:46:26 -0800774 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700775 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800776 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700777
Craig Mautnerd2328952013-03-05 12:46:26 -0800778 final ActivityRecord r = isInStackLocked(token);
779 if (r != null) {
780 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
781 if (mPausingActivity == r) {
782 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
783 + (timeout ? " (due to timeout)" : " (pause complete)"));
784 r.state = ActivityState.PAUSED;
785 completePauseLocked();
786 } else {
787 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
788 r.userId, System.identityHashCode(r), r.shortComponentName,
789 mPausingActivity != null
790 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700791 }
792 }
793 }
794
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700795 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
796 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700797 if (r.state != ActivityState.STOPPING) {
798 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
799 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
800 return;
801 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700802 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700803 if (icicle != null) {
804 // If icicle is null, this is happening due to a timeout, so we
805 // haven't really saved the state.
806 r.icicle = icicle;
807 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800808 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700809 r.updateThumbnail(thumbnail, description);
810 }
811 if (!r.stopped) {
812 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
813 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
814 r.stopped = true;
815 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700816 if (r.finishing) {
817 r.clearOptionsLocked();
818 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700819 if (r.configDestroy) {
820 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700821 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700822 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700823 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800824 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700825 }
826 }
827 }
828
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800829 private final void completePauseLocked() {
830 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800832
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800833 if (prev != null) {
834 if (prev.finishing) {
835 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700836 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800837 } else if (prev.app != null) {
838 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
839 if (prev.waitingVisible) {
840 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700841 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800842 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
843 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800844 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800845 if (prev.configDestroy) {
846 // The previous is being paused because the configuration
847 // is changing, which means it is actually stopping...
848 // To juggle the fact that we are also starting a new
849 // instance right now, we need to first completely stop
850 // the current instance before starting the new one.
851 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
852 destroyActivityLocked(prev, true, false, "pause-config");
853 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700854 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700855 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
856 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800857 // If we already have a few activities waiting to stop,
858 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700859 // them out. Or if r is the last of activity of the last task the stack
860 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800861 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700862 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800863 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700864 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 }
866 }
867 } else {
868 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
869 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800870 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800871 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700872 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800873
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700874 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700875 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700876 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700878 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700879 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700880 if (top == null || (prev != null && top != prev)) {
881 // If there are no more activities available to run,
882 // do resume anyway to start something. Also if the top
883 // activity on the stack is not the just paused activity,
884 // we need to go ahead and resume it to ensure we complete
885 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700886 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700887 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700888 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800889
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800890 if (prev != null) {
891 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892
Craig Mautner525f3d92013-05-07 14:01:50 -0700893 if (prev.app != null && prev.cpuTimeAtResume > 0
894 && mService.mBatteryStatsService.isOnBattery()) {
895 long diff;
896 synchronized (mService.mProcessStatsThread) {
897 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
898 - prev.cpuTimeAtResume;
899 }
900 if (diff > 0) {
901 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
902 synchronized (bsi) {
903 BatteryStatsImpl.Uid.Proc ps =
904 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
905 prev.info.packageName);
906 if (ps != null) {
907 ps.addForegroundTimeLocked(diff);
908 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 }
910 }
911 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700912 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 }
915
916 /**
917 * Once we know that we have asked an application to put an activity in
918 * the resumed state (either by launching it or explicitly telling it),
919 * this function updates the rest of our state to match that fact.
920 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700921 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700922 next.idle = false;
923 next.results = null;
924 next.newIntents = null;
925
926 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700927 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700929 mStackSupervisor.reportResumedActivityLocked(next);
930
931 next.resumeKeyDispatchingLocked();
932 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933
934 // Mark the point when the activity is resuming
935 // TODO: To be more accurate, the mark should be before the onCreate,
936 // not after the onResume. But for subsequent starts, onResume is fine.
937 if (next.app != null) {
938 synchronized (mService.mProcessStatsThread) {
939 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
940 }
941 } else {
942 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
943 }
944 }
945
Craig Mautner580ea812013-04-25 12:58:38 -0700946 /**
947 * Version of ensureActivitiesVisible that can easily be called anywhere.
948 */
949 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
950 return ensureActivitiesVisibleLocked(starting, configChanges, false);
951 }
952
953 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
954 boolean forceHomeShown) {
955 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700956 return r != null &&
957 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700958 }
959
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 /**
961 * Make sure that all activities that need to be visible (that is, they
962 * currently can be seen by the user) actually are.
963 */
Craig Mautner580ea812013-04-25 12:58:38 -0700964 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
965 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700966 if (DEBUG_VISBILITY) Slog.v(
967 TAG, "ensureActivitiesVisible behind " + top
968 + " configChanges=0x" + Integer.toHexString(configChanges));
969
Craig Mautner5eda9b32013-07-02 11:58:16 -0700970 if (mTranslucentActivityWaiting != top) {
971 mUndrawnActivitiesBelowTopTranslucent.clear();
972 if (mTranslucentActivityWaiting != null) {
973 // Call the callback with a timeout indication.
974 notifyActivityDrawnLocked(null);
975 mTranslucentActivityWaiting = null;
976 }
977 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
978 }
979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700980 // If the top activity is not fullscreen, then we need to
981 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800982 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -0700983 boolean showHomeBehindStack = false;
984 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
985 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700986 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -0800987 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
988 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
989 final ActivityRecord r = activities.get(activityNdx);
990 if (r.finishing) {
991 continue;
992 }
993 if (aboveTop && r != top) {
994 continue;
995 }
996 aboveTop = false;
997 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -0800999 TAG, "Make visible? " + r + " finishing=" + r.finishing
1000 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001001
Craig Mautnerd44711d2013-02-23 11:24:36 -08001002 final boolean doThisProcess = onlyThisProcess == null
1003 || onlyThisProcess.equals(r.processName);
1004
1005 // First: if this is not the current activity being started, make
1006 // sure it matches the current configuration.
1007 if (r != starting && doThisProcess) {
1008 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001010
1011 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001013 // This activity needs to be visible, but isn't even
1014 // running... get it started, but don't resume it
1015 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001016 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001017 if (r != starting) {
1018 r.startFreezingScreenLocked(r.app, configChanges);
1019 }
1020 if (!r.visible) {
1021 if (DEBUG_VISBILITY) Slog.v(
1022 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001023 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001024 }
1025 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001026 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001027 }
1028 }
1029
1030 } else if (r.visible) {
1031 // If this activity is already visible, then there is nothing
1032 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001034 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001035
1036 } else if (onlyThisProcess == null) {
1037 // This activity is not currently visible, but is running.
1038 // Tell it to become visible.
1039 r.visible = true;
1040 if (r.state != ActivityState.RESUMED && r != starting) {
1041 // If this activity is paused, tell it
1042 // to now show its window.
1043 if (DEBUG_VISBILITY) Slog.v(
1044 TAG, "Making visible and scheduling visibility: " + r);
1045 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001046 if (mTranslucentActivityWaiting != null) {
1047 mUndrawnActivitiesBelowTopTranslucent.add(r);
1048 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001049 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001050 r.sleeping = false;
1051 r.app.pendingUiClean = true;
1052 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1053 r.stopFreezingScreenLocked(false);
1054 } catch (Exception e) {
1055 // Just skip on any failure; we'll make it
1056 // visible when it next restarts.
1057 Slog.w(TAG, "Exception thrown making visibile: "
1058 + r.intent.getComponent(), e);
1059 }
1060 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062
Craig Mautnerd44711d2013-02-23 11:24:36 -08001063 // Aggregate current change flags.
1064 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065
Craig Mautnerd44711d2013-02-23 11:24:36 -08001066 if (r.fullscreen) {
1067 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001068 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1069 behindFullscreen = true;
1070 } else if (r.mLaunchHomeTaskNext) {
1071 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1072 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001073 behindFullscreen = true;
1074 }
1075 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001076 if (DEBUG_VISBILITY) Slog.v(
1077 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1078 + " state=" + r.state
1079 + " behindFullscreen=" + behindFullscreen);
1080 // Now for any activities that aren't visible to the user, make
1081 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001082 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001083 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084 r.visible = false;
1085 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001087 switch (r.state) {
1088 case STOPPING:
1089 case STOPPED:
1090 if (r.app != null && r.app.thread != null) {
1091 if (DEBUG_VISBILITY) Slog.v(
1092 TAG, "Scheduling invisibility: " + r);
1093 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1094 }
1095 break;
1096
1097 case INITIALIZING:
1098 case RESUMED:
1099 case PAUSING:
1100 case PAUSED:
1101 stopActivityLocked(r);
1102 break;
1103
1104 default:
1105 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001106 }
1107 } catch (Exception e) {
1108 // Just skip on any failure; we'll make it
1109 // visible when it next restarts.
1110 Slog.w(TAG, "Exception thrown making hidden: "
1111 + r.intent.getComponent(), e);
1112 }
1113 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001114 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 }
1117 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001118 }
Craig Mautner580ea812013-04-25 12:58:38 -07001119 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001120 }
Craig Mautner58547802013-03-05 08:23:53 -08001121
Craig Mautner5eda9b32013-07-02 11:58:16 -07001122 void convertToTranslucent(ActivityRecord r) {
1123 mTranslucentActivityWaiting = r;
1124 mUndrawnActivitiesBelowTopTranslucent.clear();
1125 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1126 }
1127
1128 /**
1129 * Called as activities below the top translucent activity are redrawn. When the last one is
1130 * redrawn notify the top activity by calling
1131 * {@link Activity#onTranslucentConversionComplete}.
1132 *
1133 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1134 * occurred and the activity will be notified immediately.
1135 */
1136 void notifyActivityDrawnLocked(ActivityRecord r) {
1137 if ((r == null)
1138 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1139 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1140 // The last undrawn activity below the top has just been drawn. If there is an
1141 // opaque activity at the top, notify it that it can become translucent safely now.
1142 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1143 mTranslucentActivityWaiting = null;
1144 mUndrawnActivitiesBelowTopTranslucent.clear();
1145 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1146
1147 if (waitingActivity != null && waitingActivity.app != null &&
1148 waitingActivity.app.thread != null) {
1149 try {
1150 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1151 waitingActivity.appToken, r != null);
1152 } catch (RemoteException e) {
1153 }
1154 }
1155 }
1156 }
1157
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001158 /**
1159 * Ensure that the top activity in the stack is resumed.
1160 *
1161 * @param prev The previously resumed activity, for when in the process
1162 * of pausing; can be null to call from elsewhere.
1163 *
1164 * @return Returns true if something is being resumed, or false if
1165 * nothing happened.
1166 */
1167 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001168 return resumeTopActivityLocked(prev, null);
1169 }
1170
1171 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001172 // Find the first activity that is not finishing.
1173 ActivityRecord next = topRunningActivityLocked(null);
1174
1175 // Remember how we'll process this pause/resume situation, and ensure
1176 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001177 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1178 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001179
1180 if (next == null) {
1181 // There are no more activities! Let's just start up the
1182 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001183 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001184 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001185 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001186 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 }
1188
1189 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001192 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1193 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 // Make sure we have executed any pending transitions, since there
1195 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001196 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001198 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001199 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001200 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201 return false;
1202 }
1203
Craig Mautner525f3d92013-05-07 14:01:50 -07001204 final TaskRecord nextTask = next.task;
1205 final TaskRecord prevTask = prev != null ? prev.task : null;
1206 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001207 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001208 if (prevTask == nextTask) {
1209 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1210 final int numActivities = activities.size();
1211 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1212 final ActivityRecord r = activities.get(activityNdx);
1213 // r is usually the same as next, but what if two activities were launched
1214 // before prev finished?
1215 if (!r.finishing) {
1216 r.mLaunchHomeTaskNext = true;
1217 r.frontOfTask = true;
1218 break;
1219 }
1220 }
1221 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001222 // This task is going away but it was supposed to return to the home task.
1223 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001224 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001225 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1226 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001227 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001228 return mStackSupervisor.resumeHomeActivity(prev);
1229 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001230 }
1231
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001232 // If we are sleeping, and there is no resumed activity, and the top
1233 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001234 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001235 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001236 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237 // Make sure we have executed any pending transitions, since there
1238 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001239 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001240 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001241 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001242 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001243 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001244 return false;
1245 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001246
1247 // Make sure that the user who owns this activity is started. If not,
1248 // we will just leave it as is because someone should be bringing
1249 // another user's activities to the top of the stack.
1250 if (mService.mStartedUsers.get(next.userId) == null) {
1251 Slog.w(TAG, "Skipping resume of top activity " + next
1252 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001253 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001254 return false;
1255 }
1256
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001257 // The activity may be waiting for stop, but that is no longer
1258 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001259 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001260 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001261 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001262 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001263
Dianne Hackborn84375872012-06-01 19:03:50 -07001264 next.updateOptionsLocked(options);
1265
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001266 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1267
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001268 // If we are currently pausing an activity, then don't do anything
1269 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001270 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001271 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1272 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001273 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001274 return false;
1275 }
1276
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001277 // Okay we are now going to start a switch, to 'next'. We may first
1278 // have to pause the current activity, but this is an important point
1279 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001280 // XXX "App Redirected" dialog is getting too many false positives
1281 // at this point, so turn off for now.
1282 if (false) {
1283 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1284 long now = SystemClock.uptimeMillis();
1285 final boolean inTime = mLastStartedActivity.startTime != 0
1286 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1287 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1288 final int nextUid = next.info.applicationInfo.uid;
1289 if (inTime && lastUid != nextUid
1290 && lastUid != next.launchedFromUid
1291 && mService.checkPermission(
1292 android.Manifest.permission.STOP_APP_SWITCHES,
1293 -1, next.launchedFromUid)
1294 != PackageManager.PERMISSION_GRANTED) {
1295 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1296 } else {
1297 next.startTime = now;
1298 mLastStartedActivity = next;
1299 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001300 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001301 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001302 mLastStartedActivity = next;
1303 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001304 }
Craig Mautner58547802013-03-05 08:23:53 -08001305
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001306 // We need to start pausing the current activity so the top one
1307 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001308 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1309 if (mResumedActivity != null) {
1310 pausing = true;
1311 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001312 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001313 }
1314 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001315 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1316 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001317 // At this point we want to put the upcoming activity's process
1318 // at the top of the LRU list, since we know we will be needing it
1319 // very soon and it would be a waste to let it get killed if it
1320 // happens to be sitting towards the end.
1321 if (next.app != null && next.app.thread != null) {
1322 // No reason to do full oom adj update here; we'll let that
1323 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001324 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001325 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001326 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001327 return true;
1328 }
1329
Christopher Tated3f175c2012-06-14 14:16:54 -07001330 // If the most recent activity was noHistory but was only stopped rather
1331 // than stopped+finished because the device went to sleep, we need to make
1332 // sure to finish it as we're making a new activity topmost.
1333 final ActivityRecord last = mLastPausedActivity;
1334 if (mService.mSleeping && last != null && !last.finishing) {
1335 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1336 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1337 if (DEBUG_STATES) {
1338 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1339 }
1340 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001341 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001342 }
1343 }
1344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345 if (prev != null && prev != next) {
1346 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1347 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001348 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001349 if (DEBUG_SWITCH) Slog.v(
1350 TAG, "Resuming top, waiting visible to hide: " + prev);
1351 } else {
1352 // The next activity is already visible, so hide the previous
1353 // activity's windows right now so we can show the new one ASAP.
1354 // We only do this if the previous is finishing, which should mean
1355 // it is on top of the one being resumed so hiding it quickly
1356 // is good. Otherwise, we want to do the normal route of allowing
1357 // the resumed activity to be shown so we can decide if the
1358 // previous should actually be hidden depending on whether the
1359 // new one is found to be full-screen or not.
1360 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001361 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001362 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1363 + prev + ", waitingVisible="
1364 + (prev != null ? prev.waitingVisible : null)
1365 + ", nowVisible=" + next.nowVisible);
1366 } else {
1367 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1368 + prev + ", waitingVisible="
1369 + (prev != null ? prev.waitingVisible : null)
1370 + ", nowVisible=" + next.nowVisible);
1371 }
1372 }
1373 }
1374
Dianne Hackborne7f97212011-02-24 14:40:20 -08001375 // Launching this app's activity, make sure the app is no longer
1376 // considered stopped.
1377 try {
1378 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001379 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001380 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001381 } catch (IllegalArgumentException e) {
1382 Slog.w(TAG, "Failed trying to unstop package "
1383 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001384 }
1385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386 // We are starting up the next activity, so tell the window manager
1387 // that the previous one will be hidden soon. This way it can know
1388 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001389 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 if (prev != null) {
1391 if (prev.finishing) {
1392 if (DEBUG_TRANSITION) Slog.v(TAG,
1393 "Prepare close transition: prev=" + prev);
1394 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001395 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001396 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001397 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001398 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001399 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1400 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001402 mWindowManager.setAppWillBeHidden(prev.appToken);
1403 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001405 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001406 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001407 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001408 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001410 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001411 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1412 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 }
1414 }
1415 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001416 mWindowManager.setAppWillBeHidden(prev.appToken);
1417 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001418 }
Craig Mautner967212c2013-04-13 21:10:58 -07001419 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001420 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001422 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001423 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001425 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 }
1427 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001428 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001429 next.applyOptionsLocked();
1430 } else {
1431 next.clearOptionsLocked();
1432 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433
Craig Mautnercf910b02013-04-23 11:23:27 -07001434 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 if (next.app != null && next.app.thread != null) {
1436 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1437
1438 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001439 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001440
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001441 // schedule launch ticks to collect information about slow apps.
1442 next.startLaunchTickingLocked();
1443
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001444 ActivityRecord lastResumedActivity =
1445 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 ActivityState lastState = next.state;
1447
1448 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001449
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001450 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 next.state = ActivityState.RESUMED;
1452 mResumedActivity = next;
1453 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001454 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001455 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001456 updateLRUListLocked(next);
1457
1458 // Have the window manager re-evaluate the orientation of
1459 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001460 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001461 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001462 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001463 mService.mConfiguration,
1464 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1465 if (config != null) {
1466 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001467 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001468 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001470
Craig Mautner525f3d92013-05-07 14:01:50 -07001471 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001472 // The configuration update wasn't able to keep the existing
1473 // instance of the activity, and instead started a new one.
1474 // We should be all done, but let's just make sure our activity
1475 // is still at the top and schedule another run if something
1476 // weird happened.
1477 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001478 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 "Activity config changed during resume: " + next
1480 + ", new next: " + nextNext);
1481 if (nextNext != next) {
1482 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001483 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001485 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001486 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001487 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001488 return true;
1489 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001490 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001491 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001492 }
Craig Mautner58547802013-03-05 08:23:53 -08001493
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494 try {
1495 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001496 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 if (a != null) {
1498 final int N = a.size();
1499 if (!next.finishing && N > 0) {
1500 if (DEBUG_RESULTS) Slog.v(
1501 TAG, "Delivering results to " + next
1502 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001503 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 }
1505 }
1506
1507 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001508 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 }
1510
1511 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001512 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001514
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001515 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001516 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001517 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001518 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1519 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001521
Craig Mautner0eea92c2013-05-16 13:35:39 -07001522 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523
Craig Mautnerac6f8432013-07-17 13:24:59 -07001524 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001525 } catch (Exception e) {
1526 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001527 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1528 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001530 if (lastStack != null) {
1531 lastStack.mResumedActivity = lastResumedActivity;
1532 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 Slog.i(TAG, "Restarting because process died: " + next);
1534 if (!next.hasBeenLaunched) {
1535 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001536 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1537 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001538 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001539 next.appToken, next.packageName, next.theme,
1540 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001541 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1542 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001544 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001545 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 return true;
1547 }
1548
1549 // From this point on, if something goes wrong there is no way
1550 // to recover the activity.
1551 try {
1552 next.visible = true;
1553 completeResumeLocked(next);
1554 } catch (Exception e) {
1555 // If any exception gets thrown, toss away this
1556 // activity and try the next one.
1557 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001558 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001559 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001560 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 return true;
1562 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 next.stopped = false;
1564
1565 } else {
1566 // Whoops, need to restart this activity!
1567 if (!next.hasBeenLaunched) {
1568 next.hasBeenLaunched = true;
1569 } else {
1570 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001571 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001572 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001573 mService.compatibilityInfoForPackageLocked(
1574 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001576 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001577 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 }
1579 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1580 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001581 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001582 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 }
1584
Craig Mautnercf910b02013-04-23 11:23:27 -07001585 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001586 return true;
1587 }
1588
Craig Mautnerac6f8432013-07-17 13:24:59 -07001589 private void insertTaskAtTop(TaskRecord task) {
1590 mTaskHistory.remove(task);
1591 // Now put task at top.
1592 int stackNdx = mTaskHistory.size();
1593 if (task.userId != mCurrentUser) {
1594 // Put non-current user tasks below current user tasks.
1595 while (--stackNdx >= 0) {
1596 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1597 break;
1598 }
1599 }
1600 ++stackNdx;
1601 }
1602 mTaskHistory.add(stackNdx, task);
1603 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001604
Craig Mautner8849a5e2013-04-02 16:41:03 -07001605 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001606 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001607 TaskRecord rTask = r.task;
1608 final int taskId = rTask.taskId;
1609 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001610 // Last activity in task had been removed or ActivityManagerService is reusing task.
1611 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001612 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001613 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001614 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001615 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001616 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001617 if (!newTask) {
1618 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001619 boolean startIt = true;
1620 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1621 task = mTaskHistory.get(taskNdx);
1622 if (task == r.task) {
1623 // Here it is! Now, if this is not yet visible to the
1624 // user, then just add it without starting; it will
1625 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001626 if (!startIt) {
1627 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1628 + task, new RuntimeException("here").fillInStackTrace());
1629 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001630 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001631 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1632 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001633 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1634 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001636 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001638 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001639 return;
1640 }
1641 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001642 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001643 startIt = false;
1644 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001645 }
1646 }
1647
1648 // Place a new activity at top of stack, so it is next to interact
1649 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001650
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 // If we are not placing the new activity frontmost, we do not want
1652 // to deliver the onUserLeaving callback to the actual frontmost
1653 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001654 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001655 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001656 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1657 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001658 }
Craig Mautner70a86932013-02-28 22:37:44 -08001659
1660 task = r.task;
1661
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001662 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001663 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001664 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001665 task.addActivityToTop(r);
1666
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001667 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001668 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001669 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001670 // We want to show the starting preview window if we are
1671 // switching to a new task, or the next activity's process is
1672 // not currently running.
1673 boolean showStartingIcon = newTask;
1674 ProcessRecord proc = r.app;
1675 if (proc == null) {
1676 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1677 }
1678 if (proc == null || proc.thread == null) {
1679 showStartingIcon = true;
1680 }
1681 if (DEBUG_TRANSITION) Slog.v(TAG,
1682 "Prepare open transition: starting " + r);
1683 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001684 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001686 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001687 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001688 ? AppTransition.TRANSIT_TASK_OPEN
1689 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 mNoAnimActivities.remove(r);
1691 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001692 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001693 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001694 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001695 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 boolean doShow = true;
1697 if (newTask) {
1698 // Even though this activity is starting fresh, we still need
1699 // to reset it to make sure we apply affinities to move any
1700 // existing activities from other tasks in to it.
1701 // If the caller has requested that the target task be
1702 // reset, then do so.
1703 if ((r.intent.getFlags()
1704 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1705 resetTaskIfNeededLocked(r, r);
1706 doShow = topRunningNonDelayedActivityLocked(null) == r;
1707 }
1708 }
1709 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1710 // Figure out if we are transitioning from another activity that is
1711 // "has the same starting icon" as the next one. This allows the
1712 // window manager to keep the previous window it had previously
1713 // created, if it still had one.
1714 ActivityRecord prev = mResumedActivity;
1715 if (prev != null) {
1716 // We don't want to reuse the previous starting preview if:
1717 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001718 if (prev.task != r.task) {
1719 prev = null;
1720 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001722 else if (prev.nowVisible) {
1723 prev = null;
1724 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001726 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001727 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001728 mService.compatibilityInfoForPackageLocked(
1729 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001730 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001731 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 }
1733 } else {
1734 // If this is the first activity, don't do any fancy animations,
1735 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001736 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001737 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001738 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001739 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 }
1741 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001742 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 }
1744
1745 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001746 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 }
1748 }
1749
Dianne Hackbornbe707852011-11-11 14:32:10 -08001750 final void validateAppTokensLocked() {
1751 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001752 mValidateAppTokens.ensureCapacity(numActivities());
1753 final int numTasks = mTaskHistory.size();
1754 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1755 TaskRecord task = mTaskHistory.get(taskNdx);
1756 final ArrayList<ActivityRecord> activities = task.mActivities;
1757 if (activities.size() == 0) {
1758 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001759 }
Craig Mautner000f0022013-02-26 15:04:29 -08001760 TaskGroup group = new TaskGroup();
1761 group.taskId = task.taskId;
1762 mValidateAppTokens.add(group);
1763 final int numActivities = activities.size();
1764 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1765 final ActivityRecord r = activities.get(activityNdx);
1766 group.tokens.add(r.appToken);
1767 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001768 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001769 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001770 }
1771
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 /**
1773 * Perform a reset of the given task, if needed as part of launching it.
1774 * Returns the new HistoryRecord at the top of the task.
1775 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001776 /**
1777 * Helper method for #resetTaskIfNeededLocked.
1778 * We are inside of the task being reset... we'll either finish this activity, push it out
1779 * for another task, or leave it as-is.
1780 * @param task The task containing the Activity (taskTop) that might be reset.
1781 * @param forceReset
1782 * @return An ActivityOptions that needs to be processed.
1783 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001784 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001785 ActivityOptions topOptions = null;
1786
1787 int replyChainEnd = -1;
1788 boolean canMoveOptions = true;
1789
1790 // We only do this for activities that are not the root of the task (since if we finish
1791 // the root, we may no longer have the task!).
1792 final ArrayList<ActivityRecord> activities = task.mActivities;
1793 final int numActivities = activities.size();
1794 for (int i = numActivities - 1; i > 0; --i ) {
1795 ActivityRecord target = activities.get(i);
1796
1797 final int flags = target.info.flags;
1798 final boolean finishOnTaskLaunch =
1799 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1800 final boolean allowTaskReparenting =
1801 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1802 final boolean clearWhenTaskReset =
1803 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1804
1805 if (!finishOnTaskLaunch
1806 && !clearWhenTaskReset
1807 && target.resultTo != null) {
1808 // If this activity is sending a reply to a previous
1809 // activity, we can't do anything with it now until
1810 // we reach the start of the reply chain.
1811 // XXX note that we are assuming the result is always
1812 // to the previous activity, which is almost always
1813 // the case but we really shouldn't count on.
1814 if (replyChainEnd < 0) {
1815 replyChainEnd = i;
1816 }
1817 } else if (!finishOnTaskLaunch
1818 && !clearWhenTaskReset
1819 && allowTaskReparenting
1820 && target.taskAffinity != null
1821 && !target.taskAffinity.equals(task.affinity)) {
1822 // If this activity has an affinity for another
1823 // task, then we need to move it out of here. We will
1824 // move it as far out of the way as possible, to the
1825 // bottom of the activity stack. This also keeps it
1826 // correctly ordered with any activities we previously
1827 // moved.
1828 TaskRecord bottomTask = mTaskHistory.get(0);
1829 ActivityRecord p = bottomTask.mActivities.get(0);
1830 if (target.taskAffinity != null
1831 && target.taskAffinity.equals(p.task.affinity)) {
1832 // If the activity currently at the bottom has the
1833 // same task affinity as the one we are moving,
1834 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001835 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001836 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1837 + " out to bottom task " + p.task);
1838 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001839 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001840 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001841 target.task.affinityIntent = target.intent;
1842 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1843 + " out to new task " + target.task);
1844 }
1845
1846 final TaskRecord targetTask = target.task;
1847 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001848 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001849
Craig Mautner525f3d92013-05-07 14:01:50 -07001850 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001851 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1852 for (int srcPos = start; srcPos >= i; --srcPos) {
1853 p = activities.get(srcPos);
1854 if (p.finishing) {
1855 continue;
1856 }
1857
Craig Mautner525f3d92013-05-07 14:01:50 -07001858 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001859 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001860 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001861 topOptions = p.takeOptionsLocked();
1862 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001863 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001864 }
1865 }
1866 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1867 + task + " adding to task=" + targetTask,
1868 new RuntimeException("here").fillInStackTrace());
1869 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1870 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001871 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001872 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001873
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001874 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001875 }
1876
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001877 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001878 if (VALIDATE_TOKENS) {
1879 validateAppTokensLocked();
1880 }
1881
1882 replyChainEnd = -1;
1883 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1884 // If the activity should just be removed -- either
1885 // because it asks for it, or the task should be
1886 // cleared -- then finish it and anything that is
1887 // part of its reply chain.
1888 int end;
1889 if (clearWhenTaskReset) {
1890 // In this case, we want to finish this activity
1891 // and everything above it, so be sneaky and pretend
1892 // like these are all in the reply chain.
1893 end = numActivities - 1;
1894 } else if (replyChainEnd < 0) {
1895 end = i;
1896 } else {
1897 end = replyChainEnd;
1898 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001899 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001900 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001901 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001902 if (p.finishing) {
1903 continue;
1904 }
1905 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001906 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001907 topOptions = p.takeOptionsLocked();
1908 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001909 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001910 }
1911 }
Craig Mautner58547802013-03-05 08:23:53 -08001912 if (DEBUG_TASKS) Slog.w(TAG,
1913 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001914 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001915 end--;
1916 srcPos--;
1917 }
1918 }
1919 replyChainEnd = -1;
1920 } else {
1921 // If we were in the middle of a chain, well the
1922 // activity that started it all doesn't want anything
1923 // special, so leave it all as-is.
1924 replyChainEnd = -1;
1925 }
1926 }
1927
1928 return topOptions;
1929 }
1930
1931 /**
1932 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1933 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1934 * @param affinityTask The task we are looking for an affinity to.
1935 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1936 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1937 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1938 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001939 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001940 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001941 int replyChainEnd = -1;
1942 final int taskId = task.taskId;
1943 final String taskAffinity = task.affinity;
1944
1945 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1946 final int numActivities = activities.size();
1947 // Do not operate on the root Activity.
1948 for (int i = numActivities - 1; i > 0; --i) {
1949 ActivityRecord target = activities.get(i);
1950
1951 final int flags = target.info.flags;
1952 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1953 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1954
1955 if (target.resultTo != null) {
1956 // If this activity is sending a reply to a previous
1957 // activity, we can't do anything with it now until
1958 // we reach the start of the reply chain.
1959 // XXX note that we are assuming the result is always
1960 // to the previous activity, which is almost always
1961 // the case but we really shouldn't count on.
1962 if (replyChainEnd < 0) {
1963 replyChainEnd = i;
1964 }
1965 } else if (topTaskIsHigher
1966 && allowTaskReparenting
1967 && taskAffinity != null
1968 && taskAffinity.equals(target.taskAffinity)) {
1969 // This activity has an affinity for our task. Either remove it if we are
1970 // clearing or move it over to our task. Note that
1971 // we currently punt on the case where we are resetting a
1972 // task that is not at the top but who has activities above
1973 // with an affinity to it... this is really not a normal
1974 // case, and we will need to later pull that task to the front
1975 // and usually at that point we will do the reset and pick
1976 // up those remaining activities. (This only happens if
1977 // someone starts an activity in a new task from an activity
1978 // in a task that is not currently on top.)
1979 if (forceReset || finishOnTaskLaunch) {
1980 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1981 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1982 for (int srcPos = start; srcPos >= i; --srcPos) {
1983 final ActivityRecord p = activities.get(srcPos);
1984 if (p.finishing) {
1985 continue;
1986 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001987 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001988 }
1989 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001990 if (taskInsertionPoint < 0) {
1991 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001992
Craig Mautner77878772013-03-04 19:46:24 -08001993 }
Craig Mautner77878772013-03-04 19:46:24 -08001994
1995 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1996 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1997 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1998 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001999 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002000 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002001 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002002
Craig Mautnere3a74d52013-02-22 14:14:58 -08002003 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2004 + " to stack at " + task,
2005 new RuntimeException("here").fillInStackTrace());
2006 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2007 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002008 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002009 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002010 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002011 if (VALIDATE_TOKENS) {
2012 validateAppTokensLocked();
2013 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002014
2015 // Now we've moved it in to place... but what if this is
2016 // a singleTop activity and we have put it on top of another
2017 // instance of the same activity? Then we drop the instance
2018 // below so it remains singleTop.
2019 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2020 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002021 int targetNdx = taskActivities.indexOf(target);
2022 if (targetNdx > 0) {
2023 ActivityRecord p = taskActivities.get(targetNdx - 1);
2024 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002025 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2026 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002027 }
2028 }
2029 }
2030 }
2031
2032 replyChainEnd = -1;
2033 }
2034 }
Craig Mautner77878772013-03-04 19:46:24 -08002035 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002036 }
2037
Craig Mautner8849a5e2013-04-02 16:41:03 -07002038 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002039 ActivityRecord newActivity) {
2040 boolean forceReset =
2041 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2042 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2043 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2044 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2045 forceReset = true;
2046 }
2047 }
2048
2049 final TaskRecord task = taskTop.task;
2050
2051 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2052 * for remaining tasks. Used for later tasks to reparent to task. */
2053 boolean taskFound = false;
2054
2055 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2056 ActivityOptions topOptions = null;
2057
Craig Mautner77878772013-03-04 19:46:24 -08002058 // Preserve the location for reparenting in the new task.
2059 int reparentInsertionPoint = -1;
2060
Craig Mautnere3a74d52013-02-22 14:14:58 -08002061 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2062 final TaskRecord targetTask = mTaskHistory.get(i);
2063
2064 if (targetTask == task) {
2065 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2066 taskFound = true;
2067 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002068 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2069 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002070 }
2071 }
2072
Craig Mautner70a86932013-02-28 22:37:44 -08002073 int taskNdx = mTaskHistory.indexOf(task);
2074 do {
2075 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2076 } while (taskTop == null && taskNdx >= 0);
2077
Craig Mautnere3a74d52013-02-22 14:14:58 -08002078 if (topOptions != null) {
2079 // If we got some ActivityOptions from an activity on top that
2080 // was removed from the task, propagate them to the new real top.
2081 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002082 taskTop.updateOptionsLocked(topOptions);
2083 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002084 topOptions.abort();
2085 }
2086 }
2087
2088 return taskTop;
2089 }
2090
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002091 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2092 String resultWho, int requestCode, int resultCode, Intent data) {
2093
2094 if (callingUid > 0) {
2095 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002096 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002097 }
2098
2099 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2100 + " : who=" + resultWho + " req=" + requestCode
2101 + " res=" + resultCode + " data=" + data);
2102 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2103 try {
2104 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2105 list.add(new ResultInfo(resultWho, requestCode,
2106 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002107 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108 return;
2109 } catch (Exception e) {
2110 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2111 }
2112 }
2113
2114 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2115 }
2116
Craig Mautnerf3333272013-04-22 10:55:53 -07002117 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2119 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2120 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2121 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002122 if (!mService.mSleeping) {
2123 if (DEBUG_STATES) {
2124 Slog.d(TAG, "no-history finish of " + r);
2125 }
2126 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002127 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002128 } else {
2129 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2130 + " on stop because we're just sleeping");
2131 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002132 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002133 }
2134
2135 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002136 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 if (mService.mFocusedActivity == r) {
2138 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2139 }
2140 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002141 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 try {
2143 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002144 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2145 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 r.state = ActivityState.STOPPING;
2147 if (DEBUG_VISBILITY) Slog.v(
2148 TAG, "Stopping visible=" + r.visible + " for " + r);
2149 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002150 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002152 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002153 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002154 r.setSleeping(true);
2155 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002156 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002157 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002158 } catch (Exception e) {
2159 // Maybe just ignore exceptions here... if the process
2160 // has crashed, our death notification will clean things
2161 // up.
2162 Slog.w(TAG, "Exception thrown during pause", e);
2163 // Just in case, assume it to be stopped.
2164 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002165 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002166 r.state = ActivityState.STOPPED;
2167 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002168 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169 }
2170 }
2171 }
2172 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 /**
2175 * @return Returns true if the activity is being finished, false if for
2176 * some reason it is being left as-is.
2177 */
2178 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002179 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002180 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002181 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002182 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002183 + ", result=" + resultCode + ", data=" + resultData
2184 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002185 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186 return false;
2187 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188
Craig Mautnerd44711d2013-02-23 11:24:36 -08002189 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 return true;
2191 }
2192
Craig Mautnerd2328952013-03-05 12:46:26 -08002193 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002194 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2195 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2196 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2197 ActivityRecord r = activities.get(activityNdx);
2198 if (r.resultTo == self && r.requestCode == requestCode) {
2199 if ((r.resultWho == null && resultWho == null) ||
2200 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2201 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2202 false);
2203 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002204 }
2205 }
2206 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002207 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002208 }
2209
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002210 final void finishTopRunningActivityLocked(ProcessRecord app) {
2211 ActivityRecord r = topRunningActivityLocked(null);
2212 if (r != null && r.app == app) {
2213 // If the top running activity is from this crashing
2214 // process, then terminate it to avoid getting in a loop.
2215 Slog.w(TAG, " Force finishing activity "
2216 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002217 int taskNdx = mTaskHistory.indexOf(r.task);
2218 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002219 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002220 // Also terminate any activities below it that aren't yet
2221 // stopped, to avoid a situation where one will get
2222 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002223 --activityNdx;
2224 if (activityNdx < 0) {
2225 do {
2226 --taskNdx;
2227 if (taskNdx < 0) {
2228 break;
2229 }
2230 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2231 } while (activityNdx < 0);
2232 }
2233 if (activityNdx >= 0) {
2234 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002235 if (r.state == ActivityState.RESUMED
2236 || r.state == ActivityState.PAUSING
2237 || r.state == ActivityState.PAUSED) {
Craig Mautner86d67a42013-05-14 10:34:38 -07002238 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002239 Slog.w(TAG, " Force finishing activity "
2240 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002241 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002242 }
2243 }
2244 }
2245 }
2246 }
2247
Craig Mautnerd2328952013-03-05 12:46:26 -08002248 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002249 ArrayList<ActivityRecord> activities = r.task.mActivities;
2250 for (int index = activities.indexOf(r); index >= 0; --index) {
2251 ActivityRecord cur = activities.get(index);
2252 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002253 break;
2254 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002255 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002256 }
2257 return true;
2258 }
2259
Dianne Hackborn5c607432012-02-28 14:44:19 -08002260 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2261 // send the result
2262 ActivityRecord resultTo = r.resultTo;
2263 if (resultTo != null) {
2264 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2265 + " who=" + r.resultWho + " req=" + r.requestCode
2266 + " res=" + resultCode + " data=" + resultData);
2267 if (r.info.applicationInfo.uid > 0) {
2268 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2269 resultTo.packageName, resultData,
2270 resultTo.getUriPermissionsLocked());
2271 }
2272 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2273 resultData);
2274 r.resultTo = null;
2275 }
2276 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2277
2278 // Make sure this HistoryRecord is not holding on to other resources,
2279 // because clients have remote IPC references to this object so we
2280 // can't assume that will go away and want to avoid circular IPC refs.
2281 r.results = null;
2282 r.pendingResults = null;
2283 r.newIntents = null;
2284 r.icicle = null;
2285 }
2286
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002287 /**
2288 * @return Returns true if this activity has been removed from the history
2289 * list, or false if it is still in the list and will be removed later.
2290 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002291 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2292 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 if (r.finishing) {
2294 Slog.w(TAG, "Duplicate finish request for " + r);
2295 return false;
2296 }
2297
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002298 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002299 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002300 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002302 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2303 final int index = activities.indexOf(r);
2304 if (index < (activities.size() - 1)) {
2305 ActivityRecord next = activities.get(index+1);
2306 if (r.frontOfTask) {
2307 // The next activity is now the front of the task.
2308 next.frontOfTask = true;
2309 }
2310 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2311 // If the caller asked that this activity (and all above it)
2312 // be cleared when the task is reset, don't lose that information,
2313 // but propagate it up to the next activity.
2314 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002315 }
2316 }
2317
2318 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002319 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002321 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 }
2323 }
2324
Dianne Hackborn5c607432012-02-28 14:44:19 -08002325 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002326
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 if (mService.mPendingThumbnails.size() > 0) {
2328 // There are clients waiting to receive thumbnails so, in case
2329 // this is an activity that someone is waiting for, add it
2330 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002331 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002332 }
2333
Craig Mautnerde4ef022013-04-07 19:01:33 -07002334 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002335 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 if (DEBUG_TRANSITION) Slog.v(TAG,
2337 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002338 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002339 ? AppTransition.TRANSIT_TASK_CLOSE
2340 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002341
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002343 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002344
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002345 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2347 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2348 startPausingLocked(false, false);
2349 }
2350
2351 } else if (r.state != ActivityState.PAUSING) {
2352 // If the activity is PAUSING, we will complete the finish once
2353 // it is done pausing; else we can just directly finish it here.
2354 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002355 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 } else {
2357 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2358 }
2359
2360 return false;
2361 }
2362
Craig Mautnerf3333272013-04-22 10:55:53 -07002363 static final int FINISH_IMMEDIATELY = 0;
2364 static final int FINISH_AFTER_PAUSE = 1;
2365 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366
Craig Mautnerf3333272013-04-22 10:55:53 -07002367 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 // First things first: if this activity is currently visible,
2369 // and the resumed activity is not yet visible, then hold off on
2370 // finishing until the resumed one becomes visible.
2371 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002372 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2373 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002374 if (mStackSupervisor.mStoppingActivities.size() > 3
2375 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 // If we already have a few activities waiting to stop,
2377 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002378 // them out. Or if r is the last of activity of the last task the stack
2379 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002380 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002381 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002382 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 }
2384 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002385 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2386 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002388 if (oomAdj) {
2389 mService.updateOomAdjLocked();
2390 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 return r;
2392 }
2393
2394 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002395 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002396 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002397 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 if (mResumedActivity == r) {
2399 mResumedActivity = null;
2400 }
2401 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002402 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002403 r.state = ActivityState.FINISHING;
2404
2405 if (mode == FINISH_IMMEDIATELY
2406 || prevState == ActivityState.STOPPED
2407 || prevState == ActivityState.INITIALIZING) {
2408 // If this activity is already stopped, we can just finish
2409 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002410 boolean activityRemoved = destroyActivityLocked(r, true,
2411 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002412 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002413 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002414 }
2415 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002417
2418 // Need to go through the full pause cycle to get this
2419 // activity into the stopped state and then finish it.
2420 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002421 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002422 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002423 return r;
2424 }
2425
Craig Mautnerd2328952013-03-05 12:46:26 -08002426 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002427 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002428 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002429 final TaskRecord task = srec.task;
2430 final ArrayList<ActivityRecord> activities = task.mActivities;
2431 final int start = activities.indexOf(srec);
2432 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002433 return false;
2434 }
2435 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002436 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002437 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002438 final ComponentName dest = destIntent.getComponent();
2439 if (start > 0 && dest != null) {
2440 for (int i = finishTo; i >= 0; i--) {
2441 ActivityRecord r = activities.get(i);
2442 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002443 r.info.name.equals(dest.getClassName())) {
2444 finishTo = i;
2445 parent = r;
2446 foundParentInTask = true;
2447 break;
2448 }
2449 }
2450 }
2451
2452 IActivityController controller = mService.mController;
2453 if (controller != null) {
2454 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2455 if (next != null) {
2456 // ask watcher if this is allowed
2457 boolean resumeOK = true;
2458 try {
2459 resumeOK = controller.activityResuming(next.packageName);
2460 } catch (RemoteException e) {
2461 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002462 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002463 }
2464
2465 if (!resumeOK) {
2466 return false;
2467 }
2468 }
2469 }
2470 final long origId = Binder.clearCallingIdentity();
2471 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002472 ActivityRecord r = activities.get(i);
2473 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002474 // Only return the supplied result for the first activity finished
2475 resultCode = Activity.RESULT_CANCELED;
2476 resultData = null;
2477 }
2478
2479 if (parent != null && foundParentInTask) {
2480 final int parentLaunchMode = parent.info.launchMode;
2481 final int destIntentFlags = destIntent.getFlags();
2482 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2483 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2484 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2485 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2486 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2487 } else {
2488 try {
2489 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2490 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002491 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002492 null, aInfo, parent.appToken, null,
2493 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2494 0, null, true, null);
2495 foundParentInTask = res == ActivityManager.START_SUCCESS;
2496 } catch (RemoteException e) {
2497 foundParentInTask = false;
2498 }
2499 requestFinishActivityLocked(parent.appToken, resultCode,
2500 resultData, "navigate-up", true);
2501 }
2502 }
2503 Binder.restoreCallingIdentity(origId);
2504 return foundParentInTask;
2505 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 /**
2507 * Perform the common clean-up of an activity record. This is called both
2508 * as part of destroyActivityLocked() (when destroying the client-side
2509 * representation) and cleaning things up as a result of its hosting
2510 * processing going away, in which case there is no remaining client-side
2511 * state to destroy so only the cleanup here is needed.
2512 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002513 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2514 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002515 if (mResumedActivity == r) {
2516 mResumedActivity = null;
2517 }
2518 if (mService.mFocusedActivity == r) {
2519 mService.mFocusedActivity = null;
2520 }
2521
2522 r.configDestroy = false;
2523 r.frozenBeforeDestroy = false;
2524
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002525 if (setState) {
2526 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2527 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002528 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002529 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002530 }
2531
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002532 // Make sure this record is no longer in the pending finishes list.
2533 // This could happen, for example, if we are trimming activities
2534 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002535 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002536 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002537
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 // Remove any pending results.
2539 if (r.finishing && r.pendingResults != null) {
2540 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2541 PendingIntentRecord rec = apr.get();
2542 if (rec != null) {
2543 mService.cancelIntentSenderLocked(rec, false);
2544 }
2545 }
2546 r.pendingResults = null;
2547 }
2548
2549 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002550 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 }
2552
2553 if (mService.mPendingThumbnails.size() > 0) {
2554 // There are clients waiting to receive thumbnails so, in case
2555 // this is an activity that someone is waiting for, add it
2556 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002557 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 }
2559
2560 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002561 removeTimeoutsForActivityLocked(r);
2562 }
2563
2564 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002565 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002566 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002567 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002568 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002569 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 }
2571
Dianne Hackborn5c607432012-02-28 14:44:19 -08002572 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002573 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2574 r.makeFinishing();
2575 if (DEBUG_ADD_REMOVE) {
2576 RuntimeException here = new RuntimeException("here");
2577 here.fillInStackTrace();
2578 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002580 final TaskRecord task = r.task;
2581 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002582 if (DEBUG_STACK) Slog.i(TAG,
2583 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002584 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002585 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002586 r.takeFromHistory();
2587 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002588 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002589 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002590 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002591 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002592 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002593 if (VALIDATE_TOKENS) {
2594 validateAppTokensLocked();
2595 }
2596 cleanUpActivityServicesLocked(r);
2597 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002598 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002599
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 /**
2601 * Perform clean-up of service connections in an activity record.
2602 */
2603 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2604 // Throw away any services that have been bound by this activity.
2605 if (r.connections != null) {
2606 Iterator<ConnectionRecord> it = r.connections.iterator();
2607 while (it.hasNext()) {
2608 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002609 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002610 }
2611 r.connections = null;
2612 }
2613 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002614
2615 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2616 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2617 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2618 mHandler.sendMessage(msg);
2619 }
2620
Dianne Hackborn28695e02011-11-02 21:59:51 -07002621 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002622 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002623 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002624 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2625 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2626 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2627 final ActivityRecord r = activities.get(activityNdx);
2628 if (r.finishing) {
2629 continue;
2630 }
2631 if (r.fullscreen) {
2632 lastIsOpaque = true;
2633 }
2634 if (owner != null && r.app != owner) {
2635 continue;
2636 }
2637 if (!lastIsOpaque) {
2638 continue;
2639 }
2640 // We can destroy this one if we have its icicle saved and
2641 // it is not in the process of pausing/stopping/finishing.
2642 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2643 && r.haveState && !r.visible && r.stopped
2644 && r.state != ActivityState.DESTROYING
2645 && r.state != ActivityState.DESTROYED) {
2646 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2647 + " resumed=" + mResumedActivity
2648 + " pausing=" + mPausingActivity);
2649 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2650 activityRemoved = true;
2651 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002652 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002653 }
2654 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002655 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002656 mStackSupervisor.resumeTopActivitiesLocked();
2657
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002658 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002659 }
2660
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 /**
2662 * Destroy the current CLIENT SIDE instance of an activity. This may be
2663 * called both when actually finishing an activity, or when performing
2664 * a configuration switch where we destroy the current client-side object
2665 * but then create a new client-side object for this same HistoryRecord.
2666 */
2667 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002668 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002669 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002670 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2672 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002673 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002674 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675
2676 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002677
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002678 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002679
2680 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 if (hadApp) {
2683 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002684 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002685 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2686 mService.mHeavyWeightProcess = null;
2687 mService.mHandler.sendEmptyMessage(
2688 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2689 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002691 // No longer have activities, so update oom adj.
2692 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002693 }
2694 }
2695
2696 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002697
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698 try {
2699 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002700 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 r.configChangeFlags);
2702 } catch (Exception e) {
2703 // We can just ignore exceptions here... if the process
2704 // has crashed, our death notification will clean things
2705 // up.
2706 //Slog.w(TAG, "Exception thrown during finish", e);
2707 if (r.finishing) {
2708 removeActivityFromHistoryLocked(r);
2709 removedFromHistory = true;
2710 skipDestroy = true;
2711 }
2712 }
2713
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002715
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002716 // If the activity is finishing, we need to wait on removing it
2717 // from the list to give it a chance to do its cleanup. During
2718 // that time it may make calls back with its token so we need to
2719 // be able to find it on the list and so we don't want to remove
2720 // it from the list yet. Otherwise, we can just immediately put
2721 // it in the destroyed state since we are not removing it from the
2722 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002724 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2725 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002726 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002727 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2729 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002730 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002732 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002733 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002734 }
2735 } else {
2736 // remove this record from the history.
2737 if (r.finishing) {
2738 removeActivityFromHistoryLocked(r);
2739 removedFromHistory = true;
2740 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002741 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002743 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002744 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 }
2746 }
2747
2748 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002749
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002750 if (!mLRUActivities.remove(r) && hadApp) {
2751 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2752 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002753
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002754 return removedFromHistory;
2755 }
2756
Craig Mautnerd2328952013-03-05 12:46:26 -08002757 final void activityDestroyedLocked(IBinder token) {
2758 final long origId = Binder.clearCallingIdentity();
2759 try {
2760 ActivityRecord r = ActivityRecord.forToken(token);
2761 if (r != null) {
2762 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002764
2765 if (isInStackLocked(token) != null) {
2766 if (r.state == ActivityState.DESTROYING) {
2767 cleanUpActivityLocked(r, true, false);
2768 removeActivityFromHistoryLocked(r);
2769 }
2770 }
Craig Mautner05d29032013-05-03 13:40:13 -07002771 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002772 } finally {
2773 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774 }
2775 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002776
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002777 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2778 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002780 if (DEBUG_CLEANUP) Slog.v(
2781 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 + " with " + i + " entries");
2783 while (i > 0) {
2784 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002785 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002786 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002788 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002790 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 }
2792 }
2793 }
2794
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002795 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2796 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002797 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2798 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002799 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2800 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002801 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002802 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002803 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2804 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002805
2806 boolean hasVisibleActivities = false;
2807
2808 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002809 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002810 if (DEBUG_CLEANUP) Slog.v(
2811 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002812 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2813 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2814 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2815 final ActivityRecord r = activities.get(activityNdx);
2816 --i;
2817 if (DEBUG_CLEANUP) Slog.v(
2818 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2819 if (r.app == app) {
2820 boolean remove;
2821 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2822 // Don't currently have state for the activity, or
2823 // it is finishing -- always remove it.
2824 remove = true;
2825 } else if (r.launchCount > 2 &&
2826 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2827 // We have launched this activity too many times since it was
2828 // able to run, so give up and remove it.
2829 remove = true;
2830 } else {
2831 // The process may be gone, but the activity lives on!
2832 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002833 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002834 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002835 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002836 RuntimeException here = new RuntimeException("here");
2837 here.fillInStackTrace();
2838 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2839 + ": haveState=" + r.haveState
2840 + " stateNotNeeded=" + r.stateNotNeeded
2841 + " finishing=" + r.finishing
2842 + " state=" + r.state, here);
2843 }
2844 if (!r.finishing) {
2845 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2846 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2847 r.userId, System.identityHashCode(r),
2848 r.task.taskId, r.shortComponentName,
2849 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002850 if (r.state == ActivityState.RESUMED) {
2851 mService.updateUsageStats(r, false);
2852 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002853 }
2854 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002855
Craig Mautner0247fc82013-02-28 14:32:06 -08002856 } else {
2857 // We have the current state for this activity, so
2858 // it can be restarted later when needed.
2859 if (localLOGV) Slog.v(
2860 TAG, "Keeping entry, setting app to null");
2861 if (r.visible) {
2862 hasVisibleActivities = true;
2863 }
2864 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2865 + r);
2866 r.app = null;
2867 r.nowVisible = false;
2868 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002869 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002870 "App died, clearing saved state of " + r);
2871 r.icicle = null;
2872 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002873 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002874
Craig Mautnerd2328952013-03-05 12:46:26 -08002875 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002876 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002877 }
2878 }
2879
2880 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002882
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002883 final void updateTransitLocked(int transit, Bundle options) {
2884 if (options != null) {
2885 ActivityRecord r = topRunningActivityLocked(null);
2886 if (r != null && r.state != ActivityState.RESUMED) {
2887 r.updateOptionsLocked(options);
2888 } else {
2889 ActivityOptions.abort(options);
2890 }
2891 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002892 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002893 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002894
Craig Mautnercae015f2013-02-08 14:31:27 -08002895 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002896 final TaskRecord task = taskForIdLocked(taskId);
2897 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002898 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002899 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002900 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002901 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2902 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002903 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002904 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002905 }
2906 moveTaskToFrontLocked(task, null, options);
2907 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002908 }
2909 return false;
2910 }
2911
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002912 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002913 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914
Craig Mautner11bf9a52013-02-19 14:08:51 -08002915 final int numTasks = mTaskHistory.size();
2916 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002917 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002918 // nothing to do!
2919 if (reason != null &&
2920 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2921 ActivityOptions.abort(options);
2922 } else {
2923 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2924 }
2925 return;
2926 }
2927
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002928 mStackSupervisor.moveHomeStack(isHomeStack());
2929
Craig Mautner11bf9a52013-02-19 14:08:51 -08002930 // Shift all activities with this task up to the top
2931 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002932 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002933
2934 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 if (reason != null &&
2936 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002937 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 ActivityRecord r = topRunningActivityLocked(null);
2939 if (r != null) {
2940 mNoAnimActivities.add(r);
2941 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002942 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002943 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002944 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002946
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002947 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948
Craig Mautner05d29032013-05-03 13:40:13 -07002949 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002950 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002951
2952 if (VALIDATE_TOKENS) {
2953 validateAppTokensLocked();
2954 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 }
2956
2957 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002958 * Worker method for rearranging history stack. Implements the function of moving all
2959 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002961 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2963 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002964 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965 * @param task The taskId to collect and move to the bottom.
2966 * @return Returns true if the move completed, false if not.
2967 */
2968 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2969 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002970
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971 // If we have a watcher, preflight the move before committing to it. First check
2972 // for *other* available tasks, but if none are available, then try again allowing the
2973 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002974 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 ActivityRecord next = topRunningActivityLocked(null, task);
2976 if (next == null) {
2977 next = topRunningActivityLocked(null, 0);
2978 }
2979 if (next != null) {
2980 // ask watcher if this is allowed
2981 boolean moveOK = true;
2982 try {
2983 moveOK = mService.mController.activityResuming(next.packageName);
2984 } catch (RemoteException e) {
2985 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002986 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987 }
2988 if (!moveOK) {
2989 return false;
2990 }
2991 }
2992 }
2993
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 if (DEBUG_TRANSITION) Slog.v(TAG,
2995 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996
Craig Mautnerd2328952013-03-05 12:46:26 -08002997 final TaskRecord tr = taskForIdLocked(task);
2998 if (tr == null) {
2999 return false;
3000 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003001 mTaskHistory.remove(tr);
3002 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003
3004 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003005 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3006 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 ActivityRecord r = topRunningActivityLocked(null);
3008 if (r != null) {
3009 mNoAnimActivities.add(r);
3010 }
3011 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003012 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003013 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003014 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003015 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003016
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003017 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003018 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003019 }
3020
Craig Mautnerde4ef022013-04-07 19:01:33 -07003021 if (mResumedActivity != null && mResumedActivity.task == tr &&
3022 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07003023 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07003024 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003025 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003026 }
3027
Craig Mautner05d29032013-05-03 13:40:13 -07003028 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 return true;
3030 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003031
Craig Mautner8849a5e2013-04-02 16:41:03 -07003032 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003034 final Uri data = r.intent.getData();
3035 final String strData = data != null ? data.toSafeString() : null;
3036
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003038 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003039 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003040 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003041 }
3042
3043 /**
3044 * Make sure the given activity matches the current configuration. Returns
3045 * false if the activity had to be destroyed. Returns true if the
3046 * configuration is the same, or the activity will remain running as-is
3047 * for whatever reason. Ensures the HistoryRecord is updated with the
3048 * correct configuration and all other bookkeeping is handled.
3049 */
3050 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3051 int globalChanges) {
3052 if (mConfigWillChange) {
3053 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3054 "Skipping config check (will change): " + r);
3055 return true;
3056 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003057
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3059 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003060
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061 // Short circuit: if the two configurations are the exact same
3062 // object (the common case), then there is nothing to do.
3063 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003064 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003065 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3066 "Configuration unchanged in " + r);
3067 return true;
3068 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003069
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 // We don't worry about activities that are finishing.
3071 if (r.finishing) {
3072 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3073 "Configuration doesn't matter in finishing " + r);
3074 r.stopFreezingScreenLocked(false);
3075 return true;
3076 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003077
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 // Okay we now are going to make this activity have the new config.
3079 // But then we need to figure out how it needs to deal with that.
3080 Configuration oldConfig = r.configuration;
3081 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003082
3083 // Determine what has changed. May be nothing, if this is a config
3084 // that has come back from the app after going idle. In that case
3085 // we just want to leave the official config object now in the
3086 // activity and do nothing else.
3087 final int changes = oldConfig.diff(newConfig);
3088 if (changes == 0 && !r.forceNewConfig) {
3089 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3090 "Configuration no differences in " + r);
3091 return true;
3092 }
3093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 // If the activity isn't currently running, just leave the new
3095 // configuration and it will pick that up next time it starts.
3096 if (r.app == null || r.app.thread == null) {
3097 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3098 "Configuration doesn't matter not running " + r);
3099 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003100 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003101 return true;
3102 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003103
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003104 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003105 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3106 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3107 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003108 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003109 + ", newConfig=" + newConfig);
3110 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003111 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003112 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3113 r.configChangeFlags |= changes;
3114 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003115 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003116 if (r.app == null || r.app.thread == null) {
3117 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003118 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003119 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003120 } else if (r.state == ActivityState.PAUSING) {
3121 // A little annoying: we are waiting for this activity to
3122 // finish pausing. Let's not do anything now, but just
3123 // flag that it needs to be restarted when done pausing.
3124 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003125 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003126 r.configDestroy = true;
3127 return true;
3128 } else if (r.state == ActivityState.RESUMED) {
3129 // Try to optimize this case: the configuration is changing
3130 // and we need to restart the top, resumed activity.
3131 // Instead of doing the normal handshaking, just say
3132 // "restart!".
3133 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003134 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003135 relaunchActivityLocked(r, r.configChangeFlags, true);
3136 r.configChangeFlags = 0;
3137 } else {
3138 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003139 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003140 relaunchActivityLocked(r, r.configChangeFlags, false);
3141 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003142 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003143
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003144 // All done... tell the caller we weren't able to keep this
3145 // activity around.
3146 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 // Default case: the activity can handle this new configuration, so
3150 // hand it over. Note that we don't need to give it the new
3151 // configuration, since we always send configuration changes to all
3152 // process when they happen so it can just use whatever configuration
3153 // it last got.
3154 if (r.app != null && r.app.thread != null) {
3155 try {
3156 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003157 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 } catch (RemoteException e) {
3159 // If process died, whatever.
3160 }
3161 }
3162 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003164 return true;
3165 }
3166
3167 private final boolean relaunchActivityLocked(ActivityRecord r,
3168 int changes, boolean andResume) {
3169 List<ResultInfo> results = null;
3170 List<Intent> newIntents = null;
3171 if (andResume) {
3172 results = r.results;
3173 newIntents = r.newIntents;
3174 }
3175 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3176 + " with results=" + results + " newIntents=" + newIntents
3177 + " andResume=" + andResume);
3178 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003179 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003181
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003182 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003185 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3186 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3187 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003188 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003189 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003190 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 // Note: don't need to call pauseIfSleepingLocked() here, because
3192 // the caller will only pass in 'andResume' if this activity is
3193 // currently resumed, which implies we aren't sleeping.
3194 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003195 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 }
3197
3198 if (andResume) {
3199 r.results = null;
3200 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003201 r.state = ActivityState.RESUMED;
3202 } else {
3203 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3204 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 }
3206
3207 return true;
3208 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003209
3210 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003211 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3212 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3213 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3214 final ActivityRecord r = activities.get(activityNdx);
3215 if (r.appToken == token) {
3216 return true;
3217 }
3218 if (r.fullscreen && !r.finishing) {
3219 return false;
3220 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003221 }
3222 }
3223 return true;
3224 }
3225
3226 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003227 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3228 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3229 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3230 final ActivityRecord r = activities.get(activityNdx);
3231 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003232 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003233 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003234 }
3235 }
3236 }
3237
3238 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3239 boolean didSomething = false;
3240 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003241 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3242 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3243 int numActivities = activities.size();
3244 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3245 ActivityRecord r = activities.get(activityNdx);
3246 final boolean samePackage = r.packageName.equals(name)
3247 || (name == null && r.userId == userId);
3248 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3249 && (samePackage || r.task == lastTask)
3250 && (r.app == null || evenPersistent || !r.app.persistent)) {
3251 if (!doit) {
3252 if (r.finishing) {
3253 // If this activity is just finishing, then it is not
3254 // interesting as far as something to stop.
3255 continue;
3256 }
3257 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003258 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003259 didSomething = true;
3260 Slog.i(TAG, " Force finishing activity " + r);
3261 if (samePackage) {
3262 if (r.app != null) {
3263 r.app.removed = true;
3264 }
3265 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003266 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003267 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003268 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3269 true)) {
3270 // r has been deleted from mActivities, accommodate.
3271 --numActivities;
3272 --activityNdx;
3273 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003274 }
3275 }
3276 }
3277 return didSomething;
3278 }
3279
3280 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003281 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003282 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003283 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3284 --maxNum, --taskNdx) {
3285 final TaskRecord task = mTaskHistory.get(taskNdx);
3286 ActivityRecord r = null;
3287 ActivityRecord top = null;
3288 int numActivities = 0;
3289 int numRunning = 0;
3290 final ArrayList<ActivityRecord> activities = task.mActivities;
3291 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3292 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003293
Craig Mautneraab647e2013-02-28 16:31:36 -08003294 // Initialize state for next task if needed.
3295 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3296 top = r;
3297 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003298 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003299
3300 // Add 'r' into the current task.
3301 numActivities++;
3302 if (r.app != null && r.app.thread != null) {
3303 numRunning++;
3304 }
3305
3306 if (localLOGV) Slog.v(
3307 TAG, r.intent.getComponent().flattenToShortString()
3308 + ": task=" + r.task);
3309 }
3310
3311 RunningTaskInfo ci = new RunningTaskInfo();
3312 ci.id = task.taskId;
3313 ci.baseActivity = r.intent.getComponent();
3314 ci.topActivity = top.intent.getComponent();
3315 if (top.thumbHolder != null) {
3316 ci.description = top.thumbHolder.lastDescription;
3317 }
3318 ci.numActivities = numActivities;
3319 ci.numRunning = numRunning;
3320 //System.out.println(
3321 // "#" + maxNum + ": " + " descr=" + ci.description);
3322 if (receiver != null) {
3323 if (localLOGV) Slog.v(
3324 TAG, "State=" + top.state + "Idle=" + top.idle
3325 + " app=" + top.app
3326 + " thr=" + (top.app != null ? top.app.thread : null));
3327 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3328 if (top.idle && top.app != null && top.app.thread != null) {
3329 topRecord = top;
3330 } else {
3331 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003332 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003333 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003334 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003335 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003336 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003337 }
3338 return topRecord;
3339 }
3340
3341 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003342 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003343 if (DEBUG_SWITCH) Slog.d(
3344 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003345 if (top >= 0) {
3346 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3347 int activityTop = activities.size() - 1;
3348 if (activityTop > 0) {
3349 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3350 "unhandled-back", true);
3351 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003352 }
3353 }
3354
Craig Mautnere79d42682013-04-01 19:01:53 -07003355 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003356 if (!containsApp(app)) {
3357 return;
3358 }
3359 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003360 if (mPausingActivity != null && mPausingActivity.app == app) {
3361 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3362 "App died while pausing: " + mPausingActivity);
3363 mPausingActivity = null;
3364 }
3365 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3366 mLastPausedActivity = null;
3367 }
Craig Mautner37f748d2013-06-24 16:19:55 -07003368 final ActivityRecord top = topRunningActivityLocked(null);
3369 final boolean launchHomeTaskNext =
3370 top != null && top.app == app && top.mLaunchHomeTaskNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003371
3372 // Remove this application's activities from active lists.
3373 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3374
3375 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003376 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautner37f748d2013-06-24 16:19:55 -07003377 if (stack == null || launchHomeTaskNext) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003378 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003379 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003380 // If there was nothing to resume, and we are not already
3381 // restarting this process, but there is a visible activity that
3382 // is hosted by the process... then make sure all visible
3383 // activities are running, taking care of restarting this
3384 // process.
3385 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003386 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003387 }
3388 }
3389 }
3390 }
3391
Craig Mautnercae015f2013-02-08 14:31:27 -08003392 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3394 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3395 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3396 final ActivityRecord r = activities.get(activityNdx);
3397 if (r.app == app) {
3398 Slog.w(TAG, " Force finishing activity "
3399 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003400 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003401 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003402 }
3403 }
3404 }
3405
Dianne Hackborn390517b2013-05-30 15:03:32 -07003406 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003407 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003408 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003409 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3410 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003411 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3412 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003413 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003414 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003415 if (printed) {
3416 header = null;
3417 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003418 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003419 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003420 }
3421
3422 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3423 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3424
3425 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003426 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3427 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003428 }
3429 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003430 final int top = mTaskHistory.size() - 1;
3431 if (top >= 0) {
3432 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3433 int listTop = list.size() - 1;
3434 if (listTop >= 0) {
3435 activities.add(list.get(listTop));
3436 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003437 }
3438 } else {
3439 ItemMatcher matcher = new ItemMatcher();
3440 matcher.build(name);
3441
Craig Mautneraab647e2013-02-28 16:31:36 -08003442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3443 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3444 if (matcher.match(r1, r1.intent.getComponent())) {
3445 activities.add(r1);
3446 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003447 }
3448 }
3449 }
3450
3451 return activities;
3452 }
3453
3454 ActivityRecord restartPackage(String packageName) {
3455 ActivityRecord starting = topRunningActivityLocked(null);
3456
3457 // All activities that came from the package must be
3458 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3460 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3461 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3462 final ActivityRecord a = activities.get(activityNdx);
3463 if (a.info.packageName.equals(packageName)) {
3464 a.forceNewConfig = true;
3465 if (starting != null && a == starting && a.visible) {
3466 a.startFreezingScreenLocked(starting.app,
3467 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3468 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003469 }
3470 }
3471 }
3472
3473 return starting;
3474 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003475
Craig Mautnerde4ef022013-04-07 19:01:33 -07003476 boolean removeTask(TaskRecord task) {
3477 mTaskHistory.remove(task);
3478 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003479 }
3480
Craig Mautnerde4ef022013-04-07 19:01:33 -07003481 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003482 TaskRecord task = new TaskRecord(taskId, info, intent);
3483 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003484 return task;
3485 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003486
3487 ArrayList<TaskRecord> getAllTasks() {
3488 return new ArrayList<TaskRecord>(mTaskHistory);
3489 }
3490
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003491 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003492 task.stack = this;
3493 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003494 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003495 } else {
3496 mTaskHistory.add(0, task);
3497 }
3498 }
3499
3500 public int getStackId() {
3501 return mStackId;
3502 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003503
3504 @Override
3505 public String toString() {
3506 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3507 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508}