blob: 10e13259beb5dc8aa61f586e0f21eec02ed6f363 [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;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.RemoteException;
73import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070077import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import java.lang.ref.WeakReference;
82import java.util.ArrayList;
83import java.util.Iterator;
84import java.util.List;
85
86/**
87 * State and management of a single stack of activities.
88 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070089final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080090
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070091 // Ticks during which we check progress while waiting for an app to launch.
92 static final int LAUNCH_TICK = 500;
93
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094 // How long we wait until giving up on the last activity to pause. This
95 // is short because it directly impacts the responsiveness of starting the
96 // next activity.
97 static final int PAUSE_TIMEOUT = 500;
98
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070099 // How long we wait for the activity to tell us it has stopped before
100 // giving up. This is a good amount of time because we really need this
101 // from the application in order to get its saved state.
102 static final int STOP_TIMEOUT = 10*1000;
103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104 // How long we wait until giving up on an activity telling us it has
105 // finished destroying itself.
106 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800109 // disabled.
110 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800111
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700112 // How long between activity launches that we consider safe to not warn
113 // the user about an unexpected activity being launched on top.
114 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // Set to false to disable the preview that is shown while a new activity
117 // is being started.
118 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800119
Craig Mautner5eda9b32013-07-02 11:58:16 -0700120 // How long to wait for all background Activities to redraw following a call to
121 // convertToTranslucent().
122 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
123
John Reck172e87c2013-10-02 16:55:16 -0700124 static final boolean SCREENSHOT_FORCE_565 = ActivityManager
125 .isLowRamDeviceStatic() ? true : false;
126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 enum ActivityState {
128 INITIALIZING,
129 RESUMED,
130 PAUSING,
131 PAUSED,
132 STOPPING,
133 STOPPED,
134 FINISHING,
135 DESTROYING,
136 DESTROYED
137 }
138
139 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700144 /**
145 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800146 * running) activities. It contains #TaskRecord objects.
147 */
148 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
149
150 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800151 * Used for validating app tokens with window manager.
152 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800153 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800154
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700155 /**
156 * List of running activities, sorted by recent usage.
157 * The first entry in the list is the least recently used.
158 * It contains HistoryRecord objects.
159 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700160 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161
162 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700163 * Animations that for the current transition have requested not to
164 * be considered for the transition animation.
165 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700166 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * When we are in the process of pausing an activity, before starting the
170 * next one, this variable holds the activity that is currently being paused.
171 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800172 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
175 * This is the last activity that we put into the paused state. This is
176 * used to determine if we need to do an activity transition while sleeping,
177 * when we normally hold the top activity paused.
178 */
179 ActivityRecord mLastPausedActivity = null;
180
181 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700182 * Activities that specify No History must be removed once the user navigates away from them.
183 * If the device goes to sleep with such an activity in the paused state then we save it here
184 * and finish it later if another activity replaces it on wakeup.
185 */
186 ActivityRecord mLastNoHistoryActivity = null;
187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * Current activity that is resumed, or null if there is none.
190 */
191 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700194 * This is the last activity that has been started. It is only used to
195 * identify when multiple activities are started at once so that the user
196 * can be warned they may not be in the activity they think they are.
197 */
198 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800199
Craig Mautner5eda9b32013-07-02 11:58:16 -0700200 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
201 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
202 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
203 // Activity in mTranslucentActivityWaiting is notified via
204 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
205 // background activity being drawn then the same call will be made with a true value.
206 ActivityRecord mTranslucentActivityWaiting = null;
207 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
208 new ArrayList<ActivityRecord>();
209
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700210 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700211 * Set when we know we are going to be calling updateConfiguration()
212 * soon, so want to skip intermediate config checks.
213 */
214 boolean mConfigWillChange;
215
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700216 long mLaunchStartTime = 0;
217 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800218
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800219 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800220 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
221 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
222 */
223 private ActivityRecord mLastScreenshotActivity = null;
224 private Bitmap mLastScreenshotBitmap = null;
225
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800226 int mThumbnailWidth = -1;
227 int mThumbnailHeight = -1;
228
Craig Mautner858d8a62013-04-23 17:08:34 -0700229 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700230
Craig Mautnerc00204b2013-03-05 15:02:14 -0800231 final int mStackId;
232
Craig Mautner27084302013-03-25 08:05:25 -0700233 /** Run all ActivityStacks through this */
234 final ActivityStackSupervisor mStackSupervisor;
235
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700236 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
238 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
239 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
240 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700241 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700242
243 static class ScheduleDestroyArgs {
244 final ProcessRecord mOwner;
245 final boolean mOomAdj;
246 final String mReason;
247 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
248 mOwner = owner;
249 mOomAdj = oomAdj;
250 mReason = reason;
251 }
252 }
253
Zoran Marcetaf958b322012-08-09 20:27:12 +0900254 final Handler mHandler;
255
256 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700257 //public Handler() {
258 // if (localLOGV) Slog.v(TAG, "Handler started!");
259 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700260 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900261 super(looper);
262 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700265 public void handleMessage(Message msg) {
266 switch (msg.what) {
267 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800268 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 // We don't at this point know if the activity is fullscreen,
270 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800271 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700272 synchronized (mService) {
273 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700274 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700275 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700276 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800277 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700279 case LAUNCH_TICK_MSG: {
280 ActivityRecord r = (ActivityRecord)msg.obj;
281 synchronized (mService) {
282 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700283 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 }
285 }
286 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800288 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 // We don't at this point know if the activity is fullscreen,
290 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800291 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800292 synchronized (mService) {
293 activityDestroyedLocked(r != null ? r.appToken : null);
294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700296 case STOP_TIMEOUT_MSG: {
297 ActivityRecord r = (ActivityRecord)msg.obj;
298 // We don't at this point know if the activity is fullscreen,
299 // so we need to be conservative and assume it isn't.
300 Slog.w(TAG, "Activity stop timeout for " + r);
301 synchronized (mService) {
302 if (r.isInHistory()) {
303 activityStoppedLocked(r, null, null, null);
304 }
305 }
306 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700307 case DESTROY_ACTIVITIES_MSG: {
308 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
309 synchronized (mService) {
310 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
311 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700312 } break;
313 case TRANSLUCENT_TIMEOUT_MSG: {
314 synchronized (mService) {
315 notifyActivityDrawnLocked(null);
316 }
317 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 }
319 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800320 }
321
Craig Mautner000f0022013-02-26 15:04:29 -0800322 private int numActivities() {
323 int count = 0;
324 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
325 count += mTaskHistory.get(taskNdx).mActivities.size();
326 }
327 return count;
328 }
329
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900331 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700332 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333 mWindowManager = service.mWindowManager;
334 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 mContext = context;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800336 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700337 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700338 }
Craig Mautner5962b122012-10-05 14:45:52 -0700339
Craig Mautner6b74cb52013-09-27 17:02:21 -0700340 boolean okToShow(ActivityRecord r) {
Craig Mautner5962b122012-10-05 14:45:52 -0700341 return r.userId == mCurrentUser
342 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
343 }
344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700345 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800346 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700347 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
348 if (r != null) {
349 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800350 }
351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700352 return null;
353 }
354
355 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800356 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
357 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800358 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800359 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
360 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800361 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
362 return r;
363 }
364 }
365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 return null;
367 }
368
369 /**
370 * This is a simplified version of topRunningActivityLocked that provides a number of
371 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800372 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 * @param token If non-null, any history records matching this token will be skipped.
374 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800375 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 * @return Returns the HistoryRecord of the next activity on the stack.
377 */
378 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800379 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
380 TaskRecord task = mTaskHistory.get(taskNdx);
381 if (task.taskId == taskId) {
382 continue;
383 }
384 ArrayList<ActivityRecord> activities = task.mActivities;
385 for (int i = activities.size() - 1; i >= 0; --i) {
386 final ActivityRecord r = activities.get(i);
387 // Note: the taskId check depends on real taskId fields being non-zero
388 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
389 return r;
390 }
391 }
392 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 return null;
394 }
395
Craig Mautner8849a5e2013-04-02 16:41:03 -0700396 final ActivityRecord topActivity() {
397 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700398 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700399 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
400 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
401 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700402 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700403 }
404 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700405 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700406 }
407
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700408 final TaskRecord topTask() {
409 final int size = mTaskHistory.size();
410 if (size > 0) {
411 return mTaskHistory.get(size - 1);
412 }
413 return null;
414 }
415
Craig Mautnerd2328952013-03-05 12:46:26 -0800416 TaskRecord taskForIdLocked(int id) {
417 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
418 final TaskRecord task = mTaskHistory.get(taskNdx);
419 if (task.taskId == id) {
420 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800421 }
422 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700423 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700424 }
425
Craig Mautnerd2328952013-03-05 12:46:26 -0800426 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800427 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800428 if (r != null) {
429 final TaskRecord task = r.task;
430 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
431 if (task.stack != this) Slog.w(TAG,
432 "Illegal state! task does not point to stack it is in.");
433 return r;
434 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800435 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800436 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800437 }
438
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700439 boolean containsApp(ProcessRecord app) {
440 if (app == null) {
441 return false;
442 }
443 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
444 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
445 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
446 final ActivityRecord r = activities.get(activityNdx);
447 if (r.finishing) {
448 continue;
449 }
450 if (r.app == app) {
451 return true;
452 }
453 }
454 }
455 return false;
456 }
457
Craig Mautner2420ead2013-04-01 17:13:20 -0700458 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700459 final boolean hadit = mLRUActivities.remove(r);
460 mLRUActivities.add(r);
461 return hadit;
462 }
463
Craig Mautnerde4ef022013-04-07 19:01:33 -0700464 final boolean isHomeStack() {
465 return mStackId == HOME_STACK_ID;
466 }
467
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700468 /**
469 * Returns the top activity in any existing task matching the given
470 * Intent. Returns null if no such task is found.
471 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700472 ActivityRecord findTaskLocked(ActivityRecord target) {
473 Intent intent = target.intent;
474 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700475 ComponentName cls = intent.getComponent();
476 if (info.targetActivity != null) {
477 cls = new ComponentName(info.packageName, info.targetActivity);
478 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700479 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800480
481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700483 if (task.userId != userId) {
484 // Looking for a different task.
485 continue;
486 }
Craig Mautner000f0022013-02-26 15:04:29 -0800487 final ActivityRecord r = task.getTopActivity();
488 if (r == null || r.finishing || r.userId != userId ||
489 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
490 continue;
491 }
492
493 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
494 // + "/aff=" + r.task.affinity + " to new cls="
495 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
496 if (task.affinity != null) {
497 if (task.affinity.equals(info.taskAffinity)) {
498 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700499 return r;
500 }
Craig Mautner000f0022013-02-26 15:04:29 -0800501 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
502 //Slog.i(TAG, "Found matching class!");
503 //dump();
504 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
505 return r;
506 } else if (task.affinityIntent != null
507 && task.affinityIntent.getComponent().equals(cls)) {
508 //Slog.i(TAG, "Found matching class!");
509 //dump();
510 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
511 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700512 }
513 }
514
515 return null;
516 }
517
518 /**
519 * Returns the first activity (starting from the top of the stack) that
520 * is the same as the given activity. Returns null if no such activity
521 * is found.
522 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700523 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700524 ComponentName cls = intent.getComponent();
525 if (info.targetActivity != null) {
526 cls = new ComponentName(info.packageName, info.targetActivity);
527 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700528 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700529
Craig Mautner000f0022013-02-26 15:04:29 -0800530 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700531 TaskRecord task = mTaskHistory.get(taskNdx);
532 if (task.userId != mCurrentUser) {
533 return null;
534 }
535 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800536 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
537 ActivityRecord r = activities.get(activityNdx);
538 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700539 //Slog.i(TAG, "Found matching class!");
540 //dump();
541 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
542 return r;
543 }
544 }
545 }
546
547 return null;
548 }
549
Amith Yamasani742a6712011-05-04 14:49:28 -0700550 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700551 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700552 * @return whether there are any activities for the specified user.
553 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700554 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800555 if (VALIDATE_TOKENS) {
556 validateAppTokensLocked();
557 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800558 if (mCurrentUser == userId) {
559 return true;
560 }
561 mCurrentUser = userId;
562
563 // Move userId's tasks to the top.
564 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800565 int index = mTaskHistory.size();
566 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700567 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800568 if (task.userId == userId) {
569 haveActivities = true;
570 mTaskHistory.remove(i);
571 mTaskHistory.add(task);
572 --index;
573 }
574 }
575
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700576 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700577 }
578
Craig Mautner2420ead2013-04-01 17:13:20 -0700579 void minimalResumeActivityLocked(ActivityRecord r) {
580 r.state = ActivityState.RESUMED;
581 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
582 + " (starting new instance)");
583 r.stopped = false;
584 mResumedActivity = r;
585 r.task.touchActiveTime();
586 mService.addRecentTaskLocked(r.task);
587 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700588 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700589 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700590 }
591
Dianne Hackborncee04b52013-07-03 17:01:28 -0700592 private void startLaunchTraces() {
593 if (mFullyDrawnStartTime != 0) {
594 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
595 }
596 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
597 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
598 }
599
600 private void stopFullyDrawnTraceIfNeeded() {
601 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
602 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
603 mFullyDrawnStartTime = 0;
604 }
605 }
606
Craig Mautnere79d42682013-04-01 19:01:53 -0700607 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700608 if (r.displayStartTime == 0) {
609 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
610 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700611 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700612 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700613 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700614 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700615 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700616 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700617 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700618 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800619
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700620 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700621 // Make sure that there is no activity waiting for this to launch.
622 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
623 r.displayStartTime = r.fullyDrawnStartTime = 0;
624 } else {
625 mStackSupervisor.removeTimeoutsForActivityLocked(r);
626 mStackSupervisor.scheduleIdleTimeoutLocked(r);
627 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700628 }
629
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800630 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800631 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800632 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
633 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
634 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
635 activities.get(activityNdx).setSleeping(false);
636 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800637 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800638 }
639
Craig Mautner0eea92c2013-05-16 13:35:39 -0700640 /**
641 * @return true if something must be done before going to sleep.
642 */
643 boolean checkReadyForSleepLocked() {
644 if (mResumedActivity != null) {
645 // Still have something resumed; can't sleep until it is paused.
646 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
647 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
648 startPausingLocked(false, true);
649 return true;
650 }
651 if (mPausingActivity != null) {
652 // Still waiting for something to pause; can't sleep yet.
653 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
654 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800655 }
656
Craig Mautner0eea92c2013-05-16 13:35:39 -0700657 return false;
658 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800659
Craig Mautner0eea92c2013-05-16 13:35:39 -0700660 void goToSleep() {
661 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800662
Craig Mautner0eea92c2013-05-16 13:35:39 -0700663 // Make sure any stopped but visible activities are now sleeping.
664 // This ensures that the activity's onStop() is called.
665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
666 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
667 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
668 final ActivityRecord r = activities.get(activityNdx);
669 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
670 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800671 }
672 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800673 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700674 }
Craig Mautner59c00972012-07-30 12:10:24 -0700675
Dianne Hackbornd2835932010-12-13 16:28:46 -0800676 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800677 if (who.noDisplay) {
678 return null;
679 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800680
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700681 TaskRecord tr = who.task;
682 if (tr.intent != null && (tr.intent.getFlags()
683 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
684 // If this task is being excluded from recents, we don't want to take
685 // the expense of capturing a thumbnail, since we will never show it.
686 return null;
687 }
688
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800689 Resources res = mService.mContext.getResources();
690 int w = mThumbnailWidth;
691 int h = mThumbnailHeight;
692 if (w < 0) {
693 mThumbnailWidth = w =
694 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
695 mThumbnailHeight = h =
696 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
697 }
698
699 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800700 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700701 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800702 || mLastScreenshotBitmap.getWidth() != w
703 || mLastScreenshotBitmap.getHeight() != h) {
704 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700705 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700706 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800707 }
708 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700709 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800710 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800711 }
712 return null;
713 }
714
Craig Mautnercf910b02013-04-23 11:23:27 -0700715 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800716 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800717 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700718 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800719 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700720 ActivityRecord prev = mResumedActivity;
721 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700722 Slog.e(TAG, "Trying to pause when nothing is resumed",
723 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700724 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 return;
726 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700727 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
728 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800730 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700731 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700732 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
733 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734 prev.state = ActivityState.PAUSING;
735 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700736 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700737 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700738 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739
740 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800741
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700742 if (prev.app != null && prev.app.thread != null) {
743 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
744 try {
745 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700746 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700747 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700748 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800749 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
750 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700751 } catch (Exception e) {
752 // Ignore exception, if process died other code will cleanup.
753 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800754 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700756 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700757 }
758 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800759 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700760 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700761 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 }
763
764 // If we are not going to sleep, we want to ensure the device is
765 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700766 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700767 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 }
769
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800770 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700771 // Have the window manager pause its key dispatching until the new
772 // activity has started. If we're pausing the activity just because
773 // the screen is being turned off and the UI is sleeping, don't interrupt
774 // key dispatch; the same activity will pick it up again on wakeup.
775 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800776 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700777 } else {
778 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
779 }
780
781 // Schedule a pause timeout in case the app doesn't respond.
782 // We don't give it much time because this directly impacts the
783 // responsiveness seen by the user.
784 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
785 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700786 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700787 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
788 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
789 } else {
790 // This activity failed to schedule the
791 // pause, so just treat it as being paused now.
792 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700793 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700794 }
795 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700796
Craig Mautnerd2328952013-03-05 12:46:26 -0800797 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800799 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800
Craig Mautnerd2328952013-03-05 12:46:26 -0800801 final ActivityRecord r = isInStackLocked(token);
802 if (r != null) {
803 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
804 if (mPausingActivity == r) {
805 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
806 + (timeout ? " (due to timeout)" : " (pause complete)"));
807 r.state = ActivityState.PAUSED;
808 completePauseLocked();
809 } else {
810 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
811 r.userId, System.identityHashCode(r), r.shortComponentName,
812 mPausingActivity != null
813 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 }
815 }
816 }
817
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700818 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
819 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700820 if (r.state != ActivityState.STOPPING) {
821 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
822 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
823 return;
824 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700825 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700826 if (icicle != null) {
827 // If icicle is null, this is happening due to a timeout, so we
828 // haven't really saved the state.
829 r.icicle = icicle;
830 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800831 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700832 r.updateThumbnail(thumbnail, description);
833 }
834 if (!r.stopped) {
835 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
836 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
837 r.stopped = true;
838 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700839 if (r.finishing) {
840 r.clearOptionsLocked();
841 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700842 if (r.configDestroy) {
843 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700844 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700845 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700846 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800847 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700848 }
849 }
850 }
851
Craig Mautnerc8143c62013-09-03 12:15:57 -0700852 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800853 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800855
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 if (prev != null) {
857 if (prev.finishing) {
858 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700859 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 } else if (prev.app != null) {
861 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
862 if (prev.waitingVisible) {
863 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
866 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800867 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 if (prev.configDestroy) {
869 // The previous is being paused because the configuration
870 // is changing, which means it is actually stopping...
871 // To juggle the fact that we are also starting a new
872 // instance right now, we need to first completely stop
873 // the current instance before starting the new one.
874 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
875 destroyActivityLocked(prev, true, false, "pause-config");
876 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700877 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700878 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
879 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800880 // If we already have a few activities waiting to stop,
881 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700882 // them out. Or if r is the last of activity of the last task the stack
883 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800884 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700885 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800886 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700887 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 }
889 }
890 } else {
891 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
892 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800893 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800894 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700895 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800896
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700897 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700898 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700899 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800900 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700901 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700903 if (top == null || (prev != null && top != prev)) {
904 // If there are no more activities available to run,
905 // do resume anyway to start something. Also if the top
906 // activity on the stack is not the just paused activity,
907 // we need to go ahead and resume it to ensure we complete
908 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700909 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700910 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700911 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800912
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800913 if (prev != null) {
914 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915
Craig Mautner525f3d92013-05-07 14:01:50 -0700916 if (prev.app != null && prev.cpuTimeAtResume > 0
917 && mService.mBatteryStatsService.isOnBattery()) {
918 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700919 synchronized (mService.mProcessCpuThread) {
920 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700921 - prev.cpuTimeAtResume;
922 }
923 if (diff > 0) {
924 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
925 synchronized (bsi) {
926 BatteryStatsImpl.Uid.Proc ps =
927 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700928 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700929 if (ps != null) {
930 ps.addForegroundTimeLocked(diff);
931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
933 }
934 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700935 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 }
938
939 /**
940 * Once we know that we have asked an application to put an activity in
941 * the resumed state (either by launching it or explicitly telling it),
942 * this function updates the rest of our state to match that fact.
943 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700944 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700945 next.idle = false;
946 next.results = null;
947 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700948 if (next.nowVisible) {
949 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
950 mStackSupervisor.dismissKeyguard();
951 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952
953 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700954 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 mStackSupervisor.reportResumedActivityLocked(next);
957
958 next.resumeKeyDispatchingLocked();
959 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960
961 // Mark the point when the activity is resuming
962 // TODO: To be more accurate, the mark should be before the onCreate,
963 // not after the onResume. But for subsequent starts, onResume is fine.
964 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700965 synchronized (mService.mProcessCpuThread) {
966 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967 }
968 } else {
969 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
970 }
971 }
972
Craig Mautner580ea812013-04-25 12:58:38 -0700973 /**
974 * Version of ensureActivitiesVisible that can easily be called anywhere.
975 */
976 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
977 return ensureActivitiesVisibleLocked(starting, configChanges, false);
978 }
979
980 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
981 boolean forceHomeShown) {
982 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700983 return r != null &&
984 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700985 }
986
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700987 /**
988 * Make sure that all activities that need to be visible (that is, they
989 * currently can be seen by the user) actually are.
990 */
Craig Mautner580ea812013-04-25 12:58:38 -0700991 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
992 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautner323f7802013-10-01 21:16:22 -0700993 if (true || DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700994 TAG, "ensureActivitiesVisible behind " + top
995 + " configChanges=0x" + Integer.toHexString(configChanges));
996
Craig Mautner5eda9b32013-07-02 11:58:16 -0700997 if (mTranslucentActivityWaiting != top) {
998 mUndrawnActivitiesBelowTopTranslucent.clear();
999 if (mTranslucentActivityWaiting != null) {
1000 // Call the callback with a timeout indication.
1001 notifyActivityDrawnLocked(null);
1002 mTranslucentActivityWaiting = null;
1003 }
1004 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1005 }
1006
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001007 // If the top activity is not fullscreen, then we need to
1008 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001009 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001010 boolean showHomeBehindStack = false;
1011 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1012 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001013 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001014 final TaskRecord task = mTaskHistory.get(taskNdx);
1015 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001016 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1017 final ActivityRecord r = activities.get(activityNdx);
1018 if (r.finishing) {
1019 continue;
1020 }
1021 if (aboveTop && r != top) {
1022 continue;
1023 }
1024 aboveTop = false;
1025 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001027 TAG, "Make visible? " + r + " finishing=" + r.finishing
1028 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001029
Craig Mautnerd44711d2013-02-23 11:24:36 -08001030 final boolean doThisProcess = onlyThisProcess == null
1031 || onlyThisProcess.equals(r.processName);
1032
1033 // First: if this is not the current activity being started, make
1034 // sure it matches the current configuration.
1035 if (r != starting && doThisProcess) {
1036 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001038
1039 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001040 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001041 // This activity needs to be visible, but isn't even
1042 // running... get it started, but don't resume it
1043 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001044 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001045 if (r != starting) {
1046 r.startFreezingScreenLocked(r.app, configChanges);
1047 }
1048 if (!r.visible) {
Craig Mautner323f7802013-10-01 21:16:22 -07001049 if (true || DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001050 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001051 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001052 }
1053 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001054 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001055 }
1056 }
1057
1058 } else if (r.visible) {
1059 // If this activity is already visible, then there is nothing
1060 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001061 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001063
1064 } else if (onlyThisProcess == null) {
1065 // This activity is not currently visible, but is running.
1066 // Tell it to become visible.
1067 r.visible = true;
1068 if (r.state != ActivityState.RESUMED && r != starting) {
1069 // If this activity is paused, tell it
1070 // to now show its window.
Craig Mautner323f7802013-10-01 21:16:22 -07001071 if (true || DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001072 TAG, "Making visible and scheduling visibility: " + r);
1073 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001074 if (mTranslucentActivityWaiting != null) {
1075 mUndrawnActivitiesBelowTopTranslucent.add(r);
1076 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001077 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001078 r.sleeping = false;
1079 r.app.pendingUiClean = true;
1080 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1081 r.stopFreezingScreenLocked(false);
1082 } catch (Exception e) {
1083 // Just skip on any failure; we'll make it
1084 // visible when it next restarts.
1085 Slog.w(TAG, "Exception thrown making visibile: "
1086 + r.intent.getComponent(), e);
1087 }
1088 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090
Craig Mautnerd44711d2013-02-23 11:24:36 -08001091 // Aggregate current change flags.
1092 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001093
Craig Mautnerd44711d2013-02-23 11:24:36 -08001094 if (r.fullscreen) {
1095 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001096 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1097 behindFullscreen = true;
Craig Mautner19d112d2013-09-30 10:34:55 -07001098 } else if (task.mOnTopOfHome) {
1099 // Work our way down from r to bottom of task and see if there are any
1100 // visible activities below r.
1101 int rIndex = task.mActivities.indexOf(r);
1102 for ( --rIndex; rIndex >= 0; --rIndex) {
1103 final ActivityRecord blocker = task.mActivities.get(rIndex);
1104 if (!blocker.finishing && blocker.visible) {
1105 if (DEBUG_VISBILITY) Slog.v(TAG, "Home visibility for " +
1106 r + " blocked by " + blocker);
1107 break;
1108 }
1109 }
1110 if (rIndex < 0) {
1111 // Got to task bottom without finding a visible activity, show home.
1112 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1113 showHomeBehindStack = true;
1114 behindFullscreen = true;
1115 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001116 }
1117 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001118 if (DEBUG_VISBILITY) Slog.v(
1119 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1120 + " state=" + r.state
1121 + " behindFullscreen=" + behindFullscreen);
1122 // Now for any activities that aren't visible to the user, make
1123 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001124 if (r.visible) {
Craig Mautner323f7802013-10-01 21:16:22 -07001125 if (true || DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126 r.visible = false;
1127 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001128 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001129 switch (r.state) {
1130 case STOPPING:
1131 case STOPPED:
1132 if (r.app != null && r.app.thread != null) {
1133 if (DEBUG_VISBILITY) Slog.v(
1134 TAG, "Scheduling invisibility: " + r);
1135 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1136 }
1137 break;
1138
1139 case INITIALIZING:
1140 case RESUMED:
1141 case PAUSING:
1142 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001143 // This case created for transitioning activities from
1144 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001145 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1146 mStackSupervisor.mStoppingActivities.add(r);
1147 }
1148 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001149 break;
1150
1151 default:
1152 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 }
1154 } catch (Exception e) {
1155 // Just skip on any failure; we'll make it
1156 // visible when it next restarts.
1157 Slog.w(TAG, "Exception thrown making hidden: "
1158 + r.intent.getComponent(), e);
1159 }
1160 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001161 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001162 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001163 }
1164 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001165 }
Craig Mautner580ea812013-04-25 12:58:38 -07001166 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001167 }
Craig Mautner58547802013-03-05 08:23:53 -08001168
Craig Mautner5eda9b32013-07-02 11:58:16 -07001169 void convertToTranslucent(ActivityRecord r) {
1170 mTranslucentActivityWaiting = r;
1171 mUndrawnActivitiesBelowTopTranslucent.clear();
1172 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1173 }
1174
1175 /**
1176 * Called as activities below the top translucent activity are redrawn. When the last one is
1177 * redrawn notify the top activity by calling
1178 * {@link Activity#onTranslucentConversionComplete}.
1179 *
1180 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1181 * occurred and the activity will be notified immediately.
1182 */
1183 void notifyActivityDrawnLocked(ActivityRecord r) {
1184 if ((r == null)
1185 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1186 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1187 // The last undrawn activity below the top has just been drawn. If there is an
1188 // opaque activity at the top, notify it that it can become translucent safely now.
1189 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1190 mTranslucentActivityWaiting = null;
1191 mUndrawnActivitiesBelowTopTranslucent.clear();
1192 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1193
1194 if (waitingActivity != null && waitingActivity.app != null &&
1195 waitingActivity.app.thread != null) {
1196 try {
1197 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1198 waitingActivity.appToken, r != null);
1199 } catch (RemoteException e) {
1200 }
1201 }
1202 }
1203 }
1204
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 /**
1206 * Ensure that the top activity in the stack is resumed.
1207 *
1208 * @param prev The previously resumed activity, for when in the process
1209 * of pausing; can be null to call from elsewhere.
1210 *
1211 * @return Returns true if something is being resumed, or false if
1212 * nothing happened.
1213 */
1214 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001215 return resumeTopActivityLocked(prev, null);
1216 }
1217
1218 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001219 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1220
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001221 // Find the first activity that is not finishing.
1222 ActivityRecord next = topRunningActivityLocked(null);
1223
1224 // Remember how we'll process this pause/resume situation, and ensure
1225 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001226 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1227 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228
1229 if (next == null) {
1230 // There are no more activities! Let's just start up the
1231 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001232 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001233 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001234 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001235 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001236 }
1237
1238 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001239
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001240 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001241 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1242 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001243 // Make sure we have executed any pending transitions, since there
1244 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001245 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001246 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001247 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001248 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001249 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001250 return false;
1251 }
1252
Craig Mautner525f3d92013-05-07 14:01:50 -07001253 final TaskRecord nextTask = next.task;
1254 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001255 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001256 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001257 if (prevTask == nextTask) {
1258 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1259 final int numActivities = activities.size();
1260 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1261 final ActivityRecord r = activities.get(activityNdx);
1262 // r is usually the same as next, but what if two activities were launched
1263 // before prev finished?
1264 if (!r.finishing) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001265 r.frontOfTask = true;
1266 break;
1267 }
1268 }
1269 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001270 // This task is going away but it was supposed to return to the home task.
1271 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001272 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001273 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001274 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001275 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001276 return mStackSupervisor.resumeHomeActivity(prev);
1277 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001278 }
1279
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001280 // If we are sleeping, and there is no resumed activity, and the top
1281 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001282 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001283 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001284 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285 // Make sure we have executed any pending transitions, since there
1286 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001287 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001288 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001289 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001290 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001291 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001292 return false;
1293 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001294
1295 // Make sure that the user who owns this activity is started. If not,
1296 // we will just leave it as is because someone should be bringing
1297 // another user's activities to the top of the stack.
1298 if (mService.mStartedUsers.get(next.userId) == null) {
1299 Slog.w(TAG, "Skipping resume of top activity " + next
1300 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001301 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001302 return false;
1303 }
1304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001305 // The activity may be waiting for stop, but that is no longer
1306 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001307 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001308 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001309 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001310 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311
Dianne Hackborn84375872012-06-01 19:03:50 -07001312 next.updateOptionsLocked(options);
1313
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001314 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1315
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001316 // If we are currently pausing an activity, then don't do anything
1317 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001318 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001319 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1320 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001321 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001322 return false;
1323 }
1324
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001325 // Okay we are now going to start a switch, to 'next'. We may first
1326 // have to pause the current activity, but this is an important point
1327 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001328 // XXX "App Redirected" dialog is getting too many false positives
1329 // at this point, so turn off for now.
1330 if (false) {
1331 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1332 long now = SystemClock.uptimeMillis();
1333 final boolean inTime = mLastStartedActivity.startTime != 0
1334 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1335 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1336 final int nextUid = next.info.applicationInfo.uid;
1337 if (inTime && lastUid != nextUid
1338 && lastUid != next.launchedFromUid
1339 && mService.checkPermission(
1340 android.Manifest.permission.STOP_APP_SWITCHES,
1341 -1, next.launchedFromUid)
1342 != PackageManager.PERMISSION_GRANTED) {
1343 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1344 } else {
1345 next.startTime = now;
1346 mLastStartedActivity = next;
1347 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001348 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001349 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001350 mLastStartedActivity = next;
1351 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001352 }
Craig Mautner58547802013-03-05 08:23:53 -08001353
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001354 // We need to start pausing the current activity so the top one
1355 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001356 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001357 if (mResumedActivity != null) {
1358 pausing = true;
1359 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001360 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001361 }
1362 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001363 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1364 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001365 // At this point we want to put the upcoming activity's process
1366 // at the top of the LRU list, since we know we will be needing it
1367 // very soon and it would be a waste to let it get killed if it
1368 // happens to be sitting towards the end.
1369 if (next.app != null && next.app.thread != null) {
1370 // No reason to do full oom adj update here; we'll let that
1371 // happen whenever it needs to later.
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001372 mService.updateLruProcessLocked(next.app, false, true);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001373 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001374 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001375 return true;
1376 }
1377
Christopher Tated3f175c2012-06-14 14:16:54 -07001378 // If the most recent activity was noHistory but was only stopped rather
1379 // than stopped+finished because the device went to sleep, we need to make
1380 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001381 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1382 !mLastNoHistoryActivity.finishing) {
1383 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1384 " on new resume");
1385 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1386 null, "no-history", false);
1387 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001388 }
1389
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 if (prev != null && prev != next) {
1391 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1392 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001393 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001394 if (DEBUG_SWITCH) Slog.v(
1395 TAG, "Resuming top, waiting visible to hide: " + prev);
1396 } else {
1397 // The next activity is already visible, so hide the previous
1398 // activity's windows right now so we can show the new one ASAP.
1399 // We only do this if the previous is finishing, which should mean
1400 // it is on top of the one being resumed so hiding it quickly
1401 // is good. Otherwise, we want to do the normal route of allowing
1402 // the resumed activity to be shown so we can decide if the
1403 // previous should actually be hidden depending on whether the
1404 // new one is found to be full-screen or not.
1405 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001406 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1408 + prev + ", waitingVisible="
1409 + (prev != null ? prev.waitingVisible : null)
1410 + ", nowVisible=" + next.nowVisible);
1411 } else {
1412 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1413 + prev + ", waitingVisible="
1414 + (prev != null ? prev.waitingVisible : null)
1415 + ", nowVisible=" + next.nowVisible);
1416 }
1417 }
1418 }
1419
Dianne Hackborne7f97212011-02-24 14:40:20 -08001420 // Launching this app's activity, make sure the app is no longer
1421 // considered stopped.
1422 try {
1423 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001424 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001425 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001426 } catch (IllegalArgumentException e) {
1427 Slog.w(TAG, "Failed trying to unstop package "
1428 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001429 }
1430
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 // We are starting up the next activity, so tell the window manager
1432 // that the previous one will be hidden soon. This way it can know
1433 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001434 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 if (prev != null) {
1436 if (prev.finishing) {
1437 if (DEBUG_TRANSITION) Slog.v(TAG,
1438 "Prepare close transition: prev=" + prev);
1439 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001440 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001441 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001443 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001444 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1445 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001447 mWindowManager.setAppWillBeHidden(prev.appToken);
1448 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001450 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001452 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001453 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001455 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001456 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1457 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 }
1459 }
1460 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001461 mWindowManager.setAppWillBeHidden(prev.appToken);
1462 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 }
Craig Mautner967212c2013-04-13 21:10:58 -07001464 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001465 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001467 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001468 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001470 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 }
1472 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001473 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001474 next.applyOptionsLocked();
1475 } else {
1476 next.clearOptionsLocked();
1477 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478
Craig Mautnercf910b02013-04-23 11:23:27 -07001479 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001480 if (next.app != null && next.app.thread != null) {
1481 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1482
1483 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001484 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001485
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001486 // schedule launch ticks to collect information about slow apps.
1487 next.startLaunchTickingLocked();
1488
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001489 ActivityRecord lastResumedActivity =
1490 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001491 ActivityState lastState = next.state;
1492
1493 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001494
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001495 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 next.state = ActivityState.RESUMED;
1497 mResumedActivity = next;
1498 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001499 mService.addRecentTaskLocked(next.task);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001500 mService.updateLruProcessLocked(next.app, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 updateLRUListLocked(next);
1502
1503 // Have the window manager re-evaluate the orientation of
1504 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001505 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001506 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001507 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001508 mService.mConfiguration,
1509 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1510 if (config != null) {
1511 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001513 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001515
Craig Mautner525f3d92013-05-07 14:01:50 -07001516 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517 // The configuration update wasn't able to keep the existing
1518 // instance of the activity, and instead started a new one.
1519 // We should be all done, but let's just make sure our activity
1520 // is still at the top and schedule another run if something
1521 // weird happened.
1522 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001523 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 "Activity config changed during resume: " + next
1525 + ", new next: " + nextNext);
1526 if (nextNext != next) {
1527 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001528 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001530 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001531 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001532 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001533 return true;
1534 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001535 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001536 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537 }
Craig Mautner58547802013-03-05 08:23:53 -08001538
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001539 try {
1540 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001541 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 if (a != null) {
1543 final int N = a.size();
1544 if (!next.finishing && N > 0) {
1545 if (DEBUG_RESULTS) Slog.v(
1546 TAG, "Delivering results to " + next
1547 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001548 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 }
1550 }
1551
1552 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001553 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001554 }
1555
1556 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001557 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001559
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001560 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001561 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001562 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001563 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1564 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001566
Craig Mautner0eea92c2013-05-16 13:35:39 -07001567 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568
Craig Mautnerac6f8432013-07-17 13:24:59 -07001569 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001570 } catch (Exception e) {
1571 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001572 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1573 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001575 if (lastStack != null) {
1576 lastStack.mResumedActivity = lastResumedActivity;
1577 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 Slog.i(TAG, "Restarting because process died: " + next);
1579 if (!next.hasBeenLaunched) {
1580 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001581 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1582 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001583 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001584 next.appToken, next.packageName, next.theme,
1585 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001586 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1587 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001589 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001590 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 return true;
1592 }
1593
1594 // From this point on, if something goes wrong there is no way
1595 // to recover the activity.
1596 try {
1597 next.visible = true;
1598 completeResumeLocked(next);
1599 } catch (Exception e) {
1600 // If any exception gets thrown, toss away this
1601 // activity and try the next one.
1602 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001603 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001604 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001605 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001606 return true;
1607 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 next.stopped = false;
1609
1610 } else {
1611 // Whoops, need to restart this activity!
1612 if (!next.hasBeenLaunched) {
1613 next.hasBeenLaunched = true;
1614 } else {
1615 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001616 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001617 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001618 mService.compatibilityInfoForPackageLocked(
1619 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001621 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001622 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 }
1624 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1625 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001626 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001627 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001628 }
1629
Craig Mautnercf910b02013-04-23 11:23:27 -07001630 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001631 return true;
1632 }
1633
Craig Mautnerac6f8432013-07-17 13:24:59 -07001634 private void insertTaskAtTop(TaskRecord task) {
1635 mTaskHistory.remove(task);
1636 // Now put task at top.
1637 int stackNdx = mTaskHistory.size();
1638 if (task.userId != mCurrentUser) {
1639 // Put non-current user tasks below current user tasks.
1640 while (--stackNdx >= 0) {
1641 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1642 break;
1643 }
1644 }
1645 ++stackNdx;
1646 }
1647 mTaskHistory.add(stackNdx, task);
1648 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001649
Craig Mautner8849a5e2013-04-02 16:41:03 -07001650 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001651 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001652 TaskRecord rTask = r.task;
1653 final int taskId = rTask.taskId;
1654 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001655 // Last activity in task had been removed or ActivityManagerService is reusing task.
1656 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001657 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001658 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001659 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001660 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001661 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001662 if (!newTask) {
1663 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001664 boolean startIt = true;
1665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1666 task = mTaskHistory.get(taskNdx);
1667 if (task == r.task) {
1668 // Here it is! Now, if this is not yet visible to the
1669 // user, then just add it without starting; it will
1670 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001671 if (!startIt) {
1672 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1673 + task, new RuntimeException("here").fillInStackTrace());
1674 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001675 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001676 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1677 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001678 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1679 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001681 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001682 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001683 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 return;
1685 }
1686 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001687 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 startIt = false;
1689 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 }
1691 }
1692
1693 // Place a new activity at top of stack, so it is next to interact
1694 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001695
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 // If we are not placing the new activity frontmost, we do not want
1697 // to deliver the onUserLeaving callback to the actual frontmost
1698 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001699 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001700 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001701 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1702 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 }
Craig Mautner70a86932013-02-28 22:37:44 -08001704
1705 task = r.task;
1706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001708 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001709 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001710 task.addActivityToTop(r);
1711
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001712 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001714 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 // We want to show the starting preview window if we are
1716 // switching to a new task, or the next activity's process is
1717 // not currently running.
1718 boolean showStartingIcon = newTask;
1719 ProcessRecord proc = r.app;
1720 if (proc == null) {
1721 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1722 }
1723 if (proc == null || proc.thread == null) {
1724 showStartingIcon = true;
1725 }
1726 if (DEBUG_TRANSITION) Slog.v(TAG,
1727 "Prepare open transition: starting " + r);
1728 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001729 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001731 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001732 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001733 ? AppTransition.TRANSIT_TASK_OPEN
1734 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 mNoAnimActivities.remove(r);
1736 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001737 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001738 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001739 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001740 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741 boolean doShow = true;
1742 if (newTask) {
1743 // Even though this activity is starting fresh, we still need
1744 // to reset it to make sure we apply affinities to move any
1745 // existing activities from other tasks in to it.
1746 // If the caller has requested that the target task be
1747 // reset, then do so.
1748 if ((r.intent.getFlags()
1749 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1750 resetTaskIfNeededLocked(r, r);
1751 doShow = topRunningNonDelayedActivityLocked(null) == r;
1752 }
1753 }
1754 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1755 // Figure out if we are transitioning from another activity that is
1756 // "has the same starting icon" as the next one. This allows the
1757 // window manager to keep the previous window it had previously
1758 // created, if it still had one.
1759 ActivityRecord prev = mResumedActivity;
1760 if (prev != null) {
1761 // We don't want to reuse the previous starting preview if:
1762 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001763 if (prev.task != r.task) {
1764 prev = null;
1765 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001766 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001767 else if (prev.nowVisible) {
1768 prev = null;
1769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001771 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001772 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001773 mService.compatibilityInfoForPackageLocked(
1774 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001775 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001776 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 }
1778 } else {
1779 // If this is the first activity, don't do any fancy animations,
1780 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001781 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001782 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001783 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001784 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 }
1786 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001787 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 }
1789
1790 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001791 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792 }
1793 }
1794
Dianne Hackbornbe707852011-11-11 14:32:10 -08001795 final void validateAppTokensLocked() {
1796 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001797 mValidateAppTokens.ensureCapacity(numActivities());
1798 final int numTasks = mTaskHistory.size();
1799 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1800 TaskRecord task = mTaskHistory.get(taskNdx);
1801 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001802 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001803 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001804 }
Craig Mautner000f0022013-02-26 15:04:29 -08001805 TaskGroup group = new TaskGroup();
1806 group.taskId = task.taskId;
1807 mValidateAppTokens.add(group);
1808 final int numActivities = activities.size();
1809 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1810 final ActivityRecord r = activities.get(activityNdx);
1811 group.tokens.add(r.appToken);
1812 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001813 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001814 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001815 }
1816
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 /**
1818 * Perform a reset of the given task, if needed as part of launching it.
1819 * Returns the new HistoryRecord at the top of the task.
1820 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001821 /**
1822 * Helper method for #resetTaskIfNeededLocked.
1823 * We are inside of the task being reset... we'll either finish this activity, push it out
1824 * for another task, or leave it as-is.
1825 * @param task The task containing the Activity (taskTop) that might be reset.
1826 * @param forceReset
1827 * @return An ActivityOptions that needs to be processed.
1828 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001829 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001830 ActivityOptions topOptions = null;
1831
1832 int replyChainEnd = -1;
1833 boolean canMoveOptions = true;
1834
1835 // We only do this for activities that are not the root of the task (since if we finish
1836 // the root, we may no longer have the task!).
1837 final ArrayList<ActivityRecord> activities = task.mActivities;
1838 final int numActivities = activities.size();
1839 for (int i = numActivities - 1; i > 0; --i ) {
1840 ActivityRecord target = activities.get(i);
1841
1842 final int flags = target.info.flags;
1843 final boolean finishOnTaskLaunch =
1844 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1845 final boolean allowTaskReparenting =
1846 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1847 final boolean clearWhenTaskReset =
1848 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1849
1850 if (!finishOnTaskLaunch
1851 && !clearWhenTaskReset
1852 && target.resultTo != null) {
1853 // If this activity is sending a reply to a previous
1854 // activity, we can't do anything with it now until
1855 // we reach the start of the reply chain.
1856 // XXX note that we are assuming the result is always
1857 // to the previous activity, which is almost always
1858 // the case but we really shouldn't count on.
1859 if (replyChainEnd < 0) {
1860 replyChainEnd = i;
1861 }
1862 } else if (!finishOnTaskLaunch
1863 && !clearWhenTaskReset
1864 && allowTaskReparenting
1865 && target.taskAffinity != null
1866 && !target.taskAffinity.equals(task.affinity)) {
1867 // If this activity has an affinity for another
1868 // task, then we need to move it out of here. We will
1869 // move it as far out of the way as possible, to the
1870 // bottom of the activity stack. This also keeps it
1871 // correctly ordered with any activities we previously
1872 // moved.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001873 final ActivityRecord bottom =
1874 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
1875 mTaskHistory.get(0).mActivities.get(0) : null;
1876 if (bottom != null && target.taskAffinity != null
1877 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001878 // If the activity currently at the bottom has the
1879 // same task affinity as the one we are moving,
1880 // then merge it into the same task.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001881 target.setTask(bottom.task, bottom.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001882 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001883 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001884 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001885 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001886 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001887 target.task.affinityIntent = target.intent;
1888 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1889 + " out to new task " + target.task);
1890 }
1891
1892 final TaskRecord targetTask = target.task;
1893 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001894 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001895
Craig Mautner525f3d92013-05-07 14:01:50 -07001896 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001897 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1898 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001899 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001900 if (p.finishing) {
1901 continue;
1902 }
1903
Craig Mautner525f3d92013-05-07 14:01:50 -07001904 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001905 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001906 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001907 topOptions = p.takeOptionsLocked();
1908 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001909 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001910 }
1911 }
1912 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1913 + task + " adding to task=" + targetTask,
1914 new RuntimeException("here").fillInStackTrace());
1915 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1916 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001917 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001918 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001919
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001920 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001921 }
1922
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001923 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001924 if (VALIDATE_TOKENS) {
1925 validateAppTokensLocked();
1926 }
1927
1928 replyChainEnd = -1;
1929 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1930 // If the activity should just be removed -- either
1931 // because it asks for it, or the task should be
1932 // cleared -- then finish it and anything that is
1933 // part of its reply chain.
1934 int end;
1935 if (clearWhenTaskReset) {
1936 // In this case, we want to finish this activity
1937 // and everything above it, so be sneaky and pretend
1938 // like these are all in the reply chain.
1939 end = numActivities - 1;
1940 } else if (replyChainEnd < 0) {
1941 end = i;
1942 } else {
1943 end = replyChainEnd;
1944 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001945 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001946 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001947 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001948 if (p.finishing) {
1949 continue;
1950 }
1951 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001952 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001953 topOptions = p.takeOptionsLocked();
1954 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001955 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001956 }
1957 }
Craig Mautner58547802013-03-05 08:23:53 -08001958 if (DEBUG_TASKS) Slog.w(TAG,
1959 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001960 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001961 end--;
1962 srcPos--;
1963 }
1964 }
1965 replyChainEnd = -1;
1966 } else {
1967 // If we were in the middle of a chain, well the
1968 // activity that started it all doesn't want anything
1969 // special, so leave it all as-is.
1970 replyChainEnd = -1;
1971 }
1972 }
1973
1974 return topOptions;
1975 }
1976
1977 /**
1978 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1979 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1980 * @param affinityTask The task we are looking for an affinity to.
1981 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1982 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1983 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1984 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001985 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001986 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001987 int replyChainEnd = -1;
1988 final int taskId = task.taskId;
1989 final String taskAffinity = task.affinity;
1990
1991 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1992 final int numActivities = activities.size();
1993 // Do not operate on the root Activity.
1994 for (int i = numActivities - 1; i > 0; --i) {
1995 ActivityRecord target = activities.get(i);
1996
1997 final int flags = target.info.flags;
1998 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1999 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2000
2001 if (target.resultTo != null) {
2002 // If this activity is sending a reply to a previous
2003 // activity, we can't do anything with it now until
2004 // we reach the start of the reply chain.
2005 // XXX note that we are assuming the result is always
2006 // to the previous activity, which is almost always
2007 // the case but we really shouldn't count on.
2008 if (replyChainEnd < 0) {
2009 replyChainEnd = i;
2010 }
2011 } else if (topTaskIsHigher
2012 && allowTaskReparenting
2013 && taskAffinity != null
2014 && taskAffinity.equals(target.taskAffinity)) {
2015 // This activity has an affinity for our task. Either remove it if we are
2016 // clearing or move it over to our task. Note that
2017 // we currently punt on the case where we are resetting a
2018 // task that is not at the top but who has activities above
2019 // with an affinity to it... this is really not a normal
2020 // case, and we will need to later pull that task to the front
2021 // and usually at that point we will do the reset and pick
2022 // up those remaining activities. (This only happens if
2023 // someone starts an activity in a new task from an activity
2024 // in a task that is not currently on top.)
2025 if (forceReset || finishOnTaskLaunch) {
2026 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2027 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2028 for (int srcPos = start; srcPos >= i; --srcPos) {
2029 final ActivityRecord p = activities.get(srcPos);
2030 if (p.finishing) {
2031 continue;
2032 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002033 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002034 }
2035 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002036 if (taskInsertionPoint < 0) {
2037 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002038
Craig Mautner77878772013-03-04 19:46:24 -08002039 }
Craig Mautner77878772013-03-04 19:46:24 -08002040
2041 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2042 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2043 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2044 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002045 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002046 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002047 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002048
Craig Mautnere3a74d52013-02-22 14:14:58 -08002049 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2050 + " to stack at " + task,
2051 new RuntimeException("here").fillInStackTrace());
2052 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2053 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002054 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002055 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002056 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002057 if (VALIDATE_TOKENS) {
2058 validateAppTokensLocked();
2059 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002060
2061 // Now we've moved it in to place... but what if this is
2062 // a singleTop activity and we have put it on top of another
2063 // instance of the same activity? Then we drop the instance
2064 // below so it remains singleTop.
2065 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2066 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002067 int targetNdx = taskActivities.indexOf(target);
2068 if (targetNdx > 0) {
2069 ActivityRecord p = taskActivities.get(targetNdx - 1);
2070 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002071 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2072 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002073 }
2074 }
2075 }
2076 }
2077
2078 replyChainEnd = -1;
2079 }
2080 }
Craig Mautner77878772013-03-04 19:46:24 -08002081 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002082 }
2083
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002085 ActivityRecord newActivity) {
2086 boolean forceReset =
2087 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2088 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2089 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2090 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2091 forceReset = true;
2092 }
2093 }
2094
2095 final TaskRecord task = taskTop.task;
2096
2097 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2098 * for remaining tasks. Used for later tasks to reparent to task. */
2099 boolean taskFound = false;
2100
2101 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2102 ActivityOptions topOptions = null;
2103
Craig Mautner77878772013-03-04 19:46:24 -08002104 // Preserve the location for reparenting in the new task.
2105 int reparentInsertionPoint = -1;
2106
Craig Mautnere3a74d52013-02-22 14:14:58 -08002107 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2108 final TaskRecord targetTask = mTaskHistory.get(i);
2109
2110 if (targetTask == task) {
2111 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2112 taskFound = true;
2113 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002114 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2115 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002116 }
2117 }
2118
Craig Mautner70a86932013-02-28 22:37:44 -08002119 int taskNdx = mTaskHistory.indexOf(task);
2120 do {
2121 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2122 } while (taskTop == null && taskNdx >= 0);
2123
Craig Mautnere3a74d52013-02-22 14:14:58 -08002124 if (topOptions != null) {
2125 // If we got some ActivityOptions from an activity on top that
2126 // was removed from the task, propagate them to the new real top.
2127 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002128 taskTop.updateOptionsLocked(topOptions);
2129 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002130 topOptions.abort();
2131 }
2132 }
2133
2134 return taskTop;
2135 }
2136
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2138 String resultWho, int requestCode, int resultCode, Intent data) {
2139
2140 if (callingUid > 0) {
2141 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002142 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 }
2144
2145 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2146 + " : who=" + resultWho + " req=" + requestCode
2147 + " res=" + resultCode + " data=" + data);
2148 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2149 try {
2150 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2151 list.add(new ResultInfo(resultWho, requestCode,
2152 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002153 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154 return;
2155 } catch (Exception e) {
2156 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2157 }
2158 }
2159
2160 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2161 }
2162
Craig Mautnerf3333272013-04-22 10:55:53 -07002163 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002164 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2165 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2166 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2167 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002168 if (!mService.mSleeping) {
2169 if (DEBUG_STATES) {
2170 Slog.d(TAG, "no-history finish of " + r);
2171 }
2172 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002173 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002174 } else {
2175 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2176 + " on stop because we're just sleeping");
2177 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002178 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002179 }
2180
2181 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002182 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002183 if (mService.mFocusedActivity == r) {
2184 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2185 }
2186 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002187 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 try {
2189 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002190 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2191 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002192 r.state = ActivityState.STOPPING;
2193 if (DEBUG_VISBILITY) Slog.v(
2194 TAG, "Stopping visible=" + r.visible + " for " + r);
2195 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002196 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002198 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002199 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002200 r.setSleeping(true);
2201 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002202 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002203 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 } catch (Exception e) {
2205 // Maybe just ignore exceptions here... if the process
2206 // has crashed, our death notification will clean things
2207 // up.
2208 Slog.w(TAG, "Exception thrown during pause", e);
2209 // Just in case, assume it to be stopped.
2210 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002211 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 r.state = ActivityState.STOPPED;
2213 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002214 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002215 }
2216 }
2217 }
2218 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 /**
2221 * @return Returns true if the activity is being finished, false if for
2222 * some reason it is being left as-is.
2223 */
2224 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002225 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002226 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002227 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002228 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002229 + ", result=" + resultCode + ", data=" + resultData
2230 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002231 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002232 return false;
2233 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234
Craig Mautnerd44711d2013-02-23 11:24:36 -08002235 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002236 return true;
2237 }
2238
Craig Mautnerd2328952013-03-05 12:46:26 -08002239 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002240 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2241 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2242 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2243 ActivityRecord r = activities.get(activityNdx);
2244 if (r.resultTo == self && r.requestCode == requestCode) {
2245 if ((r.resultWho == null && resultWho == null) ||
2246 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2247 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2248 false);
2249 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002250 }
2251 }
2252 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002253 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002254 }
2255
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002256 final void finishTopRunningActivityLocked(ProcessRecord app) {
2257 ActivityRecord r = topRunningActivityLocked(null);
2258 if (r != null && r.app == app) {
2259 // If the top running activity is from this crashing
2260 // process, then terminate it to avoid getting in a loop.
2261 Slog.w(TAG, " Force finishing activity "
2262 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002263 int taskNdx = mTaskHistory.indexOf(r.task);
2264 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002265 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002266 // Also terminate any activities below it that aren't yet
2267 // stopped, to avoid a situation where one will get
2268 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002269 --activityNdx;
2270 if (activityNdx < 0) {
2271 do {
2272 --taskNdx;
2273 if (taskNdx < 0) {
2274 break;
2275 }
2276 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2277 } while (activityNdx < 0);
2278 }
2279 if (activityNdx >= 0) {
2280 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002281 if (r.state == ActivityState.RESUMED
2282 || r.state == ActivityState.PAUSING
2283 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002284 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002285 Slog.w(TAG, " Force finishing activity "
2286 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002287 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002288 }
2289 }
2290 }
2291 }
2292 }
2293
Craig Mautnerd2328952013-03-05 12:46:26 -08002294 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002295 ArrayList<ActivityRecord> activities = r.task.mActivities;
2296 for (int index = activities.indexOf(r); index >= 0; --index) {
2297 ActivityRecord cur = activities.get(index);
2298 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002299 break;
2300 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002301 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002302 }
2303 return true;
2304 }
2305
Dianne Hackborn5c607432012-02-28 14:44:19 -08002306 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2307 // send the result
2308 ActivityRecord resultTo = r.resultTo;
2309 if (resultTo != null) {
2310 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2311 + " who=" + r.resultWho + " req=" + r.requestCode
2312 + " res=" + resultCode + " data=" + resultData);
2313 if (r.info.applicationInfo.uid > 0) {
2314 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2315 resultTo.packageName, resultData,
2316 resultTo.getUriPermissionsLocked());
2317 }
2318 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2319 resultData);
2320 r.resultTo = null;
2321 }
2322 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2323
2324 // Make sure this HistoryRecord is not holding on to other resources,
2325 // because clients have remote IPC references to this object so we
2326 // can't assume that will go away and want to avoid circular IPC refs.
2327 r.results = null;
2328 r.pendingResults = null;
2329 r.newIntents = null;
2330 r.icicle = null;
2331 }
2332
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 /**
2334 * @return Returns true if this activity has been removed from the history
2335 * list, or false if it is still in the list and will be removed later.
2336 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002337 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2338 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 if (r.finishing) {
2340 Slog.w(TAG, "Duplicate finish request for " + r);
2341 return false;
2342 }
2343
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002344 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002345 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002346 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002348 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2349 final int index = activities.indexOf(r);
2350 if (index < (activities.size() - 1)) {
2351 ActivityRecord next = activities.get(index+1);
2352 if (r.frontOfTask) {
2353 // The next activity is now the front of the task.
2354 next.frontOfTask = true;
2355 }
2356 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2357 // If the caller asked that this activity (and all above it)
2358 // be cleared when the task is reset, don't lose that information,
2359 // but propagate it up to the next activity.
2360 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002361 }
2362 }
2363
2364 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002365 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002367 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 }
2369 }
2370
Dianne Hackborn5c607432012-02-28 14:44:19 -08002371 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002372
Craig Mautnerc8143c62013-09-03 12:15:57 -07002373 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 // There are clients waiting to receive thumbnails so, in case
2375 // this is an activity that someone is waiting for, add it
2376 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002377 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002378 }
2379
Craig Mautnerde4ef022013-04-07 19:01:33 -07002380 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002381 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002382 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002384 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002385 ? AppTransition.TRANSIT_TASK_CLOSE
2386 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002387
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002388 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002389 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002390
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002391 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002392 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2393 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2394 startPausingLocked(false, false);
2395 }
2396
2397 } else if (r.state != ActivityState.PAUSING) {
2398 // If the activity is PAUSING, we will complete the finish once
2399 // it is done pausing; else we can just directly finish it here.
2400 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002401 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002402 } else {
2403 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2404 }
2405
2406 return false;
2407 }
2408
Craig Mautnerf3333272013-04-22 10:55:53 -07002409 static final int FINISH_IMMEDIATELY = 0;
2410 static final int FINISH_AFTER_PAUSE = 1;
2411 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002412
Craig Mautnerf3333272013-04-22 10:55:53 -07002413 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002414 // First things first: if this activity is currently visible,
2415 // and the resumed activity is not yet visible, then hold off on
2416 // finishing until the resumed one becomes visible.
2417 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002418 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2419 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002420 if (mStackSupervisor.mStoppingActivities.size() > 3
2421 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422 // If we already have a few activities waiting to stop,
2423 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002424 // them out. Or if r is the last of activity of the last task the stack
2425 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002426 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002427 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002428 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 }
2430 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002431 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2432 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002434 if (oomAdj) {
2435 mService.updateOomAdjLocked();
2436 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002437 return r;
2438 }
2439
2440 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002441 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002442 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002443 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 if (mResumedActivity == r) {
2445 mResumedActivity = null;
2446 }
2447 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002448 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 r.state = ActivityState.FINISHING;
2450
2451 if (mode == FINISH_IMMEDIATELY
2452 || prevState == ActivityState.STOPPED
2453 || prevState == ActivityState.INITIALIZING) {
2454 // If this activity is already stopped, we can just finish
2455 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002456 boolean activityRemoved = destroyActivityLocked(r, true,
2457 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002458 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002459 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002460 }
2461 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002463
2464 // Need to go through the full pause cycle to get this
2465 // activity into the stopped state and then finish it.
2466 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002467 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002468 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 return r;
2470 }
2471
Craig Mautnerd2328952013-03-05 12:46:26 -08002472 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002473 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002474 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002475 final TaskRecord task = srec.task;
2476 final ArrayList<ActivityRecord> activities = task.mActivities;
2477 final int start = activities.indexOf(srec);
2478 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002479 return false;
2480 }
2481 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002482 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002483 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002484 final ComponentName dest = destIntent.getComponent();
2485 if (start > 0 && dest != null) {
2486 for (int i = finishTo; i >= 0; i--) {
2487 ActivityRecord r = activities.get(i);
2488 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002489 r.info.name.equals(dest.getClassName())) {
2490 finishTo = i;
2491 parent = r;
2492 foundParentInTask = true;
2493 break;
2494 }
2495 }
2496 }
2497
2498 IActivityController controller = mService.mController;
2499 if (controller != null) {
2500 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2501 if (next != null) {
2502 // ask watcher if this is allowed
2503 boolean resumeOK = true;
2504 try {
2505 resumeOK = controller.activityResuming(next.packageName);
2506 } catch (RemoteException e) {
2507 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002508 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002509 }
2510
2511 if (!resumeOK) {
2512 return false;
2513 }
2514 }
2515 }
2516 final long origId = Binder.clearCallingIdentity();
2517 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002518 ActivityRecord r = activities.get(i);
2519 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002520 // Only return the supplied result for the first activity finished
2521 resultCode = Activity.RESULT_CANCELED;
2522 resultData = null;
2523 }
2524
2525 if (parent != null && foundParentInTask) {
2526 final int parentLaunchMode = parent.info.launchMode;
2527 final int destIntentFlags = destIntent.getFlags();
2528 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2529 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2530 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2531 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2532 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2533 } else {
2534 try {
2535 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2536 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002537 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002538 null, aInfo, parent.appToken, null,
2539 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2540 0, null, true, null);
2541 foundParentInTask = res == ActivityManager.START_SUCCESS;
2542 } catch (RemoteException e) {
2543 foundParentInTask = false;
2544 }
2545 requestFinishActivityLocked(parent.appToken, resultCode,
2546 resultData, "navigate-up", true);
2547 }
2548 }
2549 Binder.restoreCallingIdentity(origId);
2550 return foundParentInTask;
2551 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 /**
2553 * Perform the common clean-up of an activity record. This is called both
2554 * as part of destroyActivityLocked() (when destroying the client-side
2555 * representation) and cleaning things up as a result of its hosting
2556 * processing going away, in which case there is no remaining client-side
2557 * state to destroy so only the cleanup here is needed.
2558 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002559 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2560 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002561 if (mResumedActivity == r) {
2562 mResumedActivity = null;
2563 }
2564 if (mService.mFocusedActivity == r) {
2565 mService.mFocusedActivity = null;
2566 }
2567
2568 r.configDestroy = false;
2569 r.frozenBeforeDestroy = false;
2570
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002571 if (setState) {
2572 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2573 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002574 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002575 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002576 }
2577
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 // Make sure this record is no longer in the pending finishes list.
2579 // This could happen, for example, if we are trimming activities
2580 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002581 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002582 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002583
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002584 // Remove any pending results.
2585 if (r.finishing && r.pendingResults != null) {
2586 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2587 PendingIntentRecord rec = apr.get();
2588 if (rec != null) {
2589 mService.cancelIntentSenderLocked(rec, false);
2590 }
2591 }
2592 r.pendingResults = null;
2593 }
2594
2595 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002596 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 }
2598
Craig Mautnerc8143c62013-09-03 12:15:57 -07002599 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 // There are clients waiting to receive thumbnails so, in case
2601 // this is an activity that someone is waiting for, add it
2602 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002603 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002604 }
2605
2606 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002607 removeTimeoutsForActivityLocked(r);
2608 }
2609
2610 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002611 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002612 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002613 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002614 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002615 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002616 }
2617
Dianne Hackborn5c607432012-02-28 14:44:19 -08002618 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002619 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2620 r.makeFinishing();
2621 if (DEBUG_ADD_REMOVE) {
2622 RuntimeException here = new RuntimeException("here");
2623 here.fillInStackTrace();
2624 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002626 final TaskRecord task = r.task;
2627 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002628 if (DEBUG_STACK) Slog.i(TAG,
2629 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002630 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002631 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002632 r.takeFromHistory();
2633 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002634 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002635 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002636 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002637 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002638 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002639 if (VALIDATE_TOKENS) {
2640 validateAppTokensLocked();
2641 }
2642 cleanUpActivityServicesLocked(r);
2643 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002644 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002645
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 /**
2647 * Perform clean-up of service connections in an activity record.
2648 */
2649 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2650 // Throw away any services that have been bound by this activity.
2651 if (r.connections != null) {
2652 Iterator<ConnectionRecord> it = r.connections.iterator();
2653 while (it.hasNext()) {
2654 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002655 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 }
2657 r.connections = null;
2658 }
2659 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002660
2661 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2662 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2663 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2664 mHandler.sendMessage(msg);
2665 }
2666
Dianne Hackborn28695e02011-11-02 21:59:51 -07002667 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002668 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002669 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002670 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2671 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2672 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2673 final ActivityRecord r = activities.get(activityNdx);
2674 if (r.finishing) {
2675 continue;
2676 }
2677 if (r.fullscreen) {
2678 lastIsOpaque = true;
2679 }
2680 if (owner != null && r.app != owner) {
2681 continue;
2682 }
2683 if (!lastIsOpaque) {
2684 continue;
2685 }
2686 // We can destroy this one if we have its icicle saved and
2687 // it is not in the process of pausing/stopping/finishing.
2688 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2689 && r.haveState && !r.visible && r.stopped
2690 && r.state != ActivityState.DESTROYING
2691 && r.state != ActivityState.DESTROYED) {
2692 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2693 + " resumed=" + mResumedActivity
2694 + " pausing=" + mPausingActivity);
2695 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2696 activityRemoved = true;
2697 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002698 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002699 }
2700 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002701 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002702 mStackSupervisor.resumeTopActivitiesLocked();
2703
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002704 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002705 }
2706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 /**
2708 * Destroy the current CLIENT SIDE instance of an activity. This may be
2709 * called both when actually finishing an activity, or when performing
2710 * a configuration switch where we destroy the current client-side object
2711 * but then create a new client-side object for this same HistoryRecord.
2712 */
2713 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002714 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002715 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002716 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002717 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2718 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002719 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002720 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721
2722 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002723
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002724 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002725
2726 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002727
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 if (hadApp) {
2729 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002730 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2732 mService.mHeavyWeightProcess = null;
2733 mService.mHandler.sendEmptyMessage(
2734 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2735 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002736 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002737 // No longer have activities, so update LRU list and oom adj.
2738 mService.updateLruProcessLocked(r.app, false, false);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002739 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002740 }
2741 }
2742
2743 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002744
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 try {
2746 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002747 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002748 r.configChangeFlags);
2749 } catch (Exception e) {
2750 // We can just ignore exceptions here... if the process
2751 // has crashed, our death notification will clean things
2752 // up.
2753 //Slog.w(TAG, "Exception thrown during finish", e);
2754 if (r.finishing) {
2755 removeActivityFromHistoryLocked(r);
2756 removedFromHistory = true;
2757 skipDestroy = true;
2758 }
2759 }
2760
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002762
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002763 // If the activity is finishing, we need to wait on removing it
2764 // from the list to give it a chance to do its cleanup. During
2765 // that time it may make calls back with its token so we need to
2766 // be able to find it on the list and so we don't want to remove
2767 // it from the list yet. Otherwise, we can just immediately put
2768 // it in the destroyed state since we are not removing it from the
2769 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002771 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2772 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002773 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002774 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002775 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2776 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002777 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002778 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002779 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002780 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002781 }
2782 } else {
2783 // remove this record from the history.
2784 if (r.finishing) {
2785 removeActivityFromHistoryLocked(r);
2786 removedFromHistory = true;
2787 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002788 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002790 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002791 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002792 }
2793 }
2794
2795 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002796
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002797 if (!mLRUActivities.remove(r) && hadApp) {
2798 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2799 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002800
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 return removedFromHistory;
2802 }
2803
Craig Mautnerd2328952013-03-05 12:46:26 -08002804 final void activityDestroyedLocked(IBinder token) {
2805 final long origId = Binder.clearCallingIdentity();
2806 try {
2807 ActivityRecord r = ActivityRecord.forToken(token);
2808 if (r != null) {
2809 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002811
2812 if (isInStackLocked(token) != null) {
2813 if (r.state == ActivityState.DESTROYING) {
2814 cleanUpActivityLocked(r, true, false);
2815 removeActivityFromHistoryLocked(r);
2816 }
2817 }
Craig Mautner05d29032013-05-03 13:40:13 -07002818 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002819 } finally {
2820 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002821 }
2822 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002823
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002824 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2825 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002827 if (DEBUG_CLEANUP) Slog.v(
2828 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002829 + " with " + i + " entries");
2830 while (i > 0) {
2831 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002832 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002833 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002835 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002836 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002837 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 }
2839 }
2840 }
2841
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002842 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2843 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002844 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2845 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002846 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2847 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002848 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002849 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002850 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2851 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002852
2853 boolean hasVisibleActivities = false;
2854
2855 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002856 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002857 if (DEBUG_CLEANUP) Slog.v(
2858 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002859 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2860 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2861 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2862 final ActivityRecord r = activities.get(activityNdx);
2863 --i;
2864 if (DEBUG_CLEANUP) Slog.v(
2865 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2866 if (r.app == app) {
2867 boolean remove;
2868 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2869 // Don't currently have state for the activity, or
2870 // it is finishing -- always remove it.
2871 remove = true;
2872 } else if (r.launchCount > 2 &&
2873 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2874 // We have launched this activity too many times since it was
2875 // able to run, so give up and remove it.
2876 remove = true;
2877 } else {
2878 // The process may be gone, but the activity lives on!
2879 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002880 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002881 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002882 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002883 RuntimeException here = new RuntimeException("here");
2884 here.fillInStackTrace();
2885 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2886 + ": haveState=" + r.haveState
2887 + " stateNotNeeded=" + r.stateNotNeeded
2888 + " finishing=" + r.finishing
2889 + " state=" + r.state, here);
2890 }
2891 if (!r.finishing) {
2892 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2893 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2894 r.userId, System.identityHashCode(r),
2895 r.task.taskId, r.shortComponentName,
2896 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002897 if (r.state == ActivityState.RESUMED) {
2898 mService.updateUsageStats(r, false);
2899 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002900 }
2901 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002902
Craig Mautner0247fc82013-02-28 14:32:06 -08002903 } else {
2904 // We have the current state for this activity, so
2905 // it can be restarted later when needed.
2906 if (localLOGV) Slog.v(
2907 TAG, "Keeping entry, setting app to null");
2908 if (r.visible) {
2909 hasVisibleActivities = true;
2910 }
2911 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2912 + r);
2913 r.app = null;
2914 r.nowVisible = false;
2915 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002916 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002917 "App died, clearing saved state of " + r);
2918 r.icicle = null;
2919 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002920 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002921
Craig Mautnerd2328952013-03-05 12:46:26 -08002922 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002923 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002924 }
2925 }
2926
2927 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002929
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002930 final void updateTransitLocked(int transit, Bundle options) {
2931 if (options != null) {
2932 ActivityRecord r = topRunningActivityLocked(null);
2933 if (r != null && r.state != ActivityState.RESUMED) {
2934 r.updateOptionsLocked(options);
2935 } else {
2936 ActivityOptions.abort(options);
2937 }
2938 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002939 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002940 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002941
Craig Mautnera82aa092013-09-13 15:34:08 -07002942 void moveHomeTaskToTop() {
2943 final int top = mTaskHistory.size() - 1;
2944 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2945 final TaskRecord task = mTaskHistory.get(taskNdx);
2946 if (task.isHomeTask()) {
2947 mTaskHistory.remove(taskNdx);
2948 mTaskHistory.add(top, task);
2949 mWindowManager.moveTaskToTop(task.taskId);
2950 return;
2951 }
2952 }
2953 }
2954
Craig Mautnercae015f2013-02-08 14:31:27 -08002955 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002956 final TaskRecord task = taskForIdLocked(taskId);
2957 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002958 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002959 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002960 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002961 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2962 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002963 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002964 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002965 }
2966 moveTaskToFrontLocked(task, null, options);
2967 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002968 }
2969 return false;
2970 }
2971
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002972 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002973 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974
Craig Mautner11bf9a52013-02-19 14:08:51 -08002975 final int numTasks = mTaskHistory.size();
2976 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002977 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002978 // nothing to do!
2979 if (reason != null &&
2980 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2981 ActivityOptions.abort(options);
2982 } else {
2983 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2984 }
2985 return;
2986 }
2987
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002988 mStackSupervisor.moveHomeStack(isHomeStack());
2989
Craig Mautner11bf9a52013-02-19 14:08:51 -08002990 // Shift all activities with this task up to the top
2991 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002992 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002993
2994 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002995 if (reason != null &&
2996 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002997 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 ActivityRecord r = topRunningActivityLocked(null);
2999 if (r != null) {
3000 mNoAnimActivities.add(r);
3001 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003002 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003004 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003005 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003006
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003007 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003008
Craig Mautner05d29032013-05-03 13:40:13 -07003009 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003010 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003011
3012 if (VALIDATE_TOKENS) {
3013 validateAppTokensLocked();
3014 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 }
3016
3017 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003018 * Worker method for rearranging history stack. Implements the function of moving all
3019 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003021 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3023 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003024 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003025 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003026 * @return Returns true if the move completed, false if not.
3027 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003028 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3029 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003030
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 // If we have a watcher, preflight the move before committing to it. First check
3032 // for *other* available tasks, but if none are available, then try again allowing the
3033 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003034 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003035 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 if (next == null) {
3037 next = topRunningActivityLocked(null, 0);
3038 }
3039 if (next != null) {
3040 // ask watcher if this is allowed
3041 boolean moveOK = true;
3042 try {
3043 moveOK = mService.mController.activityResuming(next.packageName);
3044 } catch (RemoteException e) {
3045 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003046 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 }
3048 if (!moveOK) {
3049 return false;
3050 }
3051 }
3052 }
3053
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003055 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003056
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003057 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003058 if (tr == null) {
3059 return false;
3060 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003061
Craig Mautner11bf9a52013-02-19 14:08:51 -08003062 mTaskHistory.remove(tr);
3063 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064
Craig Mautnerc8143c62013-09-03 12:15:57 -07003065 // There is an assumption that moving a task to the back moves it behind the home activity.
3066 // We make sure here that some activity in the stack will launch home.
3067 ActivityRecord lastActivity = null;
3068 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003069 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3070 final TaskRecord task = mTaskHistory.get(taskNdx);
3071 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003072 break;
3073 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003074 if (taskNdx == 1) {
3075 // Set the last task before tr to go to home.
3076 task.mOnTopOfHome = true;
3077 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003078 }
3079
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003081 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3082 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 ActivityRecord r = topRunningActivityLocked(null);
3084 if (r != null) {
3085 mNoAnimActivities.add(r);
3086 }
3087 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003088 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003090 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003091
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003092 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003093 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 }
3095
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003096 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3097 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
3098 task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003099 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003100 }
3101
Craig Mautner05d29032013-05-03 13:40:13 -07003102 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103 return true;
3104 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003105
Craig Mautner8849a5e2013-04-02 16:41:03 -07003106 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003108 final Uri data = r.intent.getData();
3109 final String strData = data != null ? data.toSafeString() : null;
3110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003112 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003113 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003114 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003115 }
3116
3117 /**
3118 * Make sure the given activity matches the current configuration. Returns
3119 * false if the activity had to be destroyed. Returns true if the
3120 * configuration is the same, or the activity will remain running as-is
3121 * for whatever reason. Ensures the HistoryRecord is updated with the
3122 * correct configuration and all other bookkeeping is handled.
3123 */
3124 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3125 int globalChanges) {
3126 if (mConfigWillChange) {
3127 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3128 "Skipping config check (will change): " + r);
3129 return true;
3130 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3133 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 // Short circuit: if the two configurations are the exact same
3136 // object (the common case), then there is nothing to do.
3137 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003138 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003139 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3140 "Configuration unchanged in " + r);
3141 return true;
3142 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144 // We don't worry about activities that are finishing.
3145 if (r.finishing) {
3146 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3147 "Configuration doesn't matter in finishing " + r);
3148 r.stopFreezingScreenLocked(false);
3149 return true;
3150 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 // Okay we now are going to make this activity have the new config.
3153 // But then we need to figure out how it needs to deal with that.
3154 Configuration oldConfig = r.configuration;
3155 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003156
3157 // Determine what has changed. May be nothing, if this is a config
3158 // that has come back from the app after going idle. In that case
3159 // we just want to leave the official config object now in the
3160 // activity and do nothing else.
3161 final int changes = oldConfig.diff(newConfig);
3162 if (changes == 0 && !r.forceNewConfig) {
3163 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3164 "Configuration no differences in " + r);
3165 return true;
3166 }
3167
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 // If the activity isn't currently running, just leave the new
3169 // configuration and it will pick that up next time it starts.
3170 if (r.app == null || r.app.thread == null) {
3171 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3172 "Configuration doesn't matter not running " + r);
3173 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003174 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 return true;
3176 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003177
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003178 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003179 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3180 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3181 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003182 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003183 + ", newConfig=" + newConfig);
3184 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003185 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003186 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3187 r.configChangeFlags |= changes;
3188 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003189 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003190 if (r.app == null || r.app.thread == null) {
3191 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003192 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003193 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003194 } else if (r.state == ActivityState.PAUSING) {
3195 // A little annoying: we are waiting for this activity to
3196 // finish pausing. Let's not do anything now, but just
3197 // flag that it needs to be restarted when done pausing.
3198 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003199 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003200 r.configDestroy = true;
3201 return true;
3202 } else if (r.state == ActivityState.RESUMED) {
3203 // Try to optimize this case: the configuration is changing
3204 // and we need to restart the top, resumed activity.
3205 // Instead of doing the normal handshaking, just say
3206 // "restart!".
3207 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003208 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003209 relaunchActivityLocked(r, r.configChangeFlags, true);
3210 r.configChangeFlags = 0;
3211 } else {
3212 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003213 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003214 relaunchActivityLocked(r, r.configChangeFlags, false);
3215 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003217
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003218 // All done... tell the caller we weren't able to keep this
3219 // activity around.
3220 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003222
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003223 // Default case: the activity can handle this new configuration, so
3224 // hand it over. Note that we don't need to give it the new
3225 // configuration, since we always send configuration changes to all
3226 // process when they happen so it can just use whatever configuration
3227 // it last got.
3228 if (r.app != null && r.app.thread != null) {
3229 try {
3230 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003231 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003232 } catch (RemoteException e) {
3233 // If process died, whatever.
3234 }
3235 }
3236 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003237
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003238 return true;
3239 }
3240
Craig Mautnerc8143c62013-09-03 12:15:57 -07003241 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 int changes, boolean andResume) {
3243 List<ResultInfo> results = null;
3244 List<Intent> newIntents = null;
3245 if (andResume) {
3246 results = r.results;
3247 newIntents = r.newIntents;
3248 }
3249 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3250 + " with results=" + results + " newIntents=" + newIntents
3251 + " andResume=" + andResume);
3252 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003253 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003254 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003257
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003259 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3260 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3261 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003262 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003263 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003264 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003265 // Note: don't need to call pauseIfSleepingLocked() here, because
3266 // the caller will only pass in 'andResume' if this activity is
3267 // currently resumed, which implies we aren't sleeping.
3268 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003269 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 }
3271
3272 if (andResume) {
3273 r.results = null;
3274 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003275 r.state = ActivityState.RESUMED;
3276 } else {
3277 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3278 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 }
3280
3281 return true;
3282 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003283
3284 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003285 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3286 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3287 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3288 final ActivityRecord r = activities.get(activityNdx);
3289 if (r.appToken == token) {
3290 return true;
3291 }
3292 if (r.fullscreen && !r.finishing) {
3293 return false;
3294 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003295 }
3296 }
3297 return true;
3298 }
3299
3300 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003301 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3302 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3303 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3304 final ActivityRecord r = activities.get(activityNdx);
3305 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003306 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003307 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003308 }
3309 }
3310 }
3311
3312 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3313 boolean didSomething = false;
3314 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003315 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3316 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3317 int numActivities = activities.size();
3318 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3319 ActivityRecord r = activities.get(activityNdx);
3320 final boolean samePackage = r.packageName.equals(name)
3321 || (name == null && r.userId == userId);
3322 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3323 && (samePackage || r.task == lastTask)
3324 && (r.app == null || evenPersistent || !r.app.persistent)) {
3325 if (!doit) {
3326 if (r.finishing) {
3327 // If this activity is just finishing, then it is not
3328 // interesting as far as something to stop.
3329 continue;
3330 }
3331 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003332 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003333 didSomething = true;
3334 Slog.i(TAG, " Force finishing activity " + r);
3335 if (samePackage) {
3336 if (r.app != null) {
3337 r.app.removed = true;
3338 }
3339 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003340 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003341 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003342 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3343 true)) {
3344 // r has been deleted from mActivities, accommodate.
3345 --numActivities;
3346 --activityNdx;
3347 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003348 }
3349 }
3350 }
3351 return didSomething;
3352 }
3353
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003354 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003355 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003356 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003357 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003358 final TaskRecord task = mTaskHistory.get(taskNdx);
3359 ActivityRecord r = null;
3360 ActivityRecord top = null;
3361 int numActivities = 0;
3362 int numRunning = 0;
3363 final ArrayList<ActivityRecord> activities = task.mActivities;
3364 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3365 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003366
Craig Mautneraab647e2013-02-28 16:31:36 -08003367 // Initialize state for next task if needed.
3368 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3369 top = r;
3370 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003371 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003372
3373 // Add 'r' into the current task.
3374 numActivities++;
3375 if (r.app != null && r.app.thread != null) {
3376 numRunning++;
3377 }
3378
3379 if (localLOGV) Slog.v(
3380 TAG, r.intent.getComponent().flattenToShortString()
3381 + ": task=" + r.task);
3382 }
3383
3384 RunningTaskInfo ci = new RunningTaskInfo();
3385 ci.id = task.taskId;
3386 ci.baseActivity = r.intent.getComponent();
3387 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003388 ci.lastActiveTime = task.lastActiveTime;
3389
Craig Mautneraab647e2013-02-28 16:31:36 -08003390 if (top.thumbHolder != null) {
3391 ci.description = top.thumbHolder.lastDescription;
3392 }
3393 ci.numActivities = numActivities;
3394 ci.numRunning = numRunning;
3395 //System.out.println(
3396 // "#" + maxNum + ": " + " descr=" + ci.description);
3397 if (receiver != null) {
3398 if (localLOGV) Slog.v(
3399 TAG, "State=" + top.state + "Idle=" + top.idle
3400 + " app=" + top.app
3401 + " thr=" + (top.app != null ? top.app.thread : null));
3402 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3403 if (top.idle && top.app != null && top.app.thread != null) {
3404 topRecord = top;
3405 } else {
3406 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003407 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003408 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003409 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003410 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003411 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003412 }
3413 return topRecord;
3414 }
3415
3416 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003417 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003418 if (DEBUG_SWITCH) Slog.d(
3419 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003420 if (top >= 0) {
3421 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3422 int activityTop = activities.size() - 1;
3423 if (activityTop > 0) {
3424 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3425 "unhandled-back", true);
3426 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003427 }
3428 }
3429
Craig Mautner6b74cb52013-09-27 17:02:21 -07003430 /**
3431 * Reset local parameters because an app's activity died.
3432 * @param app The app of the activity that died.
3433 * @return true if home should be launched next.
3434 */
3435 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003436 if (!containsApp(app)) {
Craig Mautner6b74cb52013-09-27 17:02:21 -07003437 return false;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003438 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07003439
Craig Mautnere79d42682013-04-01 19:01:53 -07003440 if (mPausingActivity != null && mPausingActivity.app == app) {
3441 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3442 "App died while pausing: " + mPausingActivity);
3443 mPausingActivity = null;
3444 }
3445 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3446 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003447 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003448 }
3449
Craig Mautner6b74cb52013-09-27 17:02:21 -07003450 // Determine if the top task is exiting and should return to home. Do this before it gets
3451 // removed in removeHistoryRecordsForAppsLocked.
3452 boolean launchHomeNext = false;
Craig Mautner51277a82013-10-01 14:28:23 -07003453 TaskRecord topTask = mTaskHistory.get(mTaskHistory.size() - 1);
3454 ArrayList<ActivityRecord> activities = topTask.mActivities;
3455 int activityNdx;
3456 for (activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3457 ActivityRecord r = activities.get(activityNdx);
3458 if (r.finishing) {
Craig Mautner6b74cb52013-09-27 17:02:21 -07003459 continue;
Craig Mautnere79d42682013-04-01 19:01:53 -07003460 }
Craig Mautner51277a82013-10-01 14:28:23 -07003461 if (r.app != app) {
3462 // This is the dying activity.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003463 break;
3464 }
Craig Mautner51277a82013-10-01 14:28:23 -07003465 }
3466 if (activityNdx < 0) {
3467 // All activities in task belong to app. Set launchHomeNext to task's value.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003468 launchHomeNext = topTask.mOnTopOfHome;
Craig Mautnere79d42682013-04-01 19:01:53 -07003469 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07003470
3471 removeHistoryRecordsForAppLocked(app);
3472
3473 return launchHomeNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003474 }
3475
Craig Mautnercae015f2013-02-08 14:31:27 -08003476 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003477 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3478 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3479 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3480 final ActivityRecord r = activities.get(activityNdx);
3481 if (r.app == app) {
3482 Slog.w(TAG, " Force finishing activity "
3483 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003484 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003485 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003486 }
3487 }
3488 }
3489
Dianne Hackborn390517b2013-05-30 15:03:32 -07003490 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003491 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003492 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003493 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3494 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003495 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3496 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003497 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003498 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003499 if (printed) {
3500 header = null;
3501 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003502 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003503 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003504 }
3505
3506 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3507 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3508
3509 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003510 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3511 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003512 }
3513 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003514 final int top = mTaskHistory.size() - 1;
3515 if (top >= 0) {
3516 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3517 int listTop = list.size() - 1;
3518 if (listTop >= 0) {
3519 activities.add(list.get(listTop));
3520 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003521 }
3522 } else {
3523 ItemMatcher matcher = new ItemMatcher();
3524 matcher.build(name);
3525
Craig Mautneraab647e2013-02-28 16:31:36 -08003526 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3527 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3528 if (matcher.match(r1, r1.intent.getComponent())) {
3529 activities.add(r1);
3530 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003531 }
3532 }
3533 }
3534
3535 return activities;
3536 }
3537
3538 ActivityRecord restartPackage(String packageName) {
3539 ActivityRecord starting = topRunningActivityLocked(null);
3540
3541 // All activities that came from the package must be
3542 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003543 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3544 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3545 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3546 final ActivityRecord a = activities.get(activityNdx);
3547 if (a.info.packageName.equals(packageName)) {
3548 a.forceNewConfig = true;
3549 if (starting != null && a == starting && a.visible) {
3550 a.startFreezingScreenLocked(starting.app,
3551 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3552 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003553 }
3554 }
3555 }
3556
3557 return starting;
3558 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003559
Craig Mautnerde4ef022013-04-07 19:01:33 -07003560 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003561 final int taskNdx = mTaskHistory.indexOf(task);
3562 final int topTaskNdx = mTaskHistory.size() - 1;
3563 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3564 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3565 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003566 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003567 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003568 }
3569
Craig Mautnerde4ef022013-04-07 19:01:33 -07003570 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003571 TaskRecord task = new TaskRecord(taskId, info, intent);
3572 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003573 return task;
3574 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003575
3576 ArrayList<TaskRecord> getAllTasks() {
3577 return new ArrayList<TaskRecord>(mTaskHistory);
3578 }
3579
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003580 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003581 task.stack = this;
3582 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003583 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003584 } else {
3585 mTaskHistory.add(0, task);
3586 }
3587 }
3588
3589 public int getStackId() {
3590 return mStackId;
3591 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003592
3593 @Override
3594 public String toString() {
3595 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3596 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003597}