blob: 4e3e4572917c0a05a31507976a7368b471a0d0b4 [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
Jeff Sharkey35be7562012-04-18 19:16:15 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
20import static android.content.pm.PackageManager.PERMISSION_GRANTED;
21
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070022import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080023import com.android.internal.util.Objects;
Craig Mautnercae015f2013-02-08 14:31:27 -080024import com.android.server.am.ActivityManagerService.ItemMatcher;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070025import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner4b71aa12012-12-27 17:20:01 -080026import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080027import com.android.server.wm.TaskGroup;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070028
29import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070030import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070031import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070032import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080033import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070034import android.app.IActivityManager;
Craig Mautnercae015f2013-02-08 14:31:27 -080035import android.app.IThumbnailReceiver;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070036import android.app.IThumbnailRetriever;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070037import android.app.IApplicationThread;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080039import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import android.app.IActivityManager.WaitResult;
41import android.content.ComponentName;
42import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070043import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070045import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080047import android.content.res.Resources;
48import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080049import android.graphics.Bitmap.Config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070051import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.os.Handler;
53import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090054import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.os.Message;
56import android.os.PowerManager;
57import android.os.RemoteException;
58import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070059import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070062import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063
Craig Mautnercae015f2013-02-08 14:31:27 -080064import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080065import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import java.lang.ref.WeakReference;
67import java.util.ArrayList;
68import java.util.Iterator;
69import java.util.List;
70
71/**
72 * State and management of a single stack of activities.
73 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070074final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070076 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
78 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
79 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
80 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
81 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
82 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
83 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
84 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070085 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautner9658b312013-02-28 10:55:59 -080086
Craig Mautner2420ead2013-04-01 17:13:20 -070087 static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES;
88 static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE;
89 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
90 static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070091
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094 // How long we wait until giving up on the last activity telling us it
95 // is idle.
96 static final int IDLE_TIMEOUT = 10*1000;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070097
98 // Ticks during which we check progress while waiting for an app to launch.
99 static final int LAUNCH_TICK = 500;
100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700101 // How long we wait until giving up on the last activity to pause. This
102 // is short because it directly impacts the responsiveness of starting the
103 // next activity.
104 static final int PAUSE_TIMEOUT = 500;
105
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700106 // How long we wait for the activity to tell us it has stopped before
107 // giving up. This is a good amount of time because we really need this
108 // from the application in order to get its saved state.
109 static final int STOP_TIMEOUT = 10*1000;
110
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800111 // How long we can hold the sleep wake lock before giving up.
112 static final int SLEEP_TIMEOUT = 5*1000;
113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700114 // How long we can hold the launch wake lock before giving up.
115 static final int LAUNCH_TIMEOUT = 10*1000;
116
117 // How long we wait until giving up on an activity telling us it has
118 // finished destroying itself.
119 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800122 // disabled.
123 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700125 // How long between activity launches that we consider safe to not warn
126 // the user about an unexpected activity being launched on top.
127 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 // Set to false to disable the preview that is shown while a new activity
130 // is being started.
131 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133 enum ActivityState {
134 INITIALIZING,
135 RESUMED,
136 PAUSING,
137 PAUSED,
138 STOPPING,
139 STOPPED,
140 FINISHING,
141 DESTROYING,
142 DESTROYED
143 }
144
145 final ActivityManagerService mService;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800146
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700147 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 /**
150 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151 * running) activities. It contains #TaskRecord objects.
152 */
153 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
154
155 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156 * Used for validating app tokens with window manager.
157 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800158 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 /**
161 * List of running activities, sorted by recent usage.
162 * The first entry in the list is the least recently used.
163 * It contains HistoryRecord objects.
164 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700165 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
168 * List of activities that are waiting for a new activity
169 * to become visible before completing whatever operation they are
170 * supposed to do.
171 */
172 final ArrayList<ActivityRecord> mWaitingVisibleActivities
173 = new ArrayList<ActivityRecord>();
174
175 /**
176 * List of activities that are ready to be stopped, but waiting
177 * for the next activity to settle down before doing so. It contains
178 * HistoryRecord objects.
179 */
180 final ArrayList<ActivityRecord> mStoppingActivities
181 = new ArrayList<ActivityRecord>();
182
183 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800184 * List of activities that are in the process of going to sleep.
185 */
186 final ArrayList<ActivityRecord> mGoingToSleepActivities
187 = new ArrayList<ActivityRecord>();
188
189 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700190 * Animations that for the current transition have requested not to
191 * be considered for the transition animation.
192 */
193 final ArrayList<ActivityRecord> mNoAnimActivities
194 = new ArrayList<ActivityRecord>();
195
196 /**
197 * List of activities that are ready to be finished, but waiting
198 * for the previous activity to settle down before doing so. It contains
199 * HistoryRecord objects.
200 */
201 final ArrayList<ActivityRecord> mFinishingActivities
202 = new ArrayList<ActivityRecord>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800203
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700204 /**
205 * List of people waiting to find out about the next launched activity.
206 */
207 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
208 = new ArrayList<IActivityManager.WaitResult>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210 /**
211 * List of people waiting to find out about the next visible activity.
212 */
213 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
214 = new ArrayList<IActivityManager.WaitResult>();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700215
216 final ArrayList<UserStartedState> mStartingUsers
217 = new ArrayList<UserStartedState>();
218
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700219 /**
220 * Set when the system is going to sleep, until we have
221 * successfully paused the current activity and released our wake lock.
222 * At that point the system is allowed to actually sleep.
223 */
224 final PowerManager.WakeLock mGoingToSleep;
225
226 /**
227 * We don't want to allow the device to go to sleep while in the process
228 * of launching an activity. This is primarily to allow alarm intent
229 * receivers to launch an activity and get that to run before the device
230 * goes back to sleep.
231 */
232 final PowerManager.WakeLock mLaunchingActivity;
233
234 /**
235 * When we are in the process of pausing an activity, before starting the
236 * next one, this variable holds the activity that is currently being paused.
237 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800238 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700239
240 /**
241 * This is the last activity that we put into the paused state. This is
242 * used to determine if we need to do an activity transition while sleeping,
243 * when we normally hold the top activity paused.
244 */
245 ActivityRecord mLastPausedActivity = null;
246
247 /**
248 * Current activity that is resumed, or null if there is none.
249 */
250 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800251
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700252 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700253 * This is the last activity that has been started. It is only used to
254 * identify when multiple activities are started at once so that the user
255 * can be warned they may not be in the activity they think they are.
256 */
257 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800258
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700259 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700260 * Set when we know we are going to be calling updateConfiguration()
261 * soon, so want to skip intermediate config checks.
262 */
263 boolean mConfigWillChange;
264
265 /**
266 * Set to indicate whether to issue an onUserLeaving callback when a
267 * newly launched activity is being brought in front of us.
268 */
269 boolean mUserLeaving = false;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700271 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800272
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800273 /**
274 * Set when we have taken too long waiting to go to sleep.
275 */
276 boolean mSleepTimeout = false;
277
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700278 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800279 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
280 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
281 */
282 private ActivityRecord mLastScreenshotActivity = null;
283 private Bitmap mLastScreenshotBitmap = null;
284
Craig Mautnercae015f2013-02-08 14:31:27 -0800285 /**
286 * List of ActivityRecord objects that have been finished and must
287 * still report back to a pending thumbnail receiver.
288 */
289 private final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>();
290
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800291 int mThumbnailWidth = -1;
292 int mThumbnailHeight = -1;
293
Amith Yamasani742a6712011-05-04 14:49:28 -0700294 private int mCurrentUser;
295
Craig Mautnerc00204b2013-03-05 15:02:14 -0800296 final int mStackId;
297
Craig Mautner27084302013-03-25 08:05:25 -0700298 /** Run all ActivityStacks through this */
299 final ActivityStackSupervisor mStackSupervisor;
300
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800301 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
302 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
303 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
304 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
305 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
306 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
307 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700308 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700309 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700310 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
311
312 static class ScheduleDestroyArgs {
313 final ProcessRecord mOwner;
314 final boolean mOomAdj;
315 final String mReason;
316 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
317 mOwner = owner;
318 mOomAdj = oomAdj;
319 mReason = reason;
320 }
321 }
322
Zoran Marcetaf958b322012-08-09 20:27:12 +0900323 final Handler mHandler;
324
325 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 //public Handler() {
327 // if (localLOGV) Slog.v(TAG, "Handler started!");
328 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900329 public ActivityStackHandler(Looper looper) {
330 super(looper);
331 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700332
Zoran Marcetaf958b322012-08-09 20:27:12 +0900333 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700334 public void handleMessage(Message msg) {
335 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800336 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700337 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700338 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700339 Slog.w(TAG, "Sleep timeout! Sleeping now.");
340 mSleepTimeout = true;
341 checkReadyForSleepLocked();
342 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800343 }
344 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700345 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800346 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700347 // We don't at this point know if the activity is fullscreen,
348 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800349 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700350 synchronized (mService) {
351 if (r.app != null) {
352 mService.logAppTooSlow(r.app, r.pauseTime,
353 "pausing " + r);
354 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700355
Craig Mautnerd2328952013-03-05 12:46:26 -0800356 activityPausedLocked(r != null ? r.appToken : null, true);
357 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700358 } break;
359 case IDLE_TIMEOUT_MSG: {
360 if (mService.mDidDexOpt) {
361 mService.mDidDexOpt = false;
362 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
363 nmsg.obj = msg.obj;
364 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
365 return;
366 }
367 // We don't at this point know if the activity is fullscreen,
368 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800369 ActivityRecord r = (ActivityRecord)msg.obj;
370 Slog.w(TAG, "Activity idle timeout for " + r);
Craig Mautnere79d42682013-04-01 19:01:53 -0700371 synchronized (this) {
372 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
373 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700374 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700375 case LAUNCH_TICK_MSG: {
376 ActivityRecord r = (ActivityRecord)msg.obj;
377 synchronized (mService) {
378 if (r.continueLaunchTickingLocked()) {
379 mService.logAppTooSlow(r.app, r.launchTickTime,
380 "launching " + r);
381 }
382 }
383 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700384 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800385 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386 // We don't at this point know if the activity is fullscreen,
387 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800388 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800389 synchronized (mService) {
390 activityDestroyedLocked(r != null ? r.appToken : null);
391 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700392 } break;
393 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800394 ActivityRecord r = (ActivityRecord)msg.obj;
Craig Mautnere79d42682013-04-01 19:01:53 -0700395 synchronized (mService) {
396 activityIdleInternalLocked(r != null ? r.appToken : null, false, null);
397 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700398 } break;
399 case LAUNCH_TIMEOUT_MSG: {
400 if (mService.mDidDexOpt) {
401 mService.mDidDexOpt = false;
402 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
403 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
404 return;
405 }
406 synchronized (mService) {
407 if (mLaunchingActivity.isHeld()) {
408 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
409 mLaunchingActivity.release();
410 }
411 }
412 } break;
413 case RESUME_TOP_ACTIVITY_MSG: {
414 synchronized (mService) {
415 resumeTopActivityLocked(null);
416 }
417 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700418 case STOP_TIMEOUT_MSG: {
419 ActivityRecord r = (ActivityRecord)msg.obj;
420 // We don't at this point know if the activity is fullscreen,
421 // so we need to be conservative and assume it isn't.
422 Slog.w(TAG, "Activity stop timeout for " + r);
423 synchronized (mService) {
424 if (r.isInHistory()) {
425 activityStoppedLocked(r, null, null, null);
426 }
427 }
428 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700429 case DESTROY_ACTIVITIES_MSG: {
430 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
431 synchronized (mService) {
432 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
433 }
434 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 }
436 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800437 }
438
Craig Mautner000f0022013-02-26 15:04:29 -0800439 private int numActivities() {
440 int count = 0;
441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 count += mTaskHistory.get(taskNdx).mActivities.size();
443 }
444 return count;
445 }
446
Craig Mautner2219a1b2013-03-25 09:44:30 -0700447 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId,
Craig Mautner2420ead2013-04-01 17:13:20 -0700448 ActivityStackSupervisor supervisor, int userId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900449 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700450 mService = service;
451 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700452 PowerManager pm =
453 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
454 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
455 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
456 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800457 mStackId = stackId;
Craig Mautner27084302013-03-25 08:05:25 -0700458 mStackSupervisor = supervisor;
Craig Mautner2420ead2013-04-01 17:13:20 -0700459 mCurrentUser = userId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700460 }
Craig Mautner5962b122012-10-05 14:45:52 -0700461
462 private boolean okToShow(ActivityRecord r) {
463 return r.userId == mCurrentUser
464 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
465 }
466
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700467 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
469 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800470 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800471 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
472 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800473 if (!r.finishing && r != notTop && okToShow(r)) {
474 return r;
475 }
476 }
477 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700478 return null;
479 }
480
481 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800482 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
483 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800484 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800485 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
486 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800487 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
488 return r;
489 }
490 }
491 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700492 return null;
493 }
494
495 /**
496 * This is a simplified version of topRunningActivityLocked that provides a number of
497 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800498 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700499 * @param token If non-null, any history records matching this token will be skipped.
500 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800501 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700502 * @return Returns the HistoryRecord of the next activity on the stack.
503 */
504 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800505 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
506 TaskRecord task = mTaskHistory.get(taskNdx);
507 if (task.taskId == taskId) {
508 continue;
509 }
510 ArrayList<ActivityRecord> activities = task.mActivities;
511 for (int i = activities.size() - 1; i >= 0; --i) {
512 final ActivityRecord r = activities.get(i);
513 // Note: the taskId check depends on real taskId fields being non-zero
514 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
515 return r;
516 }
517 }
518 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700519 return null;
520 }
521
Craig Mautnerd2328952013-03-05 12:46:26 -0800522 TaskRecord taskForIdLocked(int id) {
523 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
524 final TaskRecord task = mTaskHistory.get(taskNdx);
525 if (task.taskId == id) {
526 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800527 }
528 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700529 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700530 }
531
Craig Mautnerd2328952013-03-05 12:46:26 -0800532 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800533 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800534 if (r != null) {
535 final TaskRecord task = r.task;
536 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
537 if (task.stack != this) Slog.w(TAG,
538 "Illegal state! task does not point to stack it is in.");
539 return r;
540 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800541 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800542 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800543 }
544
Craig Mautner2420ead2013-04-01 17:13:20 -0700545 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700546 final boolean hadit = mLRUActivities.remove(r);
547 mLRUActivities.add(r);
548 return hadit;
549 }
550
551 /**
552 * Returns the top activity in any existing task matching the given
553 * Intent. Returns null if no such task is found.
554 */
555 private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
556 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);
Craig Mautner000f0022013-02-26 15:04:29 -0800561
562 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
563 final TaskRecord task = mTaskHistory.get(taskNdx);
564 final ActivityRecord r = task.getTopActivity();
565 if (r == null || r.finishing || r.userId != userId ||
566 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
567 continue;
568 }
569
570 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
571 // + "/aff=" + r.task.affinity + " to new cls="
572 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
573 if (task.affinity != null) {
574 if (task.affinity.equals(info.taskAffinity)) {
575 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700576 return r;
577 }
Craig Mautner000f0022013-02-26 15:04:29 -0800578 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
579 //Slog.i(TAG, "Found matching class!");
580 //dump();
581 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
582 return r;
583 } else if (task.affinityIntent != null
584 && task.affinityIntent.getComponent().equals(cls)) {
585 //Slog.i(TAG, "Found matching class!");
586 //dump();
587 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
588 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700589 }
590 }
591
592 return null;
593 }
594
595 /**
596 * Returns the first activity (starting from the top of the stack) that
597 * is the same as the given activity. Returns null if no such activity
598 * is found.
599 */
600 private ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
601 ComponentName cls = intent.getComponent();
602 if (info.targetActivity != null) {
603 cls = new ComponentName(info.packageName, info.targetActivity);
604 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700605 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700606
Craig Mautner000f0022013-02-26 15:04:29 -0800607 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
608 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
609 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
610 ActivityRecord r = activities.get(activityNdx);
611 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700612 //Slog.i(TAG, "Found matching class!");
613 //dump();
614 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
615 return r;
616 }
617 }
618 }
619
620 return null;
621 }
622
Amith Yamasani742a6712011-05-04 14:49:28 -0700623 /*
624 * Move the activities around in the stack to bring a user to the foreground.
625 * @return whether there are any activities for the specified user.
626 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700627 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 if (VALIDATE_TOKENS) {
629 validateAppTokensLocked();
630 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700631 mStartingUsers.add(uss);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800632 if (mCurrentUser == userId) {
633 return true;
634 }
635 mCurrentUser = userId;
636
637 // Move userId's tasks to the top.
638 boolean haveActivities = false;
639 TaskRecord task = null;
640 int index = mTaskHistory.size();
641 for (int i = 0; i < index; ++i) {
642 task = mTaskHistory.get(i);
643 if (task.userId == userId) {
644 haveActivities = true;
645 mTaskHistory.remove(i);
646 mTaskHistory.add(task);
647 --index;
648 }
649 }
650
651 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
652 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800653 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700654 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700655 }
656
Craig Mautner2420ead2013-04-01 17:13:20 -0700657 void minimalResumeActivityLocked(ActivityRecord r) {
658 r.state = ActivityState.RESUMED;
659 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
660 + " (starting new instance)");
661 r.stopped = false;
662 mResumedActivity = r;
663 r.task.touchActiveTime();
664 mService.addRecentTaskLocked(r.task);
665 completeResumeLocked(r);
666 checkReadyForSleepLocked();
667 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700668 }
669
Craig Mautnere79d42682013-04-01 19:01:53 -0700670 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700671 if (r.launchTime == 0) {
672 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700674 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700675 }
676 } else if (mInitialStartTime == 0) {
677 mInitialStartTime = SystemClock.uptimeMillis();
678 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800680
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800681 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700682 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700683 if (!mGoingToSleep.isHeld()) {
684 mGoingToSleep.acquire();
685 if (mLaunchingActivity.isHeld()) {
686 mLaunchingActivity.release();
687 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
688 }
689 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800690 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
691 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
692 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
693 checkReadyForSleepLocked();
694 }
695 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700696
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800697 void awakeFromSleepingLocked() {
698 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
699 mSleepTimeout = false;
700 if (mGoingToSleep.isHeld()) {
701 mGoingToSleep.release();
702 }
703 // Ensure activities are no longer sleeping.
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 activities.get(activityNdx).setSleeping(false);
708 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800709 }
710 mGoingToSleepActivities.clear();
711 }
712
713 void activitySleptLocked(ActivityRecord r) {
714 mGoingToSleepActivities.remove(r);
715 checkReadyForSleepLocked();
716 }
717
Craig Mautnere79d42682013-04-01 19:01:53 -0700718 // Checked.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800719 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700720 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800721 // Do not care.
722 return;
723 }
724
725 if (!mSleepTimeout) {
726 if (mResumedActivity != null) {
727 // Still have something resumed; can't sleep until it is paused.
728 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
730 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800731 return;
732 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800733 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800734 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800735 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800736 return;
737 }
738
739 if (mStoppingActivities.size() > 0) {
740 // Still need to tell some activities to stop; can't sleep yet.
741 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
742 + mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700743 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800744 return;
745 }
746
747 ensureActivitiesVisibleLocked(null, 0);
748
749 // Make sure any stopped but visible activities are now sleeping.
750 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800751 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
752 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
753 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
754 final ActivityRecord r = activities.get(activityNdx);
755 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
756 r.setSleeping(true);
757 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800758 }
759 }
760
761 if (mGoingToSleepActivities.size() > 0) {
762 // Still need to tell some activities to sleep; can't sleep yet.
763 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
764 + mGoingToSleepActivities.size() + " activities");
765 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 }
767 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800768
769 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
770
771 if (mGoingToSleep.isHeld()) {
772 mGoingToSleep.release();
773 }
774 if (mService.mShuttingDown) {
775 mService.notifyAll();
776 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700777 }
Craig Mautner59c00972012-07-30 12:10:24 -0700778
Dianne Hackbornd2835932010-12-13 16:28:46 -0800779 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800780 if (who.noDisplay) {
781 return null;
782 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800783
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800784 Resources res = mService.mContext.getResources();
785 int w = mThumbnailWidth;
786 int h = mThumbnailHeight;
787 if (w < 0) {
788 mThumbnailWidth = w =
789 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
790 mThumbnailHeight = h =
791 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
792 }
793
794 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800795 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
796 || mLastScreenshotBitmap.getWidth() != w
797 || mLastScreenshotBitmap.getHeight() != h) {
798 mLastScreenshotActivity = who;
799 mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications(
800 who.appToken, Display.DEFAULT_DISPLAY, w, h);
801 }
802 if (mLastScreenshotBitmap != null) {
803 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
804 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800805 }
806 return null;
807 }
808
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700809 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800810 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800811 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700812 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800813 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 ActivityRecord prev = mResumedActivity;
815 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700816 Slog.e(TAG, "Trying to pause when nothing is resumed",
817 new RuntimeException("here").fillInStackTrace());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 resumeTopActivityLocked(null);
819 return;
820 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700821 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
822 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800824 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700825 mLastPausedActivity = prev;
826 prev.state = ActivityState.PAUSING;
827 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700828 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829
830 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800831
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 if (prev.app != null && prev.app.thread != null) {
833 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
834 try {
835 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700836 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800838 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
839 userLeaving, prev.configChangeFlags);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700840 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700841 mService.updateUsageStats(prev, false);
842 }
843 } catch (Exception e) {
844 // Ignore exception, if process died other code will cleanup.
845 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800846 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 mLastPausedActivity = null;
848 }
849 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800850 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 mLastPausedActivity = null;
852 }
853
854 // If we are not going to sleep, we want to ensure the device is
855 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700856 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 mLaunchingActivity.acquire();
858 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
859 // To be safe, don't allow the wake lock to be held for too long.
860 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
861 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
862 }
863 }
864
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 // Have the window manager pause its key dispatching until the new
867 // activity has started. If we're pausing the activity just because
868 // the screen is being turned off and the UI is sleeping, don't interrupt
869 // key dispatch; the same activity will pick it up again on wakeup.
870 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800871 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700872 } else {
873 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
874 }
875
876 // Schedule a pause timeout in case the app doesn't respond.
877 // We don't give it much time because this directly impacts the
878 // responsiveness seen by the user.
879 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
880 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700881 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700882 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
883 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
884 } else {
885 // This activity failed to schedule the
886 // pause, so just treat it as being paused now.
887 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700889 }
890 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700891
Craig Mautnerd2328952013-03-05 12:46:26 -0800892 final void activityResumedLocked(IBinder token) {
893 final ActivityRecord r = ActivityRecord.forToken(token);
894 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
895 r.icicle = null;
896 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700897 }
898
Craig Mautnerd2328952013-03-05 12:46:26 -0800899 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800901 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902
Craig Mautnerd2328952013-03-05 12:46:26 -0800903 final ActivityRecord r = isInStackLocked(token);
904 if (r != null) {
905 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
906 if (mPausingActivity == r) {
907 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
908 + (timeout ? " (due to timeout)" : " (pause complete)"));
909 r.state = ActivityState.PAUSED;
910 completePauseLocked();
911 } else {
912 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
913 r.userId, System.identityHashCode(r), r.shortComponentName,
914 mPausingActivity != null
915 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916 }
917 }
918 }
919
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700920 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
921 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700922 if (r.state != ActivityState.STOPPING) {
923 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
924 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
925 return;
926 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700927 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700928 if (icicle != null) {
929 // If icicle is null, this is happening due to a timeout, so we
930 // haven't really saved the state.
931 r.icicle = icicle;
932 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800933 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700934 r.updateThumbnail(thumbnail, description);
935 }
936 if (!r.stopped) {
937 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
938 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
939 r.stopped = true;
940 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700941 if (r.finishing) {
942 r.clearOptionsLocked();
943 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700944 if (r.configDestroy) {
945 destroyActivityLocked(r, true, false, "stop-config");
946 resumeTopActivityLocked(null);
947 } else {
948 // Now that this process has stopped, we may want to consider
949 // it to be the previous app to try to keep around in case
950 // the user wants to return to it.
951 ProcessRecord fgApp = null;
952 if (mResumedActivity != null) {
953 fgApp = mResumedActivity.app;
954 } else if (mPausingActivity != null) {
955 fgApp = mPausingActivity.app;
956 }
957 if (r.app != null && fgApp != null && r.app != fgApp
958 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
959 && r.app != mService.mHomeProcess) {
960 mService.mPreviousProcess = r.app;
961 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
962 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800963 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700964 }
965 }
966 }
967
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800968 private final void completePauseLocked() {
969 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700970 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800971
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800972 if (prev != null) {
973 if (prev.finishing) {
974 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700975 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800976 } else if (prev.app != null) {
977 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
978 if (prev.waitingVisible) {
979 prev.waitingVisible = false;
980 mWaitingVisibleActivities.remove(prev);
981 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
982 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800983 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 if (prev.configDestroy) {
985 // The previous is being paused because the configuration
986 // is changing, which means it is actually stopping...
987 // To juggle the fact that we are also starting a new
988 // instance right now, we need to first completely stop
989 // the current instance before starting the new one.
990 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
991 destroyActivityLocked(prev, true, false, "pause-config");
992 } else {
993 mStoppingActivities.add(prev);
994 if (mStoppingActivities.size() > 3) {
995 // If we already have a few activities waiting to stop,
996 // then give up on things going idle and start clearing
997 // them out.
998 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
999 scheduleIdleLocked();
1000 } else {
1001 checkReadyForSleepLocked();
1002 }
1003 }
1004 } else {
1005 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1006 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001007 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001008 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001010
Craig Mautnere11f2b72013-04-01 12:37:17 -07001011 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001012 resumeTopActivityLocked(prev);
1013 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001014 checkReadyForSleepLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001015 ActivityRecord top = topRunningActivityLocked(null);
1016 if (top == null || (prev != null && top != prev)) {
1017 // If there are no more activities available to run,
1018 // do resume anyway to start something. Also if the top
1019 // activity on the stack is not the just paused activity,
1020 // we need to go ahead and resume it to ensure we complete
1021 // an in-flight app switch.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001022 resumeTopActivityLocked(null);
1023 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001025
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001026 if (prev != null) {
1027 prev.resumeKeyDispatchingLocked();
1028 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029
1030 if (prev.app != null && prev.cpuTimeAtResume > 0
1031 && mService.mBatteryStatsService.isOnBattery()) {
1032 long diff = 0;
1033 synchronized (mService.mProcessStatsThread) {
1034 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1035 - prev.cpuTimeAtResume;
1036 }
1037 if (diff > 0) {
1038 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1039 synchronized (bsi) {
1040 BatteryStatsImpl.Uid.Proc ps =
1041 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1042 prev.info.packageName);
1043 if (ps != null) {
1044 ps.addForegroundTimeLocked(diff);
1045 }
1046 }
1047 }
1048 }
1049 prev.cpuTimeAtResume = 0; // reset it
1050 }
1051
Craig Mautnere79d42682013-04-01 19:01:53 -07001052 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053 /**
1054 * Once we know that we have asked an application to put an activity in
1055 * the resumed state (either by launching it or explicitly telling it),
1056 * this function updates the rest of our state to match that fact.
1057 */
1058 private final void completeResumeLocked(ActivityRecord next) {
1059 next.idle = false;
1060 next.results = null;
1061 next.newIntents = null;
1062
1063 // schedule an idle timeout in case the app doesn't do it for us.
1064 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1065 msg.obj = next;
1066 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1067
1068 if (false) {
1069 // The activity was never told to pause, so just keep
1070 // things going as-is. To maintain our own state,
1071 // we need to emulate it coming back and saying it is
1072 // idle.
1073 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1074 msg.obj = next;
1075 mHandler.sendMessage(msg);
1076 }
1077
Craig Mautner2219a1b2013-03-25 09:44:30 -07001078 if (mStackSupervisor.isMainStack(this)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001079 // Should this be done for all stacks, not just mMainStack?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001080 mService.reportResumedActivityLocked(next);
1081 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07001082
Craig Mautner2219a1b2013-03-25 09:44:30 -07001083 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084 mService.setFocusedActivityLocked(next);
1085 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001086 next.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 ensureActivitiesVisibleLocked(null, 0);
1088 mService.mWindowManager.executeAppTransition();
1089 mNoAnimActivities.clear();
1090
1091 // Mark the point when the activity is resuming
1092 // TODO: To be more accurate, the mark should be before the onCreate,
1093 // not after the onResume. But for subsequent starts, onResume is fine.
1094 if (next.app != null) {
1095 synchronized (mService.mProcessStatsThread) {
1096 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1097 }
1098 } else {
1099 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1100 }
1101 }
1102
Craig Mautnere79d42682013-04-01 19:01:53 -07001103 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001104 /**
1105 * Make sure that all activities that need to be visible (that is, they
1106 * currently can be seen by the user) actually are.
1107 */
1108 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1109 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1110 if (DEBUG_VISBILITY) Slog.v(
1111 TAG, "ensureActivitiesVisible behind " + top
1112 + " configChanges=0x" + Integer.toHexString(configChanges));
1113
1114 // If the top activity is not fullscreen, then we need to
1115 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001116 boolean aboveTop = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001117 boolean behindFullscreen = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001118 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1119 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1120 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1121 final ActivityRecord r = activities.get(activityNdx);
1122 if (r.finishing) {
1123 continue;
1124 }
1125 if (aboveTop && r != top) {
1126 continue;
1127 }
1128 aboveTop = false;
1129 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001130 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001131 TAG, "Make visible? " + r + " finishing=" + r.finishing
1132 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001133
Craig Mautnerd44711d2013-02-23 11:24:36 -08001134 final boolean doThisProcess = onlyThisProcess == null
1135 || onlyThisProcess.equals(r.processName);
1136
1137 // First: if this is not the current activity being started, make
1138 // sure it matches the current configuration.
1139 if (r != starting && doThisProcess) {
1140 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001142
1143 if (r.app == null || r.app.thread == null) {
1144 if (onlyThisProcess == null
1145 || onlyThisProcess.equals(r.processName)) {
1146 // This activity needs to be visible, but isn't even
1147 // running... get it started, but don't resume it
1148 // at this point.
1149 if (DEBUG_VISBILITY) Slog.v(
1150 TAG, "Start and freeze screen for " + r);
1151 if (r != starting) {
1152 r.startFreezingScreenLocked(r.app, configChanges);
1153 }
1154 if (!r.visible) {
1155 if (DEBUG_VISBILITY) Slog.v(
1156 TAG, "Starting and making visible: " + r);
1157 mService.mWindowManager.setAppVisibility(r.appToken, true);
1158 }
1159 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001160 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001161 }
1162 }
1163
1164 } else if (r.visible) {
1165 // If this activity is already visible, then there is nothing
1166 // else to do here.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001167 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001168 TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001169 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001170
1171 } else if (onlyThisProcess == null) {
1172 // This activity is not currently visible, but is running.
1173 // Tell it to become visible.
1174 r.visible = true;
1175 if (r.state != ActivityState.RESUMED && r != starting) {
1176 // If this activity is paused, tell it
1177 // to now show its window.
1178 if (DEBUG_VISBILITY) Slog.v(
1179 TAG, "Making visible and scheduling visibility: " + r);
1180 try {
1181 mService.mWindowManager.setAppVisibility(r.appToken, true);
1182 r.sleeping = false;
1183 r.app.pendingUiClean = true;
1184 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1185 r.stopFreezingScreenLocked(false);
1186 } catch (Exception e) {
1187 // Just skip on any failure; we'll make it
1188 // visible when it next restarts.
1189 Slog.w(TAG, "Exception thrown making visibile: "
1190 + r.intent.getComponent(), e);
1191 }
1192 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194
Craig Mautnerd44711d2013-02-23 11:24:36 -08001195 // Aggregate current change flags.
1196 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197
Craig Mautnerd44711d2013-02-23 11:24:36 -08001198 if (r.fullscreen) {
1199 // At this point, nothing else needs to be shown
1200 if (DEBUG_VISBILITY) Slog.v(
1201 TAG, "Stopping: fullscreen at " + r);
1202 behindFullscreen = true;
1203 }
1204 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 if (r.visible) {
1206 if (DEBUG_VISBILITY) Slog.v(
1207 TAG, "Making invisible: " + r);
1208 r.visible = false;
1209 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001210 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 if ((r.state == ActivityState.STOPPING
1212 || r.state == ActivityState.STOPPED)
1213 && r.app != null && r.app.thread != null) {
1214 if (DEBUG_VISBILITY) Slog.v(
1215 TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001216 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001217 }
1218 } catch (Exception e) {
1219 // Just skip on any failure; we'll make it
1220 // visible when it next restarts.
1221 Slog.w(TAG, "Exception thrown making hidden: "
1222 + r.intent.getComponent(), e);
1223 }
1224 } else {
1225 if (DEBUG_VISBILITY) Slog.v(
1226 TAG, "Already invisible: " + r);
1227 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228 }
1229 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001230 }
1231 }
1232
Craig Mautnere79d42682013-04-01 19:01:53 -07001233 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 /**
1235 * Version of ensureActivitiesVisible that can easily be called anywhere.
1236 */
1237 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1238 int configChanges) {
1239 ActivityRecord r = topRunningActivityLocked(null);
1240 if (r != null) {
1241 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1242 }
1243 }
Craig Mautner58547802013-03-05 08:23:53 -08001244
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 /**
1246 * Ensure that the top activity in the stack is resumed.
1247 *
1248 * @param prev The previously resumed activity, for when in the process
1249 * of pausing; can be null to call from elsewhere.
1250 *
1251 * @return Returns true if something is being resumed, or false if
1252 * nothing happened.
1253 */
1254 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001255 return resumeTopActivityLocked(prev, null);
1256 }
1257
1258 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 // Find the first activity that is not finishing.
1260 ActivityRecord next = topRunningActivityLocked(null);
1261
1262 // Remember how we'll process this pause/resume situation, and ensure
1263 // that the state is reset however we wind up proceeding.
1264 final boolean userLeaving = mUserLeaving;
1265 mUserLeaving = false;
1266
1267 if (next == null) {
1268 // There are no more activities! Let's just start up the
1269 // Launcher...
Craig Mautner2219a1b2013-03-25 09:44:30 -07001270 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001271 ActivityOptions.abort(options);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001272 return mService.startHomeActivityLocked(mCurrentUser);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001273 }
1274 }
1275
1276 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278 // If the top activity is the resumed one, nothing to do.
1279 if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
1280 // Make sure we have executed any pending transitions, since there
1281 // should be nothing left to do at this point.
1282 mService.mWindowManager.executeAppTransition();
1283 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001284 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285 return false;
1286 }
1287
1288 // If we are sleeping, and there is no resumed activity, and the top
1289 // activity is paused, well that is the state we want.
1290 if ((mService.mSleeping || mService.mShuttingDown)
p13451dbad2872012-04-18 11:39:23 +09001291 && mLastPausedActivity == next
1292 && (next.state == ActivityState.PAUSED
1293 || next.state == ActivityState.STOPPED
1294 || next.state == ActivityState.STOPPING)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001295 // Make sure we have executed any pending transitions, since there
1296 // should be nothing left to do at this point.
1297 mService.mWindowManager.executeAppTransition();
1298 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001299 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001300 return false;
1301 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001302
1303 // Make sure that the user who owns this activity is started. If not,
1304 // we will just leave it as is because someone should be bringing
1305 // another user's activities to the top of the stack.
1306 if (mService.mStartedUsers.get(next.userId) == null) {
1307 Slog.w(TAG, "Skipping resume of top activity " + next
1308 + ": user " + next.userId + " is stopped");
1309 return false;
1310 }
1311
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 // The activity may be waiting for stop, but that is no longer
1313 // appropriate for it.
1314 mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001315 mGoingToSleepActivities.remove(next);
1316 next.sleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 mWaitingVisibleActivities.remove(next);
1318
Dianne Hackborn84375872012-06-01 19:03:50 -07001319 next.updateOptionsLocked(options);
1320
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001321 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1322
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001323 // If we are currently pausing an activity, then don't do anything
1324 // until that is done.
1325 if (mPausingActivity != null) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001326 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG,
1327 "Skip resume: pausing=" + mPausingActivity);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001328 return false;
1329 }
1330
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001331 // Okay we are now going to start a switch, to 'next'. We may first
1332 // have to pause the current activity, but this is an important point
1333 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001334 // XXX "App Redirected" dialog is getting too many false positives
1335 // at this point, so turn off for now.
1336 if (false) {
1337 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1338 long now = SystemClock.uptimeMillis();
1339 final boolean inTime = mLastStartedActivity.startTime != 0
1340 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1341 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1342 final int nextUid = next.info.applicationInfo.uid;
1343 if (inTime && lastUid != nextUid
1344 && lastUid != next.launchedFromUid
1345 && mService.checkPermission(
1346 android.Manifest.permission.STOP_APP_SWITCHES,
1347 -1, next.launchedFromUid)
1348 != PackageManager.PERMISSION_GRANTED) {
1349 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1350 } else {
1351 next.startTime = now;
1352 mLastStartedActivity = next;
1353 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001354 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001355 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001356 mLastStartedActivity = next;
1357 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001358 }
Craig Mautner58547802013-03-05 08:23:53 -08001359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 // We need to start pausing the current activity so the top one
1361 // can be resumed...
1362 if (mResumedActivity != null) {
1363 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001364 // At this point we want to put the upcoming activity's process
1365 // at the top of the LRU list, since we know we will be needing it
1366 // very soon and it would be a waste to let it get killed if it
1367 // happens to be sitting towards the end.
1368 if (next.app != null && next.app.thread != null) {
1369 // No reason to do full oom adj update here; we'll let that
1370 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001371 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001372 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001373 startPausingLocked(userLeaving, false);
1374 return true;
1375 }
1376
Christopher Tated3f175c2012-06-14 14:16:54 -07001377 // If the most recent activity was noHistory but was only stopped rather
1378 // than stopped+finished because the device went to sleep, we need to make
1379 // sure to finish it as we're making a new activity topmost.
1380 final ActivityRecord last = mLastPausedActivity;
1381 if (mService.mSleeping && last != null && !last.finishing) {
1382 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1383 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1384 if (DEBUG_STATES) {
1385 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1386 }
1387 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001388 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001389 }
1390 }
1391
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001392 if (prev != null && prev != next) {
1393 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1394 prev.waitingVisible = true;
1395 mWaitingVisibleActivities.add(prev);
1396 if (DEBUG_SWITCH) Slog.v(
1397 TAG, "Resuming top, waiting visible to hide: " + prev);
1398 } else {
1399 // The next activity is already visible, so hide the previous
1400 // activity's windows right now so we can show the new one ASAP.
1401 // We only do this if the previous is finishing, which should mean
1402 // it is on top of the one being resumed so hiding it quickly
1403 // is good. Otherwise, we want to do the normal route of allowing
1404 // the resumed activity to be shown so we can decide if the
1405 // previous should actually be hidden depending on whether the
1406 // new one is found to be full-screen or not.
1407 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001408 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1410 + prev + ", waitingVisible="
1411 + (prev != null ? prev.waitingVisible : null)
1412 + ", nowVisible=" + next.nowVisible);
1413 } else {
1414 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1415 + prev + ", waitingVisible="
1416 + (prev != null ? prev.waitingVisible : null)
1417 + ", nowVisible=" + next.nowVisible);
1418 }
1419 }
1420 }
1421
Dianne Hackborne7f97212011-02-24 14:40:20 -08001422 // Launching this app's activity, make sure the app is no longer
1423 // considered stopped.
1424 try {
1425 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001426 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001427 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001428 } catch (IllegalArgumentException e) {
1429 Slog.w(TAG, "Failed trying to unstop package "
1430 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001431 }
1432
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 // We are starting up the next activity, so tell the window manager
1434 // that the previous one will be hidden soon. This way it can know
1435 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001436 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001437 if (prev != null) {
1438 if (prev.finishing) {
1439 if (DEBUG_TRANSITION) Slog.v(TAG,
1440 "Prepare close transition: prev=" + prev);
1441 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001442 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001443 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 } else {
1445 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001446 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1447 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001448 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001449 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1450 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 } else {
1452 if (DEBUG_TRANSITION) Slog.v(TAG,
1453 "Prepare open transition: prev=" + prev);
1454 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001455 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001456 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001457 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 } else {
1459 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001460 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1461 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001462 }
1463 }
1464 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001465 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1466 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001467 }
Craig Mautner000f0022013-02-26 15:04:29 -08001468 } else if (numActivities() > 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 if (DEBUG_TRANSITION) Slog.v(TAG,
1470 "Prepare open transition: no previous");
1471 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001472 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001473 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001474 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001475 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001476 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001477 AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478 }
1479 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001480 if (!noAnim) {
1481 next.applyOptionsLocked();
1482 } else {
1483 next.clearOptionsLocked();
1484 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001485
1486 if (next.app != null && next.app.thread != null) {
1487 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1488
1489 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001490 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001491
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001492 // schedule launch ticks to collect information about slow apps.
1493 next.startLaunchTickingLocked();
1494
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 ActivityRecord lastResumedActivity = mResumedActivity;
1496 ActivityState lastState = next.state;
1497
1498 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001499
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001500 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 next.state = ActivityState.RESUMED;
1502 mResumedActivity = next;
1503 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001504 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001505 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 updateLRUListLocked(next);
1507
1508 // Have the window manager re-evaluate the orientation of
1509 // the screen based on the new activity order.
1510 boolean updated = false;
Craig Mautner2219a1b2013-03-25 09:44:30 -07001511 if (mStackSupervisor.isMainStack(this)) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001512 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1513 mService.mConfiguration,
1514 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1515 if (config != null) {
1516 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001518 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001520
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001521 if (!updated) {
1522 // The configuration update wasn't able to keep the existing
1523 // instance of the activity, and instead started a new one.
1524 // We should be all done, but let's just make sure our activity
1525 // is still at the top and schedule another run if something
1526 // weird happened.
1527 ActivityRecord nextNext = topRunningActivityLocked(null);
1528 if (DEBUG_SWITCH) Slog.i(TAG,
1529 "Activity config changed during resume: " + next
1530 + ", new next: " + nextNext);
1531 if (nextNext != next) {
1532 // Do over!
1533 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1534 }
Craig Mautner2219a1b2013-03-25 09:44:30 -07001535 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 mService.setFocusedActivityLocked(next);
1537 }
1538 ensureActivitiesVisibleLocked(null, 0);
1539 mService.mWindowManager.executeAppTransition();
1540 mNoAnimActivities.clear();
1541 return true;
1542 }
Craig Mautner58547802013-03-05 08:23:53 -08001543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 try {
1545 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001546 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 if (a != null) {
1548 final int N = a.size();
1549 if (!next.finishing && N > 0) {
1550 if (DEBUG_RESULTS) Slog.v(
1551 TAG, "Delivering results to " + next
1552 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001553 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001554 }
1555 }
1556
1557 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001558 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 }
1560
1561 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001562 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001564
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001565 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001566 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001567 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001568 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001570
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001571 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572
1573 } catch (Exception e) {
1574 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001575 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1576 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001577 next.state = lastState;
1578 mResumedActivity = lastResumedActivity;
1579 Slog.i(TAG, "Restarting because process died: " + next);
1580 if (!next.hasBeenLaunched) {
1581 next.hasBeenLaunched = true;
1582 } else {
Craig Mautner2219a1b2013-03-25 09:44:30 -07001583 if (SHOW_APP_STARTING_PREVIEW && mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001585 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001586 mService.compatibilityInfoForPackageLocked(
1587 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001589 next.labelRes, next.icon, next.windowFlags,
1590 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 }
1592 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001593 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001594 return true;
1595 }
1596
1597 // From this point on, if something goes wrong there is no way
1598 // to recover the activity.
1599 try {
1600 next.visible = true;
1601 completeResumeLocked(next);
1602 } catch (Exception e) {
1603 // If any exception gets thrown, toss away this
1604 // activity and try the next one.
1605 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001606 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001607 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 return true;
1609 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001610 next.stopped = false;
1611
1612 } else {
1613 // Whoops, need to restart this activity!
1614 if (!next.hasBeenLaunched) {
1615 next.hasBeenLaunched = true;
1616 } else {
1617 if (SHOW_APP_STARTING_PREVIEW) {
1618 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001619 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001620 mService.compatibilityInfoForPackageLocked(
1621 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001622 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001623 next.labelRes, next.icon, next.windowFlags,
1624 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001625 }
1626 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1627 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001628 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001629 }
1630
1631 return true;
1632 }
1633
Craig Mautner11bf9a52013-02-19 14:08:51 -08001634
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 private final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001636 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001637 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001638 TaskRecord rTask = r.task;
1639 final int taskId = rTask.taskId;
1640 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001641 // Last activity in task had been removed or ActivityManagerService is reusing task.
1642 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001643 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001644 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001645 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001646 mTaskHistory.add(rTask);
1647 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001648 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 if (!newTask) {
1650 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001651 boolean startIt = true;
1652 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1653 task = mTaskHistory.get(taskNdx);
1654 if (task == r.task) {
1655 // Here it is! Now, if this is not yet visible to the
1656 // user, then just add it without starting; it will
1657 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001658 if (!startIt) {
1659 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1660 + task, new RuntimeException("here").fillInStackTrace());
1661 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001662 r.putInHistory();
Craig Mautner70a86932013-02-28 22:37:44 -08001663 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001664 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation,
1665 r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001666 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001668 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001669 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001670 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 return;
1672 }
1673 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001674 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 startIt = false;
1676 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 }
1678 }
1679
1680 // Place a new activity at top of stack, so it is next to interact
1681 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001682
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001683 // If we are not placing the new activity frontmost, we do not want
1684 // to deliver the onUserLeaving callback to the actual frontmost
1685 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001686 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001688 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1689 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 }
Craig Mautner70a86932013-02-28 22:37:44 -08001691
1692 task = r.task;
1693
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001695 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001696 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001697 task.addActivityToTop(r);
1698
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001699 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 r.frontOfTask = newTask;
Craig Mautner70a86932013-02-28 22:37:44 -08001701 if (numActivities() > 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 // We want to show the starting preview window if we are
1703 // switching to a new task, or the next activity's process is
1704 // not currently running.
1705 boolean showStartingIcon = newTask;
1706 ProcessRecord proc = r.app;
1707 if (proc == null) {
1708 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1709 }
1710 if (proc == null || proc.thread == null) {
1711 showStartingIcon = true;
1712 }
1713 if (DEBUG_TRANSITION) Slog.v(TAG,
1714 "Prepare open transition: starting " + r);
1715 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001716 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001717 AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 } else {
1720 mService.mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001721 ? AppTransition.TRANSIT_TASK_OPEN
1722 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 mNoAnimActivities.remove(r);
1724 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001725 r.updateOptionsLocked(options);
Craig Mautner70a86932013-02-28 22:37:44 -08001726 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001727 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001728 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001729 boolean doShow = true;
1730 if (newTask) {
1731 // Even though this activity is starting fresh, we still need
1732 // to reset it to make sure we apply affinities to move any
1733 // existing activities from other tasks in to it.
1734 // If the caller has requested that the target task be
1735 // reset, then do so.
1736 if ((r.intent.getFlags()
1737 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1738 resetTaskIfNeededLocked(r, r);
1739 doShow = topRunningNonDelayedActivityLocked(null) == r;
1740 }
1741 }
1742 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1743 // Figure out if we are transitioning from another activity that is
1744 // "has the same starting icon" as the next one. This allows the
1745 // window manager to keep the previous window it had previously
1746 // created, if it still had one.
1747 ActivityRecord prev = mResumedActivity;
1748 if (prev != null) {
1749 // We don't want to reuse the previous starting preview if:
1750 // (1) The current activity is in a different task.
1751 if (prev.task != r.task) prev = null;
1752 // (2) The current activity is already displayed.
1753 else if (prev.nowVisible) prev = null;
1754 }
1755 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001756 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001757 mService.compatibilityInfoForPackageLocked(
1758 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001759 r.labelRes, r.icon, r.windowFlags,
1760 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001761 }
1762 } else {
1763 // If this is the first activity, don't do any fancy animations,
1764 // because there is nothing for it to animate on top of.
Craig Mautner70a86932013-02-28 22:37:44 -08001765 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001766 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001767 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001768 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 }
1770 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001771 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 }
1773
1774 if (doResume) {
1775 resumeTopActivityLocked(null);
1776 }
1777 }
1778
Dianne Hackbornbe707852011-11-11 14:32:10 -08001779 final void validateAppTokensLocked() {
1780 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001781 mValidateAppTokens.ensureCapacity(numActivities());
1782 final int numTasks = mTaskHistory.size();
1783 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1784 TaskRecord task = mTaskHistory.get(taskNdx);
1785 final ArrayList<ActivityRecord> activities = task.mActivities;
1786 if (activities.size() == 0) {
1787 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001788 }
Craig Mautner000f0022013-02-26 15:04:29 -08001789 TaskGroup group = new TaskGroup();
1790 group.taskId = task.taskId;
1791 mValidateAppTokens.add(group);
1792 final int numActivities = activities.size();
1793 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1794 final ActivityRecord r = activities.get(activityNdx);
1795 group.tokens.add(r.appToken);
1796 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001797 }
Craig Mautner00af9fe2013-03-25 09:13:41 -07001798 mService.mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001799 }
1800
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 /**
1802 * Perform a reset of the given task, if needed as part of launching it.
1803 * Returns the new HistoryRecord at the top of the task.
1804 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001805 /**
1806 * Helper method for #resetTaskIfNeededLocked.
1807 * We are inside of the task being reset... we'll either finish this activity, push it out
1808 * for another task, or leave it as-is.
1809 * @param task The task containing the Activity (taskTop) that might be reset.
1810 * @param forceReset
1811 * @return An ActivityOptions that needs to be processed.
1812 */
1813 private final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task,
1814 boolean forceReset) {
1815 ActivityOptions topOptions = null;
1816
1817 int replyChainEnd = -1;
1818 boolean canMoveOptions = true;
1819
1820 // We only do this for activities that are not the root of the task (since if we finish
1821 // the root, we may no longer have the task!).
1822 final ArrayList<ActivityRecord> activities = task.mActivities;
1823 final int numActivities = activities.size();
1824 for (int i = numActivities - 1; i > 0; --i ) {
1825 ActivityRecord target = activities.get(i);
1826
1827 final int flags = target.info.flags;
1828 final boolean finishOnTaskLaunch =
1829 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1830 final boolean allowTaskReparenting =
1831 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1832 final boolean clearWhenTaskReset =
1833 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1834
1835 if (!finishOnTaskLaunch
1836 && !clearWhenTaskReset
1837 && target.resultTo != null) {
1838 // If this activity is sending a reply to a previous
1839 // activity, we can't do anything with it now until
1840 // we reach the start of the reply chain.
1841 // XXX note that we are assuming the result is always
1842 // to the previous activity, which is almost always
1843 // the case but we really shouldn't count on.
1844 if (replyChainEnd < 0) {
1845 replyChainEnd = i;
1846 }
1847 } else if (!finishOnTaskLaunch
1848 && !clearWhenTaskReset
1849 && allowTaskReparenting
1850 && target.taskAffinity != null
1851 && !target.taskAffinity.equals(task.affinity)) {
1852 // If this activity has an affinity for another
1853 // task, then we need to move it out of here. We will
1854 // move it as far out of the way as possible, to the
1855 // bottom of the activity stack. This also keeps it
1856 // correctly ordered with any activities we previously
1857 // moved.
1858 TaskRecord bottomTask = mTaskHistory.get(0);
1859 ActivityRecord p = bottomTask.mActivities.get(0);
1860 if (target.taskAffinity != null
1861 && target.taskAffinity.equals(p.task.affinity)) {
1862 // If the activity currently at the bottom has the
1863 // same task affinity as the one we are moving,
1864 // then merge it into the same task.
Craig Mautner0247fc82013-02-28 14:32:06 -08001865 setTask(target, p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001866 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1867 + " out to bottom task " + p.task);
1868 } else {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001869 setTask(target, createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
1870 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001871 target.task.affinityIntent = target.intent;
1872 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1873 + " out to new task " + target.task);
1874 }
1875
1876 final TaskRecord targetTask = target.task;
1877 final int targetTaskId = targetTask.taskId;
1878 mService.mWindowManager.setAppGroupId(target.appToken, targetTaskId);
1879
1880 ThumbnailHolder curThumbHolder = target.thumbHolder;
1881 boolean gotOptions = !canMoveOptions;
1882
1883 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1884 for (int srcPos = start; srcPos >= i; --srcPos) {
1885 p = activities.get(srcPos);
1886 if (p.finishing) {
1887 continue;
1888 }
1889
1890 curThumbHolder = p.thumbHolder;
1891 canMoveOptions = false;
1892 if (!gotOptions && topOptions == null) {
1893 topOptions = p.takeOptionsLocked();
1894 if (topOptions != null) {
1895 gotOptions = true;
1896 }
1897 }
1898 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1899 + task + " adding to task=" + targetTask,
1900 new RuntimeException("here").fillInStackTrace());
1901 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1902 + " out to target's task " + target.task);
Craig Mautner0247fc82013-02-28 14:32:06 -08001903 setTask(p, targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001904 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001905
Craig Mautnere3a74d52013-02-22 14:14:58 -08001906 mService.mWindowManager.setAppGroupId(p.appToken, targetTaskId);
1907 }
1908
1909 mService.mWindowManager.moveTaskToBottom(targetTaskId);
1910 if (VALIDATE_TOKENS) {
1911 validateAppTokensLocked();
1912 }
1913
1914 replyChainEnd = -1;
1915 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1916 // If the activity should just be removed -- either
1917 // because it asks for it, or the task should be
1918 // cleared -- then finish it and anything that is
1919 // part of its reply chain.
1920 int end;
1921 if (clearWhenTaskReset) {
1922 // In this case, we want to finish this activity
1923 // and everything above it, so be sneaky and pretend
1924 // like these are all in the reply chain.
1925 end = numActivities - 1;
1926 } else if (replyChainEnd < 0) {
1927 end = i;
1928 } else {
1929 end = replyChainEnd;
1930 }
1931 ActivityRecord p = null;
1932 boolean gotOptions = !canMoveOptions;
1933 for (int srcPos = i; srcPos <= end; srcPos++) {
1934 p = activities.get(srcPos);
1935 if (p.finishing) {
1936 continue;
1937 }
1938 canMoveOptions = false;
1939 if (!gotOptions && topOptions == null) {
1940 topOptions = p.takeOptionsLocked();
1941 if (topOptions != null) {
1942 gotOptions = true;
1943 }
1944 }
Craig Mautner58547802013-03-05 08:23:53 -08001945 if (DEBUG_TASKS) Slog.w(TAG,
1946 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001947 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001948 end--;
1949 srcPos--;
1950 }
1951 }
1952 replyChainEnd = -1;
1953 } else {
1954 // If we were in the middle of a chain, well the
1955 // activity that started it all doesn't want anything
1956 // special, so leave it all as-is.
1957 replyChainEnd = -1;
1958 }
1959 }
1960
1961 return topOptions;
1962 }
1963
1964 /**
1965 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1966 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1967 * @param affinityTask The task we are looking for an affinity to.
1968 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1969 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1970 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1971 */
Craig Mautner77878772013-03-04 19:46:24 -08001972 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1973 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001974 int replyChainEnd = -1;
1975 final int taskId = task.taskId;
1976 final String taskAffinity = task.affinity;
1977
1978 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1979 final int numActivities = activities.size();
1980 // Do not operate on the root Activity.
1981 for (int i = numActivities - 1; i > 0; --i) {
1982 ActivityRecord target = activities.get(i);
1983
1984 final int flags = target.info.flags;
1985 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1986 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1987
1988 if (target.resultTo != null) {
1989 // If this activity is sending a reply to a previous
1990 // activity, we can't do anything with it now until
1991 // we reach the start of the reply chain.
1992 // XXX note that we are assuming the result is always
1993 // to the previous activity, which is almost always
1994 // the case but we really shouldn't count on.
1995 if (replyChainEnd < 0) {
1996 replyChainEnd = i;
1997 }
1998 } else if (topTaskIsHigher
1999 && allowTaskReparenting
2000 && taskAffinity != null
2001 && taskAffinity.equals(target.taskAffinity)) {
2002 // This activity has an affinity for our task. Either remove it if we are
2003 // clearing or move it over to our task. Note that
2004 // we currently punt on the case where we are resetting a
2005 // task that is not at the top but who has activities above
2006 // with an affinity to it... this is really not a normal
2007 // case, and we will need to later pull that task to the front
2008 // and usually at that point we will do the reset and pick
2009 // up those remaining activities. (This only happens if
2010 // someone starts an activity in a new task from an activity
2011 // in a task that is not currently on top.)
2012 if (forceReset || finishOnTaskLaunch) {
2013 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2014 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2015 for (int srcPos = start; srcPos >= i; --srcPos) {
2016 final ActivityRecord p = activities.get(srcPos);
2017 if (p.finishing) {
2018 continue;
2019 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002020 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002021 }
2022 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002023 if (taskInsertionPoint < 0) {
2024 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002025
Craig Mautner77878772013-03-04 19:46:24 -08002026 }
Craig Mautner77878772013-03-04 19:46:24 -08002027
2028 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2029 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2030 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2031 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002032 final ActivityRecord p = activities.get(srcPos);
Craig Mautner0247fc82013-02-28 14:32:06 -08002033 setTask(p, task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002034 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002035
Craig Mautnere3a74d52013-02-22 14:14:58 -08002036 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2037 + " to stack at " + task,
2038 new RuntimeException("here").fillInStackTrace());
2039 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2040 + " in to resetting task " + task);
2041 mService.mWindowManager.setAppGroupId(p.appToken, taskId);
2042 }
2043 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002044 if (VALIDATE_TOKENS) {
2045 validateAppTokensLocked();
2046 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002047
2048 // Now we've moved it in to place... but what if this is
2049 // a singleTop activity and we have put it on top of another
2050 // instance of the same activity? Then we drop the instance
2051 // below so it remains singleTop.
2052 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2053 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2054 boolean found = false;
2055 int targetNdx = taskActivities.indexOf(target);
2056 if (targetNdx > 0) {
2057 ActivityRecord p = taskActivities.get(targetNdx - 1);
2058 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002059 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2060 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002061 }
2062 }
2063 }
2064 }
2065
2066 replyChainEnd = -1;
2067 }
2068 }
Craig Mautner77878772013-03-04 19:46:24 -08002069 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002070 }
2071
2072 private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
2073 ActivityRecord newActivity) {
2074 boolean forceReset =
2075 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2076 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2077 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2078 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2079 forceReset = true;
2080 }
2081 }
2082
2083 final TaskRecord task = taskTop.task;
2084
2085 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2086 * for remaining tasks. Used for later tasks to reparent to task. */
2087 boolean taskFound = false;
2088
2089 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2090 ActivityOptions topOptions = null;
2091
Craig Mautner77878772013-03-04 19:46:24 -08002092 // Preserve the location for reparenting in the new task.
2093 int reparentInsertionPoint = -1;
2094
Craig Mautnere3a74d52013-02-22 14:14:58 -08002095 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2096 final TaskRecord targetTask = mTaskHistory.get(i);
2097
2098 if (targetTask == task) {
2099 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2100 taskFound = true;
2101 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002102 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2103 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 }
2105 }
2106
Craig Mautner70a86932013-02-28 22:37:44 -08002107 int taskNdx = mTaskHistory.indexOf(task);
2108 do {
2109 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2110 } while (taskTop == null && taskNdx >= 0);
2111
Craig Mautnere3a74d52013-02-22 14:14:58 -08002112 if (topOptions != null) {
2113 // If we got some ActivityOptions from an activity on top that
2114 // was removed from the task, propagate them to the new real top.
2115 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002116 taskTop.updateOptionsLocked(topOptions);
2117 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002118 topOptions.abort();
2119 }
2120 }
2121
2122 return taskTop;
2123 }
2124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002125 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 * Find the activity in the history stack within the given task. Returns
2127 * the index within the history at which it's found, or < 0 if not found.
2128 */
Craig Mautner56f52db2013-02-25 10:03:01 -08002129 private final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
2130 final ComponentName realActivity = r.realActivity;
2131 ArrayList<ActivityRecord> activities = task.mActivities;
2132 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2133 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002134 if (candidate.finishing) {
2135 continue;
2136 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002137 if (candidate.realActivity.equals(realActivity)) {
2138 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 }
2140 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002141 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 }
2143
2144 /**
2145 * Reorder the history stack so that the activity at the given index is
2146 * brought to the front.
2147 */
Craig Mautner56f52db2013-02-25 10:03:01 -08002148 private final void moveActivityToFrontLocked(ActivityRecord newTop) {
2149 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop
2150 + " to stack at top", new RuntimeException("here").fillInStackTrace());
2151
2152 final TaskRecord task = newTop.task;
2153 task.getTopActivity().frontOfTask = false;
2154 task.mActivities.remove(newTop);
2155 task.mActivities.add(newTop);
2156 newTop.frontOfTask = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 }
2158
2159 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002160 Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 String resultWho, int requestCode,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08002162 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002163 boolean componentSpecified, ActivityRecord[] outActivity) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07002164 int err = ActivityManager.START_SUCCESS;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002165
2166 ProcessRecord callerApp = null;
2167 if (caller != null) {
2168 callerApp = mService.getRecordForAppLocked(caller);
2169 if (callerApp != null) {
2170 callingPid = callerApp.pid;
2171 callingUid = callerApp.info.uid;
2172 } else {
2173 Slog.w(TAG, "Unable to find app for caller " + caller
2174 + " (pid=" + callingPid + ") when starting: "
2175 + intent.toString());
Dianne Hackborna4972e92012-03-14 10:38:05 -07002176 err = ActivityManager.START_PERMISSION_DENIED;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002177 }
2178 }
2179
Dianne Hackborna4972e92012-03-14 10:38:05 -07002180 if (err == ActivityManager.START_SUCCESS) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002181 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -07002182 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
2183 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid));
Dianne Hackbornefb58102010-10-14 16:47:34 -07002184 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185
2186 ActivityRecord sourceRecord = null;
2187 ActivityRecord resultRecord = null;
2188 if (resultTo != null) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002189 sourceRecord = isInStackLocked(resultTo);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 if (DEBUG_RESULTS) Slog.v(
Craig Mautner56f52db2013-02-25 10:03:01 -08002191 TAG, "Will send result to " + resultTo + " " + sourceRecord);
2192 if (sourceRecord != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002193 if (requestCode >= 0 && !sourceRecord.finishing) {
2194 resultRecord = sourceRecord;
2195 }
2196 }
2197 }
2198
2199 int launchFlags = intent.getFlags();
2200
2201 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
2202 && sourceRecord != null) {
2203 // Transfer the result target from the source activity to the new
2204 // one being started, including any failures.
2205 if (requestCode >= 0) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002206 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002207 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 }
2209 resultRecord = sourceRecord.resultTo;
2210 resultWho = sourceRecord.resultWho;
2211 requestCode = sourceRecord.requestCode;
2212 sourceRecord.resultTo = null;
2213 if (resultRecord != null) {
2214 resultRecord.removeResultsLocked(
2215 sourceRecord, resultWho, requestCode);
2216 }
2217 }
2218
Dianne Hackborna4972e92012-03-14 10:38:05 -07002219 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 // We couldn't find a class that can handle the given Intent.
2221 // That's the end of that!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002222 err = ActivityManager.START_INTENT_NOT_RESOLVED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002223 }
2224
Dianne Hackborna4972e92012-03-14 10:38:05 -07002225 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 // We couldn't find the specific class specified in the Intent.
2227 // Also the end of the line.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002228 err = ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 }
2230
Dianne Hackborna4972e92012-03-14 10:38:05 -07002231 if (err != ActivityManager.START_SUCCESS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002232 if (resultRecord != null) {
2233 sendActivityResultLocked(-1,
2234 resultRecord, resultWho, requestCode,
2235 Activity.RESULT_CANCELED, null);
2236 }
Craig Mautner27084302013-03-25 08:05:25 -07002237 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002238 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 return err;
2240 }
2241
Jeff Sharkey35be7562012-04-18 19:16:15 -07002242 final int startAnyPerm = mService.checkPermission(
2243 START_ANY_ACTIVITY, callingPid, callingUid);
2244 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002245 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
Jeff Sharkey35be7562012-04-18 19:16:15 -07002246 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002247 if (resultRecord != null) {
2248 sendActivityResultLocked(-1,
2249 resultRecord, resultWho, requestCode,
2250 Activity.RESULT_CANCELED, null);
2251 }
Craig Mautner27084302013-03-25 08:05:25 -07002252 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002253 String msg;
2254 if (!aInfo.exported) {
2255 msg = "Permission Denial: starting " + intent.toString()
2256 + " from " + callerApp + " (pid=" + callingPid
2257 + ", uid=" + callingUid + ")"
2258 + " not exported from uid " + aInfo.applicationInfo.uid;
2259 } else {
2260 msg = "Permission Denial: starting " + intent.toString()
2261 + " from " + callerApp + " (pid=" + callingPid
2262 + ", uid=" + callingUid + ")"
2263 + " requires " + aInfo.permission;
2264 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002265 Slog.w(TAG, msg);
2266 throw new SecurityException(msg);
2267 }
2268
Craig Mautner2219a1b2013-03-25 09:44:30 -07002269 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002270 if (mService.mController != null) {
2271 boolean abort = false;
2272 try {
2273 // The Intent we give to the watcher has the extra data
2274 // stripped off, since it can contain private information.
2275 Intent watchIntent = intent.cloneFilter();
2276 abort = !mService.mController.activityStarting(watchIntent,
2277 aInfo.applicationInfo.packageName);
2278 } catch (RemoteException e) {
2279 mService.mController = null;
2280 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002281
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 if (abort) {
2283 if (resultRecord != null) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002284 sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 Activity.RESULT_CANCELED, null);
2286 }
2287 // We pretend to the caller that it was really started, but
2288 // they will just get a cancel result.
Craig Mautner27084302013-03-25 08:05:25 -07002289 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002290 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002291 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 }
2293 }
2294 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002295
Craig Mautnerd2328952013-03-05 12:46:26 -08002296 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002297 intent, resolvedType, aInfo, mService.mConfiguration,
2298 resultRecord, resultWho, requestCode, componentSpecified);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002299 if (outActivity != null) {
2300 outActivity[0] = r;
2301 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302
Craig Mautner2219a1b2013-03-25 09:44:30 -07002303 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002304 if (mResumedActivity == null
2305 || mResumedActivity.info.applicationInfo.uid != callingUid) {
2306 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
Craig Mautner64f2b172013-03-11 11:40:31 -07002307 PendingActivityLaunch pal =
2308 new PendingActivityLaunch(r, sourceRecord, startFlags, this);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 mService.mPendingActivityLaunches.add(pal);
Craig Mautner27084302013-03-25 08:05:25 -07002310 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002311 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002312 return ActivityManager.START_SWITCHES_CANCELED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002313 }
2314 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002315
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 if (mService.mDidAppSwitch) {
2317 // This is the second allowed switch since we stopped switches,
2318 // so now just generally allow switches. Use case: user presses
2319 // home (switches disabled, switch to home, mDidAppSwitch now true);
2320 // user taps a home icon (coming from home so allowed, we hit here
2321 // and now allow anyone to switch again).
2322 mService.mAppSwitchesAllowedTime = 0;
2323 } else {
2324 mService.mDidAppSwitch = true;
2325 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002327
Craig Mautner8d341ef2013-03-26 09:03:27 -07002328 mService.doPendingActivityLaunchesLocked(false);
2329
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002330 err = startActivityUncheckedLocked(r, sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002331 startFlags, true, options);
Craig Mautner27084302013-03-25 08:05:25 -07002332 if (mPausingActivity == null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002333 // Someone asked to have the keyguard dismissed on the next
2334 // activity start, but we are not actually doing an activity
2335 // switch... just dismiss the keyguard now, because we
2336 // probably want to see whatever is behind it.
Craig Mautner27084302013-03-25 08:05:25 -07002337 mStackSupervisor.dismissKeyguard();
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002338 }
2339 return err;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002341
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002342 final void moveHomeToFrontFromLaunchLocked(int launchFlags) {
2343 if ((launchFlags &
2344 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2345 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2346 // Caller wants to appear on home activity, so before starting
2347 // their own activity we will bring home to the front.
2348 moveHomeToFrontLocked();
2349 }
2350 }
2351
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002353 ActivityRecord sourceRecord, int startFlags, boolean doResume,
2354 Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 final Intent intent = r.intent;
2356 final int callingUid = r.launchedFromUid;
Amith Yamasani742a6712011-05-04 14:49:28 -07002357
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 int launchFlags = intent.getFlags();
Craig Mautnerd2328952013-03-05 12:46:26 -08002359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // We'll invoke onUserLeaving before onPause only if the launching
2361 // activity did not explicitly state that this is an automated launch.
2362 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2363 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2364 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautnerd2328952013-03-05 12:46:26 -08002365
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366 // If the caller has asked not to resume at this point, we make note
2367 // of this in the record so that we can skip it when trying to find
2368 // the top running activity.
2369 if (!doResume) {
2370 r.delayedResume = true;
2371 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002373 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
2374 != 0 ? r : null;
2375
2376 // If the onlyIfNeeded flag is set, then we can do this if the activity
2377 // being launched is the same as the one making the call... or, as
2378 // a special case, if we do not know the caller then we count the
2379 // current top activity as the caller.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002380 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 ActivityRecord checkedCaller = sourceRecord;
2382 if (checkedCaller == null) {
2383 checkedCaller = topRunningNonDelayedActivityLocked(notTop);
2384 }
2385 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2386 // Caller is not the same as launcher, so always needed.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002387 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002388 }
2389 }
2390
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 if (sourceRecord == null) {
2392 // This activity is not being started from another... in this
2393 // case we -always- start a new task.
2394 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2395 Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: "
2396 + intent);
2397 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2398 }
2399 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2400 // The original activity who is starting us is running as a single
2401 // instance... this new activity it is starting must go on its
2402 // own task.
2403 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2404 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
2405 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2406 // The activity being started is a single instance... it always
2407 // gets launched into its own task.
2408 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2409 }
2410
2411 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2412 // For whatever reason this activity is being launched into a new
2413 // task... yet the caller has requested a result back. Well, that
2414 // is pretty messed up, so instead immediately send back a cancel
2415 // and let the new task continue launched as normal without a
2416 // dependency on its originator.
2417 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2418 sendActivityResultLocked(-1,
2419 r.resultTo, r.resultWho, r.requestCode,
2420 Activity.RESULT_CANCELED, null);
2421 r.resultTo = null;
2422 }
2423
2424 boolean addingToTask = false;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002425 boolean movedHome = false;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002426 TaskRecord reuseTask = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002427 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2428 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2429 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2430 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2431 // If bring to front is requested, and no result is requested, and
2432 // we can find a task that was started with this same
2433 // component, then instead of launching bring that one to the front.
2434 if (r.resultTo == null) {
2435 // See if there is a task to bring to the front. If this is
2436 // a SINGLE_INSTANCE activity, there can be one and only one
2437 // instance of it in the history, and it is always in its own
2438 // unique task, so we do a special search.
2439 ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
2440 ? findTaskLocked(intent, r.info)
2441 : findActivityLocked(intent, r.info);
2442 if (taskTop != null) {
2443 if (taskTop.task.intent == null) {
2444 // This task was started because of movement of
2445 // the activity based on affinity... now that we
2446 // are actually launching it, we can assign the
2447 // base intent.
2448 taskTop.task.setIntent(intent, r.info);
2449 }
2450 // If the target task is not in the front, then we need
2451 // to bring it to the front... except... well, with
2452 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2453 // to have the same behavior as if a new instance was
2454 // being started, which means not bringing it to the front
2455 // if the caller is not itself in the front.
2456 ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
Jean-Baptiste Queru66a5d692010-10-25 17:27:16 -07002457 if (curTop != null && curTop.task != taskTop.task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2459 boolean callerAtFront = sourceRecord == null
2460 || curTop.task == sourceRecord.task;
2461 if (callerAtFront) {
2462 // We really do want to push this one into the
2463 // user's face, right now.
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002464 movedHome = true;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002465 moveHomeToFrontFromLaunchLocked(launchFlags);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002466 moveTaskToFrontLocked(taskTop.task, r, options);
Dianne Hackborn84375872012-06-01 19:03:50 -07002467 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 }
2469 }
2470 // If the caller has requested that the target task be
2471 // reset, then do so.
2472 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2473 taskTop = resetTaskIfNeededLocked(taskTop, r);
2474 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002475 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 // We don't need to start a new activity, and
2477 // the client said not to do anything if that
2478 // is the case, so this is it! And for paranoia, make
2479 // sure we have correctly resumed the top activity.
2480 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002481 resumeTopActivityLocked(null, options);
2482 } else {
2483 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002484 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002485 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002486 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002487 if ((launchFlags &
2488 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
2489 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
2490 // The caller has requested to completely replace any
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002491 // existing task with its new activity. Well that should
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002492 // not be too hard...
2493 reuseTask = taskTop.task;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07002494 taskTop.task.performClearTaskLocked();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002495 reuseTask.setIntent(r.intent, r.info);
2496 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2498 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2499 // In this situation we want to remove all activities
2500 // from the task up to the one being started. In most
2501 // cases this means we are resetting the task to its
2502 // initial state.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07002503 ActivityRecord top = taskTop.task.performClearTaskLocked(r, launchFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002504 if (top != null) {
2505 if (top.frontOfTask) {
2506 // Activity aliases may mean we use different
2507 // intents for the top activity, so make sure
2508 // the task now has the identity of the new
2509 // intent.
2510 top.task.setIntent(r.intent, r.info);
2511 }
2512 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002513 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002514 } else {
2515 // A special case: we need to
2516 // start the activity because it is not currently
2517 // running, and the caller has asked to clear the
2518 // current task to have this activity at the top.
2519 addingToTask = true;
2520 // Now pretend like this activity is being started
2521 // by the top of its task, so it is put in the
2522 // right place.
2523 sourceRecord = taskTop;
2524 }
2525 } else if (r.realActivity.equals(taskTop.task.realActivity)) {
2526 // In this case the top activity on the task is the
2527 // same as the one being launched, so we take that
2528 // as a request to bring the task to the foreground.
2529 // If the top activity in the task is the root
2530 // activity, deliver this new intent to it if it
2531 // desires.
Johan Viktorssonf363dfd2012-02-16 17:05:16 +01002532 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2533 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 && taskTop.realActivity.equals(r.realActivity)) {
2535 logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task);
2536 if (taskTop.frontOfTask) {
2537 taskTop.task.setIntent(r.intent, r.info);
2538 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002539 taskTop.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 } else if (!r.intent.filterEquals(taskTop.task.intent)) {
2541 // In this case we are launching the root activity
2542 // of the task, but with a different intent. We
2543 // should start a new instance on top.
2544 addingToTask = true;
2545 sourceRecord = taskTop;
2546 }
2547 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2548 // In this case an activity is being launched in to an
2549 // existing task, without resetting that task. This
2550 // is typically the situation of launching an activity
2551 // from a notification or shortcut. We want to place
2552 // the new activity on top of the current task.
2553 addingToTask = true;
2554 sourceRecord = taskTop;
2555 } else if (!taskTop.task.rootWasReset) {
2556 // In this case we are launching in to an existing task
2557 // that has not yet been started from its front door.
2558 // The current task has been brought to the front.
2559 // Ideally, we'd probably like to place this new task
2560 // at the bottom of its stack, but that's a little hard
2561 // to do with the current organization of the code so
2562 // for now we'll just drop it.
2563 taskTop.task.setIntent(r.intent, r.info);
2564 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002565 if (!addingToTask && reuseTask == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002566 // We didn't do anything... but it was needed (a.k.a., client
2567 // don't use that intent!) And for paranoia, make
2568 // sure we have correctly resumed the top activity.
2569 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002570 resumeTopActivityLocked(null, options);
2571 } else {
2572 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002573 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002574 return ActivityManager.START_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575 }
2576 }
2577 }
2578 }
2579
2580 //String uri = r.intent.toURI();
2581 //Intent intent2 = new Intent(uri);
2582 //Slog.i(TAG, "Given intent: " + r.intent);
2583 //Slog.i(TAG, "URI is: " + uri);
2584 //Slog.i(TAG, "To intent: " + intent2);
2585
2586 if (r.packageName != null) {
2587 // If the activity being launched is the same as the one currently
2588 // at the top, then we need to check if it should only be launched
2589 // once.
2590 ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
2591 if (top != null && r.resultTo == null) {
Amith Yamasani742a6712011-05-04 14:49:28 -07002592 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 if (top.app != null && top.app.thread != null) {
2594 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2595 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
2596 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2597 logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2598 // For paranoia, make sure we have correctly
2599 // resumed the top activity.
2600 if (doResume) {
2601 resumeTopActivityLocked(null);
2602 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002603 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002604 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 // We don't need to start a new activity, and
2606 // the client said not to do anything if that
2607 // is the case, so this is it!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002608 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002609 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002610 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002611 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002612 }
2613 }
2614 }
2615 }
2616
2617 } else {
2618 if (r.resultTo != null) {
2619 sendActivityResultLocked(-1,
2620 r.resultTo, r.resultWho, r.requestCode,
2621 Activity.RESULT_CANCELED, null);
2622 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002623 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002624 return ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 }
2626
2627 boolean newTask = false;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002628 boolean keepCurTransition = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002629
2630 // Should this be considered a new task?
2631 if (r.resultTo == null && !addingToTask
2632 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002633 if (reuseTask == null) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002634 setTask(r, createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent,
2635 true), null, true);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002636 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2637 + " in new task " + r.task);
2638 } else {
Craig Mautner0247fc82013-02-28 14:32:06 -08002639 setTask(r, reuseTask, reuseTask, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 newTask = true;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002642 if (!movedHome) {
2643 moveHomeToFrontFromLaunchLocked(launchFlags);
2644 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002645
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 } else if (sourceRecord != null) {
2647 if (!addingToTask &&
2648 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2649 // In this case, we are adding the activity to an existing
2650 // task, but the caller has asked to clear that task if the
2651 // activity is already running.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07002652 ActivityRecord top = sourceRecord.task.performClearTaskLocked(r, launchFlags);
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002653 keepCurTransition = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 if (top != null) {
2655 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002656 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002657 // For paranoia, make sure we have correctly
2658 // resumed the top activity.
2659 if (doResume) {
2660 resumeTopActivityLocked(null);
2661 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002662 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002663 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002664 }
2665 } else if (!addingToTask &&
2666 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2667 // In this case, we are launching an activity in our own task
2668 // that may already be running somewhere in the history, and
2669 // we want to shuffle it to the front of the stack if so.
Craig Mautner56f52db2013-02-25 10:03:01 -08002670 final ActivityRecord top = findActivityInHistoryLocked(r, sourceRecord.task);
2671 if (top != null) {
2672 moveActivityToFrontLocked(top);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002674 top.updateOptionsLocked(options);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002675 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002676 if (doResume) {
2677 resumeTopActivityLocked(null);
2678 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002679 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002680 }
2681 }
2682 // An existing activity is starting this new activity, so we want
2683 // to keep the new one in the same task as the one that is starting
2684 // it.
Craig Mautner0247fc82013-02-28 14:32:06 -08002685 setTask(r, sourceRecord.task, sourceRecord.thumbHolder, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002686 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2687 + " in existing task " + r.task);
2688
2689 } else {
2690 // This not being started from an existing activity, and not part
2691 // of a new task... just put it in the top task, though these days
2692 // this case should never happen.
Craig Mautner9658b312013-02-28 10:55:59 -08002693 ActivityRecord prev = null;
2694 // Iterate to find the first non-empty task stack. Note that this code can
2695 // go away once we stop storing tasks with empty mActivities lists.
2696 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2697 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2698 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2699 prev = activities.get(activityNdx);
2700 break;
2701 }
2702 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002703 setTask(r, prev != null
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002704 ? prev.task
Craig Mautner8d341ef2013-03-26 09:03:27 -07002705 : createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent, true),
2706 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2708 + " in new guessed " + r.task);
2709 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002710
Dianne Hackborn39792d22010-08-19 18:01:52 -07002711 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002712 intent, r.getUriPermissionsLocked());
Dianne Hackborn39792d22010-08-19 18:01:52 -07002713
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 if (newTask) {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002715 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 }
2717 logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002718 startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002719 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002720 }
2721
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
2723 long thisTime, long totalTime) {
2724 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
2725 WaitResult w = mWaitingActivityLaunched.get(i);
2726 w.timeout = timeout;
2727 if (r != null) {
2728 w.who = new ComponentName(r.info.packageName, r.info.name);
2729 }
2730 w.thisTime = thisTime;
2731 w.totalTime = totalTime;
2732 }
2733 mService.notifyAll();
2734 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002735
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 void reportActivityVisibleLocked(ActivityRecord r) {
2737 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
2738 WaitResult w = mWaitingActivityVisible.get(i);
2739 w.timeout = false;
2740 if (r != null) {
2741 w.who = new ComponentName(r.info.packageName, r.info.name);
2742 }
2743 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
2744 w.thisTime = w.totalTime;
2745 }
2746 mService.notifyAll();
Craig Mautner27084302013-03-25 08:05:25 -07002747 mStackSupervisor.dismissKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002748 }
2749
2750 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2751 String resultWho, int requestCode, int resultCode, Intent data) {
2752
2753 if (callingUid > 0) {
2754 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002755 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002756 }
2757
2758 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2759 + " : who=" + resultWho + " req=" + requestCode
2760 + " res=" + resultCode + " data=" + data);
2761 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2762 try {
2763 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2764 list.add(new ResultInfo(resultWho, requestCode,
2765 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002766 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 return;
2768 } catch (Exception e) {
2769 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2770 }
2771 }
2772
2773 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2774 }
2775
2776 private final void stopActivityLocked(ActivityRecord r) {
2777 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2778 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2779 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2780 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002781 if (!mService.mSleeping) {
2782 if (DEBUG_STATES) {
2783 Slog.d(TAG, "no-history finish of " + r);
2784 }
2785 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002786 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002787 } else {
2788 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2789 + " on stop because we're just sleeping");
2790 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002792 }
2793
2794 if (r.app != null && r.app.thread != null) {
Craig Mautner2219a1b2013-03-25 09:44:30 -07002795 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 if (mService.mFocusedActivity == r) {
2797 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2798 }
2799 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002800 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 try {
2802 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002803 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2804 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002805 r.state = ActivityState.STOPPING;
2806 if (DEBUG_VISBILITY) Slog.v(
2807 TAG, "Stopping visible=" + r.visible + " for " + r);
2808 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002809 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002811 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002812 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002813 r.setSleeping(true);
2814 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002815 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2816 msg.obj = r;
2817 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002818 } catch (Exception e) {
2819 // Maybe just ignore exceptions here... if the process
2820 // has crashed, our death notification will clean things
2821 // up.
2822 Slog.w(TAG, "Exception thrown during pause", e);
2823 // Just in case, assume it to be stopped.
2824 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002825 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 r.state = ActivityState.STOPPED;
2827 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002828 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002829 }
2830 }
2831 }
2832 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002833
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(
2835 boolean remove) {
2836 int N = mStoppingActivities.size();
2837 if (N <= 0) return null;
2838
2839 ArrayList<ActivityRecord> stops = null;
2840
2841 final boolean nowVisible = mResumedActivity != null
2842 && mResumedActivity.nowVisible
2843 && !mResumedActivity.waitingVisible;
2844 for (int i=0; i<N; i++) {
2845 ActivityRecord s = mStoppingActivities.get(i);
2846 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
2847 + nowVisible + " waitingVisible=" + s.waitingVisible
2848 + " finishing=" + s.finishing);
2849 if (s.waitingVisible && nowVisible) {
2850 mWaitingVisibleActivities.remove(s);
2851 s.waitingVisible = false;
2852 if (s.finishing) {
2853 // If this activity is finishing, it is sitting on top of
2854 // everyone else but we now know it is no longer needed...
2855 // so get rid of it. Otherwise, we need to go through the
2856 // normal flow and hide it once we determine that it is
2857 // hidden by the activities in front of it.
2858 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002859 mService.mWindowManager.setAppVisibility(s.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 }
2861 }
Craig Mautnere11f2b72013-04-01 12:37:17 -07002862 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002863 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2864 if (stops == null) {
2865 stops = new ArrayList<ActivityRecord>();
2866 }
2867 stops.add(s);
2868 mStoppingActivities.remove(i);
2869 N--;
2870 i--;
2871 }
2872 }
2873
2874 return stops;
2875 }
2876
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002877 final void scheduleIdleLocked() {
2878 Message msg = Message.obtain();
2879 msg.what = IDLE_NOW_MSG;
2880 mHandler.sendMessage(msg);
2881 }
2882
Craig Mautnere79d42682013-04-01 19:01:53 -07002883 // Checked.
2884 final ActivityRecord activityIdleInternalLocked(IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 Configuration config) {
2886 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2887
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002888 ActivityRecord res = null;
2889
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890 ArrayList<ActivityRecord> stops = null;
2891 ArrayList<ActivityRecord> finishes = null;
2892 ArrayList<ActivityRecord> thumbnails = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002893 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 int NS = 0;
2895 int NF = 0;
2896 int NT = 0;
2897 IApplicationThread sendThumbnail = null;
2898 boolean booting = false;
2899 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002900 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002901
Craig Mautnere79d42682013-04-01 19:01:53 -07002902 ActivityRecord r = ActivityRecord.forToken(token);
2903 if (r != null) {
2904 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2905 r.finishLaunchTickingLocked();
2906 }
2907
2908 // Get the activity record.
2909 if (isInStackLocked(token) != null) {
2910 res = r;
2911
2912 if (fromTimeout) {
2913 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914 }
2915
Craig Mautnere79d42682013-04-01 19:01:53 -07002916 // This is a hack to semi-deal with a race condition
2917 // in the client where it can be constructed with a
2918 // newer configuration from when we asked it to launch.
2919 // We'll update with whatever configuration it now says
2920 // it used to launch.
2921 if (config != null) {
2922 r.configuration = config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 }
2924
Craig Mautnere79d42682013-04-01 19:01:53 -07002925 // No longer need to keep the device awake.
2926 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
2927 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2928 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 }
2930
Craig Mautnere79d42682013-04-01 19:01:53 -07002931 // We are now idle. If someone is waiting for a thumbnail from
2932 // us, we can now deliver.
2933 r.idle = true;
2934 mService.scheduleAppGcsLocked();
2935 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
2936 sendThumbnail = r.app.thread;
2937 r.thumbnailNeeded = false;
2938 }
2939
2940 // If this activity is fullscreen, set up to hide those under it.
2941
2942 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
2943 ensureActivitiesVisibleLocked(null, 0);
2944
2945 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Craig Mautner2219a1b2013-03-25 09:44:30 -07002946 if (mStackSupervisor.isMainStack(this)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002947 if (!mService.mBooted) {
2948 mService.mBooted = true;
2949 enableScreen = true;
2950 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002951 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002952 } else if (fromTimeout) {
2953 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
2954 }
2955
2956 // Atomically retrieve all of the other things to do.
2957 stops = processStoppingActivitiesLocked(true);
2958 NS = stops != null ? stops.size() : 0;
2959 if ((NF=mFinishingActivities.size()) > 0) {
2960 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2961 mFinishingActivities.clear();
2962 }
2963 if ((NT=mCancelledThumbnails.size()) > 0) {
2964 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
2965 mCancelledThumbnails.clear();
2966 }
2967
2968 if (mStackSupervisor.isMainStack(this)) {
2969 booting = mService.mBooting;
2970 mService.mBooting = false;
2971 }
2972 if (mStartingUsers.size() > 0) {
2973 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2974 mStartingUsers.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 }
2976
2977 int i;
2978
2979 // Send thumbnail if requested.
2980 if (sendThumbnail != null) {
2981 try {
2982 sendThumbnail.requestThumbnail(token);
2983 } catch (Exception e) {
2984 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
2985 mService.sendPendingThumbnail(null, token, null, null, true);
2986 }
2987 }
2988
2989 // Stop any activities that are scheduled to do so but have been
2990 // waiting for the next one to start.
2991 for (i=0; i<NS; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002992 r = stops.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 synchronized (mService) {
2994 if (r.finishing) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002995 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996 } else {
2997 stopActivityLocked(r);
2998 }
2999 }
3000 }
3001
3002 // Finish any activities that are scheduled to do so but have been
3003 // waiting for the next one to start.
3004 for (i=0; i<NF; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003005 r = finishes.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003006 synchronized (mService) {
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003007 activityRemoved = destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003008 }
3009 }
3010
3011 // Report back to any thumbnail receivers.
3012 for (i=0; i<NT; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003013 r = thumbnails.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003014 mService.sendPendingThumbnail(r, null, null, null, true);
3015 }
3016
3017 if (booting) {
3018 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003019 } else if (startingUsers != null) {
3020 for (i=0; i<startingUsers.size(); i++) {
3021 mService.finishUserSwitch(startingUsers.get(i));
3022 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 }
3024
3025 mService.trimApplications();
3026 //dump();
3027 //mWindowManager.dump();
3028
3029 if (enableScreen) {
3030 mService.enableScreenAfterBoot();
3031 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003032
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003033 if (activityRemoved) {
3034 resumeTopActivityLocked(null);
3035 }
3036
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003037 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 }
3039
3040 /**
3041 * @return Returns true if the activity is being finished, false if for
3042 * some reason it is being left as-is.
3043 */
3044 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003045 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003046 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07003047 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003048 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003049 + ", result=" + resultCode + ", data=" + resultData
3050 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003051 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 return false;
3053 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054
Craig Mautnerd44711d2013-02-23 11:24:36 -08003055 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003056 return true;
3057 }
3058
Craig Mautnerd2328952013-03-05 12:46:26 -08003059 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003060 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3061 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3062 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3063 ActivityRecord r = activities.get(activityNdx);
3064 if (r.resultTo == self && r.requestCode == requestCode) {
3065 if ((r.resultWho == null && resultWho == null) ||
3066 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3067 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3068 false);
3069 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003070 }
3071 }
3072 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003073 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003074 }
3075
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003076 final void finishTopRunningActivityLocked(ProcessRecord app) {
3077 ActivityRecord r = topRunningActivityLocked(null);
3078 if (r != null && r.app == app) {
3079 // If the top running activity is from this crashing
3080 // process, then terminate it to avoid getting in a loop.
3081 Slog.w(TAG, " Force finishing activity "
3082 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08003083 int taskNdx = mTaskHistory.indexOf(r.task);
3084 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003085 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003086 // Also terminate any activities below it that aren't yet
3087 // stopped, to avoid a situation where one will get
3088 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003089 --activityNdx;
3090 if (activityNdx < 0) {
3091 do {
3092 --taskNdx;
3093 if (taskNdx < 0) {
3094 break;
3095 }
3096 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3097 } while (activityNdx < 0);
3098 }
3099 if (activityNdx >= 0) {
3100 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003101 if (r.state == ActivityState.RESUMED
3102 || r.state == ActivityState.PAUSING
3103 || r.state == ActivityState.PAUSED) {
3104 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
3105 Slog.w(TAG, " Force finishing activity "
3106 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003107 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003108 }
3109 }
3110 }
3111 }
3112 }
3113
Craig Mautnerd2328952013-03-05 12:46:26 -08003114 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003115 ArrayList<ActivityRecord> activities = r.task.mActivities;
3116 for (int index = activities.indexOf(r); index >= 0; --index) {
3117 ActivityRecord cur = activities.get(index);
3118 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003119 break;
3120 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003121 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity",
3122 true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003123 }
3124 return true;
3125 }
3126
Dianne Hackborn5c607432012-02-28 14:44:19 -08003127 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3128 // send the result
3129 ActivityRecord resultTo = r.resultTo;
3130 if (resultTo != null) {
3131 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
3132 + " who=" + r.resultWho + " req=" + r.requestCode
3133 + " res=" + resultCode + " data=" + resultData);
3134 if (r.info.applicationInfo.uid > 0) {
3135 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3136 resultTo.packageName, resultData,
3137 resultTo.getUriPermissionsLocked());
3138 }
3139 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3140 resultData);
3141 r.resultTo = null;
3142 }
3143 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
3144
3145 // Make sure this HistoryRecord is not holding on to other resources,
3146 // because clients have remote IPC references to this object so we
3147 // can't assume that will go away and want to avoid circular IPC refs.
3148 r.results = null;
3149 r.pendingResults = null;
3150 r.newIntents = null;
3151 r.icicle = null;
3152 }
3153
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 /**
3155 * @return Returns true if this activity has been removed from the history
3156 * list, or false if it is still in the list and will be removed later.
3157 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08003158 final boolean finishActivityLocked(ActivityRecord r, int resultCode,
3159 Intent resultData, String reason, boolean oomAdj) {
3160 return finishActivityLocked(r, resultCode, resultData, reason, false, oomAdj);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003161 }
3162
3163 /**
3164 * @return Returns true if this activity has been removed from the history
3165 * list, or false if it is still in the list and will be removed later.
3166 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08003167 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3168 String reason, boolean immediate, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 if (r.finishing) {
3170 Slog.w(TAG, "Duplicate finish request for " + r);
3171 return false;
3172 }
3173
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08003174 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003176 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003177 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003178 final ArrayList<ActivityRecord> activities = r.task.mActivities;
3179 final int index = activities.indexOf(r);
3180 if (index < (activities.size() - 1)) {
3181 ActivityRecord next = activities.get(index+1);
3182 if (r.frontOfTask) {
3183 // The next activity is now the front of the task.
3184 next.frontOfTask = true;
3185 }
3186 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3187 // If the caller asked that this activity (and all above it)
3188 // be cleared when the task is reset, don't lose that information,
3189 // but propagate it up to the next activity.
3190 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 }
3192 }
3193
3194 r.pauseKeyDispatchingLocked();
Craig Mautner2219a1b2013-03-25 09:44:30 -07003195 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 if (mService.mFocusedActivity == r) {
3197 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3198 }
3199 }
3200
Dianne Hackborn5c607432012-02-28 14:44:19 -08003201 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003203 if (mService.mPendingThumbnails.size() > 0) {
3204 // There are clients waiting to receive thumbnails so, in case
3205 // this is an activity that someone is waiting for, add it
3206 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08003207 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 }
3209
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003210 if (immediate) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003211 return finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, oomAdj) == null;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003212 } else if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003213 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003214 if (DEBUG_TRANSITION) Slog.v(TAG,
3215 "Prepare close transition: finishing " + r);
3216 mService.mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003217 ? AppTransition.TRANSIT_TASK_CLOSE
3218 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08003221 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003223 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
3225 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
3226 startPausingLocked(false, false);
3227 }
3228
3229 } else if (r.state != ActivityState.PAUSING) {
3230 // If the activity is PAUSING, we will complete the finish once
3231 // it is done pausing; else we can just directly finish it here.
3232 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003233 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 } else {
3235 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
3236 }
3237
3238 return false;
3239 }
3240
3241 private static final int FINISH_IMMEDIATELY = 0;
3242 private static final int FINISH_AFTER_PAUSE = 1;
3243 private static final int FINISH_AFTER_VISIBLE = 2;
3244
3245 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003246 int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 // First things first: if this activity is currently visible,
3248 // and the resumed activity is not yet visible, then hold off on
3249 // finishing until the resumed one becomes visible.
3250 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
3251 if (!mStoppingActivities.contains(r)) {
3252 mStoppingActivities.add(r);
3253 if (mStoppingActivities.size() > 3) {
3254 // If we already have a few activities waiting to stop,
3255 // then give up on things going idle and start clearing
3256 // them out.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003257 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003258 } else {
3259 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003260 }
3261 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003262 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3263 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003265 if (oomAdj) {
3266 mService.updateOomAdjLocked();
3267 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268 return r;
3269 }
3270
3271 // make sure the record is cleaned out of other places.
3272 mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003273 mGoingToSleepActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003274 mWaitingVisibleActivities.remove(r);
3275 if (mResumedActivity == r) {
3276 mResumedActivity = null;
3277 }
3278 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003279 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003280 r.state = ActivityState.FINISHING;
3281
3282 if (mode == FINISH_IMMEDIATELY
3283 || prevState == ActivityState.STOPPED
3284 || prevState == ActivityState.INITIALIZING) {
3285 // If this activity is already stopped, we can just finish
3286 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003287 boolean activityRemoved = destroyActivityLocked(r, true,
3288 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003289 if (activityRemoved) {
3290 resumeTopActivityLocked(null);
3291 }
3292 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003293 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003294
3295 // Need to go through the full pause cycle to get this
3296 // activity into the stopped state and then finish it.
3297 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
3298 mFinishingActivities.add(r);
3299 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 return r;
3301 }
3302
Craig Mautnerd2328952013-03-05 12:46:26 -08003303 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003304 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003305 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08003306 final TaskRecord task = srec.task;
3307 final ArrayList<ActivityRecord> activities = task.mActivities;
3308 final int start = activities.indexOf(srec);
3309 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003310 return false;
3311 }
3312 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003313 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003314 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003315 final ComponentName dest = destIntent.getComponent();
3316 if (start > 0 && dest != null) {
3317 for (int i = finishTo; i >= 0; i--) {
3318 ActivityRecord r = activities.get(i);
3319 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003320 r.info.name.equals(dest.getClassName())) {
3321 finishTo = i;
3322 parent = r;
3323 foundParentInTask = true;
3324 break;
3325 }
3326 }
3327 }
3328
3329 IActivityController controller = mService.mController;
3330 if (controller != null) {
3331 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3332 if (next != null) {
3333 // ask watcher if this is allowed
3334 boolean resumeOK = true;
3335 try {
3336 resumeOK = controller.activityResuming(next.packageName);
3337 } catch (RemoteException e) {
3338 mService.mController = null;
3339 }
3340
3341 if (!resumeOK) {
3342 return false;
3343 }
3344 }
3345 }
3346 final long origId = Binder.clearCallingIdentity();
3347 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003348 ActivityRecord r = activities.get(i);
3349 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003350 // Only return the supplied result for the first activity finished
3351 resultCode = Activity.RESULT_CANCELED;
3352 resultData = null;
3353 }
3354
3355 if (parent != null && foundParentInTask) {
3356 final int parentLaunchMode = parent.info.launchMode;
3357 final int destIntentFlags = destIntent.getFlags();
3358 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3359 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3360 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3361 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
3362 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
3363 } else {
3364 try {
3365 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3366 destIntent.getComponent(), 0, srec.userId);
3367 int res = startActivityLocked(srec.app.thread, destIntent,
3368 null, aInfo, parent.appToken, null,
3369 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
3370 0, null, true, null);
3371 foundParentInTask = res == ActivityManager.START_SUCCESS;
3372 } catch (RemoteException e) {
3373 foundParentInTask = false;
3374 }
3375 requestFinishActivityLocked(parent.appToken, resultCode,
3376 resultData, "navigate-up", true);
3377 }
3378 }
3379 Binder.restoreCallingIdentity(origId);
3380 return foundParentInTask;
3381 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 /**
3383 * Perform the common clean-up of an activity record. This is called both
3384 * as part of destroyActivityLocked() (when destroying the client-side
3385 * representation) and cleaning things up as a result of its hosting
3386 * processing going away, in which case there is no remaining client-side
3387 * state to destroy so only the cleanup here is needed.
3388 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003389 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3390 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003391 if (mResumedActivity == r) {
3392 mResumedActivity = null;
3393 }
3394 if (mService.mFocusedActivity == r) {
3395 mService.mFocusedActivity = null;
3396 }
3397
3398 r.configDestroy = false;
3399 r.frozenBeforeDestroy = false;
3400
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003401 if (setState) {
3402 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3403 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003404 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003405 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003406 }
3407
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003408 // Make sure this record is no longer in the pending finishes list.
3409 // This could happen, for example, if we are trimming activities
3410 // down to the max limit while they are still waiting to finish.
3411 mFinishingActivities.remove(r);
3412 mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003413
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003414 // Remove any pending results.
3415 if (r.finishing && r.pendingResults != null) {
3416 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3417 PendingIntentRecord rec = apr.get();
3418 if (rec != null) {
3419 mService.cancelIntentSenderLocked(rec, false);
3420 }
3421 }
3422 r.pendingResults = null;
3423 }
3424
3425 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003426 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 }
3428
3429 if (mService.mPendingThumbnails.size() > 0) {
3430 // There are clients waiting to receive thumbnails so, in case
3431 // this is an activity that someone is waiting for, add it
3432 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08003433 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003434 }
3435
3436 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003437 removeTimeoutsForActivityLocked(r);
3438 }
3439
3440 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003441 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003442 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003443 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003444 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003445 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003446 }
3447
Dianne Hackborn5c607432012-02-28 14:44:19 -08003448 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003449 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3450 r.makeFinishing();
3451 if (DEBUG_ADD_REMOVE) {
3452 RuntimeException here = new RuntimeException("here");
3453 here.fillInStackTrace();
3454 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003455 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003456 if (r.task != null) {
3457 removeActivity(r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003458 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003459 r.takeFromHistory();
3460 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003461 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003462 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003463 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003464 r.app = null;
3465 mService.mWindowManager.removeAppToken(r.appToken);
3466 if (VALIDATE_TOKENS) {
3467 validateAppTokensLocked();
3468 }
3469 cleanUpActivityServicesLocked(r);
3470 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 }
3472
3473 /**
3474 * Perform clean-up of service connections in an activity record.
3475 */
3476 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3477 // Throw away any services that have been bound by this activity.
3478 if (r.connections != null) {
3479 Iterator<ConnectionRecord> it = r.connections.iterator();
3480 while (it.hasNext()) {
3481 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003482 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003483 }
3484 r.connections = null;
3485 }
3486 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003487
3488 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
3489 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
3490 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
3491 mHandler.sendMessage(msg);
3492 }
3493
Dianne Hackborn28695e02011-11-02 21:59:51 -07003494 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003495 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003496 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003497 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3498 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3499 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3500 final ActivityRecord r = activities.get(activityNdx);
3501 if (r.finishing) {
3502 continue;
3503 }
3504 if (r.fullscreen) {
3505 lastIsOpaque = true;
3506 }
3507 if (owner != null && r.app != owner) {
3508 continue;
3509 }
3510 if (!lastIsOpaque) {
3511 continue;
3512 }
3513 // We can destroy this one if we have its icicle saved and
3514 // it is not in the process of pausing/stopping/finishing.
3515 if (r.app != null && r != mResumedActivity && r != mPausingActivity
3516 && r.haveState && !r.visible && r.stopped
3517 && r.state != ActivityState.DESTROYING
3518 && r.state != ActivityState.DESTROYED) {
3519 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3520 + " resumed=" + mResumedActivity
3521 + " pausing=" + mPausingActivity);
3522 if (destroyActivityLocked(r, true, oomAdj, reason)) {
3523 activityRemoved = true;
3524 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003525 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003526 }
3527 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003528 if (activityRemoved) {
3529 resumeTopActivityLocked(null);
3530 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003531 }
3532
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 /**
3534 * Destroy the current CLIENT SIDE instance of an activity. This may be
3535 * called both when actually finishing an activity, or when performing
3536 * a configuration switch where we destroy the current client-side object
3537 * but then create a new client-side object for this same HistoryRecord.
3538 */
3539 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07003540 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003541 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003542 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3544 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003545 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003546 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003547
3548 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003549
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003550 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003551
3552 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003553
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003554 if (hadApp) {
3555 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003556 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3558 mService.mHeavyWeightProcess = null;
3559 mService.mHandler.sendEmptyMessage(
3560 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3561 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003562 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003563 // No longer have activities, so update oom adj.
3564 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003565 }
3566 }
3567
3568 boolean skipDestroy = false;
3569
3570 try {
3571 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003572 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 r.configChangeFlags);
3574 } catch (Exception e) {
3575 // We can just ignore exceptions here... if the process
3576 // has crashed, our death notification will clean things
3577 // up.
3578 //Slog.w(TAG, "Exception thrown during finish", e);
3579 if (r.finishing) {
3580 removeActivityFromHistoryLocked(r);
3581 removedFromHistory = true;
3582 skipDestroy = true;
3583 }
3584 }
3585
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003586 r.nowVisible = false;
3587
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003588 // If the activity is finishing, we need to wait on removing it
3589 // from the list to give it a chance to do its cleanup. During
3590 // that time it may make calls back with its token so we need to
3591 // be able to find it on the list and so we don't want to remove
3592 // it from the list yet. Otherwise, we can just immediately put
3593 // it in the destroyed state since we are not removing it from the
3594 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003595 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003596 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3597 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003598 r.state = ActivityState.DESTROYING;
3599 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
3600 msg.obj = r;
3601 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3602 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003603 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3604 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003605 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003606 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003607 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 }
3609 } else {
3610 // remove this record from the history.
3611 if (r.finishing) {
3612 removeActivityFromHistoryLocked(r);
3613 removedFromHistory = true;
3614 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003615 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3616 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003618 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003619 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003620 }
3621 }
3622
3623 r.configChangeFlags = 0;
3624
3625 if (!mLRUActivities.remove(r) && hadApp) {
3626 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3627 }
3628
3629 return removedFromHistory;
3630 }
3631
Craig Mautnerd2328952013-03-05 12:46:26 -08003632 final void activityDestroyedLocked(IBinder token) {
3633 final long origId = Binder.clearCallingIdentity();
3634 try {
3635 ActivityRecord r = ActivityRecord.forToken(token);
3636 if (r != null) {
3637 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003639
3640 if (isInStackLocked(token) != null) {
3641 if (r.state == ActivityState.DESTROYING) {
3642 cleanUpActivityLocked(r, true, false);
3643 removeActivityFromHistoryLocked(r);
3644 }
3645 }
3646 resumeTopActivityLocked(null);
3647 } finally {
3648 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003649 }
3650 }
3651
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003652 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3653 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003655 if (DEBUG_CLEANUP) Slog.v(
3656 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657 + " with " + i + " entries");
3658 while (i > 0) {
3659 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003660 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003661 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003662 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003663 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003665 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 }
3667 }
3668 }
3669
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003670 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3671 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
3672 removeHistoryRecordsForAppLocked(mStoppingActivities, app, "mStoppingActivities");
3673 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
3674 removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app,
3675 "mWaitingVisibleActivities");
3676 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
3677
3678 boolean hasVisibleActivities = false;
3679
3680 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003681 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003682 if (DEBUG_CLEANUP) Slog.v(
3683 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003684 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3685 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3686 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3687 final ActivityRecord r = activities.get(activityNdx);
3688 --i;
3689 if (DEBUG_CLEANUP) Slog.v(
3690 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3691 if (r.app == app) {
3692 boolean remove;
3693 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3694 // Don't currently have state for the activity, or
3695 // it is finishing -- always remove it.
3696 remove = true;
3697 } else if (r.launchCount > 2 &&
3698 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3699 // We have launched this activity too many times since it was
3700 // able to run, so give up and remove it.
3701 remove = true;
3702 } else {
3703 // The process may be gone, but the activity lives on!
3704 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003705 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003706 if (remove) {
3707 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
3708 RuntimeException here = new RuntimeException("here");
3709 here.fillInStackTrace();
3710 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3711 + ": haveState=" + r.haveState
3712 + " stateNotNeeded=" + r.stateNotNeeded
3713 + " finishing=" + r.finishing
3714 + " state=" + r.state, here);
3715 }
3716 if (!r.finishing) {
3717 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3718 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3719 r.userId, System.identityHashCode(r),
3720 r.task.taskId, r.shortComponentName,
3721 "proc died without state saved");
3722 }
3723 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003724
Craig Mautner0247fc82013-02-28 14:32:06 -08003725 } else {
3726 // We have the current state for this activity, so
3727 // it can be restarted later when needed.
3728 if (localLOGV) Slog.v(
3729 TAG, "Keeping entry, setting app to null");
3730 if (r.visible) {
3731 hasVisibleActivities = true;
3732 }
3733 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3734 + r);
3735 r.app = null;
3736 r.nowVisible = false;
3737 if (!r.haveState) {
3738 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
3739 "App died, clearing saved state of " + r);
3740 r.icicle = null;
3741 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003742 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003743
Craig Mautnerd2328952013-03-05 12:46:26 -08003744 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003745 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003746 }
3747 }
3748
3749 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003750 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003751
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003752 /**
3753 * Move the current home activity's task (if one exists) to the front
3754 * of the stack.
3755 */
3756 final void moveHomeToFrontLocked() {
Craig Mautner0247fc82013-02-28 14:32:06 -08003757 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3758 final TaskRecord task = mTaskHistory.get(taskNdx);
3759 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner11bf9a52013-02-19 14:08:51 -08003760 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3761 final ActivityRecord r = activities.get(activityNdx);
3762 if (r.isHomeActivity) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003763 moveTaskToFrontLocked(task, null, null);
3764 return;
Craig Mautner11bf9a52013-02-19 14:08:51 -08003765 }
3766 }
3767 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003768 }
3769
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003770 final void updateTransitLocked(int transit, Bundle options) {
3771 if (options != null) {
3772 ActivityRecord r = topRunningActivityLocked(null);
3773 if (r != null && r.state != ActivityState.RESUMED) {
3774 r.updateOptionsLocked(options);
3775 } else {
3776 ActivityOptions.abort(options);
3777 }
3778 }
3779 mService.mWindowManager.prepareAppTransition(transit, false);
3780 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003781
Craig Mautnercae015f2013-02-08 14:31:27 -08003782 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003783 final TaskRecord task = taskForIdLocked(taskId);
3784 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003785 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
3786 mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003787 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003788 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3789 // Caller wants the home activity moved with it. To accomplish this,
3790 // we'll just move the home task to the top first.
3791 moveHomeToFrontLocked();
3792 }
3793 moveTaskToFrontLocked(task, null, options);
3794 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003795 }
3796 return false;
3797 }
3798
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003799 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003800 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003801
Craig Mautner11bf9a52013-02-19 14:08:51 -08003802 final int numTasks = mTaskHistory.size();
3803 final int index = mTaskHistory.indexOf(tr);
3804 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
3805 // nothing to do!
3806 if (reason != null &&
3807 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3808 ActivityOptions.abort(options);
3809 } else {
3810 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3811 }
3812 return;
3813 }
3814
3815 // Shift all activities with this task up to the top
3816 // of the stack, keeping them in the same internal order.
3817 mTaskHistory.remove(tr);
3818 mTaskHistory.add(tr);
3819
3820 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003821 if (reason != null &&
3822 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003823 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003824 ActivityRecord r = topRunningActivityLocked(null);
3825 if (r != null) {
3826 mNoAnimActivities.add(r);
3827 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003828 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003829 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003830 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003831 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003832
Craig Mautner58547802013-03-05 08:23:53 -08003833 mService.mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003834
Craig Mautner58547802013-03-05 08:23:53 -08003835 resumeTopActivityLocked(null);
3836 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003837
3838 if (VALIDATE_TOKENS) {
3839 validateAppTokensLocked();
3840 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003841 }
3842
3843 /**
3844 * Worker method for rearranging history stack. Implements the function of moving all
3845 * activities for a specific task (gathering them if disjoint) into a single group at the
3846 * bottom of the stack.
3847 *
3848 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3849 * to premeptively cancel the move.
3850 *
3851 * @param task The taskId to collect and move to the bottom.
3852 * @return Returns true if the move completed, false if not.
3853 */
3854 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3855 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003856
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003857 // If we have a watcher, preflight the move before committing to it. First check
3858 // for *other* available tasks, but if none are available, then try again allowing the
3859 // current task to be selected.
Craig Mautner2219a1b2013-03-25 09:44:30 -07003860 if (mStackSupervisor.isMainStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 ActivityRecord next = topRunningActivityLocked(null, task);
3862 if (next == null) {
3863 next = topRunningActivityLocked(null, 0);
3864 }
3865 if (next != null) {
3866 // ask watcher if this is allowed
3867 boolean moveOK = true;
3868 try {
3869 moveOK = mService.mController.activityResuming(next.packageName);
3870 } catch (RemoteException e) {
3871 mService.mController = null;
3872 }
3873 if (!moveOK) {
3874 return false;
3875 }
3876 }
3877 }
3878
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003879 if (DEBUG_TRANSITION) Slog.v(TAG,
3880 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003881
Craig Mautnerd2328952013-03-05 12:46:26 -08003882 final TaskRecord tr = taskForIdLocked(task);
3883 if (tr == null) {
3884 return false;
3885 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003886 mTaskHistory.remove(tr);
3887 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003888
3889 if (reason != null &&
3890 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003891 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003892 ActivityRecord r = topRunningActivityLocked(null);
3893 if (r != null) {
3894 mNoAnimActivities.add(r);
3895 }
3896 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08003897 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003898 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003899 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003900 mService.mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003901
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003903 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003904 }
3905
Craig Mautner58547802013-03-05 08:23:53 -08003906 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003907 return true;
3908 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003909
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003910 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003911 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003912 ActivityRecord resumed = mResumedActivity;
3913 if (resumed != null && resumed.thumbHolder == tr) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003914 info.mainThumbnail = screenshotActivities(resumed);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003915 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003916 if (info.mainThumbnail == null) {
3917 info.mainThumbnail = tr.lastThumbnail;
3918 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003919 return info;
3920 }
3921
Dianne Hackborn15491c62012-09-19 10:59:14 -07003922 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
3923 ActivityRecord resumed = mResumedActivity;
3924 if (resumed != null && resumed.task == tr) {
3925 // This task is the current resumed task, we just need to take
3926 // a screenshot of it and return that.
Craig Mautnerd2328952013-03-05 12:46:26 -08003927 return screenshotActivities(resumed);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003928 }
3929 // Return the information about the task, to figure out the top
3930 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003931 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003932 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003933 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003934 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003935 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003936 }
3937
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003938 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3939 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003940 final TaskRecord task = taskForIdLocked(taskId);
3941 if (task == null) {
3942 return null;
3943 }
Craig Mautner16030772013-02-26 16:05:47 -08003944 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003945 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003946 if (taskRequired) {
3947 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3948 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003949 return null;
3950 }
3951
3952 if (subTaskIndex < 0) {
3953 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003954 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003955 return info.root;
3956 }
3957
3958 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003959 if (taskRequired) {
3960 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3961 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003962 return null;
3963 }
3964
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003965 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003966 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003967 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003968 return subtask.activity;
3969 }
3970
Craig Mautner16030772013-02-26 16:05:47 -08003971 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003972 final TaskAccessInfo thumbs = new TaskAccessInfo();
3973 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003974 final ArrayList<ActivityRecord> activities = task.mActivities;
3975 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003976 int j = 0;
3977 ThumbnailHolder holder = null;
3978 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003979 ActivityRecord ar = activities.get(j);
3980 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003981 thumbs.root = ar;
3982 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003983 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003984 if (holder != null) {
3985 thumbs.mainThumbnail = holder.lastThumbnail;
3986 }
3987 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003988 break;
3989 }
3990 j++;
3991 }
3992
3993 if (j >= NA) {
3994 return thumbs;
3995 }
3996
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003997 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
3998 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003999 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08004000 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004001 j++;
4002 if (ar.finishing) {
4003 continue;
4004 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004005 if (ar.thumbHolder != holder && holder != null) {
4006 thumbs.numSubThumbbails++;
4007 holder = ar.thumbHolder;
4008 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07004009 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004010 sub.activity = ar;
4011 sub.index = j-1;
4012 subtasks.add(sub);
4013 }
4014 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004015 if (thumbs.numSubThumbbails > 0) {
4016 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004017 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004018 public Bitmap getThumbnail(int index) {
4019 if (index < 0 || index >= thumbs.subtasks.size()) {
4020 return null;
4021 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004022 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
4023 ActivityRecord resumed = mResumedActivity;
4024 if (resumed != null && resumed.thumbHolder == sub.holder) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004025 return screenshotActivities(resumed);
Dianne Hackborn15491c62012-09-19 10:59:14 -07004026 }
4027 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004028 }
4029 };
4030 }
4031 return thumbs;
4032 }
4033
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004034 private final void logStartActivity(int tag, ActivityRecord r,
4035 TaskRecord task) {
4036 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004037 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004038 r.shortComponentName, r.intent.getAction(),
4039 r.intent.getType(), r.intent.getDataString(),
4040 r.intent.getFlags());
4041 }
4042
4043 /**
4044 * Make sure the given activity matches the current configuration. Returns
4045 * false if the activity had to be destroyed. Returns true if the
4046 * configuration is the same, or the activity will remain running as-is
4047 * for whatever reason. Ensures the HistoryRecord is updated with the
4048 * correct configuration and all other bookkeeping is handled.
4049 */
4050 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
4051 int globalChanges) {
4052 if (mConfigWillChange) {
4053 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4054 "Skipping config check (will change): " + r);
4055 return true;
4056 }
4057
4058 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4059 "Ensuring correct configuration: " + r);
4060
4061 // Short circuit: if the two configurations are the exact same
4062 // object (the common case), then there is nothing to do.
4063 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004064 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4066 "Configuration unchanged in " + r);
4067 return true;
4068 }
4069
4070 // We don't worry about activities that are finishing.
4071 if (r.finishing) {
4072 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4073 "Configuration doesn't matter in finishing " + r);
4074 r.stopFreezingScreenLocked(false);
4075 return true;
4076 }
4077
4078 // Okay we now are going to make this activity have the new config.
4079 // But then we need to figure out how it needs to deal with that.
4080 Configuration oldConfig = r.configuration;
4081 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004082
4083 // Determine what has changed. May be nothing, if this is a config
4084 // that has come back from the app after going idle. In that case
4085 // we just want to leave the official config object now in the
4086 // activity and do nothing else.
4087 final int changes = oldConfig.diff(newConfig);
4088 if (changes == 0 && !r.forceNewConfig) {
4089 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4090 "Configuration no differences in " + r);
4091 return true;
4092 }
4093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004094 // If the activity isn't currently running, just leave the new
4095 // configuration and it will pick that up next time it starts.
4096 if (r.app == null || r.app.thread == null) {
4097 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4098 "Configuration doesn't matter not running " + r);
4099 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004100 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004101 return true;
4102 }
4103
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004104 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004105 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
4106 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
4107 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07004108 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004109 + ", newConfig=" + newConfig);
4110 }
Dianne Hackborne6676352011-06-01 16:51:20 -07004111 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004112 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4113 r.configChangeFlags |= changes;
4114 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004115 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004116 if (r.app == null || r.app.thread == null) {
4117 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004118 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07004119 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004120 } else if (r.state == ActivityState.PAUSING) {
4121 // A little annoying: we are waiting for this activity to
4122 // finish pausing. Let's not do anything now, but just
4123 // flag that it needs to be restarted when done pausing.
4124 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004125 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004126 r.configDestroy = true;
4127 return true;
4128 } else if (r.state == ActivityState.RESUMED) {
4129 // Try to optimize this case: the configuration is changing
4130 // and we need to restart the top, resumed activity.
4131 // Instead of doing the normal handshaking, just say
4132 // "restart!".
4133 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004134 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004135 relaunchActivityLocked(r, r.configChangeFlags, true);
4136 r.configChangeFlags = 0;
4137 } else {
4138 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004139 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004140 relaunchActivityLocked(r, r.configChangeFlags, false);
4141 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004142 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004143
4144 // All done... tell the caller we weren't able to keep this
4145 // activity around.
4146 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004147 }
4148
4149 // Default case: the activity can handle this new configuration, so
4150 // hand it over. Note that we don't need to give it the new
4151 // configuration, since we always send configuration changes to all
4152 // process when they happen so it can just use whatever configuration
4153 // it last got.
4154 if (r.app != null && r.app.thread != null) {
4155 try {
4156 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004157 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004158 } catch (RemoteException e) {
4159 // If process died, whatever.
4160 }
4161 }
4162 r.stopFreezingScreenLocked(false);
4163
4164 return true;
4165 }
4166
4167 private final boolean relaunchActivityLocked(ActivityRecord r,
4168 int changes, boolean andResume) {
4169 List<ResultInfo> results = null;
4170 List<Intent> newIntents = null;
4171 if (andResume) {
4172 results = r.results;
4173 newIntents = r.newIntents;
4174 }
4175 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
4176 + " with results=" + results + " newIntents=" + newIntents
4177 + " andResume=" + andResume);
4178 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004179 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004180 r.task.taskId, r.shortComponentName);
4181
4182 r.startFreezingScreenLocked(r.app, 0);
4183
4184 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004185 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
4186 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
4187 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004188 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08004189 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004190 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004191 // Note: don't need to call pauseIfSleepingLocked() here, because
4192 // the caller will only pass in 'andResume' if this activity is
4193 // currently resumed, which implies we aren't sleeping.
4194 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004195 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004196 }
4197
4198 if (andResume) {
4199 r.results = null;
4200 r.newIntents = null;
Craig Mautner2219a1b2013-03-25 09:44:30 -07004201 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004202 mService.reportResumedActivityLocked(r);
4203 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004204 r.state = ActivityState.RESUMED;
4205 } else {
4206 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4207 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004208 }
4209
4210 return true;
4211 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004212
4213 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004214 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4215 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4216 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4217 final ActivityRecord r = activities.get(activityNdx);
4218 if (r.appToken == token) {
4219 return true;
4220 }
4221 if (r.fullscreen && !r.finishing) {
4222 return false;
4223 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004224 }
4225 }
4226 return true;
4227 }
4228
4229 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004230 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4231 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4232 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4233 final ActivityRecord r = activities.get(activityNdx);
4234 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004235 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004236 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004237 }
4238 }
4239 }
4240
4241 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
4242 boolean didSomething = false;
4243 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004244 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4245 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4246 int numActivities = activities.size();
4247 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4248 ActivityRecord r = activities.get(activityNdx);
4249 final boolean samePackage = r.packageName.equals(name)
4250 || (name == null && r.userId == userId);
4251 if ((userId == UserHandle.USER_ALL || r.userId == userId)
4252 && (samePackage || r.task == lastTask)
4253 && (r.app == null || evenPersistent || !r.app.persistent)) {
4254 if (!doit) {
4255 if (r.finishing) {
4256 // If this activity is just finishing, then it is not
4257 // interesting as far as something to stop.
4258 continue;
4259 }
4260 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004261 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004262 didSomething = true;
4263 Slog.i(TAG, " Force finishing activity " + r);
4264 if (samePackage) {
4265 if (r.app != null) {
4266 r.app.removed = true;
4267 }
4268 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004269 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004270 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08004271 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08004272 }
4273 }
4274 }
4275 return didSomething;
4276 }
4277
4278 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08004279 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004280 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08004281 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
4282 --maxNum, --taskNdx) {
4283 final TaskRecord task = mTaskHistory.get(taskNdx);
4284 ActivityRecord r = null;
4285 ActivityRecord top = null;
4286 int numActivities = 0;
4287 int numRunning = 0;
4288 final ArrayList<ActivityRecord> activities = task.mActivities;
4289 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4290 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004291
Craig Mautneraab647e2013-02-28 16:31:36 -08004292 // Initialize state for next task if needed.
4293 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4294 top = r;
4295 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004296 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004297
4298 // Add 'r' into the current task.
4299 numActivities++;
4300 if (r.app != null && r.app.thread != null) {
4301 numRunning++;
4302 }
4303
4304 if (localLOGV) Slog.v(
4305 TAG, r.intent.getComponent().flattenToShortString()
4306 + ": task=" + r.task);
4307 }
4308
4309 RunningTaskInfo ci = new RunningTaskInfo();
4310 ci.id = task.taskId;
4311 ci.baseActivity = r.intent.getComponent();
4312 ci.topActivity = top.intent.getComponent();
4313 if (top.thumbHolder != null) {
4314 ci.description = top.thumbHolder.lastDescription;
4315 }
4316 ci.numActivities = numActivities;
4317 ci.numRunning = numRunning;
4318 //System.out.println(
4319 // "#" + maxNum + ": " + " descr=" + ci.description);
4320 if (receiver != null) {
4321 if (localLOGV) Slog.v(
4322 TAG, "State=" + top.state + "Idle=" + top.idle
4323 + " app=" + top.app
4324 + " thr=" + (top.app != null ? top.app.thread : null));
4325 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
4326 if (top.idle && top.app != null && top.app.thread != null) {
4327 topRecord = top;
4328 } else {
4329 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004330 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004331 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004332 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08004333 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004334 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004335 }
4336 return topRecord;
4337 }
4338
4339 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004340 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004341 if (DEBUG_SWITCH) Slog.d(
4342 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004343 if (top >= 0) {
4344 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4345 int activityTop = activities.size() - 1;
4346 if (activityTop > 0) {
4347 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4348 "unhandled-back", true);
4349 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004350 }
4351 }
4352
Craig Mautnere79d42682013-04-01 19:01:53 -07004353 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
4354 if (mPausingActivity != null && mPausingActivity.app == app) {
4355 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
4356 "App died while pausing: " + mPausingActivity);
4357 mPausingActivity = null;
4358 }
4359 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4360 mLastPausedActivity = null;
4361 }
4362
4363 // Remove this application's activities from active lists.
4364 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
4365
4366 if (!restarting) {
4367 if (!resumeTopActivityLocked(null)) {
4368 // If there was nothing to resume, and we are not already
4369 // restarting this process, but there is a visible activity that
4370 // is hosted by the process... then make sure all visible
4371 // activities are running, taking care of restarting this
4372 // process.
4373 if (hasVisibleActivities) {
4374 ensureActivitiesVisibleLocked(null, 0);
4375 }
4376 }
4377 }
4378 }
4379
Craig Mautnercae015f2013-02-08 14:31:27 -08004380 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4382 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4383 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4384 final ActivityRecord r = activities.get(activityNdx);
4385 if (r.app == app) {
4386 Slog.w(TAG, " Force finishing activity "
4387 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08004388 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004389 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004390 }
4391 }
4392 }
4393
4394 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4395 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004396 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4397 final TaskRecord task = mTaskHistory.get(taskNdx);
4398 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004399 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08004400 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
4401 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004402 }
4403
4404 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4405 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4406
4407 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4409 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004410 }
4411 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004412 final int top = mTaskHistory.size() - 1;
4413 if (top >= 0) {
4414 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4415 int listTop = list.size() - 1;
4416 if (listTop >= 0) {
4417 activities.add(list.get(listTop));
4418 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004419 }
4420 } else {
4421 ItemMatcher matcher = new ItemMatcher();
4422 matcher.build(name);
4423
Craig Mautneraab647e2013-02-28 16:31:36 -08004424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4425 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4426 if (matcher.match(r1, r1.intent.getComponent())) {
4427 activities.add(r1);
4428 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004429 }
4430 }
4431 }
4432
4433 return activities;
4434 }
4435
4436 ActivityRecord restartPackage(String packageName) {
4437 ActivityRecord starting = topRunningActivityLocked(null);
4438
4439 // All activities that came from the package must be
4440 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4442 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4443 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4444 final ActivityRecord a = activities.get(activityNdx);
4445 if (a.info.packageName.equals(packageName)) {
4446 a.forceNewConfig = true;
4447 if (starting != null && a == starting && a.visible) {
4448 a.startFreezingScreenLocked(starting.app,
4449 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4450 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004451 }
4452 }
4453 }
4454
4455 return starting;
4456 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004457
Craig Mautner0247fc82013-02-28 14:32:06 -08004458 private void removeActivity(ActivityRecord r) {
4459 final TaskRecord task = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08004460 // TODO: use ActivityManagerService.removeTask to do this.
Craig Mautner0247fc82013-02-28 14:32:06 -08004461 if (task.removeActivity(r)) {
4462 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "removeActivity: Removing from history, task="
4463 + task);
4464 mTaskHistory.remove(task);
Craig Mautner0247fc82013-02-28 14:32:06 -08004465 }
4466 }
4467
4468 private void setTask(ActivityRecord r, TaskRecord newTask, ThumbnailHolder newThumbHolder,
4469 boolean isRoot) {
4470 if (r.task != null) {
4471 removeActivity(r);
4472 }
4473 r.setTask(newTask, newThumbHolder, isRoot);
4474 }
4475
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004476 private TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4477 boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004478 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004479 if (toTop) {
4480 mTaskHistory.add(task);
4481 } else {
4482 mTaskHistory.add(0, task);
4483 }
4484 return task;
4485 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004486
4487 ArrayList<TaskRecord> getAllTasks() {
4488 return new ArrayList<TaskRecord>(mTaskHistory);
4489 }
4490
4491 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004492 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004493 if (task == null) {
4494 return;
4495 }
4496 task.stack.mTaskHistory.remove(task);
4497 task.stack = this;
4498 if (toTop) {
4499 mTaskHistory.add(task);
4500 } else {
4501 mTaskHistory.add(0, task);
4502 }
4503 }
4504
4505 public int getStackId() {
4506 return mStackId;
4507 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004508}