blob: 675823a0688c48763ad7ba2634e7e54257930258 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautnerde4ef022013-04-07 19:01:33 -070019import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
20
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070021import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080022import com.android.internal.util.Objects;
Craig Mautnercae015f2013-02-08 14:31:27 -080023import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080024import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080025import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070026import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070027
28import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070029import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070030import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070031import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080032import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080033import android.app.IThumbnailReceiver;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070034import android.app.IThumbnailRetriever;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070035import android.app.IApplicationThread;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070036import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080037import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import android.content.ComponentName;
39import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070043import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080044import android.content.res.Resources;
45import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080046import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080047import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070049import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.os.Handler;
51import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090052import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.os.Message;
54import android.os.PowerManager;
55import android.os.RemoteException;
56import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070057import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070060import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061
Craig Mautnercae015f2013-02-08 14:31:27 -080062import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080063import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import java.lang.ref.WeakReference;
65import java.util.ArrayList;
66import java.util.Iterator;
67import java.util.List;
68
69/**
70 * State and management of a single stack of activities.
71 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070072final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070074 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
76 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
Craig Mautner29219d92013-04-16 20:19:12 -070077 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
79 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
80 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
81 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
82 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070083 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautnera9a3fb12013-04-18 10:01:00 -070084 static final boolean DEBUG_STACK = ActivityManagerService.DEBUG_STACK;
Craig Mautner9658b312013-02-28 10:55:59 -080085
Craig Mautner2420ead2013-04-01 17:13:20 -070086 static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES;
87 static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE;
88 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
89 static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070090
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070091 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080092
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070093 // Ticks during which we check progress while waiting for an app to launch.
94 static final int LAUNCH_TICK = 500;
95
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096 // How long we wait until giving up on the last activity to pause. This
97 // is short because it directly impacts the responsiveness of starting the
98 // next activity.
99 static final int PAUSE_TIMEOUT = 500;
100
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700101 // How long we wait for the activity to tell us it has stopped before
102 // giving up. This is a good amount of time because we really need this
103 // from the application in order to get its saved state.
104 static final int STOP_TIMEOUT = 10*1000;
105
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800106 // How long we can hold the sleep wake lock before giving up.
107 static final int SLEEP_TIMEOUT = 5*1000;
108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109 // How long we can hold the launch wake lock before giving up.
110 static final int LAUNCH_TIMEOUT = 10*1000;
111
112 // How long we wait until giving up on an activity telling us it has
113 // finished destroying itself.
114 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800117 // disabled.
118 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700120 // How long between activity launches that we consider safe to not warn
121 // the user about an unexpected activity being launched on top.
122 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124 // Set to false to disable the preview that is shown while a new activity
125 // is being started.
126 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 enum ActivityState {
129 INITIALIZING,
130 RESUMED,
131 PAUSING,
132 PAUSED,
133 STOPPING,
134 STOPPED,
135 FINISHING,
136 DESTROYING,
137 DESTROYED
138 }
139
140 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700141 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700145 /**
146 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800147 * running) activities. It contains #TaskRecord objects.
148 */
149 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
150
151 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152 * Used for validating app tokens with window manager.
153 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800154 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700156 /**
157 * List of running activities, sorted by recent usage.
158 * The first entry in the list is the least recently used.
159 * It contains HistoryRecord objects.
160 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700161 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162
163 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800164 * List of activities that are in the process of going to sleep.
165 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700166 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * Animations that for the current transition have requested not to
170 * be considered for the transition animation.
171 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 * Set when the system is going to sleep, until we have
176 * successfully paused the current activity and released our wake lock.
177 * At that point the system is allowed to actually sleep.
178 */
179 final PowerManager.WakeLock mGoingToSleep;
180
181 /**
182 * We don't want to allow the device to go to sleep while in the process
183 * of launching an activity. This is primarily to allow alarm intent
184 * receivers to launch an activity and get that to run before the device
185 * goes back to sleep.
186 */
187 final PowerManager.WakeLock mLaunchingActivity;
188
189 /**
190 * When we are in the process of pausing an activity, before starting the
191 * next one, this variable holds the activity that is currently being paused.
192 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800193 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194
195 /**
196 * This is the last activity that we put into the paused state. This is
197 * used to determine if we need to do an activity transition while sleeping,
198 * when we normally hold the top activity paused.
199 */
200 ActivityRecord mLastPausedActivity = null;
201
202 /**
203 * Current activity that is resumed, or null if there is none.
204 */
205 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800206
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700207 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700208 * This is the last activity that has been started. It is only used to
209 * identify when multiple activities are started at once so that the user
210 * can be warned they may not be in the activity they think they are.
211 */
212 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800213
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700215 * Set when we know we are going to be calling updateConfiguration()
216 * soon, so want to skip intermediate config checks.
217 */
218 boolean mConfigWillChange;
219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700220 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800221
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800222 /**
223 * Set when we have taken too long waiting to go to sleep.
224 */
225 boolean mSleepTimeout = false;
226
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700227 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800228 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
229 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
230 */
231 private ActivityRecord mLastScreenshotActivity = null;
232 private Bitmap mLastScreenshotBitmap = null;
233
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800234 int mThumbnailWidth = -1;
235 int mThumbnailHeight = -1;
236
Craig Mautner858d8a62013-04-23 17:08:34 -0700237 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700238
Craig Mautnerc00204b2013-03-05 15:02:14 -0800239 final int mStackId;
240
Craig Mautner27084302013-03-25 08:05:25 -0700241 /** Run all ActivityStacks through this */
242 final ActivityStackSupervisor mStackSupervisor;
243
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800244 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
245 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautnerf3333272013-04-22 10:55:53 -0700246 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
247 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
248 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
249 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
250 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
251 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700252
253 static class ScheduleDestroyArgs {
254 final ProcessRecord mOwner;
255 final boolean mOomAdj;
256 final String mReason;
257 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
258 mOwner = owner;
259 mOomAdj = oomAdj;
260 mReason = reason;
261 }
262 }
263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 final Handler mHandler;
265
266 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267 //public Handler() {
268 // if (localLOGV) Slog.v(TAG, "Handler started!");
269 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900270 public ActivityStackHandler(Looper looper) {
271 super(looper);
272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 public void handleMessage(Message msg) {
276 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800277 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700278 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700279 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700280 Slog.w(TAG, "Sleep timeout! Sleeping now.");
281 mSleepTimeout = true;
282 checkReadyForSleepLocked();
283 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800284 }
285 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800287 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 // We don't at this point know if the activity is fullscreen,
289 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700291 synchronized (mService) {
292 if (r.app != null) {
293 mService.logAppTooSlow(r.app, r.pauseTime,
294 "pausing " + r);
295 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700296
Craig Mautnerd2328952013-03-05 12:46:26 -0800297 activityPausedLocked(r != null ? r.appToken : null, true);
298 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700299 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700300 case LAUNCH_TICK_MSG: {
301 ActivityRecord r = (ActivityRecord)msg.obj;
302 synchronized (mService) {
303 if (r.continueLaunchTickingLocked()) {
304 mService.logAppTooSlow(r.app, r.launchTickTime,
305 "launching " + r);
306 }
307 }
308 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700311 // We don't at this point know if the activity is fullscreen,
312 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800313 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800314 synchronized (mService) {
315 activityDestroyedLocked(r != null ? r.appToken : null);
316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 case LAUNCH_TIMEOUT_MSG: {
319 if (mService.mDidDexOpt) {
320 mService.mDidDexOpt = false;
321 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
322 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
323 return;
324 }
325 synchronized (mService) {
326 if (mLaunchingActivity.isHeld()) {
327 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
328 mLaunchingActivity.release();
329 }
330 }
331 } break;
332 case RESUME_TOP_ACTIVITY_MSG: {
333 synchronized (mService) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700334 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 }
336 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700337 case STOP_TIMEOUT_MSG: {
338 ActivityRecord r = (ActivityRecord)msg.obj;
339 // We don't at this point know if the activity is fullscreen,
340 // so we need to be conservative and assume it isn't.
341 Slog.w(TAG, "Activity stop timeout for " + r);
342 synchronized (mService) {
343 if (r.isInHistory()) {
344 activityStoppedLocked(r, null, null, null);
345 }
346 }
347 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700348 case DESTROY_ACTIVITIES_MSG: {
349 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
350 synchronized (mService) {
351 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
352 }
353 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700354 }
355 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800356 }
357
Craig Mautner000f0022013-02-26 15:04:29 -0800358 private int numActivities() {
359 int count = 0;
360 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
361 count += mTaskHistory.get(taskNdx).mActivities.size();
362 }
363 return count;
364 }
365
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700366 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900367 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700369 mWindowManager = service.mWindowManager;
370 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700371 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 PowerManager pm =
373 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
374 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700375 mLaunchingActivity =
376 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700377 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800378 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700379 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 }
Craig Mautner5962b122012-10-05 14:45:52 -0700381
382 private boolean okToShow(ActivityRecord r) {
383 return r.userId == mCurrentUser
384 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
385 }
386
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800388 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
389 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800390 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800391 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
392 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800393 if (!r.finishing && r != notTop && okToShow(r)) {
394 return r;
395 }
396 }
397 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700398 return null;
399 }
400
401 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800402 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
403 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800404 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800405 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
406 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800407 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
408 return r;
409 }
410 }
411 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 return null;
413 }
414
415 /**
416 * This is a simplified version of topRunningActivityLocked that provides a number of
417 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800418 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700419 * @param token If non-null, any history records matching this token will be skipped.
420 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800421 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700422 * @return Returns the HistoryRecord of the next activity on the stack.
423 */
424 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800425 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
426 TaskRecord task = mTaskHistory.get(taskNdx);
427 if (task.taskId == taskId) {
428 continue;
429 }
430 ArrayList<ActivityRecord> activities = task.mActivities;
431 for (int i = activities.size() - 1; i >= 0; --i) {
432 final ActivityRecord r = activities.get(i);
433 // Note: the taskId check depends on real taskId fields being non-zero
434 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
435 return r;
436 }
437 }
438 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700439 return null;
440 }
441
Craig Mautner8849a5e2013-04-02 16:41:03 -0700442 final ActivityRecord topActivity() {
443 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700444 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700448 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700449 }
450 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700451 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700452 }
453
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 TaskRecord taskForIdLocked(int id) {
455 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
456 final TaskRecord task = mTaskHistory.get(taskNdx);
457 if (task.taskId == id) {
458 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800459 }
460 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700461 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 }
463
Craig Mautnerd2328952013-03-05 12:46:26 -0800464 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800465 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800466 if (r != null) {
467 final TaskRecord task = r.task;
468 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
469 if (task.stack != this) Slog.w(TAG,
470 "Illegal state! task does not point to stack it is in.");
471 return r;
472 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800473 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800474 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800475 }
476
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700477 boolean containsApp(ProcessRecord app) {
478 if (app == null) {
479 return false;
480 }
481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
483 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
484 final ActivityRecord r = activities.get(activityNdx);
485 if (r.finishing) {
486 continue;
487 }
488 if (r.app == app) {
489 return true;
490 }
491 }
492 }
493 return false;
494 }
495
Craig Mautner2420ead2013-04-01 17:13:20 -0700496 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 final boolean hadit = mLRUActivities.remove(r);
498 mLRUActivities.add(r);
499 return hadit;
500 }
501
Craig Mautnerde4ef022013-04-07 19:01:33 -0700502 final boolean isHomeStack() {
503 return mStackId == HOME_STACK_ID;
504 }
505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700506 /**
507 * Returns the top activity in any existing task matching the given
508 * Intent. Returns null if no such task is found.
509 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700510 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700511 ComponentName cls = intent.getComponent();
512 if (info.targetActivity != null) {
513 cls = new ComponentName(info.packageName, info.targetActivity);
514 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700515 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800516
517 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
518 final TaskRecord task = mTaskHistory.get(taskNdx);
519 final ActivityRecord r = task.getTopActivity();
520 if (r == null || r.finishing || r.userId != userId ||
521 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
522 continue;
523 }
524
525 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
526 // + "/aff=" + r.task.affinity + " to new cls="
527 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
528 if (task.affinity != null) {
529 if (task.affinity.equals(info.taskAffinity)) {
530 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700531 return r;
532 }
Craig Mautner000f0022013-02-26 15:04:29 -0800533 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
534 //Slog.i(TAG, "Found matching class!");
535 //dump();
536 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
537 return r;
538 } else if (task.affinityIntent != null
539 && task.affinityIntent.getComponent().equals(cls)) {
540 //Slog.i(TAG, "Found matching class!");
541 //dump();
542 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
543 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700544 }
545 }
546
547 return null;
548 }
549
550 /**
551 * Returns the first activity (starting from the top of the stack) that
552 * is the same as the given activity. Returns null if no such activity
553 * is found.
554 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700555 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700556 ComponentName cls = intent.getComponent();
557 if (info.targetActivity != null) {
558 cls = new ComponentName(info.packageName, info.targetActivity);
559 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700560 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700561
Craig Mautner000f0022013-02-26 15:04:29 -0800562 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
563 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
564 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
565 ActivityRecord r = activities.get(activityNdx);
566 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700567 //Slog.i(TAG, "Found matching class!");
568 //dump();
569 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
570 return r;
571 }
572 }
573 }
574
575 return null;
576 }
577
Amith Yamasani742a6712011-05-04 14:49:28 -0700578 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700579 * Move the activities around in the stack to bring a user to the foreground. This only
580 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700581 * @return whether there are any activities for the specified user.
582 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700583 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800584 if (VALIDATE_TOKENS) {
585 validateAppTokensLocked();
586 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800587 if (mCurrentUser == userId) {
588 return true;
589 }
590 mCurrentUser = userId;
591
592 // Move userId's tasks to the top.
593 boolean haveActivities = false;
594 TaskRecord task = null;
595 int index = mTaskHistory.size();
596 for (int i = 0; i < index; ++i) {
597 task = mTaskHistory.get(i);
598 if (task.userId == userId) {
599 haveActivities = true;
600 mTaskHistory.remove(i);
601 mTaskHistory.add(task);
602 --index;
603 }
604 }
605
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700606 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700607 }
608
Craig Mautner2420ead2013-04-01 17:13:20 -0700609 void minimalResumeActivityLocked(ActivityRecord r) {
610 r.state = ActivityState.RESUMED;
611 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
612 + " (starting new instance)");
613 r.stopped = false;
614 mResumedActivity = r;
615 r.task.touchActiveTime();
616 mService.addRecentTaskLocked(r.task);
617 completeResumeLocked(r);
618 checkReadyForSleepLocked();
619 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 }
621
Craig Mautnere79d42682013-04-01 19:01:53 -0700622 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700623 if (r.launchTime == 0) {
624 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700625 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700626 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700627 }
628 } else if (mInitialStartTime == 0) {
629 mInitialStartTime = SystemClock.uptimeMillis();
630 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800632
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800633 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700634 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700635 if (!mGoingToSleep.isHeld()) {
636 mGoingToSleep.acquire();
637 if (mLaunchingActivity.isHeld()) {
638 mLaunchingActivity.release();
639 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
640 }
641 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800642 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
643 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
644 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
645 checkReadyForSleepLocked();
646 }
647 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700648
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800649 void awakeFromSleepingLocked() {
650 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
651 mSleepTimeout = false;
652 if (mGoingToSleep.isHeld()) {
653 mGoingToSleep.release();
654 }
655 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
657 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
658 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700659 // TODO: Skip if finishing?
Craig Mautnerd44711d2013-02-23 11:24:36 -0800660 activities.get(activityNdx).setSleeping(false);
661 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800662 }
663 mGoingToSleepActivities.clear();
664 }
665
666 void activitySleptLocked(ActivityRecord r) {
667 mGoingToSleepActivities.remove(r);
668 checkReadyForSleepLocked();
669 }
670
Craig Mautnere79d42682013-04-01 19:01:53 -0700671 // Checked.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800672 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700673 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800674 // Do not care.
675 return;
676 }
677
678 if (!mSleepTimeout) {
679 if (mResumedActivity != null) {
680 // Still have something resumed; can't sleep until it is paused.
681 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
683 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800684 return;
685 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800686 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800687 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800688 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800689 return;
690 }
691
Craig Mautnerde4ef022013-04-07 19:01:33 -0700692 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800693 // Still need to tell some activities to stop; can't sleep yet.
694 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700695 + mStackSupervisor.mStoppingActivities.size() + " activities");
Craig Mautnerf3333272013-04-22 10:55:53 -0700696 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800697 return;
698 }
699
700 ensureActivitiesVisibleLocked(null, 0);
701
702 // Make sure any stopped but visible activities are now sleeping.
703 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800704 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
705 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
706 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
707 final ActivityRecord r = activities.get(activityNdx);
708 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
709 r.setSleeping(true);
710 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800711 }
712 }
713
714 if (mGoingToSleepActivities.size() > 0) {
715 // Still need to tell some activities to sleep; can't sleep yet.
716 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
717 + mGoingToSleepActivities.size() + " activities");
718 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700719 }
720 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800721
722 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
723
724 if (mGoingToSleep.isHeld()) {
725 mGoingToSleep.release();
726 }
727 if (mService.mShuttingDown) {
728 mService.notifyAll();
729 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700730 }
Craig Mautner59c00972012-07-30 12:10:24 -0700731
Dianne Hackbornd2835932010-12-13 16:28:46 -0800732 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800733 if (who.noDisplay) {
734 return null;
735 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800736
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800737 Resources res = mService.mContext.getResources();
738 int w = mThumbnailWidth;
739 int h = mThumbnailHeight;
740 if (w < 0) {
741 mThumbnailWidth = w =
742 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
743 mThumbnailHeight = h =
744 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
745 }
746
747 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800748 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
749 || mLastScreenshotBitmap.getWidth() != w
750 || mLastScreenshotBitmap.getHeight() != h) {
751 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700752 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800753 who.appToken, Display.DEFAULT_DISPLAY, w, h);
754 }
755 if (mLastScreenshotBitmap != null) {
756 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
757 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800758 }
759 return null;
760 }
761
Craig Mautnercf910b02013-04-23 11:23:27 -0700762 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800763 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800764 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700765 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800766 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700767 ActivityRecord prev = mResumedActivity;
768 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700769 Slog.e(TAG, "Trying to pause when nothing is resumed",
770 new RuntimeException("here").fillInStackTrace());
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700771 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700772 return;
773 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700774 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
775 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800777 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700778 mLastPausedActivity = prev;
779 prev.state = ActivityState.PAUSING;
780 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700781 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782
783 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800784
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700785 if (prev.app != null && prev.app.thread != null) {
786 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
787 try {
788 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700789 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700790 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800791 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
792 userLeaving, prev.configChangeFlags);
Craig Mautner7d9eaa42013-04-19 13:57:33 -0700793 mService.updateUsageStats(prev, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700794 } catch (Exception e) {
795 // Ignore exception, if process died other code will cleanup.
796 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800797 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 mLastPausedActivity = null;
799 }
800 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800801 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700802 mLastPausedActivity = null;
803 }
804
805 // If we are not going to sleep, we want to ensure the device is
806 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700807 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 mLaunchingActivity.acquire();
809 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
810 // To be safe, don't allow the wake lock to be held for too long.
811 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
812 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
813 }
814 }
815
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800816 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 // Have the window manager pause its key dispatching until the new
818 // activity has started. If we're pausing the activity just because
819 // the screen is being turned off and the UI is sleeping, don't interrupt
820 // key dispatch; the same activity will pick it up again on wakeup.
821 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800822 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 } else {
824 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
825 }
826
827 // Schedule a pause timeout in case the app doesn't respond.
828 // We don't give it much time because this directly impacts the
829 // responsiveness seen by the user.
830 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
831 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700832 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700833 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
834 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
835 } else {
836 // This activity failed to schedule the
837 // pause, so just treat it as being paused now.
838 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700839 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 }
841 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700842
Craig Mautnerd2328952013-03-05 12:46:26 -0800843 final void activityResumedLocked(IBinder token) {
844 final ActivityRecord r = ActivityRecord.forToken(token);
845 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
846 r.icicle = null;
847 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700848 }
849
Craig Mautnerd2328952013-03-05 12:46:26 -0800850 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800852 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853
Craig Mautnerd2328952013-03-05 12:46:26 -0800854 final ActivityRecord r = isInStackLocked(token);
855 if (r != null) {
856 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
857 if (mPausingActivity == r) {
858 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
859 + (timeout ? " (due to timeout)" : " (pause complete)"));
860 r.state = ActivityState.PAUSED;
861 completePauseLocked();
862 } else {
863 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
864 r.userId, System.identityHashCode(r), r.shortComponentName,
865 mPausingActivity != null
866 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700867 }
868 }
869 }
870
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700871 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
872 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700873 if (r.state != ActivityState.STOPPING) {
874 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
875 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
876 return;
877 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700878 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700879 if (icicle != null) {
880 // If icicle is null, this is happening due to a timeout, so we
881 // haven't really saved the state.
882 r.icicle = icicle;
883 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800884 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700885 r.updateThumbnail(thumbnail, description);
886 }
887 if (!r.stopped) {
888 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
889 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
890 r.stopped = true;
891 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700892 if (r.finishing) {
893 r.clearOptionsLocked();
894 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700895 if (r.configDestroy) {
896 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700897 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700898 } else {
899 // Now that this process has stopped, we may want to consider
900 // it to be the previous app to try to keep around in case
901 // the user wants to return to it.
902 ProcessRecord fgApp = null;
903 if (mResumedActivity != null) {
904 fgApp = mResumedActivity.app;
905 } else if (mPausingActivity != null) {
906 fgApp = mPausingActivity.app;
907 }
908 if (r.app != null && fgApp != null && r.app != fgApp
909 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
910 && r.app != mService.mHomeProcess) {
911 mService.mPreviousProcess = r.app;
912 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
913 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800914 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700915 }
916 }
917 }
918
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800919 private final void completePauseLocked() {
920 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800922
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800923 if (prev != null) {
924 if (prev.finishing) {
925 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700926 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800927 } else if (prev.app != null) {
928 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
929 if (prev.waitingVisible) {
930 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700931 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800932 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
933 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800934 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800935 if (prev.configDestroy) {
936 // The previous is being paused because the configuration
937 // is changing, which means it is actually stopping...
938 // To juggle the fact that we are also starting a new
939 // instance right now, we need to first completely stop
940 // the current instance before starting the new one.
941 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
942 destroyActivityLocked(prev, true, false, "pause-config");
943 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700944 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700945 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
946 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800947 // If we already have a few activities waiting to stop,
948 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700949 // them out. Or if r is the last of activity of the last task the stack
950 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700952 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800953 } else {
954 checkReadyForSleepLocked();
955 }
956 }
957 } else {
958 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
959 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800960 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800961 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700962 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800963
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700964 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700965 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700966 topStack.resumeTopActivityLocked(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800967 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800968 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700970 if (top == null || (prev != null && top != prev)) {
971 // If there are no more activities available to run,
972 // do resume anyway to start something. Also if the top
973 // activity on the stack is not the just paused activity,
974 // we need to go ahead and resume it to ensure we complete
975 // an in-flight app switch.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700976 topStack.resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700977 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700978 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800979
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800980 if (prev != null) {
981 prev.resumeKeyDispatchingLocked();
982 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983
984 if (prev.app != null && prev.cpuTimeAtResume > 0
985 && mService.mBatteryStatsService.isOnBattery()) {
986 long diff = 0;
987 synchronized (mService.mProcessStatsThread) {
988 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
989 - prev.cpuTimeAtResume;
990 }
991 if (diff > 0) {
992 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
993 synchronized (bsi) {
994 BatteryStatsImpl.Uid.Proc ps =
995 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
996 prev.info.packageName);
997 if (ps != null) {
998 ps.addForegroundTimeLocked(diff);
999 }
1000 }
1001 }
1002 }
1003 prev.cpuTimeAtResume = 0; // reset it
1004 }
1005
Craig Mautnere79d42682013-04-01 19:01:53 -07001006 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001007 /**
1008 * Once we know that we have asked an application to put an activity in
1009 * the resumed state (either by launching it or explicitly telling it),
1010 * this function updates the rest of our state to match that fact.
1011 */
1012 private final void completeResumeLocked(ActivityRecord next) {
1013 next.idle = false;
1014 next.results = null;
1015 next.newIntents = null;
1016
1017 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001018 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001019
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001020 mStackSupervisor.reportResumedActivityLocked(next);
1021
1022 next.resumeKeyDispatchingLocked();
1023 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024
1025 // Mark the point when the activity is resuming
1026 // TODO: To be more accurate, the mark should be before the onCreate,
1027 // not after the onResume. But for subsequent starts, onResume is fine.
1028 if (next.app != null) {
1029 synchronized (mService.mProcessStatsThread) {
1030 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1031 }
1032 } else {
1033 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1034 }
1035 }
1036
Craig Mautnere79d42682013-04-01 19:01:53 -07001037 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001038 /**
1039 * Make sure that all activities that need to be visible (that is, they
1040 * currently can be seen by the user) actually are.
1041 */
1042 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1043 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1044 if (DEBUG_VISBILITY) Slog.v(
1045 TAG, "ensureActivitiesVisible behind " + top
1046 + " configChanges=0x" + Integer.toHexString(configChanges));
1047
1048 // If the top activity is not fullscreen, then we need to
1049 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001050 boolean aboveTop = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001051 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this);
1052 int taskNdx;
1053 for (taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001054 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1055 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1056 final ActivityRecord r = activities.get(activityNdx);
1057 if (r.finishing) {
1058 continue;
1059 }
1060 if (aboveTop && r != top) {
1061 continue;
1062 }
1063 aboveTop = false;
1064 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001066 TAG, "Make visible? " + r + " finishing=" + r.finishing
1067 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001068
Craig Mautnerd44711d2013-02-23 11:24:36 -08001069 final boolean doThisProcess = onlyThisProcess == null
1070 || onlyThisProcess.equals(r.processName);
1071
1072 // First: if this is not the current activity being started, make
1073 // sure it matches the current configuration.
1074 if (r != starting && doThisProcess) {
1075 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001076 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001077
1078 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001079 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001080 // This activity needs to be visible, but isn't even
1081 // running... get it started, but don't resume it
1082 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001083 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001084 if (r != starting) {
1085 r.startFreezingScreenLocked(r.app, configChanges);
1086 }
1087 if (!r.visible) {
1088 if (DEBUG_VISBILITY) Slog.v(
1089 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001090 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001091 }
1092 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001093 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001094 }
1095 }
1096
1097 } else if (r.visible) {
1098 // If this activity is already visible, then there is nothing
1099 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001100 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001101 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001102
1103 } else if (onlyThisProcess == null) {
1104 // This activity is not currently visible, but is running.
1105 // Tell it to become visible.
1106 r.visible = true;
1107 if (r.state != ActivityState.RESUMED && r != starting) {
1108 // If this activity is paused, tell it
1109 // to now show its window.
1110 if (DEBUG_VISBILITY) Slog.v(
1111 TAG, "Making visible and scheduling visibility: " + r);
1112 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001113 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001114 r.sleeping = false;
1115 r.app.pendingUiClean = true;
1116 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1117 r.stopFreezingScreenLocked(false);
1118 } catch (Exception e) {
1119 // Just skip on any failure; we'll make it
1120 // visible when it next restarts.
1121 Slog.w(TAG, "Exception thrown making visibile: "
1122 + r.intent.getComponent(), e);
1123 }
1124 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001125 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126
Craig Mautnerd44711d2013-02-23 11:24:36 -08001127 // Aggregate current change flags.
1128 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001129
Craig Mautnerd44711d2013-02-23 11:24:36 -08001130 if (r.fullscreen) {
1131 // At this point, nothing else needs to be shown
1132 if (DEBUG_VISBILITY) Slog.v(
1133 TAG, "Stopping: fullscreen at " + r);
1134 behindFullscreen = true;
1135 }
1136 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001137 if (DEBUG_VISBILITY) Slog.v(
1138 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1139 + " state=" + r.state
1140 + " behindFullscreen=" + behindFullscreen);
1141 // Now for any activities that aren't visible to the user, make
1142 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001143 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001144 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145 r.visible = false;
1146 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001147 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001148 if ((r.state == ActivityState.STOPPING
1149 || r.state == ActivityState.STOPPED)
1150 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001151 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001152 r.app.thread.scheduleWindowVisibility(r.appToken, false);
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 }
1166 }
1167
Craig Mautnere79d42682013-04-01 19:01:53 -07001168 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001169 /**
1170 * Version of ensureActivitiesVisible that can easily be called anywhere.
1171 */
1172 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1173 int configChanges) {
1174 ActivityRecord r = topRunningActivityLocked(null);
1175 if (r != null) {
1176 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1177 }
1178 }
Craig Mautner58547802013-03-05 08:23:53 -08001179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180 /**
1181 * Ensure that the top activity in the stack is resumed.
1182 *
1183 * @param prev The previously resumed activity, for when in the process
1184 * of pausing; can be null to call from elsewhere.
1185 *
1186 * @return Returns true if something is being resumed, or false if
1187 * nothing happened.
1188 */
1189 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001190 return resumeTopActivityLocked(prev, null);
1191 }
1192
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07001193 // Checked.
Dianne Hackborn84375872012-06-01 19:03:50 -07001194 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195 // Find the first activity that is not finishing.
1196 ActivityRecord next = topRunningActivityLocked(null);
1197
1198 // Remember how we'll process this pause/resume situation, and ensure
1199 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001200 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1201 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001202
1203 if (next == null) {
1204 // There are no more activities! Let's just start up the
1205 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001206 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001207 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001208 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001209 }
1210
1211 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001213 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001214 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1215 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001216 // Make sure we have executed any pending transitions, since there
1217 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001218 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001220 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001221 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222 return false;
1223 }
1224
Craig Mautnerde4ef022013-04-07 19:01:33 -07001225 if (prev != null && prev.mLaunchHomeTaskNext && prev.finishing &&
1226 prev.task.getTopActivity() == null) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001227 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001228 return mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001229 }
1230
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 // If we are sleeping, and there is no resumed activity, and the top
1232 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001233 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001234 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001235 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001236 // Make sure we have executed any pending transitions, since there
1237 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001238 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001239 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001240 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001241 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001242 return false;
1243 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001244
1245 // Make sure that the user who owns this activity is started. If not,
1246 // we will just leave it as is because someone should be bringing
1247 // another user's activities to the top of the stack.
1248 if (mService.mStartedUsers.get(next.userId) == null) {
1249 Slog.w(TAG, "Skipping resume of top activity " + next
1250 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001251 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001252 return false;
1253 }
1254
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001255 // The activity may be waiting for stop, but that is no longer
1256 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001257 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001258 mGoingToSleepActivities.remove(next);
1259 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001260 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261
Dianne Hackborn84375872012-06-01 19:03:50 -07001262 next.updateOptionsLocked(options);
1263
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001264 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1265
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001266 // If we are currently pausing an activity, then don't do anything
1267 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001268 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1269 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001270 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001271 return false;
1272 }
1273
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001274 // Okay we are now going to start a switch, to 'next'. We may first
1275 // have to pause the current activity, but this is an important point
1276 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001277 // XXX "App Redirected" dialog is getting too many false positives
1278 // at this point, so turn off for now.
1279 if (false) {
1280 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1281 long now = SystemClock.uptimeMillis();
1282 final boolean inTime = mLastStartedActivity.startTime != 0
1283 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1284 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1285 final int nextUid = next.info.applicationInfo.uid;
1286 if (inTime && lastUid != nextUid
1287 && lastUid != next.launchedFromUid
1288 && mService.checkPermission(
1289 android.Manifest.permission.STOP_APP_SWITCHES,
1290 -1, next.launchedFromUid)
1291 != PackageManager.PERMISSION_GRANTED) {
1292 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1293 } else {
1294 next.startTime = now;
1295 mLastStartedActivity = next;
1296 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001297 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001298 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001299 mLastStartedActivity = next;
1300 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001301 }
Craig Mautner58547802013-03-05 08:23:53 -08001302
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001303 // We need to start pausing the current activity so the top one
1304 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001305 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1306 if (mResumedActivity != null) {
1307 pausing = true;
1308 startPausingLocked(userLeaving, false);
1309 }
1310 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001312 // At this point we want to put the upcoming activity's process
1313 // at the top of the LRU list, since we know we will be needing it
1314 // very soon and it would be a waste to let it get killed if it
1315 // happens to be sitting towards the end.
1316 if (next.app != null && next.app.thread != null) {
1317 // No reason to do full oom adj update here; we'll let that
1318 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001319 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001320 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001321 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001322 return true;
1323 }
1324
Christopher Tated3f175c2012-06-14 14:16:54 -07001325 // If the most recent activity was noHistory but was only stopped rather
1326 // than stopped+finished because the device went to sleep, we need to make
1327 // sure to finish it as we're making a new activity topmost.
1328 final ActivityRecord last = mLastPausedActivity;
1329 if (mService.mSleeping && last != null && !last.finishing) {
1330 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1331 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1332 if (DEBUG_STATES) {
1333 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1334 }
1335 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001336 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001337 }
1338 }
1339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 if (prev != null && prev != next) {
1341 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1342 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001343 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344 if (DEBUG_SWITCH) Slog.v(
1345 TAG, "Resuming top, waiting visible to hide: " + prev);
1346 } else {
1347 // The next activity is already visible, so hide the previous
1348 // activity's windows right now so we can show the new one ASAP.
1349 // We only do this if the previous is finishing, which should mean
1350 // it is on top of the one being resumed so hiding it quickly
1351 // is good. Otherwise, we want to do the normal route of allowing
1352 // the resumed activity to be shown so we can decide if the
1353 // previous should actually be hidden depending on whether the
1354 // new one is found to be full-screen or not.
1355 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001356 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1358 + prev + ", waitingVisible="
1359 + (prev != null ? prev.waitingVisible : null)
1360 + ", nowVisible=" + next.nowVisible);
1361 } else {
1362 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1363 + prev + ", waitingVisible="
1364 + (prev != null ? prev.waitingVisible : null)
1365 + ", nowVisible=" + next.nowVisible);
1366 }
1367 }
1368 }
1369
Dianne Hackborne7f97212011-02-24 14:40:20 -08001370 // Launching this app's activity, make sure the app is no longer
1371 // considered stopped.
1372 try {
1373 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001374 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001375 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001376 } catch (IllegalArgumentException e) {
1377 Slog.w(TAG, "Failed trying to unstop package "
1378 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001379 }
1380
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381 // We are starting up the next activity, so tell the window manager
1382 // that the previous one will be hidden soon. This way it can know
1383 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001384 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 if (prev != null) {
1386 if (prev.finishing) {
1387 if (DEBUG_TRANSITION) Slog.v(TAG,
1388 "Prepare close transition: prev=" + prev);
1389 if (mNoAnimActivities.contains(prev)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001390 noAnim = true;
1391 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001392 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001393 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001394 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1395 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001396 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001397 mWindowManager.setAppWillBeHidden(prev.appToken);
1398 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001399 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001400 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001402 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001403 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001405 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001406 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1407 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 }
1409 }
1410 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001411 mWindowManager.setAppWillBeHidden(prev.appToken);
1412 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 }
Craig Mautner967212c2013-04-13 21:10:58 -07001414 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001415 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001417 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001418 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001420 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 }
1422 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001423 if (!noAnim) {
1424 next.applyOptionsLocked();
1425 } else {
1426 next.clearOptionsLocked();
1427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001428
Craig Mautnercf910b02013-04-23 11:23:27 -07001429 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 if (next.app != null && next.app.thread != null) {
1431 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1432
1433 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001434 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001436 // schedule launch ticks to collect information about slow apps.
1437 next.startLaunchTickingLocked();
1438
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001439 ActivityRecord lastResumedActivity =
1440 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 ActivityState lastState = next.state;
1442
1443 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001444
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001445 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 next.state = ActivityState.RESUMED;
1447 mResumedActivity = next;
1448 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001449 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001450 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 updateLRUListLocked(next);
1452
1453 // Have the window manager re-evaluate the orientation of
1454 // the screen based on the new activity order.
1455 boolean updated = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001456 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001457 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001458 mService.mConfiguration,
1459 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1460 if (config != null) {
1461 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001462 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001463 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001465
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 if (!updated) {
1467 // The configuration update wasn't able to keep the existing
1468 // instance of the activity, and instead started a new one.
1469 // We should be all done, but let's just make sure our activity
1470 // is still at the top and schedule another run if something
1471 // weird happened.
1472 ActivityRecord nextNext = topRunningActivityLocked(null);
1473 if (DEBUG_SWITCH) Slog.i(TAG,
1474 "Activity config changed during resume: " + next
1475 + ", new next: " + nextNext);
1476 if (nextNext != next) {
1477 // Do over!
1478 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1479 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001480 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001481 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001482 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001483 return true;
1484 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001485 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001486 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001487 }
Craig Mautner58547802013-03-05 08:23:53 -08001488
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489 try {
1490 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001491 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001492 if (a != null) {
1493 final int N = a.size();
1494 if (!next.finishing && N > 0) {
1495 if (DEBUG_RESULTS) Slog.v(
1496 TAG, "Delivering results to " + next
1497 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001498 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001499 }
1500 }
1501
1502 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001503 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 }
1505
1506 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001507 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001509
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001510 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001511 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001512 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001513 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001515
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001516 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517
1518 } catch (Exception e) {
1519 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001520 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1521 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001523 if (lastStack != null) {
1524 lastStack.mResumedActivity = lastResumedActivity;
1525 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 Slog.i(TAG, "Restarting because process died: " + next);
1527 if (!next.hasBeenLaunched) {
1528 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001529 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1530 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001531 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001532 next.appToken, next.packageName, next.theme,
1533 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
1534 next.nonLocalizedLabel, next.labelRes, next.icon, next.windowFlags,
1535 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001537 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001538 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001539 return true;
1540 }
1541
1542 // From this point on, if something goes wrong there is no way
1543 // to recover the activity.
1544 try {
1545 next.visible = true;
1546 completeResumeLocked(next);
1547 } catch (Exception e) {
1548 // If any exception gets thrown, toss away this
1549 // activity and try the next one.
1550 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001551 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001552 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001553 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001554 return true;
1555 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 next.stopped = false;
1557
1558 } else {
1559 // Whoops, need to restart this activity!
1560 if (!next.hasBeenLaunched) {
1561 next.hasBeenLaunched = true;
1562 } else {
1563 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001564 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001565 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001566 mService.compatibilityInfoForPackageLocked(
1567 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001569 next.labelRes, next.icon, next.windowFlags,
1570 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 }
1572 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1573 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001574 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 }
1576
Craig Mautnercf910b02013-04-23 11:23:27 -07001577 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 return true;
1579 }
1580
Craig Mautner11bf9a52013-02-19 14:08:51 -08001581
Craig Mautner8849a5e2013-04-02 16:41:03 -07001582 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001583 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001584 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001585 TaskRecord rTask = r.task;
1586 final int taskId = rTask.taskId;
1587 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001588 // Last activity in task had been removed or ActivityManagerService is reusing task.
1589 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001590 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001591 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001592 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001593 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001594 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001595 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 if (!newTask) {
1597 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001598 boolean startIt = true;
1599 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1600 task = mTaskHistory.get(taskNdx);
1601 if (task == r.task) {
1602 // Here it is! Now, if this is not yet visible to the
1603 // user, then just add it without starting; it will
1604 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001605 if (!startIt) {
1606 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1607 + task, new RuntimeException("here").fillInStackTrace());
1608 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001609 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001610 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1611 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001612 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001614 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001616 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001617 return;
1618 }
1619 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001620 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 startIt = false;
1622 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 }
1624 }
1625
1626 // Place a new activity at top of stack, so it is next to interact
1627 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001628
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001629 // If we are not placing the new activity frontmost, we do not want
1630 // to deliver the onUserLeaving callback to the actual frontmost
1631 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001632 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001633 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001634 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1635 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 }
Craig Mautner70a86932013-02-28 22:37:44 -08001637
1638 task = r.task;
1639
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001641 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001642 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001643 task.addActivityToTop(r);
1644
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001645 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001647 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001648 // We want to show the starting preview window if we are
1649 // switching to a new task, or the next activity's process is
1650 // not currently running.
1651 boolean showStartingIcon = newTask;
1652 ProcessRecord proc = r.app;
1653 if (proc == null) {
1654 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1655 }
1656 if (proc == null || proc.thread == null) {
1657 showStartingIcon = true;
1658 }
1659 if (DEBUG_TRANSITION) Slog.v(TAG,
1660 "Prepare open transition: starting " + r);
1661 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001662 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001664 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001665 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001666 ? AppTransition.TRANSIT_TASK_OPEN
1667 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001668 mNoAnimActivities.remove(r);
1669 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001670 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001671 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001672 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001673 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001674 boolean doShow = true;
1675 if (newTask) {
1676 // Even though this activity is starting fresh, we still need
1677 // to reset it to make sure we apply affinities to move any
1678 // existing activities from other tasks in to it.
1679 // If the caller has requested that the target task be
1680 // reset, then do so.
1681 if ((r.intent.getFlags()
1682 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1683 resetTaskIfNeededLocked(r, r);
1684 doShow = topRunningNonDelayedActivityLocked(null) == r;
1685 }
1686 }
1687 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1688 // Figure out if we are transitioning from another activity that is
1689 // "has the same starting icon" as the next one. This allows the
1690 // window manager to keep the previous window it had previously
1691 // created, if it still had one.
1692 ActivityRecord prev = mResumedActivity;
1693 if (prev != null) {
1694 // We don't want to reuse the previous starting preview if:
1695 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001696 if (prev.task != r.task) {
1697 prev = null;
1698 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001700 else if (prev.nowVisible) {
1701 prev = null;
1702 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001704 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001705 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001706 mService.compatibilityInfoForPackageLocked(
1707 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001708 r.labelRes, r.icon, r.windowFlags,
1709 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 }
1711 } else {
1712 // If this is the first activity, don't do any fancy animations,
1713 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001714 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001715 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001716 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001717 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 }
1719 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001720 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 }
1722
1723 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001724 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 }
1726 }
1727
Dianne Hackbornbe707852011-11-11 14:32:10 -08001728 final void validateAppTokensLocked() {
1729 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001730 mValidateAppTokens.ensureCapacity(numActivities());
1731 final int numTasks = mTaskHistory.size();
1732 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1733 TaskRecord task = mTaskHistory.get(taskNdx);
1734 final ArrayList<ActivityRecord> activities = task.mActivities;
1735 if (activities.size() == 0) {
1736 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001737 }
Craig Mautner000f0022013-02-26 15:04:29 -08001738 TaskGroup group = new TaskGroup();
1739 group.taskId = task.taskId;
1740 mValidateAppTokens.add(group);
1741 final int numActivities = activities.size();
1742 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1743 final ActivityRecord r = activities.get(activityNdx);
1744 group.tokens.add(r.appToken);
1745 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001746 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001747 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001748 }
1749
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 /**
1751 * Perform a reset of the given task, if needed as part of launching it.
1752 * Returns the new HistoryRecord at the top of the task.
1753 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001754 /**
1755 * Helper method for #resetTaskIfNeededLocked.
1756 * We are inside of the task being reset... we'll either finish this activity, push it out
1757 * for another task, or leave it as-is.
1758 * @param task The task containing the Activity (taskTop) that might be reset.
1759 * @param forceReset
1760 * @return An ActivityOptions that needs to be processed.
1761 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001762 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001763 ActivityOptions topOptions = null;
1764
1765 int replyChainEnd = -1;
1766 boolean canMoveOptions = true;
1767
1768 // We only do this for activities that are not the root of the task (since if we finish
1769 // the root, we may no longer have the task!).
1770 final ArrayList<ActivityRecord> activities = task.mActivities;
1771 final int numActivities = activities.size();
1772 for (int i = numActivities - 1; i > 0; --i ) {
1773 ActivityRecord target = activities.get(i);
1774
1775 final int flags = target.info.flags;
1776 final boolean finishOnTaskLaunch =
1777 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1778 final boolean allowTaskReparenting =
1779 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1780 final boolean clearWhenTaskReset =
1781 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1782
1783 if (!finishOnTaskLaunch
1784 && !clearWhenTaskReset
1785 && target.resultTo != null) {
1786 // If this activity is sending a reply to a previous
1787 // activity, we can't do anything with it now until
1788 // we reach the start of the reply chain.
1789 // XXX note that we are assuming the result is always
1790 // to the previous activity, which is almost always
1791 // the case but we really shouldn't count on.
1792 if (replyChainEnd < 0) {
1793 replyChainEnd = i;
1794 }
1795 } else if (!finishOnTaskLaunch
1796 && !clearWhenTaskReset
1797 && allowTaskReparenting
1798 && target.taskAffinity != null
1799 && !target.taskAffinity.equals(task.affinity)) {
1800 // If this activity has an affinity for another
1801 // task, then we need to move it out of here. We will
1802 // move it as far out of the way as possible, to the
1803 // bottom of the activity stack. This also keeps it
1804 // correctly ordered with any activities we previously
1805 // moved.
1806 TaskRecord bottomTask = mTaskHistory.get(0);
1807 ActivityRecord p = bottomTask.mActivities.get(0);
1808 if (target.taskAffinity != null
1809 && target.taskAffinity.equals(p.task.affinity)) {
1810 // If the activity currently at the bottom has the
1811 // same task affinity as the one we are moving,
1812 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001813 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001814 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1815 + " out to bottom task " + p.task);
1816 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001817 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001818 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001819 target.task.affinityIntent = target.intent;
1820 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1821 + " out to new task " + target.task);
1822 }
1823
1824 final TaskRecord targetTask = target.task;
1825 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001826 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001827
1828 ThumbnailHolder curThumbHolder = target.thumbHolder;
1829 boolean gotOptions = !canMoveOptions;
1830
1831 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1832 for (int srcPos = start; srcPos >= i; --srcPos) {
1833 p = activities.get(srcPos);
1834 if (p.finishing) {
1835 continue;
1836 }
1837
1838 curThumbHolder = p.thumbHolder;
1839 canMoveOptions = false;
1840 if (!gotOptions && topOptions == null) {
1841 topOptions = p.takeOptionsLocked();
1842 if (topOptions != null) {
1843 gotOptions = true;
1844 }
1845 }
1846 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1847 + task + " adding to task=" + targetTask,
1848 new RuntimeException("here").fillInStackTrace());
1849 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1850 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001851 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001852 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001853
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001854 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001855 }
1856
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001857 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001858 if (VALIDATE_TOKENS) {
1859 validateAppTokensLocked();
1860 }
1861
1862 replyChainEnd = -1;
1863 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1864 // If the activity should just be removed -- either
1865 // because it asks for it, or the task should be
1866 // cleared -- then finish it and anything that is
1867 // part of its reply chain.
1868 int end;
1869 if (clearWhenTaskReset) {
1870 // In this case, we want to finish this activity
1871 // and everything above it, so be sneaky and pretend
1872 // like these are all in the reply chain.
1873 end = numActivities - 1;
1874 } else if (replyChainEnd < 0) {
1875 end = i;
1876 } else {
1877 end = replyChainEnd;
1878 }
1879 ActivityRecord p = null;
1880 boolean gotOptions = !canMoveOptions;
1881 for (int srcPos = i; srcPos <= end; srcPos++) {
1882 p = activities.get(srcPos);
1883 if (p.finishing) {
1884 continue;
1885 }
1886 canMoveOptions = false;
1887 if (!gotOptions && topOptions == null) {
1888 topOptions = p.takeOptionsLocked();
1889 if (topOptions != null) {
1890 gotOptions = true;
1891 }
1892 }
Craig Mautner58547802013-03-05 08:23:53 -08001893 if (DEBUG_TASKS) Slog.w(TAG,
1894 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001895 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001896 end--;
1897 srcPos--;
1898 }
1899 }
1900 replyChainEnd = -1;
1901 } else {
1902 // If we were in the middle of a chain, well the
1903 // activity that started it all doesn't want anything
1904 // special, so leave it all as-is.
1905 replyChainEnd = -1;
1906 }
1907 }
1908
1909 return topOptions;
1910 }
1911
1912 /**
1913 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1914 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1915 * @param affinityTask The task we are looking for an affinity to.
1916 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1917 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1918 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1919 */
Craig Mautner77878772013-03-04 19:46:24 -08001920 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1921 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001922 int replyChainEnd = -1;
1923 final int taskId = task.taskId;
1924 final String taskAffinity = task.affinity;
1925
1926 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1927 final int numActivities = activities.size();
1928 // Do not operate on the root Activity.
1929 for (int i = numActivities - 1; i > 0; --i) {
1930 ActivityRecord target = activities.get(i);
1931
1932 final int flags = target.info.flags;
1933 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1934 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1935
1936 if (target.resultTo != null) {
1937 // If this activity is sending a reply to a previous
1938 // activity, we can't do anything with it now until
1939 // we reach the start of the reply chain.
1940 // XXX note that we are assuming the result is always
1941 // to the previous activity, which is almost always
1942 // the case but we really shouldn't count on.
1943 if (replyChainEnd < 0) {
1944 replyChainEnd = i;
1945 }
1946 } else if (topTaskIsHigher
1947 && allowTaskReparenting
1948 && taskAffinity != null
1949 && taskAffinity.equals(target.taskAffinity)) {
1950 // This activity has an affinity for our task. Either remove it if we are
1951 // clearing or move it over to our task. Note that
1952 // we currently punt on the case where we are resetting a
1953 // task that is not at the top but who has activities above
1954 // with an affinity to it... this is really not a normal
1955 // case, and we will need to later pull that task to the front
1956 // and usually at that point we will do the reset and pick
1957 // up those remaining activities. (This only happens if
1958 // someone starts an activity in a new task from an activity
1959 // in a task that is not currently on top.)
1960 if (forceReset || finishOnTaskLaunch) {
1961 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1962 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1963 for (int srcPos = start; srcPos >= i; --srcPos) {
1964 final ActivityRecord p = activities.get(srcPos);
1965 if (p.finishing) {
1966 continue;
1967 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001968 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001969 }
1970 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001971 if (taskInsertionPoint < 0) {
1972 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001973
Craig Mautner77878772013-03-04 19:46:24 -08001974 }
Craig Mautner77878772013-03-04 19:46:24 -08001975
1976 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1977 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1978 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1979 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001980 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001981 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001982 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001983
Craig Mautnere3a74d52013-02-22 14:14:58 -08001984 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1985 + " to stack at " + task,
1986 new RuntimeException("here").fillInStackTrace());
1987 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
1988 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001989 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001990 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08001992 if (VALIDATE_TOKENS) {
1993 validateAppTokensLocked();
1994 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08001995
1996 // Now we've moved it in to place... but what if this is
1997 // a singleTop activity and we have put it on top of another
1998 // instance of the same activity? Then we drop the instance
1999 // below so it remains singleTop.
2000 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2001 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2002 boolean found = false;
2003 int targetNdx = taskActivities.indexOf(target);
2004 if (targetNdx > 0) {
2005 ActivityRecord p = taskActivities.get(targetNdx - 1);
2006 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002007 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2008 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002009 }
2010 }
2011 }
2012 }
2013
2014 replyChainEnd = -1;
2015 }
2016 }
Craig Mautner77878772013-03-04 19:46:24 -08002017 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002018 }
2019
Craig Mautner8849a5e2013-04-02 16:41:03 -07002020 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002021 ActivityRecord newActivity) {
2022 boolean forceReset =
2023 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2024 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2025 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2026 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2027 forceReset = true;
2028 }
2029 }
2030
2031 final TaskRecord task = taskTop.task;
2032
2033 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2034 * for remaining tasks. Used for later tasks to reparent to task. */
2035 boolean taskFound = false;
2036
2037 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2038 ActivityOptions topOptions = null;
2039
Craig Mautner77878772013-03-04 19:46:24 -08002040 // Preserve the location for reparenting in the new task.
2041 int reparentInsertionPoint = -1;
2042
Craig Mautnere3a74d52013-02-22 14:14:58 -08002043 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2044 final TaskRecord targetTask = mTaskHistory.get(i);
2045
2046 if (targetTask == task) {
2047 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2048 taskFound = true;
2049 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002050 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2051 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002052 }
2053 }
2054
Craig Mautner70a86932013-02-28 22:37:44 -08002055 int taskNdx = mTaskHistory.indexOf(task);
2056 do {
2057 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2058 } while (taskTop == null && taskNdx >= 0);
2059
Craig Mautnere3a74d52013-02-22 14:14:58 -08002060 if (topOptions != null) {
2061 // If we got some ActivityOptions from an activity on top that
2062 // was removed from the task, propagate them to the new real top.
2063 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002064 taskTop.updateOptionsLocked(topOptions);
2065 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002066 topOptions.abort();
2067 }
2068 }
2069
2070 return taskTop;
2071 }
2072
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002073 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 * Find the activity in the history stack within the given task. Returns
2075 * the index within the history at which it's found, or < 0 if not found.
2076 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002078 final ComponentName realActivity = r.realActivity;
2079 ArrayList<ActivityRecord> activities = task.mActivities;
2080 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2081 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002082 if (candidate.finishing) {
2083 continue;
2084 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002085 if (candidate.realActivity.equals(realActivity)) {
2086 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 }
2088 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002089 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 }
2091
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2093 String resultWho, int requestCode, int resultCode, Intent data) {
2094
2095 if (callingUid > 0) {
2096 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002097 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002098 }
2099
2100 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2101 + " : who=" + resultWho + " req=" + requestCode
2102 + " res=" + resultCode + " data=" + data);
2103 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2104 try {
2105 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2106 list.add(new ResultInfo(resultWho, requestCode,
2107 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002108 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 return;
2110 } catch (Exception e) {
2111 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2112 }
2113 }
2114
2115 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2116 }
2117
Craig Mautnerf3333272013-04-22 10:55:53 -07002118 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2120 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2121 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2122 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002123 if (!mService.mSleeping) {
2124 if (DEBUG_STATES) {
2125 Slog.d(TAG, "no-history finish of " + r);
2126 }
2127 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002128 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002129 } else {
2130 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2131 + " on stop because we're just sleeping");
2132 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002134 }
2135
2136 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002137 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 if (mService.mFocusedActivity == r) {
2139 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2140 }
2141 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002142 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 try {
2144 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002145 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2146 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147 r.state = ActivityState.STOPPING;
2148 if (DEBUG_VISBILITY) Slog.v(
2149 TAG, "Stopping visible=" + r.visible + " for " + r);
2150 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002151 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002153 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002154 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002155 r.setSleeping(true);
2156 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002157 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2158 msg.obj = r;
2159 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 } catch (Exception e) {
2161 // Maybe just ignore exceptions here... if the process
2162 // has crashed, our death notification will clean things
2163 // up.
2164 Slog.w(TAG, "Exception thrown during pause", e);
2165 // Just in case, assume it to be stopped.
2166 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002167 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002168 r.state = ActivityState.STOPPED;
2169 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002170 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 }
2172 }
2173 }
2174 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002175
Craig Mautnere79d42682013-04-01 19:01:53 -07002176 // Checked.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002177 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002178 Configuration config) {
2179 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2180
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002181 ActivityRecord res = null;
2182
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002183 ArrayList<ActivityRecord> stops = null;
2184 ArrayList<ActivityRecord> finishes = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002185 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186 int NS = 0;
2187 int NF = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 IApplicationThread sendThumbnail = null;
2189 boolean booting = false;
2190 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002191 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002192
Craig Mautnere79d42682013-04-01 19:01:53 -07002193 // Get the activity record.
Craig Mautnerf3333272013-04-22 10:55:53 -07002194 res = isInStackLocked(token);
2195 if (res != null) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002196 // No longer need to keep the device awake.
Craig Mautnerf3333272013-04-22 10:55:53 -07002197 if (mResumedActivity == res && mLaunchingActivity.isHeld()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002198 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2199 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002200 }
2201
Craig Mautnere79d42682013-04-01 19:01:53 -07002202 // If this activity is fullscreen, set up to hide those under it.
Craig Mautnerf3333272013-04-22 10:55:53 -07002203 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + res);
Craig Mautnere79d42682013-04-01 19:01:53 -07002204 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002205 }
2206
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002207 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 }
2209
2210 /**
2211 * @return Returns true if the activity is being finished, false if for
2212 * some reason it is being left as-is.
2213 */
2214 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002215 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002216 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002217 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002218 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002219 + ", result=" + resultCode + ", data=" + resultData
2220 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002221 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002222 return false;
2223 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224
Craig Mautnerd44711d2013-02-23 11:24:36 -08002225 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 return true;
2227 }
2228
Craig Mautnerd2328952013-03-05 12:46:26 -08002229 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002230 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2231 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2232 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2233 ActivityRecord r = activities.get(activityNdx);
2234 if (r.resultTo == self && r.requestCode == requestCode) {
2235 if ((r.resultWho == null && resultWho == null) ||
2236 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2237 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2238 false);
2239 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002240 }
2241 }
2242 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002243 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002244 }
2245
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002246 final void finishTopRunningActivityLocked(ProcessRecord app) {
2247 ActivityRecord r = topRunningActivityLocked(null);
2248 if (r != null && r.app == app) {
2249 // If the top running activity is from this crashing
2250 // process, then terminate it to avoid getting in a loop.
2251 Slog.w(TAG, " Force finishing activity "
2252 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002253 int taskNdx = mTaskHistory.indexOf(r.task);
2254 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002255 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002256 // Also terminate any activities below it that aren't yet
2257 // stopped, to avoid a situation where one will get
2258 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002259 --activityNdx;
2260 if (activityNdx < 0) {
2261 do {
2262 --taskNdx;
2263 if (taskNdx < 0) {
2264 break;
2265 }
2266 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2267 } while (activityNdx < 0);
2268 }
2269 if (activityNdx >= 0) {
2270 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002271 if (r.state == ActivityState.RESUMED
2272 || r.state == ActivityState.PAUSING
2273 || r.state == ActivityState.PAUSED) {
2274 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2275 Slog.w(TAG, " Force finishing activity "
2276 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002277 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002278 }
2279 }
2280 }
2281 }
2282 }
2283
Craig Mautnerd2328952013-03-05 12:46:26 -08002284 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002285 ArrayList<ActivityRecord> activities = r.task.mActivities;
2286 for (int index = activities.indexOf(r); index >= 0; --index) {
2287 ActivityRecord cur = activities.get(index);
2288 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002289 break;
2290 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002291 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002292 }
2293 return true;
2294 }
2295
Dianne Hackborn5c607432012-02-28 14:44:19 -08002296 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2297 // send the result
2298 ActivityRecord resultTo = r.resultTo;
2299 if (resultTo != null) {
2300 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2301 + " who=" + r.resultWho + " req=" + r.requestCode
2302 + " res=" + resultCode + " data=" + resultData);
2303 if (r.info.applicationInfo.uid > 0) {
2304 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2305 resultTo.packageName, resultData,
2306 resultTo.getUriPermissionsLocked());
2307 }
2308 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2309 resultData);
2310 r.resultTo = null;
2311 }
2312 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2313
2314 // Make sure this HistoryRecord is not holding on to other resources,
2315 // because clients have remote IPC references to this object so we
2316 // can't assume that will go away and want to avoid circular IPC refs.
2317 r.results = null;
2318 r.pendingResults = null;
2319 r.newIntents = null;
2320 r.icicle = null;
2321 }
2322
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 /**
2324 * @return Returns true if this activity has been removed from the history
2325 * list, or false if it is still in the list and will be removed later.
2326 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002327 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2328 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 if (r.finishing) {
2330 Slog.w(TAG, "Duplicate finish request for " + r);
2331 return false;
2332 }
2333
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002334 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002336 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002338 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2339 final int index = activities.indexOf(r);
2340 if (index < (activities.size() - 1)) {
2341 ActivityRecord next = activities.get(index+1);
2342 if (r.frontOfTask) {
2343 // The next activity is now the front of the task.
2344 next.frontOfTask = true;
2345 }
2346 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2347 // If the caller asked that this activity (and all above it)
2348 // be cleared when the task is reset, don't lose that information,
2349 // but propagate it up to the next activity.
2350 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 }
2352 }
2353
2354 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002355 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002357 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 }
2359 }
2360
Dianne Hackborn5c607432012-02-28 14:44:19 -08002361 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002362
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002363 if (mService.mPendingThumbnails.size() > 0) {
2364 // There are clients waiting to receive thumbnails so, in case
2365 // this is an activity that someone is waiting for, add it
2366 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002367 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 }
2369
Craig Mautnerde4ef022013-04-07 19:01:33 -07002370 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002371 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 if (DEBUG_TRANSITION) Slog.v(TAG,
2373 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002374 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002375 ? AppTransition.TRANSIT_TASK_CLOSE
2376 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002377
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002378 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002379 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002380
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002381 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2383 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2384 startPausingLocked(false, false);
2385 }
2386
2387 } else if (r.state != ActivityState.PAUSING) {
2388 // If the activity is PAUSING, we will complete the finish once
2389 // it is done pausing; else we can just directly finish it here.
2390 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002391 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002392 } else {
2393 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2394 }
2395
2396 return false;
2397 }
2398
Craig Mautnerf3333272013-04-22 10:55:53 -07002399 static final int FINISH_IMMEDIATELY = 0;
2400 static final int FINISH_AFTER_PAUSE = 1;
2401 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002402
Craig Mautnerf3333272013-04-22 10:55:53 -07002403 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 // First things first: if this activity is currently visible,
2405 // and the resumed activity is not yet visible, then hold off on
2406 // finishing until the resumed one becomes visible.
2407 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002408 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2409 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002410 if (mStackSupervisor.mStoppingActivities.size() > 3
2411 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002412 // If we already have a few activities waiting to stop,
2413 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002414 // them out. Or if r is the last of activity of the last task the stack
2415 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002416 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002417 } else {
2418 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002419 }
2420 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002421 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2422 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002423 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002424 if (oomAdj) {
2425 mService.updateOomAdjLocked();
2426 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002427 return r;
2428 }
2429
2430 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002431 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002432 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002433 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 if (mResumedActivity == r) {
2435 mResumedActivity = null;
2436 }
2437 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002438 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 r.state = ActivityState.FINISHING;
2440
2441 if (mode == FINISH_IMMEDIATELY
2442 || prevState == ActivityState.STOPPED
2443 || prevState == ActivityState.INITIALIZING) {
2444 // If this activity is already stopped, we can just finish
2445 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002446 boolean activityRemoved = destroyActivityLocked(r, true,
2447 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002448 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002449 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002450 }
2451 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002452 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002453
2454 // Need to go through the full pause cycle to get this
2455 // activity into the stopped state and then finish it.
2456 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002457 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002458 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 return r;
2460 }
2461
Craig Mautnerd2328952013-03-05 12:46:26 -08002462 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002463 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002464 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002465 final TaskRecord task = srec.task;
2466 final ArrayList<ActivityRecord> activities = task.mActivities;
2467 final int start = activities.indexOf(srec);
2468 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002469 return false;
2470 }
2471 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002472 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002473 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002474 final ComponentName dest = destIntent.getComponent();
2475 if (start > 0 && dest != null) {
2476 for (int i = finishTo; i >= 0; i--) {
2477 ActivityRecord r = activities.get(i);
2478 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002479 r.info.name.equals(dest.getClassName())) {
2480 finishTo = i;
2481 parent = r;
2482 foundParentInTask = true;
2483 break;
2484 }
2485 }
2486 }
2487
2488 IActivityController controller = mService.mController;
2489 if (controller != null) {
2490 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2491 if (next != null) {
2492 // ask watcher if this is allowed
2493 boolean resumeOK = true;
2494 try {
2495 resumeOK = controller.activityResuming(next.packageName);
2496 } catch (RemoteException e) {
2497 mService.mController = null;
2498 }
2499
2500 if (!resumeOK) {
2501 return false;
2502 }
2503 }
2504 }
2505 final long origId = Binder.clearCallingIdentity();
2506 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002507 ActivityRecord r = activities.get(i);
2508 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002509 // Only return the supplied result for the first activity finished
2510 resultCode = Activity.RESULT_CANCELED;
2511 resultData = null;
2512 }
2513
2514 if (parent != null && foundParentInTask) {
2515 final int parentLaunchMode = parent.info.launchMode;
2516 final int destIntentFlags = destIntent.getFlags();
2517 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2518 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2519 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2520 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2521 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2522 } else {
2523 try {
2524 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2525 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002526 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002527 null, aInfo, parent.appToken, null,
2528 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2529 0, null, true, null);
2530 foundParentInTask = res == ActivityManager.START_SUCCESS;
2531 } catch (RemoteException e) {
2532 foundParentInTask = false;
2533 }
2534 requestFinishActivityLocked(parent.appToken, resultCode,
2535 resultData, "navigate-up", true);
2536 }
2537 }
2538 Binder.restoreCallingIdentity(origId);
2539 return foundParentInTask;
2540 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002541 /**
2542 * Perform the common clean-up of an activity record. This is called both
2543 * as part of destroyActivityLocked() (when destroying the client-side
2544 * representation) and cleaning things up as a result of its hosting
2545 * processing going away, in which case there is no remaining client-side
2546 * state to destroy so only the cleanup here is needed.
2547 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002548 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2549 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 if (mResumedActivity == r) {
2551 mResumedActivity = null;
2552 }
2553 if (mService.mFocusedActivity == r) {
2554 mService.mFocusedActivity = null;
2555 }
2556
2557 r.configDestroy = false;
2558 r.frozenBeforeDestroy = false;
2559
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002560 if (setState) {
2561 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2562 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002563 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002564 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002565 }
2566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 // Make sure this record is no longer in the pending finishes list.
2568 // This could happen, for example, if we are trimming activities
2569 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002570 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002571 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002572
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002573 // Remove any pending results.
2574 if (r.finishing && r.pendingResults != null) {
2575 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2576 PendingIntentRecord rec = apr.get();
2577 if (rec != null) {
2578 mService.cancelIntentSenderLocked(rec, false);
2579 }
2580 }
2581 r.pendingResults = null;
2582 }
2583
2584 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002585 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 }
2587
2588 if (mService.mPendingThumbnails.size() > 0) {
2589 // There are clients waiting to receive thumbnails so, in case
2590 // this is an activity that someone is waiting for, add it
2591 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002592 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 }
2594
2595 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002596 removeTimeoutsForActivityLocked(r);
2597 }
2598
2599 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002600 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002601 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002602 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002603 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002604 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 }
2606
Dianne Hackborn5c607432012-02-28 14:44:19 -08002607 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002608 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2609 r.makeFinishing();
2610 if (DEBUG_ADD_REMOVE) {
2611 RuntimeException here = new RuntimeException("here");
2612 here.fillInStackTrace();
2613 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002615 final TaskRecord task = r.task;
2616 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002617 if (DEBUG_STACK) Slog.i(TAG,
2618 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002619 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002620 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002621 r.takeFromHistory();
2622 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002623 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002624 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002625 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002626 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002627 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002628 if (VALIDATE_TOKENS) {
2629 validateAppTokensLocked();
2630 }
2631 cleanUpActivityServicesLocked(r);
2632 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002634
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002635 /**
2636 * Perform clean-up of service connections in an activity record.
2637 */
2638 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2639 // Throw away any services that have been bound by this activity.
2640 if (r.connections != null) {
2641 Iterator<ConnectionRecord> it = r.connections.iterator();
2642 while (it.hasNext()) {
2643 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002644 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002645 }
2646 r.connections = null;
2647 }
2648 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002649
2650 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2651 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2652 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2653 mHandler.sendMessage(msg);
2654 }
2655
Dianne Hackborn28695e02011-11-02 21:59:51 -07002656 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002657 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002658 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002659 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2660 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2661 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2662 final ActivityRecord r = activities.get(activityNdx);
2663 if (r.finishing) {
2664 continue;
2665 }
2666 if (r.fullscreen) {
2667 lastIsOpaque = true;
2668 }
2669 if (owner != null && r.app != owner) {
2670 continue;
2671 }
2672 if (!lastIsOpaque) {
2673 continue;
2674 }
2675 // We can destroy this one if we have its icicle saved and
2676 // it is not in the process of pausing/stopping/finishing.
2677 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2678 && r.haveState && !r.visible && r.stopped
2679 && r.state != ActivityState.DESTROYING
2680 && r.state != ActivityState.DESTROYED) {
2681 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2682 + " resumed=" + mResumedActivity
2683 + " pausing=" + mPausingActivity);
2684 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2685 activityRemoved = true;
2686 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002687 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002688 }
2689 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002690 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002691 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002692 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002693 }
2694
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 /**
2696 * Destroy the current CLIENT SIDE instance of an activity. This may be
2697 * called both when actually finishing an activity, or when performing
2698 * a configuration switch where we destroy the current client-side object
2699 * but then create a new client-side object for this same HistoryRecord.
2700 */
2701 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002702 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002703 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002704 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002705 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2706 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002707 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002708 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709
2710 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002711
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002712 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713
2714 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002715
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 if (hadApp) {
2717 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002718 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2720 mService.mHeavyWeightProcess = null;
2721 mService.mHandler.sendEmptyMessage(
2722 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2723 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002724 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002725 // No longer have activities, so update oom adj.
2726 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002727 }
2728 }
2729
2730 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002731
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002732 try {
2733 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002734 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735 r.configChangeFlags);
2736 } catch (Exception e) {
2737 // We can just ignore exceptions here... if the process
2738 // has crashed, our death notification will clean things
2739 // up.
2740 //Slog.w(TAG, "Exception thrown during finish", e);
2741 if (r.finishing) {
2742 removeActivityFromHistoryLocked(r);
2743 removedFromHistory = true;
2744 skipDestroy = true;
2745 }
2746 }
2747
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002748 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002749
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002750 // If the activity is finishing, we need to wait on removing it
2751 // from the list to give it a chance to do its cleanup. During
2752 // that time it may make calls back with its token so we need to
2753 // be able to find it on the list and so we don't want to remove
2754 // it from the list yet. Otherwise, we can just immediately put
2755 // it in the destroyed state since we are not removing it from the
2756 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002757 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002758 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2759 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 r.state = ActivityState.DESTROYING;
2761 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
2762 msg.obj = r;
2763 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2764 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002765 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2766 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002768 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002769 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 }
2771 } else {
2772 // remove this record from the history.
2773 if (r.finishing) {
2774 removeActivityFromHistoryLocked(r);
2775 removedFromHistory = true;
2776 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002777 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2778 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002780 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002781 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 }
2783 }
2784
2785 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002786
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 if (!mLRUActivities.remove(r) && hadApp) {
2788 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2789 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002790
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 return removedFromHistory;
2792 }
2793
Craig Mautnerd2328952013-03-05 12:46:26 -08002794 final void activityDestroyedLocked(IBinder token) {
2795 final long origId = Binder.clearCallingIdentity();
2796 try {
2797 ActivityRecord r = ActivityRecord.forToken(token);
2798 if (r != null) {
2799 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002801
2802 if (isInStackLocked(token) != null) {
2803 if (r.state == ActivityState.DESTROYING) {
2804 cleanUpActivityLocked(r, true, false);
2805 removeActivityFromHistoryLocked(r);
2806 }
2807 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002808 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautnerd2328952013-03-05 12:46:26 -08002809 } finally {
2810 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002811 }
2812 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002813
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002814 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2815 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002816 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002817 if (DEBUG_CLEANUP) Slog.v(
2818 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 + " with " + i + " entries");
2820 while (i > 0) {
2821 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002822 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002823 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002825 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002827 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828 }
2829 }
2830 }
2831
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002832 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2833 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002834 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2835 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002836 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002837 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002838 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002839 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2840 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002841
2842 boolean hasVisibleActivities = false;
2843
2844 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002845 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002846 if (DEBUG_CLEANUP) Slog.v(
2847 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002848 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2849 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2850 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2851 final ActivityRecord r = activities.get(activityNdx);
2852 --i;
2853 if (DEBUG_CLEANUP) Slog.v(
2854 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2855 if (r.app == app) {
2856 boolean remove;
2857 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2858 // Don't currently have state for the activity, or
2859 // it is finishing -- always remove it.
2860 remove = true;
2861 } else if (r.launchCount > 2 &&
2862 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2863 // We have launched this activity too many times since it was
2864 // able to run, so give up and remove it.
2865 remove = true;
2866 } else {
2867 // The process may be gone, but the activity lives on!
2868 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002869 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002870 if (remove) {
2871 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
2872 RuntimeException here = new RuntimeException("here");
2873 here.fillInStackTrace();
2874 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2875 + ": haveState=" + r.haveState
2876 + " stateNotNeeded=" + r.stateNotNeeded
2877 + " finishing=" + r.finishing
2878 + " state=" + r.state, here);
2879 }
2880 if (!r.finishing) {
2881 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2882 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2883 r.userId, System.identityHashCode(r),
2884 r.task.taskId, r.shortComponentName,
2885 "proc died without state saved");
2886 }
2887 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002888
Craig Mautner0247fc82013-02-28 14:32:06 -08002889 } else {
2890 // We have the current state for this activity, so
2891 // it can be restarted later when needed.
2892 if (localLOGV) Slog.v(
2893 TAG, "Keeping entry, setting app to null");
2894 if (r.visible) {
2895 hasVisibleActivities = true;
2896 }
2897 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2898 + r);
2899 r.app = null;
2900 r.nowVisible = false;
2901 if (!r.haveState) {
2902 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
2903 "App died, clearing saved state of " + r);
2904 r.icicle = null;
2905 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002906 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002907
Craig Mautnerd2328952013-03-05 12:46:26 -08002908 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002909 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002910 }
2911 }
2912
2913 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002915
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002916 final void updateTransitLocked(int transit, Bundle options) {
2917 if (options != null) {
2918 ActivityRecord r = topRunningActivityLocked(null);
2919 if (r != null && r.state != ActivityState.RESUMED) {
2920 r.updateOptionsLocked(options);
2921 } else {
2922 ActivityOptions.abort(options);
2923 }
2924 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002925 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002926 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002927
Craig Mautnercae015f2013-02-08 14:31:27 -08002928 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002929 final TaskRecord task = taskForIdLocked(taskId);
2930 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002931 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002932 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002933 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002934 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2935 // Caller wants the home activity moved with it. To accomplish this,
2936 // we'll just move the home task to the top first.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002937 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002938 }
2939 moveTaskToFrontLocked(task, null, options);
2940 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002941 }
2942 return false;
2943 }
2944
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002945 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002946 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947
Craig Mautner11bf9a52013-02-19 14:08:51 -08002948 final int numTasks = mTaskHistory.size();
2949 final int index = mTaskHistory.indexOf(tr);
2950 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
2951 // nothing to do!
2952 if (reason != null &&
2953 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2954 ActivityOptions.abort(options);
2955 } else {
2956 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2957 }
2958 return;
2959 }
2960
2961 // Shift all activities with this task up to the top
2962 // of the stack, keeping them in the same internal order.
2963 mTaskHistory.remove(tr);
2964 mTaskHistory.add(tr);
2965
2966 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002967 if (reason != null &&
2968 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002969 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 ActivityRecord r = topRunningActivityLocked(null);
2971 if (r != null) {
2972 mNoAnimActivities.add(r);
2973 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002974 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002976 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002978
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002979 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002981 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautner58547802013-03-05 08:23:53 -08002982 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002983
2984 if (VALIDATE_TOKENS) {
2985 validateAppTokensLocked();
2986 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987 }
2988
2989 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002990 * Worker method for rearranging history stack. Implements the function of moving all
2991 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002993 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2995 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002996 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002997 * @param task The taskId to collect and move to the bottom.
2998 * @return Returns true if the move completed, false if not.
2999 */
3000 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3001 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003002
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 // If we have a watcher, preflight the move before committing to it. First check
3004 // for *other* available tasks, but if none are available, then try again allowing the
3005 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003006 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 ActivityRecord next = topRunningActivityLocked(null, task);
3008 if (next == null) {
3009 next = topRunningActivityLocked(null, 0);
3010 }
3011 if (next != null) {
3012 // ask watcher if this is allowed
3013 boolean moveOK = true;
3014 try {
3015 moveOK = mService.mController.activityResuming(next.packageName);
3016 } catch (RemoteException e) {
3017 mService.mController = null;
3018 }
3019 if (!moveOK) {
3020 return false;
3021 }
3022 }
3023 }
3024
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003025 if (DEBUG_TRANSITION) Slog.v(TAG,
3026 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027
Craig Mautnerd2328952013-03-05 12:46:26 -08003028 final TaskRecord tr = taskForIdLocked(task);
3029 if (tr == null) {
3030 return false;
3031 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003032 mTaskHistory.remove(tr);
3033 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003034
3035 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003036 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3037 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 ActivityRecord r = topRunningActivityLocked(null);
3039 if (r != null) {
3040 mNoAnimActivities.add(r);
3041 }
3042 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003043 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003044 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003046 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003047
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003049 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 }
3051
Craig Mautnerde4ef022013-04-07 19:01:33 -07003052 if (mResumedActivity != null && mResumedActivity.task == tr &&
3053 mResumedActivity.mLaunchHomeTaskNext) {
3054 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003055 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003056 }
3057
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003058 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003059 return true;
3060 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003061
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003062 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003063 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003064 if (mResumedActivity != null && mResumedActivity.thumbHolder == tr) {
3065 info.mainThumbnail = screenshotActivities(mResumedActivity);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003066 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003067 if (info.mainThumbnail == null) {
3068 info.mainThumbnail = tr.lastThumbnail;
3069 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003070 return info;
3071 }
3072
Dianne Hackborn15491c62012-09-19 10:59:14 -07003073 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003074 if (mResumedActivity != null && mResumedActivity.task == tr) {
Dianne Hackborn15491c62012-09-19 10:59:14 -07003075 // This task is the current resumed task, we just need to take
3076 // a screenshot of it and return that.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003078 }
3079 // Return the information about the task, to figure out the top
3080 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003081 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003082 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003083 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003084 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003085 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003086 }
3087
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003088 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3089 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003090 final TaskRecord task = taskForIdLocked(taskId);
3091 if (task == null) {
3092 return null;
3093 }
Craig Mautner16030772013-02-26 16:05:47 -08003094 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003095 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003096 if (taskRequired) {
3097 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3098 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003099 return null;
3100 }
3101
3102 if (subTaskIndex < 0) {
3103 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003104 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003105 return info.root;
3106 }
3107
3108 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003109 if (taskRequired) {
3110 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3111 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003112 return null;
3113 }
3114
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003115 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003116 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003117 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003118 return subtask.activity;
3119 }
3120
Craig Mautner16030772013-02-26 16:05:47 -08003121 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003122 final TaskAccessInfo thumbs = new TaskAccessInfo();
3123 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003124 final ArrayList<ActivityRecord> activities = task.mActivities;
3125 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003126 int j = 0;
3127 ThumbnailHolder holder = null;
3128 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003129 ActivityRecord ar = activities.get(j);
3130 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003131 thumbs.root = ar;
3132 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003133 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003134 if (holder != null) {
3135 thumbs.mainThumbnail = holder.lastThumbnail;
3136 }
3137 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003138 break;
3139 }
3140 j++;
3141 }
3142
3143 if (j >= NA) {
3144 return thumbs;
3145 }
3146
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003147 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
3148 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003149 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003150 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003151 j++;
3152 if (ar.finishing) {
3153 continue;
3154 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003155 if (ar.thumbHolder != holder && holder != null) {
3156 thumbs.numSubThumbbails++;
3157 holder = ar.thumbHolder;
3158 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07003159 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003160 sub.activity = ar;
3161 sub.index = j-1;
3162 subtasks.add(sub);
3163 }
3164 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003165 if (thumbs.numSubThumbbails > 0) {
3166 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003167 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003168 public Bitmap getThumbnail(int index) {
3169 if (index < 0 || index >= thumbs.subtasks.size()) {
3170 return null;
3171 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003172 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003173 if (mResumedActivity != null && mResumedActivity.thumbHolder == sub.holder) {
3174 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003175 }
3176 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003177 }
3178 };
3179 }
3180 return thumbs;
3181 }
3182
Craig Mautner8849a5e2013-04-02 16:41:03 -07003183 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003185 final Uri data = r.intent.getData();
3186 final String strData = data != null ? data.toSafeString() : null;
3187
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003189 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003190 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003191 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003192 }
3193
3194 /**
3195 * Make sure the given activity matches the current configuration. Returns
3196 * false if the activity had to be destroyed. Returns true if the
3197 * configuration is the same, or the activity will remain running as-is
3198 * for whatever reason. Ensures the HistoryRecord is updated with the
3199 * correct configuration and all other bookkeeping is handled.
3200 */
3201 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3202 int globalChanges) {
3203 if (mConfigWillChange) {
3204 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3205 "Skipping config check (will change): " + r);
3206 return true;
3207 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003208
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003209 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3210 "Ensuring correct configuration: " + r);
3211
3212 // Short circuit: if the two configurations are the exact same
3213 // object (the common case), then there is nothing to do.
3214 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003215 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3217 "Configuration unchanged in " + r);
3218 return true;
3219 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003220
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221 // We don't worry about activities that are finishing.
3222 if (r.finishing) {
3223 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3224 "Configuration doesn't matter in finishing " + r);
3225 r.stopFreezingScreenLocked(false);
3226 return true;
3227 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003228
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 // Okay we now are going to make this activity have the new config.
3230 // But then we need to figure out how it needs to deal with that.
3231 Configuration oldConfig = r.configuration;
3232 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003233
3234 // Determine what has changed. May be nothing, if this is a config
3235 // that has come back from the app after going idle. In that case
3236 // we just want to leave the official config object now in the
3237 // activity and do nothing else.
3238 final int changes = oldConfig.diff(newConfig);
3239 if (changes == 0 && !r.forceNewConfig) {
3240 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3241 "Configuration no differences in " + r);
3242 return true;
3243 }
3244
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 // If the activity isn't currently running, just leave the new
3246 // configuration and it will pick that up next time it starts.
3247 if (r.app == null || r.app.thread == null) {
3248 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3249 "Configuration doesn't matter not running " + r);
3250 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003251 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003252 return true;
3253 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003254
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003255 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003256 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3257 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3258 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003259 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003260 + ", newConfig=" + newConfig);
3261 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003262 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003263 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3264 r.configChangeFlags |= changes;
3265 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003266 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003267 if (r.app == null || r.app.thread == null) {
3268 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003269 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003270 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003271 } else if (r.state == ActivityState.PAUSING) {
3272 // A little annoying: we are waiting for this activity to
3273 // finish pausing. Let's not do anything now, but just
3274 // flag that it needs to be restarted when done pausing.
3275 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003276 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003277 r.configDestroy = true;
3278 return true;
3279 } else if (r.state == ActivityState.RESUMED) {
3280 // Try to optimize this case: the configuration is changing
3281 // and we need to restart the top, resumed activity.
3282 // Instead of doing the normal handshaking, just say
3283 // "restart!".
3284 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003285 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003286 relaunchActivityLocked(r, r.configChangeFlags, true);
3287 r.configChangeFlags = 0;
3288 } else {
3289 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003290 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003291 relaunchActivityLocked(r, r.configChangeFlags, false);
3292 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003293 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003294
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003295 // All done... tell the caller we weren't able to keep this
3296 // activity around.
3297 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003298 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003299
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 // Default case: the activity can handle this new configuration, so
3301 // hand it over. Note that we don't need to give it the new
3302 // configuration, since we always send configuration changes to all
3303 // process when they happen so it can just use whatever configuration
3304 // it last got.
3305 if (r.app != null && r.app.thread != null) {
3306 try {
3307 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003308 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003309 } catch (RemoteException e) {
3310 // If process died, whatever.
3311 }
3312 }
3313 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003315 return true;
3316 }
3317
3318 private final boolean relaunchActivityLocked(ActivityRecord r,
3319 int changes, boolean andResume) {
3320 List<ResultInfo> results = null;
3321 List<Intent> newIntents = null;
3322 if (andResume) {
3323 results = r.results;
3324 newIntents = r.newIntents;
3325 }
3326 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3327 + " with results=" + results + " newIntents=" + newIntents
3328 + " andResume=" + andResume);
3329 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003330 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331 r.task.taskId, r.shortComponentName);
3332
3333 r.startFreezingScreenLocked(r.app, 0);
3334
3335 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003336 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3337 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3338 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003339 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003340 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003341 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003342 // Note: don't need to call pauseIfSleepingLocked() here, because
3343 // the caller will only pass in 'andResume' if this activity is
3344 // currently resumed, which implies we aren't sleeping.
3345 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003346 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 }
3348
3349 if (andResume) {
3350 r.results = null;
3351 r.newIntents = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003352 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 mService.reportResumedActivityLocked(r);
3354 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003355 r.state = ActivityState.RESUMED;
3356 } else {
3357 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3358 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003359 }
3360
3361 return true;
3362 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003363
3364 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003365 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3366 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3367 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3368 final ActivityRecord r = activities.get(activityNdx);
3369 if (r.appToken == token) {
3370 return true;
3371 }
3372 if (r.fullscreen && !r.finishing) {
3373 return false;
3374 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003375 }
3376 }
3377 return true;
3378 }
3379
3380 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3382 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3383 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3384 final ActivityRecord r = activities.get(activityNdx);
3385 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003386 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003387 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003388 }
3389 }
3390 }
3391
3392 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3393 boolean didSomething = false;
3394 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003395 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3396 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3397 int numActivities = activities.size();
3398 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3399 ActivityRecord r = activities.get(activityNdx);
3400 final boolean samePackage = r.packageName.equals(name)
3401 || (name == null && r.userId == userId);
3402 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3403 && (samePackage || r.task == lastTask)
3404 && (r.app == null || evenPersistent || !r.app.persistent)) {
3405 if (!doit) {
3406 if (r.finishing) {
3407 // If this activity is just finishing, then it is not
3408 // interesting as far as something to stop.
3409 continue;
3410 }
3411 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003412 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003413 didSomething = true;
3414 Slog.i(TAG, " Force finishing activity " + r);
3415 if (samePackage) {
3416 if (r.app != null) {
3417 r.app.removed = true;
3418 }
3419 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003420 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003421 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08003422 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08003423 }
3424 }
3425 }
3426 return didSomething;
3427 }
3428
3429 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003430 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003431 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003432 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3433 --maxNum, --taskNdx) {
3434 final TaskRecord task = mTaskHistory.get(taskNdx);
3435 ActivityRecord r = null;
3436 ActivityRecord top = null;
3437 int numActivities = 0;
3438 int numRunning = 0;
3439 final ArrayList<ActivityRecord> activities = task.mActivities;
3440 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3441 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003442
Craig Mautneraab647e2013-02-28 16:31:36 -08003443 // Initialize state for next task if needed.
3444 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3445 top = r;
3446 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003447 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003448
3449 // Add 'r' into the current task.
3450 numActivities++;
3451 if (r.app != null && r.app.thread != null) {
3452 numRunning++;
3453 }
3454
3455 if (localLOGV) Slog.v(
3456 TAG, r.intent.getComponent().flattenToShortString()
3457 + ": task=" + r.task);
3458 }
3459
3460 RunningTaskInfo ci = new RunningTaskInfo();
3461 ci.id = task.taskId;
3462 ci.baseActivity = r.intent.getComponent();
3463 ci.topActivity = top.intent.getComponent();
3464 if (top.thumbHolder != null) {
3465 ci.description = top.thumbHolder.lastDescription;
3466 }
3467 ci.numActivities = numActivities;
3468 ci.numRunning = numRunning;
3469 //System.out.println(
3470 // "#" + maxNum + ": " + " descr=" + ci.description);
3471 if (receiver != null) {
3472 if (localLOGV) Slog.v(
3473 TAG, "State=" + top.state + "Idle=" + top.idle
3474 + " app=" + top.app
3475 + " thr=" + (top.app != null ? top.app.thread : null));
3476 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3477 if (top.idle && top.app != null && top.app.thread != null) {
3478 topRecord = top;
3479 } else {
3480 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003481 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003482 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003483 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003484 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003485 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003486 }
3487 return topRecord;
3488 }
3489
3490 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003491 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003492 if (DEBUG_SWITCH) Slog.d(
3493 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003494 if (top >= 0) {
3495 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3496 int activityTop = activities.size() - 1;
3497 if (activityTop > 0) {
3498 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3499 "unhandled-back", true);
3500 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003501 }
3502 }
3503
Craig Mautnere79d42682013-04-01 19:01:53 -07003504 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003505 if (!containsApp(app)) {
3506 return;
3507 }
3508 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003509 if (mPausingActivity != null && mPausingActivity.app == app) {
3510 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3511 "App died while pausing: " + mPausingActivity);
3512 mPausingActivity = null;
3513 }
3514 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3515 mLastPausedActivity = null;
3516 }
3517
3518 // Remove this application's activities from active lists.
3519 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3520
3521 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003522 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003523 if (stack == null) {
3524 mStackSupervisor.resumeHomeActivity(null);
3525 } else if (!stack.resumeTopActivityLocked(null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003526 // If there was nothing to resume, and we are not already
3527 // restarting this process, but there is a visible activity that
3528 // is hosted by the process... then make sure all visible
3529 // activities are running, taking care of restarting this
3530 // process.
3531 if (hasVisibleActivities) {
3532 ensureActivitiesVisibleLocked(null, 0);
3533 }
3534 }
3535 }
3536 }
3537
Craig Mautnercae015f2013-02-08 14:31:27 -08003538 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003539 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3540 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3541 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3542 final ActivityRecord r = activities.get(activityNdx);
3543 if (r.app == app) {
3544 Slog.w(TAG, " Force finishing activity "
3545 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003546 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003547 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003548 }
3549 }
3550 }
3551
3552 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3553 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003554 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3555 final TaskRecord task = mTaskHistory.get(taskNdx);
3556 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003557 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003558 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3559 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003560 }
3561
3562 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3563 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3564
3565 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003566 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3567 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003568 }
3569 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003570 final int top = mTaskHistory.size() - 1;
3571 if (top >= 0) {
3572 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3573 int listTop = list.size() - 1;
3574 if (listTop >= 0) {
3575 activities.add(list.get(listTop));
3576 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003577 }
3578 } else {
3579 ItemMatcher matcher = new ItemMatcher();
3580 matcher.build(name);
3581
Craig Mautneraab647e2013-02-28 16:31:36 -08003582 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3583 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3584 if (matcher.match(r1, r1.intent.getComponent())) {
3585 activities.add(r1);
3586 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003587 }
3588 }
3589 }
3590
3591 return activities;
3592 }
3593
3594 ActivityRecord restartPackage(String packageName) {
3595 ActivityRecord starting = topRunningActivityLocked(null);
3596
3597 // All activities that came from the package must be
3598 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003599 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3600 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3601 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3602 final ActivityRecord a = activities.get(activityNdx);
3603 if (a.info.packageName.equals(packageName)) {
3604 a.forceNewConfig = true;
3605 if (starting != null && a == starting && a.visible) {
3606 a.startFreezingScreenLocked(starting.app,
3607 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3608 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003609 }
3610 }
3611 }
3612
3613 return starting;
3614 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003615
Craig Mautnerde4ef022013-04-07 19:01:33 -07003616 boolean removeTask(TaskRecord task) {
3617 mTaskHistory.remove(task);
3618 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003619 }
3620
Craig Mautnerde4ef022013-04-07 19:01:33 -07003621 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003622 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003623 if (toTop) {
3624 mTaskHistory.add(task);
3625 } else {
3626 mTaskHistory.add(0, task);
3627 }
3628 return task;
3629 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003630
3631 ArrayList<TaskRecord> getAllTasks() {
3632 return new ArrayList<TaskRecord>(mTaskHistory);
3633 }
3634
3635 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003636 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003637 if (task == null) {
3638 return;
3639 }
3640 task.stack.mTaskHistory.remove(task);
3641 task.stack = this;
3642 if (toTop) {
3643 mTaskHistory.add(task);
3644 } else {
3645 mTaskHistory.add(0, task);
3646 }
3647 }
3648
3649 public int getStackId() {
3650 return mStackId;
3651 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003652
3653 @Override
3654 public String toString() {
3655 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3656 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657}