blob: be03ee3a5ef8da1ce26a5c1a02e1a0b3c89e18ef [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 Mautner8849a5e2013-04-02 16:41:03 -0700468 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700469 ComponentName cls = intent.getComponent();
470 if (info.targetActivity != null) {
471 cls = new ComponentName(info.packageName, info.targetActivity);
472 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700473 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800474
475 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
476 final TaskRecord task = mTaskHistory.get(taskNdx);
477 final ActivityRecord r = task.getTopActivity();
478 if (r == null || r.finishing || r.userId != userId ||
479 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
480 continue;
481 }
482
483 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
484 // + "/aff=" + r.task.affinity + " to new cls="
485 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
486 if (task.affinity != null) {
487 if (task.affinity.equals(info.taskAffinity)) {
488 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700489 return r;
490 }
Craig Mautner000f0022013-02-26 15:04:29 -0800491 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
492 //Slog.i(TAG, "Found matching class!");
493 //dump();
494 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
495 return r;
496 } else if (task.affinityIntent != null
497 && task.affinityIntent.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;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700502 }
503 }
504
505 return null;
506 }
507
508 /**
509 * Returns the first activity (starting from the top of the stack) that
510 * is the same as the given activity. Returns null if no such activity
511 * is found.
512 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700513 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700514 ComponentName cls = intent.getComponent();
515 if (info.targetActivity != null) {
516 cls = new ComponentName(info.packageName, info.targetActivity);
517 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700518 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700519
Craig Mautner000f0022013-02-26 15:04:29 -0800520 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
521 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
522 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
523 ActivityRecord r = activities.get(activityNdx);
524 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700525 //Slog.i(TAG, "Found matching class!");
526 //dump();
527 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
528 return r;
529 }
530 }
531 }
532
533 return null;
534 }
535
Amith Yamasani742a6712011-05-04 14:49:28 -0700536 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700537 * Move the activities around in the stack to bring a user to the foreground. This only
538 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700539 * @return whether there are any activities for the specified user.
540 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700541 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800542 if (VALIDATE_TOKENS) {
543 validateAppTokensLocked();
544 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800545 if (mCurrentUser == userId) {
546 return true;
547 }
548 mCurrentUser = userId;
549
550 // Move userId's tasks to the top.
551 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800552 int index = mTaskHistory.size();
553 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700554 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800555 if (task.userId == userId) {
556 haveActivities = true;
557 mTaskHistory.remove(i);
558 mTaskHistory.add(task);
559 --index;
560 }
561 }
562
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700563 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700564 }
565
Craig Mautner2420ead2013-04-01 17:13:20 -0700566 void minimalResumeActivityLocked(ActivityRecord r) {
567 r.state = ActivityState.RESUMED;
568 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
569 + " (starting new instance)");
570 r.stopped = false;
571 mResumedActivity = r;
572 r.task.touchActiveTime();
573 mService.addRecentTaskLocked(r.task);
574 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700575 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700576 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700577 }
578
Dianne Hackborncee04b52013-07-03 17:01:28 -0700579 private void startLaunchTraces() {
580 if (mFullyDrawnStartTime != 0) {
581 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
582 }
583 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
584 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
585 }
586
587 private void stopFullyDrawnTraceIfNeeded() {
588 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
589 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
590 mFullyDrawnStartTime = 0;
591 }
592 }
593
Craig Mautnere79d42682013-04-01 19:01:53 -0700594 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700595 if (r.displayStartTime == 0) {
596 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
597 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700598 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700599 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700600 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700601 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700602 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700603 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700604 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800606
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700607 void clearLaunchTime(ActivityRecord r) {
608 r.displayStartTime = r.fullyDrawnStartTime = 0;
609 }
610
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800611 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800612 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800613 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
614 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
615 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
616 activities.get(activityNdx).setSleeping(false);
617 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800618 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800619 }
620
Craig Mautner0eea92c2013-05-16 13:35:39 -0700621 /**
622 * @return true if something must be done before going to sleep.
623 */
624 boolean checkReadyForSleepLocked() {
625 if (mResumedActivity != null) {
626 // Still have something resumed; can't sleep until it is paused.
627 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
628 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
629 startPausingLocked(false, true);
630 return true;
631 }
632 if (mPausingActivity != null) {
633 // Still waiting for something to pause; can't sleep yet.
634 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
635 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800636 }
637
Craig Mautner0eea92c2013-05-16 13:35:39 -0700638 return false;
639 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800640
Craig Mautner0eea92c2013-05-16 13:35:39 -0700641 void goToSleep() {
642 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800643
Craig Mautner0eea92c2013-05-16 13:35:39 -0700644 // Make sure any stopped but visible activities are now sleeping.
645 // This ensures that the activity's onStop() is called.
646 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
647 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
648 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
649 final ActivityRecord r = activities.get(activityNdx);
650 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
651 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652 }
653 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800654 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700655 }
Craig Mautner59c00972012-07-30 12:10:24 -0700656
Dianne Hackbornd2835932010-12-13 16:28:46 -0800657 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800658 if (who.noDisplay) {
659 return null;
660 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800661
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800662 Resources res = mService.mContext.getResources();
663 int w = mThumbnailWidth;
664 int h = mThumbnailHeight;
665 if (w < 0) {
666 mThumbnailWidth = w =
667 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
668 mThumbnailHeight = h =
669 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
670 }
671
672 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800673 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
674 || mLastScreenshotBitmap.getWidth() != w
675 || mLastScreenshotBitmap.getHeight() != h) {
676 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700677 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800678 who.appToken, Display.DEFAULT_DISPLAY, w, h);
679 }
680 if (mLastScreenshotBitmap != null) {
681 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
682 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800683 }
684 return null;
685 }
686
Craig Mautnercf910b02013-04-23 11:23:27 -0700687 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800688 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800689 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700690 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800691 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700692 ActivityRecord prev = mResumedActivity;
693 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700694 Slog.e(TAG, "Trying to pause when nothing is resumed",
695 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700696 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700697 return;
698 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700699 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
700 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700701 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800702 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700703 mLastPausedActivity = prev;
704 prev.state = ActivityState.PAUSING;
705 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700706 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700707 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700708 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709
710 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800711
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700712 if (prev.app != null && prev.app.thread != null) {
713 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
714 try {
715 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700716 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700718 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800719 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
720 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700721 } catch (Exception e) {
722 // Ignore exception, if process died other code will cleanup.
723 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800724 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 mLastPausedActivity = null;
726 }
727 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800728 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 mLastPausedActivity = null;
730 }
731
732 // If we are not going to sleep, we want to ensure the device is
733 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700734 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700735 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700736 }
737
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800738 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 // Have the window manager pause its key dispatching until the new
740 // activity has started. If we're pausing the activity just because
741 // the screen is being turned off and the UI is sleeping, don't interrupt
742 // key dispatch; the same activity will pick it up again on wakeup.
743 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800744 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700745 } else {
746 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
747 }
748
749 // Schedule a pause timeout in case the app doesn't respond.
750 // We don't give it much time because this directly impacts the
751 // responsiveness seen by the user.
752 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
753 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700754 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
756 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
757 } else {
758 // This activity failed to schedule the
759 // pause, so just treat it as being paused now.
760 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700761 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 }
763 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700764
Craig Mautnerd2328952013-03-05 12:46:26 -0800765 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800767 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768
Craig Mautnerd2328952013-03-05 12:46:26 -0800769 final ActivityRecord r = isInStackLocked(token);
770 if (r != null) {
771 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
772 if (mPausingActivity == r) {
773 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
774 + (timeout ? " (due to timeout)" : " (pause complete)"));
775 r.state = ActivityState.PAUSED;
776 completePauseLocked();
777 } else {
778 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
779 r.userId, System.identityHashCode(r), r.shortComponentName,
780 mPausingActivity != null
781 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 }
783 }
784 }
785
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700786 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
787 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700788 if (r.state != ActivityState.STOPPING) {
789 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
790 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
791 return;
792 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700793 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700794 if (icicle != null) {
795 // If icicle is null, this is happening due to a timeout, so we
796 // haven't really saved the state.
797 r.icicle = icicle;
798 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800799 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700800 r.updateThumbnail(thumbnail, description);
801 }
802 if (!r.stopped) {
803 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
804 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
805 r.stopped = true;
806 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700807 if (r.finishing) {
808 r.clearOptionsLocked();
809 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700810 if (r.configDestroy) {
811 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700812 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700813 } else {
814 // Now that this process has stopped, we may want to consider
815 // it to be the previous app to try to keep around in case
816 // the user wants to return to it.
817 ProcessRecord fgApp = null;
818 if (mResumedActivity != null) {
819 fgApp = mResumedActivity.app;
820 } else if (mPausingActivity != null) {
821 fgApp = mPausingActivity.app;
822 }
823 if (r.app != null && fgApp != null && r.app != fgApp
824 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
825 && r.app != mService.mHomeProcess) {
826 mService.mPreviousProcess = r.app;
827 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
828 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800829 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700830 }
831 }
832 }
833
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800834 private final void completePauseLocked() {
835 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800837
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800838 if (prev != null) {
839 if (prev.finishing) {
840 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700841 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800842 } else if (prev.app != null) {
843 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
844 if (prev.waitingVisible) {
845 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700846 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800847 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
848 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800849 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800850 if (prev.configDestroy) {
851 // The previous is being paused because the configuration
852 // is changing, which means it is actually stopping...
853 // To juggle the fact that we are also starting a new
854 // instance right now, we need to first completely stop
855 // the current instance before starting the new one.
856 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
857 destroyActivityLocked(prev, true, false, "pause-config");
858 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700859 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700860 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
861 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800862 // If we already have a few activities waiting to stop,
863 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700864 // them out. Or if r is the last of activity of the last task the stack
865 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800866 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700867 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700869 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800870 }
871 }
872 } else {
873 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
874 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800875 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800876 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700877 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800878
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700879 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700880 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700881 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800882 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700883 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700884 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700885 if (top == null || (prev != null && top != prev)) {
886 // If there are no more activities available to run,
887 // do resume anyway to start something. Also if the top
888 // activity on the stack is not the just paused activity,
889 // we need to go ahead and resume it to ensure we complete
890 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700891 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700892 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700893 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800894
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800895 if (prev != null) {
896 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700897
Craig Mautner525f3d92013-05-07 14:01:50 -0700898 if (prev.app != null && prev.cpuTimeAtResume > 0
899 && mService.mBatteryStatsService.isOnBattery()) {
900 long diff;
901 synchronized (mService.mProcessStatsThread) {
902 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
903 - prev.cpuTimeAtResume;
904 }
905 if (diff > 0) {
906 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
907 synchronized (bsi) {
908 BatteryStatsImpl.Uid.Proc ps =
909 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
910 prev.info.packageName);
911 if (ps != null) {
912 ps.addForegroundTimeLocked(diff);
913 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 }
915 }
916 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700917 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 }
920
921 /**
922 * Once we know that we have asked an application to put an activity in
923 * the resumed state (either by launching it or explicitly telling it),
924 * this function updates the rest of our state to match that fact.
925 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700926 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 next.idle = false;
928 next.results = null;
929 next.newIntents = null;
930
931 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700932 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700934 mStackSupervisor.reportResumedActivityLocked(next);
935
936 next.resumeKeyDispatchingLocked();
937 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938
939 // Mark the point when the activity is resuming
940 // TODO: To be more accurate, the mark should be before the onCreate,
941 // not after the onResume. But for subsequent starts, onResume is fine.
942 if (next.app != null) {
943 synchronized (mService.mProcessStatsThread) {
944 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
945 }
946 } else {
947 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
948 }
949 }
950
Craig Mautner580ea812013-04-25 12:58:38 -0700951 /**
952 * Version of ensureActivitiesVisible that can easily be called anywhere.
953 */
954 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
955 return ensureActivitiesVisibleLocked(starting, configChanges, false);
956 }
957
958 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
959 boolean forceHomeShown) {
960 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700961 return r != null &&
962 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700963 }
964
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700965 /**
966 * Make sure that all activities that need to be visible (that is, they
967 * currently can be seen by the user) actually are.
968 */
Craig Mautner580ea812013-04-25 12:58:38 -0700969 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
970 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700971 if (DEBUG_VISBILITY) Slog.v(
972 TAG, "ensureActivitiesVisible behind " + top
973 + " configChanges=0x" + Integer.toHexString(configChanges));
974
Craig Mautner5eda9b32013-07-02 11:58:16 -0700975 if (mTranslucentActivityWaiting != top) {
976 mUndrawnActivitiesBelowTopTranslucent.clear();
977 if (mTranslucentActivityWaiting != null) {
978 // Call the callback with a timeout indication.
979 notifyActivityDrawnLocked(null);
980 mTranslucentActivityWaiting = null;
981 }
982 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
983 }
984
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985 // If the top activity is not fullscreen, then we need to
986 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800987 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -0700988 boolean showHomeBehindStack = false;
989 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
990 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700991 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -0800992 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
993 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
994 final ActivityRecord r = activities.get(activityNdx);
995 if (r.finishing) {
996 continue;
997 }
998 if (aboveTop && r != top) {
999 continue;
1000 }
1001 aboveTop = false;
1002 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001003 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001004 TAG, "Make visible? " + r + " finishing=" + r.finishing
1005 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001006
Craig Mautnerd44711d2013-02-23 11:24:36 -08001007 final boolean doThisProcess = onlyThisProcess == null
1008 || onlyThisProcess.equals(r.processName);
1009
1010 // First: if this is not the current activity being started, make
1011 // sure it matches the current configuration.
1012 if (r != starting && doThisProcess) {
1013 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001015
1016 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001017 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001018 // This activity needs to be visible, but isn't even
1019 // running... get it started, but don't resume it
1020 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001021 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001022 if (r != starting) {
1023 r.startFreezingScreenLocked(r.app, configChanges);
1024 }
1025 if (!r.visible) {
1026 if (DEBUG_VISBILITY) Slog.v(
1027 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001028 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001029 }
1030 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001031 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001032 }
1033 }
1034
1035 } else if (r.visible) {
1036 // If this activity is already visible, then there is nothing
1037 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001038 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001039 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001040
1041 } else if (onlyThisProcess == null) {
1042 // This activity is not currently visible, but is running.
1043 // Tell it to become visible.
1044 r.visible = true;
1045 if (r.state != ActivityState.RESUMED && r != starting) {
1046 // If this activity is paused, tell it
1047 // to now show its window.
1048 if (DEBUG_VISBILITY) Slog.v(
1049 TAG, "Making visible and scheduling visibility: " + r);
1050 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001051 if (mTranslucentActivityWaiting != null) {
1052 mUndrawnActivitiesBelowTopTranslucent.add(r);
1053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001054 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001055 r.sleeping = false;
1056 r.app.pendingUiClean = true;
1057 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1058 r.stopFreezingScreenLocked(false);
1059 } catch (Exception e) {
1060 // Just skip on any failure; we'll make it
1061 // visible when it next restarts.
1062 Slog.w(TAG, "Exception thrown making visibile: "
1063 + r.intent.getComponent(), e);
1064 }
1065 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001066 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067
Craig Mautnerd44711d2013-02-23 11:24:36 -08001068 // Aggregate current change flags.
1069 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070
Craig Mautnerd44711d2013-02-23 11:24:36 -08001071 if (r.fullscreen) {
1072 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001073 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1074 behindFullscreen = true;
1075 } else if (r.mLaunchHomeTaskNext) {
1076 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1077 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001078 behindFullscreen = true;
1079 }
1080 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001081 if (DEBUG_VISBILITY) Slog.v(
1082 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1083 + " state=" + r.state
1084 + " behindFullscreen=" + behindFullscreen);
1085 // Now for any activities that aren't visible to the user, make
1086 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001088 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 r.visible = false;
1090 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001091 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001092 switch (r.state) {
1093 case STOPPING:
1094 case STOPPED:
1095 if (r.app != null && r.app.thread != null) {
1096 if (DEBUG_VISBILITY) Slog.v(
1097 TAG, "Scheduling invisibility: " + r);
1098 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1099 }
1100 break;
1101
1102 case INITIALIZING:
1103 case RESUMED:
1104 case PAUSING:
1105 case PAUSED:
1106 stopActivityLocked(r);
1107 break;
1108
1109 default:
1110 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001111 }
1112 } catch (Exception e) {
1113 // Just skip on any failure; we'll make it
1114 // visible when it next restarts.
1115 Slog.w(TAG, "Exception thrown making hidden: "
1116 + r.intent.getComponent(), e);
1117 }
1118 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001119 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001120 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001121 }
1122 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 }
Craig Mautner580ea812013-04-25 12:58:38 -07001124 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001125 }
Craig Mautner58547802013-03-05 08:23:53 -08001126
Craig Mautner5eda9b32013-07-02 11:58:16 -07001127 void convertToTranslucent(ActivityRecord r) {
1128 mTranslucentActivityWaiting = r;
1129 mUndrawnActivitiesBelowTopTranslucent.clear();
1130 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1131 }
1132
1133 /**
1134 * Called as activities below the top translucent activity are redrawn. When the last one is
1135 * redrawn notify the top activity by calling
1136 * {@link Activity#onTranslucentConversionComplete}.
1137 *
1138 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1139 * occurred and the activity will be notified immediately.
1140 */
1141 void notifyActivityDrawnLocked(ActivityRecord r) {
1142 if ((r == null)
1143 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1144 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1145 // The last undrawn activity below the top has just been drawn. If there is an
1146 // opaque activity at the top, notify it that it can become translucent safely now.
1147 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1148 mTranslucentActivityWaiting = null;
1149 mUndrawnActivitiesBelowTopTranslucent.clear();
1150 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1151
1152 if (waitingActivity != null && waitingActivity.app != null &&
1153 waitingActivity.app.thread != null) {
1154 try {
1155 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1156 waitingActivity.appToken, r != null);
1157 } catch (RemoteException e) {
1158 }
1159 }
1160 }
1161 }
1162
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001163 /**
1164 * Ensure that the top activity in the stack is resumed.
1165 *
1166 * @param prev The previously resumed activity, for when in the process
1167 * of pausing; can be null to call from elsewhere.
1168 *
1169 * @return Returns true if something is being resumed, or false if
1170 * nothing happened.
1171 */
1172 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001173 return resumeTopActivityLocked(prev, null);
1174 }
1175
1176 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001177 // Find the first activity that is not finishing.
1178 ActivityRecord next = topRunningActivityLocked(null);
1179
1180 // Remember how we'll process this pause/resume situation, and ensure
1181 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001182 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1183 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184
1185 if (next == null) {
1186 // There are no more activities! Let's just start up the
1187 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001188 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001189 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001190 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 }
1192
1193 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001194
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001196 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1197 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198 // Make sure we have executed any pending transitions, since there
1199 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001200 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001202 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001203 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204 return false;
1205 }
1206
Craig Mautner525f3d92013-05-07 14:01:50 -07001207 final TaskRecord nextTask = next.task;
1208 final TaskRecord prevTask = prev != null ? prev.task : null;
1209 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001210 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001211 if (prevTask == nextTask) {
1212 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1213 final int numActivities = activities.size();
1214 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1215 final ActivityRecord r = activities.get(activityNdx);
1216 // r is usually the same as next, but what if two activities were launched
1217 // before prev finished?
1218 if (!r.finishing) {
1219 r.mLaunchHomeTaskNext = true;
1220 r.frontOfTask = true;
1221 break;
1222 }
1223 }
1224 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001225 // This task is going away but it was supposed to return to the home task.
1226 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001227 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001228 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1229 } else {
1230 return mStackSupervisor.resumeHomeActivity(prev);
1231 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001232 }
1233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 // If we are sleeping, and there is no resumed activity, and the top
1235 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001236 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001237 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001238 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001239 // Make sure we have executed any pending transitions, since there
1240 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001241 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001242 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001243 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001244 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 return false;
1246 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001247
1248 // Make sure that the user who owns this activity is started. If not,
1249 // we will just leave it as is because someone should be bringing
1250 // another user's activities to the top of the stack.
1251 if (mService.mStartedUsers.get(next.userId) == null) {
1252 Slog.w(TAG, "Skipping resume of top activity " + next
1253 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001254 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001255 return false;
1256 }
1257
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258 // The activity may be waiting for stop, but that is no longer
1259 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001260 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001261 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001262 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001263 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001264
Dianne Hackborn84375872012-06-01 19:03:50 -07001265 next.updateOptionsLocked(options);
1266
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001267 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1268
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001269 // If we are currently pausing an activity, then don't do anything
1270 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001271 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1272 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "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);
1312 }
1313 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001314 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001315 // At this point we want to put the upcoming activity's process
1316 // at the top of the LRU list, since we know we will be needing it
1317 // very soon and it would be a waste to let it get killed if it
1318 // happens to be sitting towards the end.
1319 if (next.app != null && next.app.thread != null) {
1320 // No reason to do full oom adj update here; we'll let that
1321 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001322 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001323 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001324 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001325 return true;
1326 }
1327
Christopher Tated3f175c2012-06-14 14:16:54 -07001328 // If the most recent activity was noHistory but was only stopped rather
1329 // than stopped+finished because the device went to sleep, we need to make
1330 // sure to finish it as we're making a new activity topmost.
1331 final ActivityRecord last = mLastPausedActivity;
1332 if (mService.mSleeping && last != null && !last.finishing) {
1333 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1334 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1335 if (DEBUG_STATES) {
1336 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1337 }
1338 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001339 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001340 }
1341 }
1342
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001343 if (prev != null && prev != next) {
1344 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1345 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001346 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001347 if (DEBUG_SWITCH) Slog.v(
1348 TAG, "Resuming top, waiting visible to hide: " + prev);
1349 } else {
1350 // The next activity is already visible, so hide the previous
1351 // activity's windows right now so we can show the new one ASAP.
1352 // We only do this if the previous is finishing, which should mean
1353 // it is on top of the one being resumed so hiding it quickly
1354 // is good. Otherwise, we want to do the normal route of allowing
1355 // the resumed activity to be shown so we can decide if the
1356 // previous should actually be hidden depending on whether the
1357 // new one is found to be full-screen or not.
1358 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001359 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1361 + prev + ", waitingVisible="
1362 + (prev != null ? prev.waitingVisible : null)
1363 + ", nowVisible=" + next.nowVisible);
1364 } else {
1365 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1366 + prev + ", waitingVisible="
1367 + (prev != null ? prev.waitingVisible : null)
1368 + ", nowVisible=" + next.nowVisible);
1369 }
1370 }
1371 }
1372
Dianne Hackborne7f97212011-02-24 14:40:20 -08001373 // Launching this app's activity, make sure the app is no longer
1374 // considered stopped.
1375 try {
1376 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001377 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001378 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001379 } catch (IllegalArgumentException e) {
1380 Slog.w(TAG, "Failed trying to unstop package "
1381 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001382 }
1383
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001384 // We are starting up the next activity, so tell the window manager
1385 // that the previous one will be hidden soon. This way it can know
1386 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001387 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001388 if (prev != null) {
1389 if (prev.finishing) {
1390 if (DEBUG_TRANSITION) Slog.v(TAG,
1391 "Prepare close transition: prev=" + prev);
1392 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001393 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001394 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001396 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001397 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1398 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001399 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001400 mWindowManager.setAppWillBeHidden(prev.appToken);
1401 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001403 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001405 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001406 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001408 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001409 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1410 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001411 }
1412 }
1413 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001414 mWindowManager.setAppWillBeHidden(prev.appToken);
1415 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 }
Craig Mautner967212c2013-04-13 21:10:58 -07001417 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001418 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001420 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001421 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001423 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 }
1425 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001426 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001427 next.applyOptionsLocked();
1428 } else {
1429 next.clearOptionsLocked();
1430 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431
Craig Mautnercf910b02013-04-23 11:23:27 -07001432 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 if (next.app != null && next.app.thread != null) {
1434 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1435
1436 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001437 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001439 // schedule launch ticks to collect information about slow apps.
1440 next.startLaunchTickingLocked();
1441
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001442 ActivityRecord lastResumedActivity =
1443 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 ActivityState lastState = next.state;
1445
1446 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001447
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001448 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 next.state = ActivityState.RESUMED;
1450 mResumedActivity = next;
1451 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001452 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001453 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 updateLRUListLocked(next);
1455
1456 // Have the window manager re-evaluate the orientation of
1457 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001458 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001459 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001460 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001461 mService.mConfiguration,
1462 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1463 if (config != null) {
1464 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001465 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001466 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001467 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001468
Craig Mautner525f3d92013-05-07 14:01:50 -07001469 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 // The configuration update wasn't able to keep the existing
1471 // instance of the activity, and instead started a new one.
1472 // We should be all done, but let's just make sure our activity
1473 // is still at the top and schedule another run if something
1474 // weird happened.
1475 ActivityRecord nextNext = topRunningActivityLocked(null);
1476 if (DEBUG_SWITCH) Slog.i(TAG,
1477 "Activity config changed during resume: " + next
1478 + ", new next: " + nextNext);
1479 if (nextNext != next) {
1480 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001481 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001483 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001484 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001485 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001486 return true;
1487 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001488 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001489 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 }
Craig Mautner58547802013-03-05 08:23:53 -08001491
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001492 try {
1493 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001494 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 if (a != null) {
1496 final int N = a.size();
1497 if (!next.finishing && N > 0) {
1498 if (DEBUG_RESULTS) Slog.v(
1499 TAG, "Delivering results to " + next
1500 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001501 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502 }
1503 }
1504
1505 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001506 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 }
1508
1509 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001510 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001512
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001513 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001514 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001515 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001516 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001518
Craig Mautner0eea92c2013-05-16 13:35:39 -07001519 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520
1521 } catch (Exception e) {
1522 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001523 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1524 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001525 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001526 if (lastStack != null) {
1527 lastStack.mResumedActivity = lastResumedActivity;
1528 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 Slog.i(TAG, "Restarting because process died: " + next);
1530 if (!next.hasBeenLaunched) {
1531 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001532 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1533 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001534 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001535 next.appToken, next.packageName, next.theme,
1536 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001537 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1538 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001539 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001540 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001541 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 return true;
1543 }
1544
1545 // From this point on, if something goes wrong there is no way
1546 // to recover the activity.
1547 try {
1548 next.visible = true;
1549 completeResumeLocked(next);
1550 } catch (Exception e) {
1551 // If any exception gets thrown, toss away this
1552 // activity and try the next one.
1553 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001554 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001555 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001556 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001557 return true;
1558 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 next.stopped = false;
1560
1561 } else {
1562 // Whoops, need to restart this activity!
1563 if (!next.hasBeenLaunched) {
1564 next.hasBeenLaunched = true;
1565 } else {
1566 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001567 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001568 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001569 mService.compatibilityInfoForPackageLocked(
1570 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001572 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001573 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 }
1575 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1576 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001577 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 }
1579
Craig Mautnercf910b02013-04-23 11:23:27 -07001580 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 return true;
1582 }
1583
Craig Mautner11bf9a52013-02-19 14:08:51 -08001584
Craig Mautner8849a5e2013-04-02 16:41:03 -07001585 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001586 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001587 TaskRecord rTask = r.task;
1588 final int taskId = rTask.taskId;
1589 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001590 // Last activity in task had been removed or ActivityManagerService is reusing task.
1591 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001592 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001593 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001594 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001595 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001596 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001597 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001598 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 if (!newTask) {
1600 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001601 boolean startIt = true;
1602 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1603 task = mTaskHistory.get(taskNdx);
1604 if (task == r.task) {
1605 // Here it is! Now, if this is not yet visible to the
1606 // user, then just add it without starting; it will
1607 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001608 if (!startIt) {
1609 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1610 + task, new RuntimeException("here").fillInStackTrace());
1611 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001612 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001613 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1614 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001615 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001617 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001619 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 return;
1621 }
1622 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001623 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 startIt = false;
1625 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 }
1627 }
1628
1629 // Place a new activity at top of stack, so it is next to interact
1630 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 // If we are not placing the new activity frontmost, we do not want
1633 // to deliver the onUserLeaving callback to the actual frontmost
1634 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001635 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001636 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001637 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1638 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001639 }
Craig Mautner70a86932013-02-28 22:37:44 -08001640
1641 task = r.task;
1642
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001643 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001644 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001645 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001646 task.addActivityToTop(r);
1647
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001648 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001650 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 // We want to show the starting preview window if we are
1652 // switching to a new task, or the next activity's process is
1653 // not currently running.
1654 boolean showStartingIcon = newTask;
1655 ProcessRecord proc = r.app;
1656 if (proc == null) {
1657 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1658 }
1659 if (proc == null || proc.thread == null) {
1660 showStartingIcon = true;
1661 }
1662 if (DEBUG_TRANSITION) Slog.v(TAG,
1663 "Prepare open transition: starting " + r);
1664 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001665 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001666 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001668 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001669 ? AppTransition.TRANSIT_TASK_OPEN
1670 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 mNoAnimActivities.remove(r);
1672 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001673 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001674 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001675 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001676 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 boolean doShow = true;
1678 if (newTask) {
1679 // Even though this activity is starting fresh, we still need
1680 // to reset it to make sure we apply affinities to move any
1681 // existing activities from other tasks in to it.
1682 // If the caller has requested that the target task be
1683 // reset, then do so.
1684 if ((r.intent.getFlags()
1685 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1686 resetTaskIfNeededLocked(r, r);
1687 doShow = topRunningNonDelayedActivityLocked(null) == r;
1688 }
1689 }
1690 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1691 // Figure out if we are transitioning from another activity that is
1692 // "has the same starting icon" as the next one. This allows the
1693 // window manager to keep the previous window it had previously
1694 // created, if it still had one.
1695 ActivityRecord prev = mResumedActivity;
1696 if (prev != null) {
1697 // We don't want to reuse the previous starting preview if:
1698 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001699 if (prev.task != r.task) {
1700 prev = null;
1701 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001703 else if (prev.nowVisible) {
1704 prev = null;
1705 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001706 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001707 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001708 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001709 mService.compatibilityInfoForPackageLocked(
1710 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001711 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001712 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 }
1714 } else {
1715 // If this is the first activity, don't do any fancy animations,
1716 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001717 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001718 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001719 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001720 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 }
1722 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001723 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 }
1725
1726 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001727 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 }
1729 }
1730
Dianne Hackbornbe707852011-11-11 14:32:10 -08001731 final void validateAppTokensLocked() {
1732 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001733 mValidateAppTokens.ensureCapacity(numActivities());
1734 final int numTasks = mTaskHistory.size();
1735 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1736 TaskRecord task = mTaskHistory.get(taskNdx);
1737 final ArrayList<ActivityRecord> activities = task.mActivities;
1738 if (activities.size() == 0) {
1739 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001740 }
Craig Mautner000f0022013-02-26 15:04:29 -08001741 TaskGroup group = new TaskGroup();
1742 group.taskId = task.taskId;
1743 mValidateAppTokens.add(group);
1744 final int numActivities = activities.size();
1745 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1746 final ActivityRecord r = activities.get(activityNdx);
1747 group.tokens.add(r.appToken);
1748 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001749 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001750 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001751 }
1752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 /**
1754 * Perform a reset of the given task, if needed as part of launching it.
1755 * Returns the new HistoryRecord at the top of the task.
1756 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001757 /**
1758 * Helper method for #resetTaskIfNeededLocked.
1759 * We are inside of the task being reset... we'll either finish this activity, push it out
1760 * for another task, or leave it as-is.
1761 * @param task The task containing the Activity (taskTop) that might be reset.
1762 * @param forceReset
1763 * @return An ActivityOptions that needs to be processed.
1764 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001765 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001766 ActivityOptions topOptions = null;
1767
1768 int replyChainEnd = -1;
1769 boolean canMoveOptions = true;
1770
1771 // We only do this for activities that are not the root of the task (since if we finish
1772 // the root, we may no longer have the task!).
1773 final ArrayList<ActivityRecord> activities = task.mActivities;
1774 final int numActivities = activities.size();
1775 for (int i = numActivities - 1; i > 0; --i ) {
1776 ActivityRecord target = activities.get(i);
1777
1778 final int flags = target.info.flags;
1779 final boolean finishOnTaskLaunch =
1780 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1781 final boolean allowTaskReparenting =
1782 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1783 final boolean clearWhenTaskReset =
1784 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1785
1786 if (!finishOnTaskLaunch
1787 && !clearWhenTaskReset
1788 && target.resultTo != null) {
1789 // If this activity is sending a reply to a previous
1790 // activity, we can't do anything with it now until
1791 // we reach the start of the reply chain.
1792 // XXX note that we are assuming the result is always
1793 // to the previous activity, which is almost always
1794 // the case but we really shouldn't count on.
1795 if (replyChainEnd < 0) {
1796 replyChainEnd = i;
1797 }
1798 } else if (!finishOnTaskLaunch
1799 && !clearWhenTaskReset
1800 && allowTaskReparenting
1801 && target.taskAffinity != null
1802 && !target.taskAffinity.equals(task.affinity)) {
1803 // If this activity has an affinity for another
1804 // task, then we need to move it out of here. We will
1805 // move it as far out of the way as possible, to the
1806 // bottom of the activity stack. This also keeps it
1807 // correctly ordered with any activities we previously
1808 // moved.
1809 TaskRecord bottomTask = mTaskHistory.get(0);
1810 ActivityRecord p = bottomTask.mActivities.get(0);
1811 if (target.taskAffinity != null
1812 && target.taskAffinity.equals(p.task.affinity)) {
1813 // If the activity currently at the bottom has the
1814 // same task affinity as the one we are moving,
1815 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001816 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001817 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1818 + " out to bottom task " + p.task);
1819 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001820 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001821 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001822 target.task.affinityIntent = target.intent;
1823 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1824 + " out to new task " + target.task);
1825 }
1826
1827 final TaskRecord targetTask = target.task;
1828 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001829 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001830
Craig Mautner525f3d92013-05-07 14:01:50 -07001831 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001832 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1833 for (int srcPos = start; srcPos >= i; --srcPos) {
1834 p = activities.get(srcPos);
1835 if (p.finishing) {
1836 continue;
1837 }
1838
Craig Mautner525f3d92013-05-07 14:01:50 -07001839 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001840 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001841 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001842 topOptions = p.takeOptionsLocked();
1843 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001844 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001845 }
1846 }
1847 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1848 + task + " adding to task=" + targetTask,
1849 new RuntimeException("here").fillInStackTrace());
1850 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1851 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001852 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001853 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001854
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001855 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001856 }
1857
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001858 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001859 if (VALIDATE_TOKENS) {
1860 validateAppTokensLocked();
1861 }
1862
1863 replyChainEnd = -1;
1864 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1865 // If the activity should just be removed -- either
1866 // because it asks for it, or the task should be
1867 // cleared -- then finish it and anything that is
1868 // part of its reply chain.
1869 int end;
1870 if (clearWhenTaskReset) {
1871 // In this case, we want to finish this activity
1872 // and everything above it, so be sneaky and pretend
1873 // like these are all in the reply chain.
1874 end = numActivities - 1;
1875 } else if (replyChainEnd < 0) {
1876 end = i;
1877 } else {
1878 end = replyChainEnd;
1879 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001880 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001881 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001882 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001883 if (p.finishing) {
1884 continue;
1885 }
1886 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001887 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001888 topOptions = p.takeOptionsLocked();
1889 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001890 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001891 }
1892 }
Craig Mautner58547802013-03-05 08:23:53 -08001893 if (DEBUG_TASKS) Slog.w(TAG,
1894 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001895 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001896 end--;
1897 srcPos--;
1898 }
1899 }
1900 replyChainEnd = -1;
1901 } else {
1902 // If we were in the middle of a chain, well the
1903 // activity that started it all doesn't want anything
1904 // special, so leave it all as-is.
1905 replyChainEnd = -1;
1906 }
1907 }
1908
1909 return topOptions;
1910 }
1911
1912 /**
1913 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1914 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1915 * @param affinityTask The task we are looking for an affinity to.
1916 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1917 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1918 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1919 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001920 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001921 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001922 int replyChainEnd = -1;
1923 final int taskId = task.taskId;
1924 final String taskAffinity = task.affinity;
1925
1926 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1927 final int numActivities = activities.size();
1928 // Do not operate on the root Activity.
1929 for (int i = numActivities - 1; i > 0; --i) {
1930 ActivityRecord target = activities.get(i);
1931
1932 final int flags = target.info.flags;
1933 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1934 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1935
1936 if (target.resultTo != null) {
1937 // If this activity is sending a reply to a previous
1938 // activity, we can't do anything with it now until
1939 // we reach the start of the reply chain.
1940 // XXX note that we are assuming the result is always
1941 // to the previous activity, which is almost always
1942 // the case but we really shouldn't count on.
1943 if (replyChainEnd < 0) {
1944 replyChainEnd = i;
1945 }
1946 } else if (topTaskIsHigher
1947 && allowTaskReparenting
1948 && taskAffinity != null
1949 && taskAffinity.equals(target.taskAffinity)) {
1950 // This activity has an affinity for our task. Either remove it if we are
1951 // clearing or move it over to our task. Note that
1952 // we currently punt on the case where we are resetting a
1953 // task that is not at the top but who has activities above
1954 // with an affinity to it... this is really not a normal
1955 // case, and we will need to later pull that task to the front
1956 // and usually at that point we will do the reset and pick
1957 // up those remaining activities. (This only happens if
1958 // someone starts an activity in a new task from an activity
1959 // in a task that is not currently on top.)
1960 if (forceReset || finishOnTaskLaunch) {
1961 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1962 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1963 for (int srcPos = start; srcPos >= i; --srcPos) {
1964 final ActivityRecord p = activities.get(srcPos);
1965 if (p.finishing) {
1966 continue;
1967 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001968 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001969 }
1970 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001971 if (taskInsertionPoint < 0) {
1972 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001973
Craig Mautner77878772013-03-04 19:46:24 -08001974 }
Craig Mautner77878772013-03-04 19:46:24 -08001975
1976 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1977 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1978 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1979 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001980 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001981 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001982 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001983
Craig Mautnere3a74d52013-02-22 14:14:58 -08001984 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1985 + " to stack at " + task,
1986 new RuntimeException("here").fillInStackTrace());
1987 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
1988 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001989 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001990 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08001992 if (VALIDATE_TOKENS) {
1993 validateAppTokensLocked();
1994 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08001995
1996 // Now we've moved it in to place... but what if this is
1997 // a singleTop activity and we have put it on top of another
1998 // instance of the same activity? Then we drop the instance
1999 // below so it remains singleTop.
2000 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2001 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002002 int targetNdx = taskActivities.indexOf(target);
2003 if (targetNdx > 0) {
2004 ActivityRecord p = taskActivities.get(targetNdx - 1);
2005 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002006 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2007 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002008 }
2009 }
2010 }
2011 }
2012
2013 replyChainEnd = -1;
2014 }
2015 }
Craig Mautner77878772013-03-04 19:46:24 -08002016 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002017 }
2018
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002020 ActivityRecord newActivity) {
2021 boolean forceReset =
2022 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2023 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2024 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2025 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2026 forceReset = true;
2027 }
2028 }
2029
2030 final TaskRecord task = taskTop.task;
2031
2032 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2033 * for remaining tasks. Used for later tasks to reparent to task. */
2034 boolean taskFound = false;
2035
2036 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2037 ActivityOptions topOptions = null;
2038
Craig Mautner77878772013-03-04 19:46:24 -08002039 // Preserve the location for reparenting in the new task.
2040 int reparentInsertionPoint = -1;
2041
Craig Mautnere3a74d52013-02-22 14:14:58 -08002042 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2043 final TaskRecord targetTask = mTaskHistory.get(i);
2044
2045 if (targetTask == task) {
2046 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2047 taskFound = true;
2048 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002049 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2050 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002051 }
2052 }
2053
Craig Mautner70a86932013-02-28 22:37:44 -08002054 int taskNdx = mTaskHistory.indexOf(task);
2055 do {
2056 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2057 } while (taskTop == null && taskNdx >= 0);
2058
Craig Mautnere3a74d52013-02-22 14:14:58 -08002059 if (topOptions != null) {
2060 // If we got some ActivityOptions from an activity on top that
2061 // was removed from the task, propagate them to the new real top.
2062 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002063 taskTop.updateOptionsLocked(topOptions);
2064 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002065 topOptions.abort();
2066 }
2067 }
2068
2069 return taskTop;
2070 }
2071
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002072 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2073 String resultWho, int requestCode, int resultCode, Intent data) {
2074
2075 if (callingUid > 0) {
2076 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002077 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002078 }
2079
2080 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2081 + " : who=" + resultWho + " req=" + requestCode
2082 + " res=" + resultCode + " data=" + data);
2083 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2084 try {
2085 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2086 list.add(new ResultInfo(resultWho, requestCode,
2087 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002088 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 return;
2090 } catch (Exception e) {
2091 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2092 }
2093 }
2094
2095 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2096 }
2097
Craig Mautnerf3333272013-04-22 10:55:53 -07002098 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2100 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2101 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2102 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002103 if (!mService.mSleeping) {
2104 if (DEBUG_STATES) {
2105 Slog.d(TAG, "no-history finish of " + r);
2106 }
2107 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002108 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002109 } else {
2110 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2111 + " on stop because we're just sleeping");
2112 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002114 }
2115
2116 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002117 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 if (mService.mFocusedActivity == r) {
2119 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2120 }
2121 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002122 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 try {
2124 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002125 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2126 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 r.state = ActivityState.STOPPING;
2128 if (DEBUG_VISBILITY) Slog.v(
2129 TAG, "Stopping visible=" + r.visible + " for " + r);
2130 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002131 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002132 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002133 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002134 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002135 r.setSleeping(true);
2136 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002137 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002138 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 } catch (Exception e) {
2140 // Maybe just ignore exceptions here... if the process
2141 // has crashed, our death notification will clean things
2142 // up.
2143 Slog.w(TAG, "Exception thrown during pause", e);
2144 // Just in case, assume it to be stopped.
2145 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002146 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147 r.state = ActivityState.STOPPED;
2148 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002149 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150 }
2151 }
2152 }
2153 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002154
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 /**
2156 * @return Returns true if the activity is being finished, false if for
2157 * some reason it is being left as-is.
2158 */
2159 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002160 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002161 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002162 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002163 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002164 + ", result=" + resultCode + ", data=" + resultData
2165 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002166 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 return false;
2168 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169
Craig Mautnerd44711d2013-02-23 11:24:36 -08002170 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 return true;
2172 }
2173
Craig Mautnerd2328952013-03-05 12:46:26 -08002174 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002175 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2176 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2177 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2178 ActivityRecord r = activities.get(activityNdx);
2179 if (r.resultTo == self && r.requestCode == requestCode) {
2180 if ((r.resultWho == null && resultWho == null) ||
2181 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2182 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2183 false);
2184 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002185 }
2186 }
2187 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002188 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002189 }
2190
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002191 final void finishTopRunningActivityLocked(ProcessRecord app) {
2192 ActivityRecord r = topRunningActivityLocked(null);
2193 if (r != null && r.app == app) {
2194 // If the top running activity is from this crashing
2195 // process, then terminate it to avoid getting in a loop.
2196 Slog.w(TAG, " Force finishing activity "
2197 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002198 int taskNdx = mTaskHistory.indexOf(r.task);
2199 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002200 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002201 // Also terminate any activities below it that aren't yet
2202 // stopped, to avoid a situation where one will get
2203 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002204 --activityNdx;
2205 if (activityNdx < 0) {
2206 do {
2207 --taskNdx;
2208 if (taskNdx < 0) {
2209 break;
2210 }
2211 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2212 } while (activityNdx < 0);
2213 }
2214 if (activityNdx >= 0) {
2215 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002216 if (r.state == ActivityState.RESUMED
2217 || r.state == ActivityState.PAUSING
2218 || r.state == ActivityState.PAUSED) {
Craig Mautner86d67a42013-05-14 10:34:38 -07002219 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002220 Slog.w(TAG, " Force finishing activity "
2221 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002222 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002223 }
2224 }
2225 }
2226 }
2227 }
2228
Craig Mautnerd2328952013-03-05 12:46:26 -08002229 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002230 ArrayList<ActivityRecord> activities = r.task.mActivities;
2231 for (int index = activities.indexOf(r); index >= 0; --index) {
2232 ActivityRecord cur = activities.get(index);
2233 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002234 break;
2235 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002236 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002237 }
2238 return true;
2239 }
2240
Dianne Hackborn5c607432012-02-28 14:44:19 -08002241 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2242 // send the result
2243 ActivityRecord resultTo = r.resultTo;
2244 if (resultTo != null) {
2245 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2246 + " who=" + r.resultWho + " req=" + r.requestCode
2247 + " res=" + resultCode + " data=" + resultData);
2248 if (r.info.applicationInfo.uid > 0) {
2249 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2250 resultTo.packageName, resultData,
2251 resultTo.getUriPermissionsLocked());
2252 }
2253 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2254 resultData);
2255 r.resultTo = null;
2256 }
2257 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2258
2259 // Make sure this HistoryRecord is not holding on to other resources,
2260 // because clients have remote IPC references to this object so we
2261 // can't assume that will go away and want to avoid circular IPC refs.
2262 r.results = null;
2263 r.pendingResults = null;
2264 r.newIntents = null;
2265 r.icicle = null;
2266 }
2267
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 /**
2269 * @return Returns true if this activity has been removed from the history
2270 * list, or false if it is still in the list and will be removed later.
2271 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002272 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2273 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 if (r.finishing) {
2275 Slog.w(TAG, "Duplicate finish request for " + r);
2276 return false;
2277 }
2278
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002279 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002281 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002283 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2284 final int index = activities.indexOf(r);
2285 if (index < (activities.size() - 1)) {
2286 ActivityRecord next = activities.get(index+1);
2287 if (r.frontOfTask) {
2288 // The next activity is now the front of the task.
2289 next.frontOfTask = true;
2290 }
2291 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2292 // If the caller asked that this activity (and all above it)
2293 // be cleared when the task is reset, don't lose that information,
2294 // but propagate it up to the next activity.
2295 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002296 }
2297 }
2298
2299 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002300 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002302 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 }
2304 }
2305
Dianne Hackborn5c607432012-02-28 14:44:19 -08002306 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002307
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002308 if (mService.mPendingThumbnails.size() > 0) {
2309 // There are clients waiting to receive thumbnails so, in case
2310 // this is an activity that someone is waiting for, add it
2311 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002312 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002313 }
2314
Craig Mautnerde4ef022013-04-07 19:01:33 -07002315 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002316 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 if (DEBUG_TRANSITION) Slog.v(TAG,
2318 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002319 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002320 ? AppTransition.TRANSIT_TASK_CLOSE
2321 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002322
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002324 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002325
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002326 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2328 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2329 startPausingLocked(false, false);
2330 }
2331
2332 } else if (r.state != ActivityState.PAUSING) {
2333 // If the activity is PAUSING, we will complete the finish once
2334 // it is done pausing; else we can just directly finish it here.
2335 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002336 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 } else {
2338 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2339 }
2340
2341 return false;
2342 }
2343
Craig Mautnerf3333272013-04-22 10:55:53 -07002344 static final int FINISH_IMMEDIATELY = 0;
2345 static final int FINISH_AFTER_PAUSE = 1;
2346 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347
Craig Mautnerf3333272013-04-22 10:55:53 -07002348 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002349 // First things first: if this activity is currently visible,
2350 // and the resumed activity is not yet visible, then hold off on
2351 // finishing until the resumed one becomes visible.
2352 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002353 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2354 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002355 if (mStackSupervisor.mStoppingActivities.size() > 3
2356 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002357 // If we already have a few activities waiting to stop,
2358 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002359 // them out. Or if r is the last of activity of the last task the stack
2360 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002361 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002362 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002363 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 }
2365 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002366 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2367 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002369 if (oomAdj) {
2370 mService.updateOomAdjLocked();
2371 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 return r;
2373 }
2374
2375 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002376 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002377 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002378 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 if (mResumedActivity == r) {
2380 mResumedActivity = null;
2381 }
2382 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002383 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 r.state = ActivityState.FINISHING;
2385
2386 if (mode == FINISH_IMMEDIATELY
2387 || prevState == ActivityState.STOPPED
2388 || prevState == ActivityState.INITIALIZING) {
2389 // If this activity is already stopped, we can just finish
2390 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002391 boolean activityRemoved = destroyActivityLocked(r, true,
2392 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002393 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002394 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002395 }
2396 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002398
2399 // Need to go through the full pause cycle to get this
2400 // activity into the stopped state and then finish it.
2401 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002402 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002403 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 return r;
2405 }
2406
Craig Mautnerd2328952013-03-05 12:46:26 -08002407 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002408 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002409 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002410 final TaskRecord task = srec.task;
2411 final ArrayList<ActivityRecord> activities = task.mActivities;
2412 final int start = activities.indexOf(srec);
2413 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002414 return false;
2415 }
2416 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002417 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002418 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002419 final ComponentName dest = destIntent.getComponent();
2420 if (start > 0 && dest != null) {
2421 for (int i = finishTo; i >= 0; i--) {
2422 ActivityRecord r = activities.get(i);
2423 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002424 r.info.name.equals(dest.getClassName())) {
2425 finishTo = i;
2426 parent = r;
2427 foundParentInTask = true;
2428 break;
2429 }
2430 }
2431 }
2432
2433 IActivityController controller = mService.mController;
2434 if (controller != null) {
2435 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2436 if (next != null) {
2437 // ask watcher if this is allowed
2438 boolean resumeOK = true;
2439 try {
2440 resumeOK = controller.activityResuming(next.packageName);
2441 } catch (RemoteException e) {
2442 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002443 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002444 }
2445
2446 if (!resumeOK) {
2447 return false;
2448 }
2449 }
2450 }
2451 final long origId = Binder.clearCallingIdentity();
2452 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002453 ActivityRecord r = activities.get(i);
2454 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002455 // Only return the supplied result for the first activity finished
2456 resultCode = Activity.RESULT_CANCELED;
2457 resultData = null;
2458 }
2459
2460 if (parent != null && foundParentInTask) {
2461 final int parentLaunchMode = parent.info.launchMode;
2462 final int destIntentFlags = destIntent.getFlags();
2463 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2464 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2465 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2466 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2467 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2468 } else {
2469 try {
2470 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2471 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002472 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002473 null, aInfo, parent.appToken, null,
2474 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2475 0, null, true, null);
2476 foundParentInTask = res == ActivityManager.START_SUCCESS;
2477 } catch (RemoteException e) {
2478 foundParentInTask = false;
2479 }
2480 requestFinishActivityLocked(parent.appToken, resultCode,
2481 resultData, "navigate-up", true);
2482 }
2483 }
2484 Binder.restoreCallingIdentity(origId);
2485 return foundParentInTask;
2486 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 /**
2488 * Perform the common clean-up of an activity record. This is called both
2489 * as part of destroyActivityLocked() (when destroying the client-side
2490 * representation) and cleaning things up as a result of its hosting
2491 * processing going away, in which case there is no remaining client-side
2492 * state to destroy so only the cleanup here is needed.
2493 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002494 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2495 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 if (mResumedActivity == r) {
2497 mResumedActivity = null;
2498 }
2499 if (mService.mFocusedActivity == r) {
2500 mService.mFocusedActivity = null;
2501 }
2502
2503 r.configDestroy = false;
2504 r.frozenBeforeDestroy = false;
2505
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002506 if (setState) {
2507 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2508 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002509 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002510 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002511 }
2512
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002513 // Make sure this record is no longer in the pending finishes list.
2514 // This could happen, for example, if we are trimming activities
2515 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002516 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002517 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002518
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002519 // Remove any pending results.
2520 if (r.finishing && r.pendingResults != null) {
2521 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2522 PendingIntentRecord rec = apr.get();
2523 if (rec != null) {
2524 mService.cancelIntentSenderLocked(rec, false);
2525 }
2526 }
2527 r.pendingResults = null;
2528 }
2529
2530 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002531 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002532 }
2533
2534 if (mService.mPendingThumbnails.size() > 0) {
2535 // There are clients waiting to receive thumbnails so, in case
2536 // this is an activity that someone is waiting for, add it
2537 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002538 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002539 }
2540
2541 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002542 removeTimeoutsForActivityLocked(r);
2543 }
2544
2545 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002546 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002548 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002549 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002550 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 }
2552
Dianne Hackborn5c607432012-02-28 14:44:19 -08002553 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002554 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2555 r.makeFinishing();
2556 if (DEBUG_ADD_REMOVE) {
2557 RuntimeException here = new RuntimeException("here");
2558 here.fillInStackTrace();
2559 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002561 final TaskRecord task = r.task;
2562 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002563 if (DEBUG_STACK) Slog.i(TAG,
2564 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002565 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002566 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002567 r.takeFromHistory();
2568 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002569 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002570 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002571 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002572 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002573 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002574 if (VALIDATE_TOKENS) {
2575 validateAppTokensLocked();
2576 }
2577 cleanUpActivityServicesLocked(r);
2578 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002580
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002581 /**
2582 * Perform clean-up of service connections in an activity record.
2583 */
2584 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2585 // Throw away any services that have been bound by this activity.
2586 if (r.connections != null) {
2587 Iterator<ConnectionRecord> it = r.connections.iterator();
2588 while (it.hasNext()) {
2589 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002590 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002591 }
2592 r.connections = null;
2593 }
2594 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002595
2596 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2597 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2598 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2599 mHandler.sendMessage(msg);
2600 }
2601
Dianne Hackborn28695e02011-11-02 21:59:51 -07002602 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002603 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002604 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002605 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2606 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2607 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2608 final ActivityRecord r = activities.get(activityNdx);
2609 if (r.finishing) {
2610 continue;
2611 }
2612 if (r.fullscreen) {
2613 lastIsOpaque = true;
2614 }
2615 if (owner != null && r.app != owner) {
2616 continue;
2617 }
2618 if (!lastIsOpaque) {
2619 continue;
2620 }
2621 // We can destroy this one if we have its icicle saved and
2622 // it is not in the process of pausing/stopping/finishing.
2623 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2624 && r.haveState && !r.visible && r.stopped
2625 && r.state != ActivityState.DESTROYING
2626 && r.state != ActivityState.DESTROYED) {
2627 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2628 + " resumed=" + mResumedActivity
2629 + " pausing=" + mPausingActivity);
2630 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2631 activityRemoved = true;
2632 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002633 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002634 }
2635 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002636 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002637 mStackSupervisor.resumeTopActivitiesLocked();
2638
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002639 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002640 }
2641
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002642 /**
2643 * Destroy the current CLIENT SIDE instance of an activity. This may be
2644 * called both when actually finishing an activity, or when performing
2645 * a configuration switch where we destroy the current client-side object
2646 * but then create a new client-side object for this same HistoryRecord.
2647 */
2648 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002649 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002650 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002651 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2653 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002654 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002655 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656
2657 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002658
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002659 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002660
2661 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002662
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 if (hadApp) {
2664 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002665 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2667 mService.mHeavyWeightProcess = null;
2668 mService.mHandler.sendEmptyMessage(
2669 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2670 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002672 // No longer have activities, so update oom adj.
2673 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 }
2675 }
2676
2677 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002678
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002679 try {
2680 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002681 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 r.configChangeFlags);
2683 } catch (Exception e) {
2684 // We can just ignore exceptions here... if the process
2685 // has crashed, our death notification will clean things
2686 // up.
2687 //Slog.w(TAG, "Exception thrown during finish", e);
2688 if (r.finishing) {
2689 removeActivityFromHistoryLocked(r);
2690 removedFromHistory = true;
2691 skipDestroy = true;
2692 }
2693 }
2694
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002696
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002697 // If the activity is finishing, we need to wait on removing it
2698 // from the list to give it a chance to do its cleanup. During
2699 // that time it may make calls back with its token so we need to
2700 // be able to find it on the list and so we don't want to remove
2701 // it from the list yet. Otherwise, we can just immediately put
2702 // it in the destroyed state since we are not removing it from the
2703 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002704 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002705 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2706 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002708 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2710 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002711 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002713 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002714 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002715 }
2716 } else {
2717 // remove this record from the history.
2718 if (r.finishing) {
2719 removeActivityFromHistoryLocked(r);
2720 removedFromHistory = true;
2721 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002722 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002724 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002725 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002726 }
2727 }
2728
2729 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002730
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 if (!mLRUActivities.remove(r) && hadApp) {
2732 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2733 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735 return removedFromHistory;
2736 }
2737
Craig Mautnerd2328952013-03-05 12:46:26 -08002738 final void activityDestroyedLocked(IBinder token) {
2739 final long origId = Binder.clearCallingIdentity();
2740 try {
2741 ActivityRecord r = ActivityRecord.forToken(token);
2742 if (r != null) {
2743 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002744 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002745
2746 if (isInStackLocked(token) != null) {
2747 if (r.state == ActivityState.DESTROYING) {
2748 cleanUpActivityLocked(r, true, false);
2749 removeActivityFromHistoryLocked(r);
2750 }
2751 }
Craig Mautner05d29032013-05-03 13:40:13 -07002752 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002753 } finally {
2754 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 }
2756 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002757
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002758 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2759 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002761 if (DEBUG_CLEANUP) Slog.v(
2762 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 + " with " + i + " entries");
2764 while (i > 0) {
2765 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002766 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002767 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002768 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002769 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002771 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 }
2773 }
2774 }
2775
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002776 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2777 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002778 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2779 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002780 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2781 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002782 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002783 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002784 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2785 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002786
2787 boolean hasVisibleActivities = false;
2788
2789 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002790 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002791 if (DEBUG_CLEANUP) Slog.v(
2792 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002793 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2794 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2795 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2796 final ActivityRecord r = activities.get(activityNdx);
2797 --i;
2798 if (DEBUG_CLEANUP) Slog.v(
2799 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2800 if (r.app == app) {
2801 boolean remove;
2802 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2803 // Don't currently have state for the activity, or
2804 // it is finishing -- always remove it.
2805 remove = true;
2806 } else if (r.launchCount > 2 &&
2807 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2808 // We have launched this activity too many times since it was
2809 // able to run, so give up and remove it.
2810 remove = true;
2811 } else {
2812 // The process may be gone, but the activity lives on!
2813 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002814 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002815 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002816 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002817 RuntimeException here = new RuntimeException("here");
2818 here.fillInStackTrace();
2819 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2820 + ": haveState=" + r.haveState
2821 + " stateNotNeeded=" + r.stateNotNeeded
2822 + " finishing=" + r.finishing
2823 + " state=" + r.state, here);
2824 }
2825 if (!r.finishing) {
2826 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2827 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2828 r.userId, System.identityHashCode(r),
2829 r.task.taskId, r.shortComponentName,
2830 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002831 if (r.state == ActivityState.RESUMED) {
2832 mService.updateUsageStats(r, false);
2833 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002834 }
2835 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002836
Craig Mautner0247fc82013-02-28 14:32:06 -08002837 } else {
2838 // We have the current state for this activity, so
2839 // it can be restarted later when needed.
2840 if (localLOGV) Slog.v(
2841 TAG, "Keeping entry, setting app to null");
2842 if (r.visible) {
2843 hasVisibleActivities = true;
2844 }
2845 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2846 + r);
2847 r.app = null;
2848 r.nowVisible = false;
2849 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002850 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002851 "App died, clearing saved state of " + r);
2852 r.icicle = null;
2853 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002854 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002855
Craig Mautnerd2328952013-03-05 12:46:26 -08002856 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002857 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002858 }
2859 }
2860
2861 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002862 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002863
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002864 final void updateTransitLocked(int transit, Bundle options) {
2865 if (options != null) {
2866 ActivityRecord r = topRunningActivityLocked(null);
2867 if (r != null && r.state != ActivityState.RESUMED) {
2868 r.updateOptionsLocked(options);
2869 } else {
2870 ActivityOptions.abort(options);
2871 }
2872 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002873 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002874 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002875
Craig Mautnercae015f2013-02-08 14:31:27 -08002876 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002877 final TaskRecord task = taskForIdLocked(taskId);
2878 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002879 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002880 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002881 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002882 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2883 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002884 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002885 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002886 }
2887 moveTaskToFrontLocked(task, null, options);
2888 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002889 }
2890 return false;
2891 }
2892
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002893 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002894 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895
Craig Mautner11bf9a52013-02-19 14:08:51 -08002896 final int numTasks = mTaskHistory.size();
2897 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002898 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002899 // nothing to do!
2900 if (reason != null &&
2901 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2902 ActivityOptions.abort(options);
2903 } else {
2904 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2905 }
2906 return;
2907 }
2908
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002909 mStackSupervisor.moveHomeStack(isHomeStack());
2910
Craig Mautner11bf9a52013-02-19 14:08:51 -08002911 // Shift all activities with this task up to the top
2912 // of the stack, keeping them in the same internal order.
2913 mTaskHistory.remove(tr);
2914 mTaskHistory.add(tr);
2915
2916 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 if (reason != null &&
2918 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002919 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002920 ActivityRecord r = topRunningActivityLocked(null);
2921 if (r != null) {
2922 mNoAnimActivities.add(r);
2923 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002924 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002926 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002928
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002929 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002930
Craig Mautner05d29032013-05-03 13:40:13 -07002931 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002932 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002933
2934 if (VALIDATE_TOKENS) {
2935 validateAppTokensLocked();
2936 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 }
2938
2939 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002940 * Worker method for rearranging history stack. Implements the function of moving all
2941 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002942 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002943 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002944 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2945 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002946 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 * @param task The taskId to collect and move to the bottom.
2948 * @return Returns true if the move completed, false if not.
2949 */
2950 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2951 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002952
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002953 // If we have a watcher, preflight the move before committing to it. First check
2954 // for *other* available tasks, but if none are available, then try again allowing the
2955 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002956 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 ActivityRecord next = topRunningActivityLocked(null, task);
2958 if (next == null) {
2959 next = topRunningActivityLocked(null, 0);
2960 }
2961 if (next != null) {
2962 // ask watcher if this is allowed
2963 boolean moveOK = true;
2964 try {
2965 moveOK = mService.mController.activityResuming(next.packageName);
2966 } catch (RemoteException e) {
2967 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002968 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 }
2970 if (!moveOK) {
2971 return false;
2972 }
2973 }
2974 }
2975
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 if (DEBUG_TRANSITION) Slog.v(TAG,
2977 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002978
Craig Mautnerd2328952013-03-05 12:46:26 -08002979 final TaskRecord tr = taskForIdLocked(task);
2980 if (tr == null) {
2981 return false;
2982 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002983 mTaskHistory.remove(tr);
2984 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985
2986 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002987 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2988 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 ActivityRecord r = topRunningActivityLocked(null);
2990 if (r != null) {
2991 mNoAnimActivities.add(r);
2992 }
2993 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002994 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08002995 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002997 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002998
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003000 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003001 }
3002
Craig Mautnerde4ef022013-04-07 19:01:33 -07003003 if (mResumedActivity != null && mResumedActivity.task == tr &&
3004 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07003005 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07003006 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003007 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003008 }
3009
Craig Mautner05d29032013-05-03 13:40:13 -07003010 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 return true;
3012 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003013
Craig Mautner8849a5e2013-04-02 16:41:03 -07003014 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003016 final Uri data = r.intent.getData();
3017 final String strData = data != null ? data.toSafeString() : null;
3018
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003019 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003020 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003022 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 }
3024
3025 /**
3026 * Make sure the given activity matches the current configuration. Returns
3027 * false if the activity had to be destroyed. Returns true if the
3028 * configuration is the same, or the activity will remain running as-is
3029 * for whatever reason. Ensures the HistoryRecord is updated with the
3030 * correct configuration and all other bookkeeping is handled.
3031 */
3032 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3033 int globalChanges) {
3034 if (mConfigWillChange) {
3035 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3036 "Skipping config check (will change): " + r);
3037 return true;
3038 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003039
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3041 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003042
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003043 // Short circuit: if the two configurations are the exact same
3044 // object (the common case), then there is nothing to do.
3045 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003046 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3048 "Configuration unchanged in " + r);
3049 return true;
3050 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003051
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 // We don't worry about activities that are finishing.
3053 if (r.finishing) {
3054 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3055 "Configuration doesn't matter in finishing " + r);
3056 r.stopFreezingScreenLocked(false);
3057 return true;
3058 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003059
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 // Okay we now are going to make this activity have the new config.
3061 // But then we need to figure out how it needs to deal with that.
3062 Configuration oldConfig = r.configuration;
3063 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003064
3065 // Determine what has changed. May be nothing, if this is a config
3066 // that has come back from the app after going idle. In that case
3067 // we just want to leave the official config object now in the
3068 // activity and do nothing else.
3069 final int changes = oldConfig.diff(newConfig);
3070 if (changes == 0 && !r.forceNewConfig) {
3071 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3072 "Configuration no differences in " + r);
3073 return true;
3074 }
3075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 // If the activity isn't currently running, just leave the new
3077 // configuration and it will pick that up next time it starts.
3078 if (r.app == null || r.app.thread == null) {
3079 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3080 "Configuration doesn't matter not running " + r);
3081 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003082 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 return true;
3084 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003085
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003086 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003087 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3088 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3089 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003090 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003091 + ", newConfig=" + newConfig);
3092 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003093 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003094 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3095 r.configChangeFlags |= changes;
3096 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003097 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003098 if (r.app == null || r.app.thread == null) {
3099 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003100 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003101 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003102 } else if (r.state == ActivityState.PAUSING) {
3103 // A little annoying: we are waiting for this activity to
3104 // finish pausing. Let's not do anything now, but just
3105 // flag that it needs to be restarted when done pausing.
3106 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003107 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003108 r.configDestroy = true;
3109 return true;
3110 } else if (r.state == ActivityState.RESUMED) {
3111 // Try to optimize this case: the configuration is changing
3112 // and we need to restart the top, resumed activity.
3113 // Instead of doing the normal handshaking, just say
3114 // "restart!".
3115 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003116 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003117 relaunchActivityLocked(r, r.configChangeFlags, true);
3118 r.configChangeFlags = 0;
3119 } else {
3120 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003121 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003122 relaunchActivityLocked(r, r.configChangeFlags, false);
3123 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003125
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003126 // All done... tell the caller we weren't able to keep this
3127 // activity around.
3128 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003130
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003131 // Default case: the activity can handle this new configuration, so
3132 // hand it over. Note that we don't need to give it the new
3133 // configuration, since we always send configuration changes to all
3134 // process when they happen so it can just use whatever configuration
3135 // it last got.
3136 if (r.app != null && r.app.thread != null) {
3137 try {
3138 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003139 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 } catch (RemoteException e) {
3141 // If process died, whatever.
3142 }
3143 }
3144 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 return true;
3147 }
3148
3149 private final boolean relaunchActivityLocked(ActivityRecord r,
3150 int changes, boolean andResume) {
3151 List<ResultInfo> results = null;
3152 List<Intent> newIntents = null;
3153 if (andResume) {
3154 results = r.results;
3155 newIntents = r.newIntents;
3156 }
3157 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3158 + " with results=" + results + " newIntents=" + newIntents
3159 + " andResume=" + andResume);
3160 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003161 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003164 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003165
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003167 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3168 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3169 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003170 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003171 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003172 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173 // Note: don't need to call pauseIfSleepingLocked() here, because
3174 // the caller will only pass in 'andResume' if this activity is
3175 // currently resumed, which implies we aren't sleeping.
3176 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003177 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003178 }
3179
3180 if (andResume) {
3181 r.results = null;
3182 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003183 r.state = ActivityState.RESUMED;
3184 } else {
3185 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3186 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003187 }
3188
3189 return true;
3190 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003191
3192 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003193 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3194 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3195 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3196 final ActivityRecord r = activities.get(activityNdx);
3197 if (r.appToken == token) {
3198 return true;
3199 }
3200 if (r.fullscreen && !r.finishing) {
3201 return false;
3202 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003203 }
3204 }
3205 return true;
3206 }
3207
3208 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003209 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3210 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3211 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3212 final ActivityRecord r = activities.get(activityNdx);
3213 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003214 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003215 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003216 }
3217 }
3218 }
3219
3220 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3221 boolean didSomething = false;
3222 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003223 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3224 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3225 int numActivities = activities.size();
3226 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3227 ActivityRecord r = activities.get(activityNdx);
3228 final boolean samePackage = r.packageName.equals(name)
3229 || (name == null && r.userId == userId);
3230 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3231 && (samePackage || r.task == lastTask)
3232 && (r.app == null || evenPersistent || !r.app.persistent)) {
3233 if (!doit) {
3234 if (r.finishing) {
3235 // If this activity is just finishing, then it is not
3236 // interesting as far as something to stop.
3237 continue;
3238 }
3239 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003240 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003241 didSomething = true;
3242 Slog.i(TAG, " Force finishing activity " + r);
3243 if (samePackage) {
3244 if (r.app != null) {
3245 r.app.removed = true;
3246 }
3247 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003248 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003249 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003250 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3251 true)) {
3252 // r has been deleted from mActivities, accommodate.
3253 --numActivities;
3254 --activityNdx;
3255 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003256 }
3257 }
3258 }
3259 return didSomething;
3260 }
3261
3262 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003263 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003264 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003265 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3266 --maxNum, --taskNdx) {
3267 final TaskRecord task = mTaskHistory.get(taskNdx);
3268 ActivityRecord r = null;
3269 ActivityRecord top = null;
3270 int numActivities = 0;
3271 int numRunning = 0;
3272 final ArrayList<ActivityRecord> activities = task.mActivities;
3273 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3274 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003275
Craig Mautneraab647e2013-02-28 16:31:36 -08003276 // Initialize state for next task if needed.
3277 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3278 top = r;
3279 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003280 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003281
3282 // Add 'r' into the current task.
3283 numActivities++;
3284 if (r.app != null && r.app.thread != null) {
3285 numRunning++;
3286 }
3287
3288 if (localLOGV) Slog.v(
3289 TAG, r.intent.getComponent().flattenToShortString()
3290 + ": task=" + r.task);
3291 }
3292
3293 RunningTaskInfo ci = new RunningTaskInfo();
3294 ci.id = task.taskId;
3295 ci.baseActivity = r.intent.getComponent();
3296 ci.topActivity = top.intent.getComponent();
3297 if (top.thumbHolder != null) {
3298 ci.description = top.thumbHolder.lastDescription;
3299 }
3300 ci.numActivities = numActivities;
3301 ci.numRunning = numRunning;
3302 //System.out.println(
3303 // "#" + maxNum + ": " + " descr=" + ci.description);
3304 if (receiver != null) {
3305 if (localLOGV) Slog.v(
3306 TAG, "State=" + top.state + "Idle=" + top.idle
3307 + " app=" + top.app
3308 + " thr=" + (top.app != null ? top.app.thread : null));
3309 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3310 if (top.idle && top.app != null && top.app.thread != null) {
3311 topRecord = top;
3312 } else {
3313 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003314 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003315 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003316 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003317 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003318 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003319 }
3320 return topRecord;
3321 }
3322
3323 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003324 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003325 if (DEBUG_SWITCH) Slog.d(
3326 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003327 if (top >= 0) {
3328 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3329 int activityTop = activities.size() - 1;
3330 if (activityTop > 0) {
3331 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3332 "unhandled-back", true);
3333 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003334 }
3335 }
3336
Craig Mautnere79d42682013-04-01 19:01:53 -07003337 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003338 if (!containsApp(app)) {
3339 return;
3340 }
3341 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003342 if (mPausingActivity != null && mPausingActivity.app == app) {
3343 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3344 "App died while pausing: " + mPausingActivity);
3345 mPausingActivity = null;
3346 }
3347 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3348 mLastPausedActivity = null;
3349 }
Craig Mautner37f748d2013-06-24 16:19:55 -07003350 final ActivityRecord top = topRunningActivityLocked(null);
3351 final boolean launchHomeTaskNext =
3352 top != null && top.app == app && top.mLaunchHomeTaskNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003353
3354 // Remove this application's activities from active lists.
3355 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3356
3357 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003358 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautner37f748d2013-06-24 16:19:55 -07003359 if (stack == null || launchHomeTaskNext) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003360 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003361 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003362 // If there was nothing to resume, and we are not already
3363 // restarting this process, but there is a visible activity that
3364 // is hosted by the process... then make sure all visible
3365 // activities are running, taking care of restarting this
3366 // process.
3367 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003368 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003369 }
3370 }
3371 }
3372 }
3373
Craig Mautnercae015f2013-02-08 14:31:27 -08003374 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003375 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3376 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3377 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3378 final ActivityRecord r = activities.get(activityNdx);
3379 if (r.app == app) {
3380 Slog.w(TAG, " Force finishing activity "
3381 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003382 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003383 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003384 }
3385 }
3386 }
3387
Dianne Hackborn390517b2013-05-30 15:03:32 -07003388 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003389 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003390 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003391 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3392 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003393 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3394 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003395 dumpClient, dumpPackage, needSep, header,
3396 " Task " + taskNdx + ": id #" + task.taskId);
3397 if (printed) {
3398 header = null;
3399 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003400 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003401 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003402 }
3403
3404 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3405 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3406
3407 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3409 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003410 }
3411 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003412 final int top = mTaskHistory.size() - 1;
3413 if (top >= 0) {
3414 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3415 int listTop = list.size() - 1;
3416 if (listTop >= 0) {
3417 activities.add(list.get(listTop));
3418 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003419 }
3420 } else {
3421 ItemMatcher matcher = new ItemMatcher();
3422 matcher.build(name);
3423
Craig Mautneraab647e2013-02-28 16:31:36 -08003424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3425 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3426 if (matcher.match(r1, r1.intent.getComponent())) {
3427 activities.add(r1);
3428 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003429 }
3430 }
3431 }
3432
3433 return activities;
3434 }
3435
3436 ActivityRecord restartPackage(String packageName) {
3437 ActivityRecord starting = topRunningActivityLocked(null);
3438
3439 // All activities that came from the package must be
3440 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3442 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3443 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3444 final ActivityRecord a = activities.get(activityNdx);
3445 if (a.info.packageName.equals(packageName)) {
3446 a.forceNewConfig = true;
3447 if (starting != null && a == starting && a.visible) {
3448 a.startFreezingScreenLocked(starting.app,
3449 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3450 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003451 }
3452 }
3453 }
3454
3455 return starting;
3456 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003457
Craig Mautnerde4ef022013-04-07 19:01:33 -07003458 boolean removeTask(TaskRecord task) {
3459 mTaskHistory.remove(task);
3460 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003461 }
3462
Craig Mautnerde4ef022013-04-07 19:01:33 -07003463 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003464 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003465 if (toTop) {
3466 mTaskHistory.add(task);
3467 } else {
3468 mTaskHistory.add(0, task);
3469 }
3470 return task;
3471 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003472
3473 ArrayList<TaskRecord> getAllTasks() {
3474 return new ArrayList<TaskRecord>(mTaskHistory);
3475 }
3476
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003477 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003478 task.stack = this;
3479 if (toTop) {
3480 mTaskHistory.add(task);
3481 } else {
3482 mTaskHistory.add(0, task);
3483 }
3484 }
3485
3486 public int getStackId() {
3487 return mStackId;
3488 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003489
3490 @Override
3491 public String toString() {
3492 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3493 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003494}