blob: d18cc4c050fd36374d639aaa03925f0b42643f3c [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);
371 activityIdleInternal(r != null ? r.appToken : null, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700373 case LAUNCH_TICK_MSG: {
374 ActivityRecord r = (ActivityRecord)msg.obj;
375 synchronized (mService) {
376 if (r.continueLaunchTickingLocked()) {
377 mService.logAppTooSlow(r.app, r.launchTickTime,
378 "launching " + r);
379 }
380 }
381 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700382 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800383 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700384 // We don't at this point know if the activity is fullscreen,
385 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800386 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800387 synchronized (mService) {
388 activityDestroyedLocked(r != null ? r.appToken : null);
389 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700390 } break;
391 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800392 ActivityRecord r = (ActivityRecord)msg.obj;
393 activityIdleInternal(r != null ? r.appToken : null, false, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 } break;
395 case LAUNCH_TIMEOUT_MSG: {
396 if (mService.mDidDexOpt) {
397 mService.mDidDexOpt = false;
398 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
399 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
400 return;
401 }
402 synchronized (mService) {
403 if (mLaunchingActivity.isHeld()) {
404 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
405 mLaunchingActivity.release();
406 }
407 }
408 } break;
409 case RESUME_TOP_ACTIVITY_MSG: {
410 synchronized (mService) {
411 resumeTopActivityLocked(null);
412 }
413 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700414 case STOP_TIMEOUT_MSG: {
415 ActivityRecord r = (ActivityRecord)msg.obj;
416 // We don't at this point know if the activity is fullscreen,
417 // so we need to be conservative and assume it isn't.
418 Slog.w(TAG, "Activity stop timeout for " + r);
419 synchronized (mService) {
420 if (r.isInHistory()) {
421 activityStoppedLocked(r, null, null, null);
422 }
423 }
424 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700425 case DESTROY_ACTIVITIES_MSG: {
426 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
427 synchronized (mService) {
428 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
429 }
430 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700431 }
432 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800433 }
434
Craig Mautner000f0022013-02-26 15:04:29 -0800435 private int numActivities() {
436 int count = 0;
437 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
438 count += mTaskHistory.get(taskNdx).mActivities.size();
439 }
440 return count;
441 }
442
Craig Mautner2219a1b2013-03-25 09:44:30 -0700443 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId,
Craig Mautner2420ead2013-04-01 17:13:20 -0700444 ActivityStackSupervisor supervisor, int userId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900445 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700446 mService = service;
447 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700448 PowerManager pm =
449 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
450 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
451 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
452 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800453 mStackId = stackId;
Craig Mautner27084302013-03-25 08:05:25 -0700454 mStackSupervisor = supervisor;
Craig Mautner2420ead2013-04-01 17:13:20 -0700455 mCurrentUser = userId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 }
Craig Mautner5962b122012-10-05 14:45:52 -0700457
458 private boolean okToShow(ActivityRecord r) {
459 return r.userId == mCurrentUser
460 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
461 }
462
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700463 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800464 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
465 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800466 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800467 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
468 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800469 if (!r.finishing && r != notTop && okToShow(r)) {
470 return r;
471 }
472 }
473 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700474 return null;
475 }
476
477 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800478 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
479 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800480 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800481 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
482 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800483 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
484 return r;
485 }
486 }
487 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700488 return null;
489 }
490
491 /**
492 * This is a simplified version of topRunningActivityLocked that provides a number of
493 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800494 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700495 * @param token If non-null, any history records matching this token will be skipped.
496 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800497 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700498 * @return Returns the HistoryRecord of the next activity on the stack.
499 */
500 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800501 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
502 TaskRecord task = mTaskHistory.get(taskNdx);
503 if (task.taskId == taskId) {
504 continue;
505 }
506 ArrayList<ActivityRecord> activities = task.mActivities;
507 for (int i = activities.size() - 1; i >= 0; --i) {
508 final ActivityRecord r = activities.get(i);
509 // Note: the taskId check depends on real taskId fields being non-zero
510 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
511 return r;
512 }
513 }
514 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700515 return null;
516 }
517
Craig Mautnerd2328952013-03-05 12:46:26 -0800518 TaskRecord taskForIdLocked(int id) {
519 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
520 final TaskRecord task = mTaskHistory.get(taskNdx);
521 if (task.taskId == id) {
522 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800523 }
524 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700525 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700526 }
527
Craig Mautnerd2328952013-03-05 12:46:26 -0800528 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800529 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800530 if (r != null) {
531 final TaskRecord task = r.task;
532 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
533 if (task.stack != this) Slog.w(TAG,
534 "Illegal state! task does not point to stack it is in.");
535 return r;
536 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800537 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800538 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800539 }
540
Craig Mautner2420ead2013-04-01 17:13:20 -0700541 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700542 final boolean hadit = mLRUActivities.remove(r);
543 mLRUActivities.add(r);
544 return hadit;
545 }
546
547 /**
548 * Returns the top activity in any existing task matching the given
549 * Intent. Returns null if no such task is found.
550 */
551 private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
552 ComponentName cls = intent.getComponent();
553 if (info.targetActivity != null) {
554 cls = new ComponentName(info.packageName, info.targetActivity);
555 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700556 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800557
558 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
559 final TaskRecord task = mTaskHistory.get(taskNdx);
560 final ActivityRecord r = task.getTopActivity();
561 if (r == null || r.finishing || r.userId != userId ||
562 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
563 continue;
564 }
565
566 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
567 // + "/aff=" + r.task.affinity + " to new cls="
568 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
569 if (task.affinity != null) {
570 if (task.affinity.equals(info.taskAffinity)) {
571 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700572 return r;
573 }
Craig Mautner000f0022013-02-26 15:04:29 -0800574 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
575 //Slog.i(TAG, "Found matching class!");
576 //dump();
577 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
578 return r;
579 } else if (task.affinityIntent != null
580 && task.affinityIntent.getComponent().equals(cls)) {
581 //Slog.i(TAG, "Found matching class!");
582 //dump();
583 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
584 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700585 }
586 }
587
588 return null;
589 }
590
591 /**
592 * Returns the first activity (starting from the top of the stack) that
593 * is the same as the given activity. Returns null if no such activity
594 * is found.
595 */
596 private ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
597 ComponentName cls = intent.getComponent();
598 if (info.targetActivity != null) {
599 cls = new ComponentName(info.packageName, info.targetActivity);
600 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700601 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602
Craig Mautner000f0022013-02-26 15:04:29 -0800603 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
604 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
605 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
606 ActivityRecord r = activities.get(activityNdx);
607 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700608 //Slog.i(TAG, "Found matching class!");
609 //dump();
610 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
611 return r;
612 }
613 }
614 }
615
616 return null;
617 }
618
Amith Yamasani742a6712011-05-04 14:49:28 -0700619 /*
620 * Move the activities around in the stack to bring a user to the foreground.
621 * @return whether there are any activities for the specified user.
622 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700623 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800624 if (VALIDATE_TOKENS) {
625 validateAppTokensLocked();
626 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700627 mStartingUsers.add(uss);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 if (mCurrentUser == userId) {
629 return true;
630 }
631 mCurrentUser = userId;
632
633 // Move userId's tasks to the top.
634 boolean haveActivities = false;
635 TaskRecord task = null;
636 int index = mTaskHistory.size();
637 for (int i = 0; i < index; ++i) {
638 task = mTaskHistory.get(i);
639 if (task.userId == userId) {
640 haveActivities = true;
641 mTaskHistory.remove(i);
642 mTaskHistory.add(task);
643 --index;
644 }
645 }
646
647 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
648 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800649 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700650 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700651 }
652
Craig Mautner2420ead2013-04-01 17:13:20 -0700653 void minimalResumeActivityLocked(ActivityRecord r) {
654 r.state = ActivityState.RESUMED;
655 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
656 + " (starting new instance)");
657 r.stopped = false;
658 mResumedActivity = r;
659 r.task.touchActiveTime();
660 mService.addRecentTaskLocked(r.task);
661 completeResumeLocked(r);
662 checkReadyForSleepLocked();
663 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700664 }
665
666 private final void startSpecificActivityLocked(ActivityRecord r,
667 boolean andResume, boolean checkConfig) {
668 // Is this activity's application already running?
669 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
670 r.info.applicationInfo.uid);
Dianne Hackborn07981492013-01-28 11:36:23 -0800671
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700672 if (r.launchTime == 0) {
673 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700674 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700675 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700676 }
677 } else if (mInitialStartTime == 0) {
678 mInitialStartTime = SystemClock.uptimeMillis();
679 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800680
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 if (app != null && app.thread != null) {
682 try {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700683 app.addPackage(r.info.packageName);
Craig Mautner2420ead2013-04-01 17:13:20 -0700684 mStackSupervisor.realStartActivityLocked(r, app, andResume, checkConfig);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700685 return;
686 } catch (RemoteException e) {
687 Slog.w(TAG, "Exception when starting activity "
688 + r.intent.getComponent().flattenToShortString(), e);
689 }
690
691 // If a dead object exception was thrown -- fall through to
692 // restart the application.
693 }
694
695 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800696 "activity", r.intent.getComponent(), false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700697 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800698
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800699 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700700 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700701 if (!mGoingToSleep.isHeld()) {
702 mGoingToSleep.acquire();
703 if (mLaunchingActivity.isHeld()) {
704 mLaunchingActivity.release();
705 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
706 }
707 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800708 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
709 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
710 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
711 checkReadyForSleepLocked();
712 }
713 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700714
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800715 void awakeFromSleepingLocked() {
716 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
717 mSleepTimeout = false;
718 if (mGoingToSleep.isHeld()) {
719 mGoingToSleep.release();
720 }
721 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800722 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
723 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
724 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
725 activities.get(activityNdx).setSleeping(false);
726 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727 }
728 mGoingToSleepActivities.clear();
729 }
730
731 void activitySleptLocked(ActivityRecord r) {
732 mGoingToSleepActivities.remove(r);
733 checkReadyForSleepLocked();
734 }
735
736 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700737 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800738 // Do not care.
739 return;
740 }
741
742 if (!mSleepTimeout) {
743 if (mResumedActivity != null) {
744 // Still have something resumed; can't sleep until it is paused.
745 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700746 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
747 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800748 return;
749 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800750 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800751 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800752 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800753 return;
754 }
755
756 if (mStoppingActivities.size() > 0) {
757 // Still need to tell some activities to stop; can't sleep yet.
758 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
759 + mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700760 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800761 return;
762 }
763
764 ensureActivitiesVisibleLocked(null, 0);
765
766 // Make sure any stopped but visible activities are now sleeping.
767 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800768 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
769 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
770 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
771 final ActivityRecord r = activities.get(activityNdx);
772 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
773 r.setSleeping(true);
774 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800775 }
776 }
777
778 if (mGoingToSleepActivities.size() > 0) {
779 // Still need to tell some activities to sleep; can't sleep yet.
780 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
781 + mGoingToSleepActivities.size() + " activities");
782 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700783 }
784 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800785
786 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
787
788 if (mGoingToSleep.isHeld()) {
789 mGoingToSleep.release();
790 }
791 if (mService.mShuttingDown) {
792 mService.notifyAll();
793 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700794 }
Craig Mautner59c00972012-07-30 12:10:24 -0700795
Dianne Hackbornd2835932010-12-13 16:28:46 -0800796 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800797 if (who.noDisplay) {
798 return null;
799 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800800
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800801 Resources res = mService.mContext.getResources();
802 int w = mThumbnailWidth;
803 int h = mThumbnailHeight;
804 if (w < 0) {
805 mThumbnailWidth = w =
806 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
807 mThumbnailHeight = h =
808 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
809 }
810
811 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800812 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
813 || mLastScreenshotBitmap.getWidth() != w
814 || mLastScreenshotBitmap.getHeight() != h) {
815 mLastScreenshotActivity = who;
816 mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications(
817 who.appToken, Display.DEFAULT_DISPLAY, w, h);
818 }
819 if (mLastScreenshotBitmap != null) {
820 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
821 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800822 }
823 return null;
824 }
825
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800827 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800828 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700829 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800830 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 ActivityRecord prev = mResumedActivity;
832 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700833 Slog.e(TAG, "Trying to pause when nothing is resumed",
834 new RuntimeException("here").fillInStackTrace());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 resumeTopActivityLocked(null);
836 return;
837 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700838 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
839 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800841 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842 mLastPausedActivity = prev;
843 prev.state = ActivityState.PAUSING;
844 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700845 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846
847 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800848
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 if (prev.app != null && prev.app.thread != null) {
850 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
851 try {
852 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700853 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800855 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
856 userLeaving, prev.configChangeFlags);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700857 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 mService.updateUsageStats(prev, false);
859 }
860 } catch (Exception e) {
861 // Ignore exception, if process died other code will cleanup.
862 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800863 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 mLastPausedActivity = null;
865 }
866 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800867 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 mLastPausedActivity = null;
869 }
870
871 // If we are not going to sleep, we want to ensure the device is
872 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700873 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700874 mLaunchingActivity.acquire();
875 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
876 // To be safe, don't allow the wake lock to be held for too long.
877 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
878 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
879 }
880 }
881
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800882 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 // Have the window manager pause its key dispatching until the new
884 // activity has started. If we're pausing the activity just because
885 // the screen is being turned off and the UI is sleeping, don't interrupt
886 // key dispatch; the same activity will pick it up again on wakeup.
887 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700889 } else {
890 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
891 }
892
893 // Schedule a pause timeout in case the app doesn't respond.
894 // We don't give it much time because this directly impacts the
895 // responsiveness seen by the user.
896 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
897 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700898 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700899 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
900 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
901 } else {
902 // This activity failed to schedule the
903 // pause, so just treat it as being paused now.
904 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800905 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906 }
907 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700908
Craig Mautnerd2328952013-03-05 12:46:26 -0800909 final void activityResumedLocked(IBinder token) {
910 final ActivityRecord r = ActivityRecord.forToken(token);
911 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
912 r.icicle = null;
913 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700914 }
915
Craig Mautnerd2328952013-03-05 12:46:26 -0800916 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800918 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919
Craig Mautnerd2328952013-03-05 12:46:26 -0800920 final ActivityRecord r = isInStackLocked(token);
921 if (r != null) {
922 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
923 if (mPausingActivity == r) {
924 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
925 + (timeout ? " (due to timeout)" : " (pause complete)"));
926 r.state = ActivityState.PAUSED;
927 completePauseLocked();
928 } else {
929 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
930 r.userId, System.identityHashCode(r), r.shortComponentName,
931 mPausingActivity != null
932 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 }
934 }
935 }
936
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700937 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
938 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700939 if (r.state != ActivityState.STOPPING) {
940 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
941 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
942 return;
943 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700944 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700945 if (icicle != null) {
946 // If icicle is null, this is happening due to a timeout, so we
947 // haven't really saved the state.
948 r.icicle = icicle;
949 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800950 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700951 r.updateThumbnail(thumbnail, description);
952 }
953 if (!r.stopped) {
954 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
955 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
956 r.stopped = true;
957 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700958 if (r.finishing) {
959 r.clearOptionsLocked();
960 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700961 if (r.configDestroy) {
962 destroyActivityLocked(r, true, false, "stop-config");
963 resumeTopActivityLocked(null);
964 } else {
965 // Now that this process has stopped, we may want to consider
966 // it to be the previous app to try to keep around in case
967 // the user wants to return to it.
968 ProcessRecord fgApp = null;
969 if (mResumedActivity != null) {
970 fgApp = mResumedActivity.app;
971 } else if (mPausingActivity != null) {
972 fgApp = mPausingActivity.app;
973 }
974 if (r.app != null && fgApp != null && r.app != fgApp
975 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
976 && r.app != mService.mHomeProcess) {
977 mService.mPreviousProcess = r.app;
978 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
979 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800980 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700981 }
982 }
983 }
984
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800985 private final void completePauseLocked() {
986 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700987 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800988
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 if (prev != null) {
990 if (prev.finishing) {
991 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700992 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 } else if (prev.app != null) {
994 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
995 if (prev.waitingVisible) {
996 prev.waitingVisible = false;
997 mWaitingVisibleActivities.remove(prev);
998 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
999 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001000 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 if (prev.configDestroy) {
1002 // The previous is being paused because the configuration
1003 // is changing, which means it is actually stopping...
1004 // To juggle the fact that we are also starting a new
1005 // instance right now, we need to first completely stop
1006 // the current instance before starting the new one.
1007 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
1008 destroyActivityLocked(prev, true, false, "pause-config");
1009 } else {
1010 mStoppingActivities.add(prev);
1011 if (mStoppingActivities.size() > 3) {
1012 // If we already have a few activities waiting to stop,
1013 // then give up on things going idle and start clearing
1014 // them out.
1015 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
1016 scheduleIdleLocked();
1017 } else {
1018 checkReadyForSleepLocked();
1019 }
1020 }
1021 } else {
1022 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1023 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001024 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001025 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001027
Craig Mautnere11f2b72013-04-01 12:37:17 -07001028 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001029 resumeTopActivityLocked(prev);
1030 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001031 checkReadyForSleepLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001032 ActivityRecord top = topRunningActivityLocked(null);
1033 if (top == null || (prev != null && top != prev)) {
1034 // If there are no more activities available to run,
1035 // do resume anyway to start something. Also if the top
1036 // activity on the stack is not the just paused activity,
1037 // we need to go ahead and resume it to ensure we complete
1038 // an in-flight app switch.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001039 resumeTopActivityLocked(null);
1040 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001042
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001043 if (prev != null) {
1044 prev.resumeKeyDispatchingLocked();
1045 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001046
1047 if (prev.app != null && prev.cpuTimeAtResume > 0
1048 && mService.mBatteryStatsService.isOnBattery()) {
1049 long diff = 0;
1050 synchronized (mService.mProcessStatsThread) {
1051 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1052 - prev.cpuTimeAtResume;
1053 }
1054 if (diff > 0) {
1055 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1056 synchronized (bsi) {
1057 BatteryStatsImpl.Uid.Proc ps =
1058 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1059 prev.info.packageName);
1060 if (ps != null) {
1061 ps.addForegroundTimeLocked(diff);
1062 }
1063 }
1064 }
1065 }
1066 prev.cpuTimeAtResume = 0; // reset it
1067 }
1068
1069 /**
1070 * Once we know that we have asked an application to put an activity in
1071 * the resumed state (either by launching it or explicitly telling it),
1072 * this function updates the rest of our state to match that fact.
1073 */
1074 private final void completeResumeLocked(ActivityRecord next) {
1075 next.idle = false;
1076 next.results = null;
1077 next.newIntents = null;
1078
1079 // schedule an idle timeout in case the app doesn't do it for us.
1080 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1081 msg.obj = next;
1082 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1083
1084 if (false) {
1085 // The activity was never told to pause, so just keep
1086 // things going as-is. To maintain our own state,
1087 // we need to emulate it coming back and saying it is
1088 // idle.
1089 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1090 msg.obj = next;
1091 mHandler.sendMessage(msg);
1092 }
1093
Craig Mautner2219a1b2013-03-25 09:44:30 -07001094 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001095 mService.reportResumedActivityLocked(next);
1096 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07001097
Craig Mautner2219a1b2013-03-25 09:44:30 -07001098 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001099 mService.setFocusedActivityLocked(next);
1100 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001101 next.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001102 ensureActivitiesVisibleLocked(null, 0);
1103 mService.mWindowManager.executeAppTransition();
1104 mNoAnimActivities.clear();
1105
1106 // Mark the point when the activity is resuming
1107 // TODO: To be more accurate, the mark should be before the onCreate,
1108 // not after the onResume. But for subsequent starts, onResume is fine.
1109 if (next.app != null) {
1110 synchronized (mService.mProcessStatsThread) {
1111 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1112 }
1113 } else {
1114 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1115 }
1116 }
1117
1118 /**
1119 * Make sure that all activities that need to be visible (that is, they
1120 * currently can be seen by the user) actually are.
1121 */
1122 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1123 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1124 if (DEBUG_VISBILITY) Slog.v(
1125 TAG, "ensureActivitiesVisible behind " + top
1126 + " configChanges=0x" + Integer.toHexString(configChanges));
1127
1128 // If the top activity is not fullscreen, then we need to
1129 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001130 boolean aboveTop = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131 boolean behindFullscreen = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001132 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1133 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1134 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1135 final ActivityRecord r = activities.get(activityNdx);
1136 if (r.finishing) {
1137 continue;
1138 }
1139 if (aboveTop && r != top) {
1140 continue;
1141 }
1142 aboveTop = false;
1143 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001144 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001145 TAG, "Make visible? " + r + " finishing=" + r.finishing
1146 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001147
Craig Mautnerd44711d2013-02-23 11:24:36 -08001148 final boolean doThisProcess = onlyThisProcess == null
1149 || onlyThisProcess.equals(r.processName);
1150
1151 // First: if this is not the current activity being started, make
1152 // sure it matches the current configuration.
1153 if (r != starting && doThisProcess) {
1154 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001155 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001156
1157 if (r.app == null || r.app.thread == null) {
1158 if (onlyThisProcess == null
1159 || onlyThisProcess.equals(r.processName)) {
1160 // This activity needs to be visible, but isn't even
1161 // running... get it started, but don't resume it
1162 // at this point.
1163 if (DEBUG_VISBILITY) Slog.v(
1164 TAG, "Start and freeze screen for " + r);
1165 if (r != starting) {
1166 r.startFreezingScreenLocked(r.app, configChanges);
1167 }
1168 if (!r.visible) {
1169 if (DEBUG_VISBILITY) Slog.v(
1170 TAG, "Starting and making visible: " + r);
1171 mService.mWindowManager.setAppVisibility(r.appToken, true);
1172 }
1173 if (r != starting) {
1174 startSpecificActivityLocked(r, false, false);
1175 }
1176 }
1177
1178 } else if (r.visible) {
1179 // If this activity is already visible, then there is nothing
1180 // else to do here.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001181 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001182 TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001184
1185 } else if (onlyThisProcess == null) {
1186 // This activity is not currently visible, but is running.
1187 // Tell it to become visible.
1188 r.visible = true;
1189 if (r.state != ActivityState.RESUMED && r != starting) {
1190 // If this activity is paused, tell it
1191 // to now show its window.
1192 if (DEBUG_VISBILITY) Slog.v(
1193 TAG, "Making visible and scheduling visibility: " + r);
1194 try {
1195 mService.mWindowManager.setAppVisibility(r.appToken, true);
1196 r.sleeping = false;
1197 r.app.pendingUiClean = true;
1198 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1199 r.stopFreezingScreenLocked(false);
1200 } catch (Exception e) {
1201 // Just skip on any failure; we'll make it
1202 // visible when it next restarts.
1203 Slog.w(TAG, "Exception thrown making visibile: "
1204 + r.intent.getComponent(), e);
1205 }
1206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208
Craig Mautnerd44711d2013-02-23 11:24:36 -08001209 // Aggregate current change flags.
1210 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211
Craig Mautnerd44711d2013-02-23 11:24:36 -08001212 if (r.fullscreen) {
1213 // At this point, nothing else needs to be shown
1214 if (DEBUG_VISBILITY) Slog.v(
1215 TAG, "Stopping: fullscreen at " + r);
1216 behindFullscreen = true;
1217 }
1218 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219 if (r.visible) {
1220 if (DEBUG_VISBILITY) Slog.v(
1221 TAG, "Making invisible: " + r);
1222 r.visible = false;
1223 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001224 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001225 if ((r.state == ActivityState.STOPPING
1226 || r.state == ActivityState.STOPPED)
1227 && r.app != null && r.app.thread != null) {
1228 if (DEBUG_VISBILITY) Slog.v(
1229 TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001230 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 }
1232 } catch (Exception e) {
1233 // Just skip on any failure; we'll make it
1234 // visible when it next restarts.
1235 Slog.w(TAG, "Exception thrown making hidden: "
1236 + r.intent.getComponent(), e);
1237 }
1238 } else {
1239 if (DEBUG_VISBILITY) Slog.v(
1240 TAG, "Already invisible: " + r);
1241 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001242 }
1243 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001244 }
1245 }
1246
1247 /**
1248 * Version of ensureActivitiesVisible that can easily be called anywhere.
1249 */
1250 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1251 int configChanges) {
1252 ActivityRecord r = topRunningActivityLocked(null);
1253 if (r != null) {
1254 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1255 }
1256 }
Craig Mautner58547802013-03-05 08:23:53 -08001257
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258 /**
1259 * Ensure that the top activity in the stack is resumed.
1260 *
1261 * @param prev The previously resumed activity, for when in the process
1262 * of pausing; can be null to call from elsewhere.
1263 *
1264 * @return Returns true if something is being resumed, or false if
1265 * nothing happened.
1266 */
1267 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001268 return resumeTopActivityLocked(prev, null);
1269 }
1270
1271 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272 // Find the first activity that is not finishing.
1273 ActivityRecord next = topRunningActivityLocked(null);
1274
1275 // Remember how we'll process this pause/resume situation, and ensure
1276 // that the state is reset however we wind up proceeding.
1277 final boolean userLeaving = mUserLeaving;
1278 mUserLeaving = false;
1279
1280 if (next == null) {
1281 // There are no more activities! Let's just start up the
1282 // Launcher...
Craig Mautner2219a1b2013-03-25 09:44:30 -07001283 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001284 ActivityOptions.abort(options);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001285 return mService.startHomeActivityLocked(mCurrentUser);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 }
1287 }
1288
1289 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001290
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291 // If the top activity is the resumed one, nothing to do.
1292 if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
1293 // Make sure we have executed any pending transitions, since there
1294 // should be nothing left to do at this point.
1295 mService.mWindowManager.executeAppTransition();
1296 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001297 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001298 return false;
1299 }
1300
1301 // If we are sleeping, and there is no resumed activity, and the top
1302 // activity is paused, well that is the state we want.
1303 if ((mService.mSleeping || mService.mShuttingDown)
p13451dbad2872012-04-18 11:39:23 +09001304 && mLastPausedActivity == next
1305 && (next.state == ActivityState.PAUSED
1306 || next.state == ActivityState.STOPPED
1307 || next.state == ActivityState.STOPPING)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001308 // Make sure we have executed any pending transitions, since there
1309 // should be nothing left to do at this point.
1310 mService.mWindowManager.executeAppTransition();
1311 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001312 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 return false;
1314 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001315
1316 // Make sure that the user who owns this activity is started. If not,
1317 // we will just leave it as is because someone should be bringing
1318 // another user's activities to the top of the stack.
1319 if (mService.mStartedUsers.get(next.userId) == null) {
1320 Slog.w(TAG, "Skipping resume of top activity " + next
1321 + ": user " + next.userId + " is stopped");
1322 return false;
1323 }
1324
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001325 // The activity may be waiting for stop, but that is no longer
1326 // appropriate for it.
1327 mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001328 mGoingToSleepActivities.remove(next);
1329 next.sleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001330 mWaitingVisibleActivities.remove(next);
1331
Dianne Hackborn84375872012-06-01 19:03:50 -07001332 next.updateOptionsLocked(options);
1333
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001334 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1335
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001336 // If we are currently pausing an activity, then don't do anything
1337 // until that is done.
1338 if (mPausingActivity != null) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001339 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG,
1340 "Skip resume: pausing=" + mPausingActivity);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001341 return false;
1342 }
1343
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001344 // Okay we are now going to start a switch, to 'next'. We may first
1345 // have to pause the current activity, but this is an important point
1346 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001347 // XXX "App Redirected" dialog is getting too many false positives
1348 // at this point, so turn off for now.
1349 if (false) {
1350 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1351 long now = SystemClock.uptimeMillis();
1352 final boolean inTime = mLastStartedActivity.startTime != 0
1353 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1354 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1355 final int nextUid = next.info.applicationInfo.uid;
1356 if (inTime && lastUid != nextUid
1357 && lastUid != next.launchedFromUid
1358 && mService.checkPermission(
1359 android.Manifest.permission.STOP_APP_SWITCHES,
1360 -1, next.launchedFromUid)
1361 != PackageManager.PERMISSION_GRANTED) {
1362 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1363 } else {
1364 next.startTime = now;
1365 mLastStartedActivity = next;
1366 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001367 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001368 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001369 mLastStartedActivity = next;
1370 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001371 }
Craig Mautner58547802013-03-05 08:23:53 -08001372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001373 // We need to start pausing the current activity so the top one
1374 // can be resumed...
1375 if (mResumedActivity != null) {
1376 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001377 // At this point we want to put the upcoming activity's process
1378 // at the top of the LRU list, since we know we will be needing it
1379 // very soon and it would be a waste to let it get killed if it
1380 // happens to be sitting towards the end.
1381 if (next.app != null && next.app.thread != null) {
1382 // No reason to do full oom adj update here; we'll let that
1383 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001384 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001385 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386 startPausingLocked(userLeaving, false);
1387 return true;
1388 }
1389
Christopher Tated3f175c2012-06-14 14:16:54 -07001390 // If the most recent activity was noHistory but was only stopped rather
1391 // than stopped+finished because the device went to sleep, we need to make
1392 // sure to finish it as we're making a new activity topmost.
1393 final ActivityRecord last = mLastPausedActivity;
1394 if (mService.mSleeping && last != null && !last.finishing) {
1395 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1396 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1397 if (DEBUG_STATES) {
1398 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1399 }
1400 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001401 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001402 }
1403 }
1404
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001405 if (prev != null && prev != next) {
1406 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1407 prev.waitingVisible = true;
1408 mWaitingVisibleActivities.add(prev);
1409 if (DEBUG_SWITCH) Slog.v(
1410 TAG, "Resuming top, waiting visible to hide: " + prev);
1411 } else {
1412 // The next activity is already visible, so hide the previous
1413 // activity's windows right now so we can show the new one ASAP.
1414 // We only do this if the previous is finishing, which should mean
1415 // it is on top of the one being resumed so hiding it quickly
1416 // is good. Otherwise, we want to do the normal route of allowing
1417 // the resumed activity to be shown so we can decide if the
1418 // previous should actually be hidden depending on whether the
1419 // new one is found to be full-screen or not.
1420 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001421 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1423 + prev + ", waitingVisible="
1424 + (prev != null ? prev.waitingVisible : null)
1425 + ", nowVisible=" + next.nowVisible);
1426 } else {
1427 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1428 + prev + ", waitingVisible="
1429 + (prev != null ? prev.waitingVisible : null)
1430 + ", nowVisible=" + next.nowVisible);
1431 }
1432 }
1433 }
1434
Dianne Hackborne7f97212011-02-24 14:40:20 -08001435 // Launching this app's activity, make sure the app is no longer
1436 // considered stopped.
1437 try {
1438 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001439 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001440 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001441 } catch (IllegalArgumentException e) {
1442 Slog.w(TAG, "Failed trying to unstop package "
1443 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001444 }
1445
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 // We are starting up the next activity, so tell the window manager
1447 // that the previous one will be hidden soon. This way it can know
1448 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001449 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001450 if (prev != null) {
1451 if (prev.finishing) {
1452 if (DEBUG_TRANSITION) Slog.v(TAG,
1453 "Prepare close transition: prev=" + prev);
1454 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001455 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001456 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001457 } else {
1458 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001459 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1460 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001462 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1463 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 } else {
1465 if (DEBUG_TRANSITION) Slog.v(TAG,
1466 "Prepare open transition: prev=" + prev);
1467 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001468 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001469 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001470 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 } else {
1472 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001473 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1474 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001475 }
1476 }
1477 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001478 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1479 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001480 }
Craig Mautner000f0022013-02-26 15:04:29 -08001481 } else if (numActivities() > 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 if (DEBUG_TRANSITION) Slog.v(TAG,
1483 "Prepare open transition: no previous");
1484 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001485 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001486 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001487 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001489 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001490 AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001491 }
1492 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001493 if (!noAnim) {
1494 next.applyOptionsLocked();
1495 } else {
1496 next.clearOptionsLocked();
1497 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001498
1499 if (next.app != null && next.app.thread != null) {
1500 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1501
1502 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001503 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001505 // schedule launch ticks to collect information about slow apps.
1506 next.startLaunchTickingLocked();
1507
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 ActivityRecord lastResumedActivity = mResumedActivity;
1509 ActivityState lastState = next.state;
1510
1511 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001512
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001513 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514 next.state = ActivityState.RESUMED;
1515 mResumedActivity = next;
1516 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001517 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001518 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 updateLRUListLocked(next);
1520
1521 // Have the window manager re-evaluate the orientation of
1522 // the screen based on the new activity order.
1523 boolean updated = false;
Craig Mautner2219a1b2013-03-25 09:44:30 -07001524 if (mStackSupervisor.isMainStack(this)) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001525 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1526 mService.mConfiguration,
1527 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1528 if (config != null) {
1529 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001531 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001532 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001533
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534 if (!updated) {
1535 // The configuration update wasn't able to keep the existing
1536 // instance of the activity, and instead started a new one.
1537 // We should be all done, but let's just make sure our activity
1538 // is still at the top and schedule another run if something
1539 // weird happened.
1540 ActivityRecord nextNext = topRunningActivityLocked(null);
1541 if (DEBUG_SWITCH) Slog.i(TAG,
1542 "Activity config changed during resume: " + next
1543 + ", new next: " + nextNext);
1544 if (nextNext != next) {
1545 // Do over!
1546 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1547 }
Craig Mautner2219a1b2013-03-25 09:44:30 -07001548 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 mService.setFocusedActivityLocked(next);
1550 }
1551 ensureActivitiesVisibleLocked(null, 0);
1552 mService.mWindowManager.executeAppTransition();
1553 mNoAnimActivities.clear();
1554 return true;
1555 }
Craig Mautner58547802013-03-05 08:23:53 -08001556
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001557 try {
1558 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001559 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001560 if (a != null) {
1561 final int N = a.size();
1562 if (!next.finishing && N > 0) {
1563 if (DEBUG_RESULTS) Slog.v(
1564 TAG, "Delivering results to " + next
1565 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001566 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 }
1568 }
1569
1570 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001571 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 }
1573
1574 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001575 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001577
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001578 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001579 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001580 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001581 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001583
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001584 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585
1586 } catch (Exception e) {
1587 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001588 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1589 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001590 next.state = lastState;
1591 mResumedActivity = lastResumedActivity;
1592 Slog.i(TAG, "Restarting because process died: " + next);
1593 if (!next.hasBeenLaunched) {
1594 next.hasBeenLaunched = true;
1595 } else {
Craig Mautner2219a1b2013-03-25 09:44:30 -07001596 if (SHOW_APP_STARTING_PREVIEW && mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001598 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001599 mService.compatibilityInfoForPackageLocked(
1600 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001602 next.labelRes, next.icon, next.windowFlags,
1603 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001604 }
1605 }
1606 startSpecificActivityLocked(next, true, false);
1607 return true;
1608 }
1609
1610 // From this point on, if something goes wrong there is no way
1611 // to recover the activity.
1612 try {
1613 next.visible = true;
1614 completeResumeLocked(next);
1615 } catch (Exception e) {
1616 // If any exception gets thrown, toss away this
1617 // activity and try the next one.
1618 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001619 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001620 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 return true;
1622 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 next.stopped = false;
1624
1625 } else {
1626 // Whoops, need to restart this activity!
1627 if (!next.hasBeenLaunched) {
1628 next.hasBeenLaunched = true;
1629 } else {
1630 if (SHOW_APP_STARTING_PREVIEW) {
1631 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001632 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001633 mService.compatibilityInfoForPackageLocked(
1634 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001636 next.labelRes, next.icon, next.windowFlags,
1637 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 }
1639 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1640 }
1641 startSpecificActivityLocked(next, true, true);
1642 }
1643
1644 return true;
1645 }
1646
Craig Mautner11bf9a52013-02-19 14:08:51 -08001647
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001648 private final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001649 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001650 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001651 TaskRecord rTask = r.task;
1652 final int taskId = rTask.taskId;
1653 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001654 // Last activity in task had been removed or ActivityManagerService is reusing task.
1655 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001656 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001657 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001658 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001659 mTaskHistory.add(rTask);
1660 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001661 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001662 if (!newTask) {
1663 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001664 boolean startIt = true;
1665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1666 task = mTaskHistory.get(taskNdx);
1667 if (task == r.task) {
1668 // Here it is! Now, if this is not yet visible to the
1669 // user, then just add it without starting; it will
1670 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001671 if (!startIt) {
1672 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1673 + task, new RuntimeException("here").fillInStackTrace());
1674 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001675 r.putInHistory();
Craig Mautner70a86932013-02-28 22:37:44 -08001676 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001677 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation,
1678 r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001679 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001681 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001682 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001683 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 return;
1685 }
1686 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001687 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 startIt = false;
1689 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 }
1691 }
1692
1693 // Place a new activity at top of stack, so it is next to interact
1694 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001695
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 // If we are not placing the new activity frontmost, we do not want
1697 // to deliver the onUserLeaving callback to the actual frontmost
1698 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001699 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001701 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1702 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 }
Craig Mautner70a86932013-02-28 22:37:44 -08001704
1705 task = r.task;
1706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001708 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001709 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001710 task.addActivityToTop(r);
1711
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001712 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 r.frontOfTask = newTask;
Craig Mautner70a86932013-02-28 22:37:44 -08001714 if (numActivities() > 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 // We want to show the starting preview window if we are
1716 // switching to a new task, or the next activity's process is
1717 // not currently running.
1718 boolean showStartingIcon = newTask;
1719 ProcessRecord proc = r.app;
1720 if (proc == null) {
1721 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1722 }
1723 if (proc == null || proc.thread == null) {
1724 showStartingIcon = true;
1725 }
1726 if (DEBUG_TRANSITION) Slog.v(TAG,
1727 "Prepare open transition: starting " + r);
1728 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001729 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001730 AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001731 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 } else {
1733 mService.mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001734 ? AppTransition.TRANSIT_TASK_OPEN
1735 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 mNoAnimActivities.remove(r);
1737 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001738 r.updateOptionsLocked(options);
Craig Mautner70a86932013-02-28 22:37:44 -08001739 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001740 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001741 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 boolean doShow = true;
1743 if (newTask) {
1744 // Even though this activity is starting fresh, we still need
1745 // to reset it to make sure we apply affinities to move any
1746 // existing activities from other tasks in to it.
1747 // If the caller has requested that the target task be
1748 // reset, then do so.
1749 if ((r.intent.getFlags()
1750 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1751 resetTaskIfNeededLocked(r, r);
1752 doShow = topRunningNonDelayedActivityLocked(null) == r;
1753 }
1754 }
1755 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1756 // Figure out if we are transitioning from another activity that is
1757 // "has the same starting icon" as the next one. This allows the
1758 // window manager to keep the previous window it had previously
1759 // created, if it still had one.
1760 ActivityRecord prev = mResumedActivity;
1761 if (prev != null) {
1762 // We don't want to reuse the previous starting preview if:
1763 // (1) The current activity is in a different task.
1764 if (prev.task != r.task) prev = null;
1765 // (2) The current activity is already displayed.
1766 else if (prev.nowVisible) prev = null;
1767 }
1768 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001769 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001770 mService.compatibilityInfoForPackageLocked(
1771 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001772 r.labelRes, r.icon, r.windowFlags,
1773 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 }
1775 } else {
1776 // If this is the first activity, don't do any fancy animations,
1777 // because there is nothing for it to animate on top of.
Craig Mautner70a86932013-02-28 22:37:44 -08001778 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001779 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001780 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001781 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 }
1783 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001784 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 }
1786
1787 if (doResume) {
1788 resumeTopActivityLocked(null);
1789 }
1790 }
1791
Dianne Hackbornbe707852011-11-11 14:32:10 -08001792 final void validateAppTokensLocked() {
1793 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001794 mValidateAppTokens.ensureCapacity(numActivities());
1795 final int numTasks = mTaskHistory.size();
1796 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1797 TaskRecord task = mTaskHistory.get(taskNdx);
1798 final ArrayList<ActivityRecord> activities = task.mActivities;
1799 if (activities.size() == 0) {
1800 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001801 }
Craig Mautner000f0022013-02-26 15:04:29 -08001802 TaskGroup group = new TaskGroup();
1803 group.taskId = task.taskId;
1804 mValidateAppTokens.add(group);
1805 final int numActivities = activities.size();
1806 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1807 final ActivityRecord r = activities.get(activityNdx);
1808 group.tokens.add(r.appToken);
1809 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001810 }
Craig Mautner00af9fe2013-03-25 09:13:41 -07001811 mService.mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001812 }
1813
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 /**
1815 * Perform a reset of the given task, if needed as part of launching it.
1816 * Returns the new HistoryRecord at the top of the task.
1817 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001818 /**
1819 * Helper method for #resetTaskIfNeededLocked.
1820 * We are inside of the task being reset... we'll either finish this activity, push it out
1821 * for another task, or leave it as-is.
1822 * @param task The task containing the Activity (taskTop) that might be reset.
1823 * @param forceReset
1824 * @return An ActivityOptions that needs to be processed.
1825 */
1826 private final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task,
1827 boolean forceReset) {
1828 ActivityOptions topOptions = null;
1829
1830 int replyChainEnd = -1;
1831 boolean canMoveOptions = true;
1832
1833 // We only do this for activities that are not the root of the task (since if we finish
1834 // the root, we may no longer have the task!).
1835 final ArrayList<ActivityRecord> activities = task.mActivities;
1836 final int numActivities = activities.size();
1837 for (int i = numActivities - 1; i > 0; --i ) {
1838 ActivityRecord target = activities.get(i);
1839
1840 final int flags = target.info.flags;
1841 final boolean finishOnTaskLaunch =
1842 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1843 final boolean allowTaskReparenting =
1844 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1845 final boolean clearWhenTaskReset =
1846 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1847
1848 if (!finishOnTaskLaunch
1849 && !clearWhenTaskReset
1850 && target.resultTo != null) {
1851 // If this activity is sending a reply to a previous
1852 // activity, we can't do anything with it now until
1853 // we reach the start of the reply chain.
1854 // XXX note that we are assuming the result is always
1855 // to the previous activity, which is almost always
1856 // the case but we really shouldn't count on.
1857 if (replyChainEnd < 0) {
1858 replyChainEnd = i;
1859 }
1860 } else if (!finishOnTaskLaunch
1861 && !clearWhenTaskReset
1862 && allowTaskReparenting
1863 && target.taskAffinity != null
1864 && !target.taskAffinity.equals(task.affinity)) {
1865 // If this activity has an affinity for another
1866 // task, then we need to move it out of here. We will
1867 // move it as far out of the way as possible, to the
1868 // bottom of the activity stack. This also keeps it
1869 // correctly ordered with any activities we previously
1870 // moved.
1871 TaskRecord bottomTask = mTaskHistory.get(0);
1872 ActivityRecord p = bottomTask.mActivities.get(0);
1873 if (target.taskAffinity != null
1874 && target.taskAffinity.equals(p.task.affinity)) {
1875 // If the activity currently at the bottom has the
1876 // same task affinity as the one we are moving,
1877 // then merge it into the same task.
Craig Mautner0247fc82013-02-28 14:32:06 -08001878 setTask(target, p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001879 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1880 + " out to bottom task " + p.task);
1881 } else {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001882 setTask(target, createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
1883 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001884 target.task.affinityIntent = target.intent;
1885 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1886 + " out to new task " + target.task);
1887 }
1888
1889 final TaskRecord targetTask = target.task;
1890 final int targetTaskId = targetTask.taskId;
1891 mService.mWindowManager.setAppGroupId(target.appToken, targetTaskId);
1892
1893 ThumbnailHolder curThumbHolder = target.thumbHolder;
1894 boolean gotOptions = !canMoveOptions;
1895
1896 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1897 for (int srcPos = start; srcPos >= i; --srcPos) {
1898 p = activities.get(srcPos);
1899 if (p.finishing) {
1900 continue;
1901 }
1902
1903 curThumbHolder = p.thumbHolder;
1904 canMoveOptions = false;
1905 if (!gotOptions && topOptions == null) {
1906 topOptions = p.takeOptionsLocked();
1907 if (topOptions != null) {
1908 gotOptions = true;
1909 }
1910 }
1911 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1912 + task + " adding to task=" + targetTask,
1913 new RuntimeException("here").fillInStackTrace());
1914 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1915 + " out to target's task " + target.task);
Craig Mautner0247fc82013-02-28 14:32:06 -08001916 setTask(p, targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001917 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001918
Craig Mautnere3a74d52013-02-22 14:14:58 -08001919 mService.mWindowManager.setAppGroupId(p.appToken, targetTaskId);
1920 }
1921
1922 mService.mWindowManager.moveTaskToBottom(targetTaskId);
1923 if (VALIDATE_TOKENS) {
1924 validateAppTokensLocked();
1925 }
1926
1927 replyChainEnd = -1;
1928 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1929 // If the activity should just be removed -- either
1930 // because it asks for it, or the task should be
1931 // cleared -- then finish it and anything that is
1932 // part of its reply chain.
1933 int end;
1934 if (clearWhenTaskReset) {
1935 // In this case, we want to finish this activity
1936 // and everything above it, so be sneaky and pretend
1937 // like these are all in the reply chain.
1938 end = numActivities - 1;
1939 } else if (replyChainEnd < 0) {
1940 end = i;
1941 } else {
1942 end = replyChainEnd;
1943 }
1944 ActivityRecord p = null;
1945 boolean gotOptions = !canMoveOptions;
1946 for (int srcPos = i; srcPos <= end; srcPos++) {
1947 p = activities.get(srcPos);
1948 if (p.finishing) {
1949 continue;
1950 }
1951 canMoveOptions = false;
1952 if (!gotOptions && topOptions == null) {
1953 topOptions = p.takeOptionsLocked();
1954 if (topOptions != null) {
1955 gotOptions = true;
1956 }
1957 }
Craig Mautner58547802013-03-05 08:23:53 -08001958 if (DEBUG_TASKS) Slog.w(TAG,
1959 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001960 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001961 end--;
1962 srcPos--;
1963 }
1964 }
1965 replyChainEnd = -1;
1966 } else {
1967 // If we were in the middle of a chain, well the
1968 // activity that started it all doesn't want anything
1969 // special, so leave it all as-is.
1970 replyChainEnd = -1;
1971 }
1972 }
1973
1974 return topOptions;
1975 }
1976
1977 /**
1978 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1979 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1980 * @param affinityTask The task we are looking for an affinity to.
1981 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1982 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1983 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1984 */
Craig Mautner77878772013-03-04 19:46:24 -08001985 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1986 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001987 int replyChainEnd = -1;
1988 final int taskId = task.taskId;
1989 final String taskAffinity = task.affinity;
1990
1991 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1992 final int numActivities = activities.size();
1993 // Do not operate on the root Activity.
1994 for (int i = numActivities - 1; i > 0; --i) {
1995 ActivityRecord target = activities.get(i);
1996
1997 final int flags = target.info.flags;
1998 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1999 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2000
2001 if (target.resultTo != null) {
2002 // If this activity is sending a reply to a previous
2003 // activity, we can't do anything with it now until
2004 // we reach the start of the reply chain.
2005 // XXX note that we are assuming the result is always
2006 // to the previous activity, which is almost always
2007 // the case but we really shouldn't count on.
2008 if (replyChainEnd < 0) {
2009 replyChainEnd = i;
2010 }
2011 } else if (topTaskIsHigher
2012 && allowTaskReparenting
2013 && taskAffinity != null
2014 && taskAffinity.equals(target.taskAffinity)) {
2015 // This activity has an affinity for our task. Either remove it if we are
2016 // clearing or move it over to our task. Note that
2017 // we currently punt on the case where we are resetting a
2018 // task that is not at the top but who has activities above
2019 // with an affinity to it... this is really not a normal
2020 // case, and we will need to later pull that task to the front
2021 // and usually at that point we will do the reset and pick
2022 // up those remaining activities. (This only happens if
2023 // someone starts an activity in a new task from an activity
2024 // in a task that is not currently on top.)
2025 if (forceReset || finishOnTaskLaunch) {
2026 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2027 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2028 for (int srcPos = start; srcPos >= i; --srcPos) {
2029 final ActivityRecord p = activities.get(srcPos);
2030 if (p.finishing) {
2031 continue;
2032 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002033 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002034 }
2035 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002036 if (taskInsertionPoint < 0) {
2037 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002038
Craig Mautner77878772013-03-04 19:46:24 -08002039 }
Craig Mautner77878772013-03-04 19:46:24 -08002040
2041 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2042 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2043 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2044 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002045 final ActivityRecord p = activities.get(srcPos);
Craig Mautner0247fc82013-02-28 14:32:06 -08002046 setTask(p, task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002047 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002048
Craig Mautnere3a74d52013-02-22 14:14:58 -08002049 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2050 + " to stack at " + task,
2051 new RuntimeException("here").fillInStackTrace());
2052 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2053 + " in to resetting task " + task);
2054 mService.mWindowManager.setAppGroupId(p.appToken, taskId);
2055 }
2056 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002057 if (VALIDATE_TOKENS) {
2058 validateAppTokensLocked();
2059 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002060
2061 // Now we've moved it in to place... but what if this is
2062 // a singleTop activity and we have put it on top of another
2063 // instance of the same activity? Then we drop the instance
2064 // below so it remains singleTop.
2065 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2066 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2067 boolean found = false;
2068 int targetNdx = taskActivities.indexOf(target);
2069 if (targetNdx > 0) {
2070 ActivityRecord p = taskActivities.get(targetNdx - 1);
2071 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002072 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2073 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002074 }
2075 }
2076 }
2077 }
2078
2079 replyChainEnd = -1;
2080 }
2081 }
Craig Mautner77878772013-03-04 19:46:24 -08002082 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002083 }
2084
2085 private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
2086 ActivityRecord newActivity) {
2087 boolean forceReset =
2088 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2089 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2090 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2091 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2092 forceReset = true;
2093 }
2094 }
2095
2096 final TaskRecord task = taskTop.task;
2097
2098 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2099 * for remaining tasks. Used for later tasks to reparent to task. */
2100 boolean taskFound = false;
2101
2102 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2103 ActivityOptions topOptions = null;
2104
Craig Mautner77878772013-03-04 19:46:24 -08002105 // Preserve the location for reparenting in the new task.
2106 int reparentInsertionPoint = -1;
2107
Craig Mautnere3a74d52013-02-22 14:14:58 -08002108 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2109 final TaskRecord targetTask = mTaskHistory.get(i);
2110
2111 if (targetTask == task) {
2112 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2113 taskFound = true;
2114 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002115 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2116 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002117 }
2118 }
2119
Craig Mautner70a86932013-02-28 22:37:44 -08002120 int taskNdx = mTaskHistory.indexOf(task);
2121 do {
2122 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2123 } while (taskTop == null && taskNdx >= 0);
2124
Craig Mautnere3a74d52013-02-22 14:14:58 -08002125 if (topOptions != null) {
2126 // If we got some ActivityOptions from an activity on top that
2127 // was removed from the task, propagate them to the new real top.
2128 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002129 taskTop.updateOptionsLocked(topOptions);
2130 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002131 topOptions.abort();
2132 }
2133 }
2134
2135 return taskTop;
2136 }
2137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 * Find the activity in the history stack within the given task. Returns
2140 * the index within the history at which it's found, or < 0 if not found.
2141 */
Craig Mautner56f52db2013-02-25 10:03:01 -08002142 private final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
2143 final ComponentName realActivity = r.realActivity;
2144 ArrayList<ActivityRecord> activities = task.mActivities;
2145 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2146 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002147 if (candidate.finishing) {
2148 continue;
2149 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002150 if (candidate.realActivity.equals(realActivity)) {
2151 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 }
2153 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002154 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 }
2156
2157 /**
2158 * Reorder the history stack so that the activity at the given index is
2159 * brought to the front.
2160 */
Craig Mautner56f52db2013-02-25 10:03:01 -08002161 private final void moveActivityToFrontLocked(ActivityRecord newTop) {
2162 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop
2163 + " to stack at top", new RuntimeException("here").fillInStackTrace());
2164
2165 final TaskRecord task = newTop.task;
2166 task.getTopActivity().frontOfTask = false;
2167 task.mActivities.remove(newTop);
2168 task.mActivities.add(newTop);
2169 newTop.frontOfTask = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 }
2171
2172 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002173 Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 String resultWho, int requestCode,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08002175 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002176 boolean componentSpecified, ActivityRecord[] outActivity) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07002177 int err = ActivityManager.START_SUCCESS;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002178
2179 ProcessRecord callerApp = null;
2180 if (caller != null) {
2181 callerApp = mService.getRecordForAppLocked(caller);
2182 if (callerApp != null) {
2183 callingPid = callerApp.pid;
2184 callingUid = callerApp.info.uid;
2185 } else {
2186 Slog.w(TAG, "Unable to find app for caller " + caller
2187 + " (pid=" + callingPid + ") when starting: "
2188 + intent.toString());
Dianne Hackborna4972e92012-03-14 10:38:05 -07002189 err = ActivityManager.START_PERMISSION_DENIED;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002190 }
2191 }
2192
Dianne Hackborna4972e92012-03-14 10:38:05 -07002193 if (err == ActivityManager.START_SUCCESS) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002194 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -07002195 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
2196 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid));
Dianne Hackbornefb58102010-10-14 16:47:34 -07002197 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002198
2199 ActivityRecord sourceRecord = null;
2200 ActivityRecord resultRecord = null;
2201 if (resultTo != null) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002202 sourceRecord = isInStackLocked(resultTo);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002203 if (DEBUG_RESULTS) Slog.v(
Craig Mautner56f52db2013-02-25 10:03:01 -08002204 TAG, "Will send result to " + resultTo + " " + sourceRecord);
2205 if (sourceRecord != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002206 if (requestCode >= 0 && !sourceRecord.finishing) {
2207 resultRecord = sourceRecord;
2208 }
2209 }
2210 }
2211
2212 int launchFlags = intent.getFlags();
2213
2214 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
2215 && sourceRecord != null) {
2216 // Transfer the result target from the source activity to the new
2217 // one being started, including any failures.
2218 if (requestCode >= 0) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002219 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002220 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 }
2222 resultRecord = sourceRecord.resultTo;
2223 resultWho = sourceRecord.resultWho;
2224 requestCode = sourceRecord.requestCode;
2225 sourceRecord.resultTo = null;
2226 if (resultRecord != null) {
2227 resultRecord.removeResultsLocked(
2228 sourceRecord, resultWho, requestCode);
2229 }
2230 }
2231
Dianne Hackborna4972e92012-03-14 10:38:05 -07002232 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002233 // We couldn't find a class that can handle the given Intent.
2234 // That's the end of that!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002235 err = ActivityManager.START_INTENT_NOT_RESOLVED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002236 }
2237
Dianne Hackborna4972e92012-03-14 10:38:05 -07002238 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 // We couldn't find the specific class specified in the Intent.
2240 // Also the end of the line.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002241 err = ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002242 }
2243
Dianne Hackborna4972e92012-03-14 10:38:05 -07002244 if (err != ActivityManager.START_SUCCESS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002245 if (resultRecord != null) {
2246 sendActivityResultLocked(-1,
2247 resultRecord, resultWho, requestCode,
2248 Activity.RESULT_CANCELED, null);
2249 }
Craig Mautner27084302013-03-25 08:05:25 -07002250 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002251 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002252 return err;
2253 }
2254
Jeff Sharkey35be7562012-04-18 19:16:15 -07002255 final int startAnyPerm = mService.checkPermission(
2256 START_ANY_ACTIVITY, callingPid, callingUid);
2257 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002258 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
Jeff Sharkey35be7562012-04-18 19:16:15 -07002259 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 if (resultRecord != null) {
2261 sendActivityResultLocked(-1,
2262 resultRecord, resultWho, requestCode,
2263 Activity.RESULT_CANCELED, null);
2264 }
Craig Mautner27084302013-03-25 08:05:25 -07002265 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002266 String msg;
2267 if (!aInfo.exported) {
2268 msg = "Permission Denial: starting " + intent.toString()
2269 + " from " + callerApp + " (pid=" + callingPid
2270 + ", uid=" + callingUid + ")"
2271 + " not exported from uid " + aInfo.applicationInfo.uid;
2272 } else {
2273 msg = "Permission Denial: starting " + intent.toString()
2274 + " from " + callerApp + " (pid=" + callingPid
2275 + ", uid=" + callingUid + ")"
2276 + " requires " + aInfo.permission;
2277 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 Slog.w(TAG, msg);
2279 throw new SecurityException(msg);
2280 }
2281
Craig Mautner2219a1b2013-03-25 09:44:30 -07002282 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283 if (mService.mController != null) {
2284 boolean abort = false;
2285 try {
2286 // The Intent we give to the watcher has the extra data
2287 // stripped off, since it can contain private information.
2288 Intent watchIntent = intent.cloneFilter();
2289 abort = !mService.mController.activityStarting(watchIntent,
2290 aInfo.applicationInfo.packageName);
2291 } catch (RemoteException e) {
2292 mService.mController = null;
2293 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002294
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 if (abort) {
2296 if (resultRecord != null) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002297 sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002298 Activity.RESULT_CANCELED, null);
2299 }
2300 // We pretend to the caller that it was really started, but
2301 // they will just get a cancel result.
Craig Mautner27084302013-03-25 08:05:25 -07002302 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002303 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002304 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002305 }
2306 }
2307 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002308
Craig Mautnerd2328952013-03-05 12:46:26 -08002309 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002310 intent, resolvedType, aInfo, mService.mConfiguration,
2311 resultRecord, resultWho, requestCode, componentSpecified);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002312 if (outActivity != null) {
2313 outActivity[0] = r;
2314 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002315
Craig Mautner2219a1b2013-03-25 09:44:30 -07002316 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 if (mResumedActivity == null
2318 || mResumedActivity.info.applicationInfo.uid != callingUid) {
2319 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
Craig Mautner64f2b172013-03-11 11:40:31 -07002320 PendingActivityLaunch pal =
2321 new PendingActivityLaunch(r, sourceRecord, startFlags, this);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 mService.mPendingActivityLaunches.add(pal);
Craig Mautner27084302013-03-25 08:05:25 -07002323 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002324 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002325 return ActivityManager.START_SWITCHES_CANCELED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 }
2327 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002328
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 if (mService.mDidAppSwitch) {
2330 // This is the second allowed switch since we stopped switches,
2331 // so now just generally allow switches. Use case: user presses
2332 // home (switches disabled, switch to home, mDidAppSwitch now true);
2333 // user taps a home icon (coming from home so allowed, we hit here
2334 // and now allow anyone to switch again).
2335 mService.mAppSwitchesAllowedTime = 0;
2336 } else {
2337 mService.mDidAppSwitch = true;
2338 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002340
Craig Mautner8d341ef2013-03-26 09:03:27 -07002341 mService.doPendingActivityLaunchesLocked(false);
2342
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002343 err = startActivityUncheckedLocked(r, sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002344 startFlags, true, options);
Craig Mautner27084302013-03-25 08:05:25 -07002345 if (mPausingActivity == null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002346 // Someone asked to have the keyguard dismissed on the next
2347 // activity start, but we are not actually doing an activity
2348 // switch... just dismiss the keyguard now, because we
2349 // probably want to see whatever is behind it.
Craig Mautner27084302013-03-25 08:05:25 -07002350 mStackSupervisor.dismissKeyguard();
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002351 }
2352 return err;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002353 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002354
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002355 final void moveHomeToFrontFromLaunchLocked(int launchFlags) {
2356 if ((launchFlags &
2357 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2358 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2359 // Caller wants to appear on home activity, so before starting
2360 // their own activity we will bring home to the front.
2361 moveHomeToFrontLocked();
2362 }
2363 }
2364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002366 ActivityRecord sourceRecord, int startFlags, boolean doResume,
2367 Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 final Intent intent = r.intent;
2369 final int callingUid = r.launchedFromUid;
Amith Yamasani742a6712011-05-04 14:49:28 -07002370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 int launchFlags = intent.getFlags();
Craig Mautnerd2328952013-03-05 12:46:26 -08002372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002373 // We'll invoke onUserLeaving before onPause only if the launching
2374 // activity did not explicitly state that this is an automated launch.
2375 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2376 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2377 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautnerd2328952013-03-05 12:46:26 -08002378
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 // If the caller has asked not to resume at this point, we make note
2380 // of this in the record so that we can skip it when trying to find
2381 // the top running activity.
2382 if (!doResume) {
2383 r.delayedResume = true;
2384 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
2387 != 0 ? r : null;
2388
2389 // If the onlyIfNeeded flag is set, then we can do this if the activity
2390 // being launched is the same as the one making the call... or, as
2391 // a special case, if we do not know the caller then we count the
2392 // current top activity as the caller.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002393 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 ActivityRecord checkedCaller = sourceRecord;
2395 if (checkedCaller == null) {
2396 checkedCaller = topRunningNonDelayedActivityLocked(notTop);
2397 }
2398 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2399 // Caller is not the same as launcher, so always needed.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002400 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 }
2402 }
2403
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 if (sourceRecord == null) {
2405 // This activity is not being started from another... in this
2406 // case we -always- start a new task.
2407 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2408 Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: "
2409 + intent);
2410 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2411 }
2412 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2413 // The original activity who is starting us is running as a single
2414 // instance... this new activity it is starting must go on its
2415 // own task.
2416 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2417 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
2418 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2419 // The activity being started is a single instance... it always
2420 // gets launched into its own task.
2421 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2422 }
2423
2424 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2425 // For whatever reason this activity is being launched into a new
2426 // task... yet the caller has requested a result back. Well, that
2427 // is pretty messed up, so instead immediately send back a cancel
2428 // and let the new task continue launched as normal without a
2429 // dependency on its originator.
2430 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2431 sendActivityResultLocked(-1,
2432 r.resultTo, r.resultWho, r.requestCode,
2433 Activity.RESULT_CANCELED, null);
2434 r.resultTo = null;
2435 }
2436
2437 boolean addingToTask = false;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002438 boolean movedHome = false;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002439 TaskRecord reuseTask = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002440 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2441 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2442 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2443 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2444 // If bring to front is requested, and no result is requested, and
2445 // we can find a task that was started with this same
2446 // component, then instead of launching bring that one to the front.
2447 if (r.resultTo == null) {
2448 // See if there is a task to bring to the front. If this is
2449 // a SINGLE_INSTANCE activity, there can be one and only one
2450 // instance of it in the history, and it is always in its own
2451 // unique task, so we do a special search.
2452 ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
2453 ? findTaskLocked(intent, r.info)
2454 : findActivityLocked(intent, r.info);
2455 if (taskTop != null) {
2456 if (taskTop.task.intent == null) {
2457 // This task was started because of movement of
2458 // the activity based on affinity... now that we
2459 // are actually launching it, we can assign the
2460 // base intent.
2461 taskTop.task.setIntent(intent, r.info);
2462 }
2463 // If the target task is not in the front, then we need
2464 // to bring it to the front... except... well, with
2465 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2466 // to have the same behavior as if a new instance was
2467 // being started, which means not bringing it to the front
2468 // if the caller is not itself in the front.
2469 ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
Jean-Baptiste Queru66a5d692010-10-25 17:27:16 -07002470 if (curTop != null && curTop.task != taskTop.task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002471 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2472 boolean callerAtFront = sourceRecord == null
2473 || curTop.task == sourceRecord.task;
2474 if (callerAtFront) {
2475 // We really do want to push this one into the
2476 // user's face, right now.
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002477 movedHome = true;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002478 moveHomeToFrontFromLaunchLocked(launchFlags);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002479 moveTaskToFrontLocked(taskTop.task, r, options);
Dianne Hackborn84375872012-06-01 19:03:50 -07002480 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 }
2482 }
2483 // If the caller has requested that the target task be
2484 // reset, then do so.
2485 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2486 taskTop = resetTaskIfNeededLocked(taskTop, r);
2487 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002488 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 // We don't need to start a new activity, and
2490 // the client said not to do anything if that
2491 // is the case, so this is it! And for paranoia, make
2492 // sure we have correctly resumed the top activity.
2493 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002494 resumeTopActivityLocked(null, options);
2495 } else {
2496 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002498 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002499 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002500 if ((launchFlags &
2501 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
2502 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
2503 // The caller has requested to completely replace any
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002504 // existing task with its new activity. Well that should
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002505 // not be too hard...
2506 reuseTask = taskTop.task;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07002507 taskTop.task.performClearTaskLocked();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002508 reuseTask.setIntent(r.intent, r.info);
2509 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002510 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2511 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2512 // In this situation we want to remove all activities
2513 // from the task up to the one being started. In most
2514 // cases this means we are resetting the task to its
2515 // initial state.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07002516 ActivityRecord top = taskTop.task.performClearTaskLocked(r, launchFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002517 if (top != null) {
2518 if (top.frontOfTask) {
2519 // Activity aliases may mean we use different
2520 // intents for the top activity, so make sure
2521 // the task now has the identity of the new
2522 // intent.
2523 top.task.setIntent(r.intent, r.info);
2524 }
2525 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002526 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002527 } else {
2528 // A special case: we need to
2529 // start the activity because it is not currently
2530 // running, and the caller has asked to clear the
2531 // current task to have this activity at the top.
2532 addingToTask = true;
2533 // Now pretend like this activity is being started
2534 // by the top of its task, so it is put in the
2535 // right place.
2536 sourceRecord = taskTop;
2537 }
2538 } else if (r.realActivity.equals(taskTop.task.realActivity)) {
2539 // In this case the top activity on the task is the
2540 // same as the one being launched, so we take that
2541 // as a request to bring the task to the foreground.
2542 // If the top activity in the task is the root
2543 // activity, deliver this new intent to it if it
2544 // desires.
Johan Viktorssonf363dfd2012-02-16 17:05:16 +01002545 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2546 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 && taskTop.realActivity.equals(r.realActivity)) {
2548 logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task);
2549 if (taskTop.frontOfTask) {
2550 taskTop.task.setIntent(r.intent, r.info);
2551 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002552 taskTop.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 } else if (!r.intent.filterEquals(taskTop.task.intent)) {
2554 // In this case we are launching the root activity
2555 // of the task, but with a different intent. We
2556 // should start a new instance on top.
2557 addingToTask = true;
2558 sourceRecord = taskTop;
2559 }
2560 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2561 // In this case an activity is being launched in to an
2562 // existing task, without resetting that task. This
2563 // is typically the situation of launching an activity
2564 // from a notification or shortcut. We want to place
2565 // the new activity on top of the current task.
2566 addingToTask = true;
2567 sourceRecord = taskTop;
2568 } else if (!taskTop.task.rootWasReset) {
2569 // In this case we are launching in to an existing task
2570 // that has not yet been started from its front door.
2571 // The current task has been brought to the front.
2572 // Ideally, we'd probably like to place this new task
2573 // at the bottom of its stack, but that's a little hard
2574 // to do with the current organization of the code so
2575 // for now we'll just drop it.
2576 taskTop.task.setIntent(r.intent, r.info);
2577 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002578 if (!addingToTask && reuseTask == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579 // We didn't do anything... but it was needed (a.k.a., client
2580 // don't use that intent!) And for paranoia, make
2581 // sure we have correctly resumed the top activity.
2582 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002583 resumeTopActivityLocked(null, options);
2584 } else {
2585 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002587 return ActivityManager.START_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002588 }
2589 }
2590 }
2591 }
2592
2593 //String uri = r.intent.toURI();
2594 //Intent intent2 = new Intent(uri);
2595 //Slog.i(TAG, "Given intent: " + r.intent);
2596 //Slog.i(TAG, "URI is: " + uri);
2597 //Slog.i(TAG, "To intent: " + intent2);
2598
2599 if (r.packageName != null) {
2600 // If the activity being launched is the same as the one currently
2601 // at the top, then we need to check if it should only be launched
2602 // once.
2603 ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
2604 if (top != null && r.resultTo == null) {
Amith Yamasani742a6712011-05-04 14:49:28 -07002605 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002606 if (top.app != null && top.app.thread != null) {
2607 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2608 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
2609 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2610 logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2611 // For paranoia, make sure we have correctly
2612 // resumed the top activity.
2613 if (doResume) {
2614 resumeTopActivityLocked(null);
2615 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002616 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002617 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002618 // We don't need to start a new activity, and
2619 // the client said not to do anything if that
2620 // is the case, so this is it!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002621 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002622 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002623 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002624 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 }
2626 }
2627 }
2628 }
2629
2630 } else {
2631 if (r.resultTo != null) {
2632 sendActivityResultLocked(-1,
2633 r.resultTo, r.resultWho, r.requestCode,
2634 Activity.RESULT_CANCELED, null);
2635 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002636 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002637 return ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002638 }
2639
2640 boolean newTask = false;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002641 boolean keepCurTransition = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002642
2643 // Should this be considered a new task?
2644 if (r.resultTo == null && !addingToTask
2645 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002646 if (reuseTask == null) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002647 setTask(r, createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent,
2648 true), null, true);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002649 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2650 + " in new task " + r.task);
2651 } else {
Craig Mautner0247fc82013-02-28 14:32:06 -08002652 setTask(r, reuseTask, reuseTask, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 newTask = true;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002655 if (!movedHome) {
2656 moveHomeToFrontFromLaunchLocked(launchFlags);
2657 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002658
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002659 } else if (sourceRecord != null) {
2660 if (!addingToTask &&
2661 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2662 // In this case, we are adding the activity to an existing
2663 // task, but the caller has asked to clear that task if the
2664 // activity is already running.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07002665 ActivityRecord top = sourceRecord.task.performClearTaskLocked(r, launchFlags);
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002666 keepCurTransition = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002667 if (top != null) {
2668 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002669 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002670 // For paranoia, make sure we have correctly
2671 // resumed the top activity.
2672 if (doResume) {
2673 resumeTopActivityLocked(null);
2674 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002675 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002676 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677 }
2678 } else if (!addingToTask &&
2679 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2680 // In this case, we are launching an activity in our own task
2681 // that may already be running somewhere in the history, and
2682 // we want to shuffle it to the front of the stack if so.
Craig Mautner56f52db2013-02-25 10:03:01 -08002683 final ActivityRecord top = findActivityInHistoryLocked(r, sourceRecord.task);
2684 if (top != null) {
2685 moveActivityToFrontLocked(top);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002686 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002687 top.updateOptionsLocked(options);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002688 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 if (doResume) {
2690 resumeTopActivityLocked(null);
2691 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002692 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002693 }
2694 }
2695 // An existing activity is starting this new activity, so we want
2696 // to keep the new one in the same task as the one that is starting
2697 // it.
Craig Mautner0247fc82013-02-28 14:32:06 -08002698 setTask(r, sourceRecord.task, sourceRecord.thumbHolder, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2700 + " in existing task " + r.task);
2701
2702 } else {
2703 // This not being started from an existing activity, and not part
2704 // of a new task... just put it in the top task, though these days
2705 // this case should never happen.
Craig Mautner9658b312013-02-28 10:55:59 -08002706 ActivityRecord prev = null;
2707 // Iterate to find the first non-empty task stack. Note that this code can
2708 // go away once we stop storing tasks with empty mActivities lists.
2709 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2710 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2711 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2712 prev = activities.get(activityNdx);
2713 break;
2714 }
2715 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002716 setTask(r, prev != null
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002717 ? prev.task
Craig Mautner8d341ef2013-03-26 09:03:27 -07002718 : createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent, true),
2719 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002720 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2721 + " in new guessed " + r.task);
2722 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002723
Dianne Hackborn39792d22010-08-19 18:01:52 -07002724 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002725 intent, r.getUriPermissionsLocked());
Dianne Hackborn39792d22010-08-19 18:01:52 -07002726
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002727 if (newTask) {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002728 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002729 }
2730 logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002731 startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002732 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002733 }
2734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
2736 long thisTime, long totalTime) {
2737 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
2738 WaitResult w = mWaitingActivityLaunched.get(i);
2739 w.timeout = timeout;
2740 if (r != null) {
2741 w.who = new ComponentName(r.info.packageName, r.info.name);
2742 }
2743 w.thisTime = thisTime;
2744 w.totalTime = totalTime;
2745 }
2746 mService.notifyAll();
2747 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002748
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 void reportActivityVisibleLocked(ActivityRecord r) {
2750 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
2751 WaitResult w = mWaitingActivityVisible.get(i);
2752 w.timeout = false;
2753 if (r != null) {
2754 w.who = new ComponentName(r.info.packageName, r.info.name);
2755 }
2756 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
2757 w.thisTime = w.totalTime;
2758 }
2759 mService.notifyAll();
Craig Mautner27084302013-03-25 08:05:25 -07002760 mStackSupervisor.dismissKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 }
2762
2763 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2764 String resultWho, int requestCode, int resultCode, Intent data) {
2765
2766 if (callingUid > 0) {
2767 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002768 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 }
2770
2771 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2772 + " : who=" + resultWho + " req=" + requestCode
2773 + " res=" + resultCode + " data=" + data);
2774 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2775 try {
2776 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2777 list.add(new ResultInfo(resultWho, requestCode,
2778 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002779 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002780 return;
2781 } catch (Exception e) {
2782 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2783 }
2784 }
2785
2786 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2787 }
2788
2789 private final void stopActivityLocked(ActivityRecord r) {
2790 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2791 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2792 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2793 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002794 if (!mService.mSleeping) {
2795 if (DEBUG_STATES) {
2796 Slog.d(TAG, "no-history finish of " + r);
2797 }
2798 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002799 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002800 } else {
2801 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2802 + " on stop because we're just sleeping");
2803 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002804 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002805 }
2806
2807 if (r.app != null && r.app.thread != null) {
Craig Mautner2219a1b2013-03-25 09:44:30 -07002808 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809 if (mService.mFocusedActivity == r) {
2810 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2811 }
2812 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002813 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 try {
2815 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002816 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2817 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002818 r.state = ActivityState.STOPPING;
2819 if (DEBUG_VISBILITY) Slog.v(
2820 TAG, "Stopping visible=" + r.visible + " for " + r);
2821 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002822 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002824 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002825 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002826 r.setSleeping(true);
2827 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002828 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2829 msg.obj = r;
2830 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 } catch (Exception e) {
2832 // Maybe just ignore exceptions here... if the process
2833 // has crashed, our death notification will clean things
2834 // up.
2835 Slog.w(TAG, "Exception thrown during pause", e);
2836 // Just in case, assume it to be stopped.
2837 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002838 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 r.state = ActivityState.STOPPED;
2840 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002841 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 }
2843 }
2844 }
2845 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002846
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(
2848 boolean remove) {
2849 int N = mStoppingActivities.size();
2850 if (N <= 0) return null;
2851
2852 ArrayList<ActivityRecord> stops = null;
2853
2854 final boolean nowVisible = mResumedActivity != null
2855 && mResumedActivity.nowVisible
2856 && !mResumedActivity.waitingVisible;
2857 for (int i=0; i<N; i++) {
2858 ActivityRecord s = mStoppingActivities.get(i);
2859 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
2860 + nowVisible + " waitingVisible=" + s.waitingVisible
2861 + " finishing=" + s.finishing);
2862 if (s.waitingVisible && nowVisible) {
2863 mWaitingVisibleActivities.remove(s);
2864 s.waitingVisible = false;
2865 if (s.finishing) {
2866 // If this activity is finishing, it is sitting on top of
2867 // everyone else but we now know it is no longer needed...
2868 // so get rid of it. Otherwise, we need to go through the
2869 // normal flow and hide it once we determine that it is
2870 // hidden by the activities in front of it.
2871 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002872 mService.mWindowManager.setAppVisibility(s.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 }
2874 }
Craig Mautnere11f2b72013-04-01 12:37:17 -07002875 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002876 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2877 if (stops == null) {
2878 stops = new ArrayList<ActivityRecord>();
2879 }
2880 stops.add(s);
2881 mStoppingActivities.remove(i);
2882 N--;
2883 i--;
2884 }
2885 }
2886
2887 return stops;
2888 }
2889
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002890 final void scheduleIdleLocked() {
2891 Message msg = Message.obtain();
2892 msg.what = IDLE_NOW_MSG;
2893 mHandler.sendMessage(msg);
2894 }
2895
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002896 final ActivityRecord activityIdleInternal(IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 Configuration config) {
2898 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2899
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002900 ActivityRecord res = null;
2901
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 ArrayList<ActivityRecord> stops = null;
2903 ArrayList<ActivityRecord> finishes = null;
2904 ArrayList<ActivityRecord> thumbnails = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002905 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002906 int NS = 0;
2907 int NF = 0;
2908 int NT = 0;
2909 IApplicationThread sendThumbnail = null;
2910 boolean booting = false;
2911 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002912 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913
2914 synchronized (mService) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002915 ActivityRecord r = ActivityRecord.forToken(token);
2916 if (r != null) {
2917 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002918 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 }
2920
2921 // Get the activity record.
Craig Mautnerd44711d2013-02-23 11:24:36 -08002922 if (isInStackLocked(token) != null) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002923 res = r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924
2925 if (fromTimeout) {
2926 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
2927 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002928
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 // This is a hack to semi-deal with a race condition
2930 // in the client where it can be constructed with a
2931 // newer configuration from when we asked it to launch.
2932 // We'll update with whatever configuration it now says
2933 // it used to launch.
2934 if (config != null) {
2935 r.configuration = config;
2936 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002937
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 // No longer need to keep the device awake.
2939 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
2940 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2941 mLaunchingActivity.release();
2942 }
2943
2944 // We are now idle. If someone is waiting for a thumbnail from
2945 // us, we can now deliver.
2946 r.idle = true;
2947 mService.scheduleAppGcsLocked();
2948 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
2949 sendThumbnail = r.app.thread;
2950 r.thumbnailNeeded = false;
2951 }
2952
2953 // If this activity is fullscreen, set up to hide those under it.
2954
2955 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
2956 ensureActivitiesVisibleLocked(null, 0);
2957
2958 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Craig Mautner2219a1b2013-03-25 09:44:30 -07002959 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07002960 if (!mService.mBooted) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002961 mService.mBooted = true;
2962 enableScreen = true;
2963 }
2964 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965 } else if (fromTimeout) {
2966 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
2967 }
2968
2969 // Atomically retrieve all of the other things to do.
2970 stops = processStoppingActivitiesLocked(true);
2971 NS = stops != null ? stops.size() : 0;
2972 if ((NF=mFinishingActivities.size()) > 0) {
2973 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2974 mFinishingActivities.clear();
2975 }
Craig Mautnercae015f2013-02-08 14:31:27 -08002976 if ((NT=mCancelledThumbnails.size()) > 0) {
2977 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
2978 mCancelledThumbnails.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 }
2980
Craig Mautner2219a1b2013-03-25 09:44:30 -07002981 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 booting = mService.mBooting;
2983 mService.mBooting = false;
2984 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002985 if (mStartingUsers.size() > 0) {
2986 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2987 mStartingUsers.clear();
2988 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 }
2990
2991 int i;
2992
2993 // Send thumbnail if requested.
2994 if (sendThumbnail != null) {
2995 try {
2996 sendThumbnail.requestThumbnail(token);
2997 } catch (Exception e) {
2998 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
2999 mService.sendPendingThumbnail(null, token, null, null, true);
3000 }
3001 }
3002
3003 // Stop any activities that are scheduled to do so but have been
3004 // waiting for the next one to start.
3005 for (i=0; i<NS; i++) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003006 ActivityRecord r = stops.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 synchronized (mService) {
3008 if (r.finishing) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003009 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003010 } else {
3011 stopActivityLocked(r);
3012 }
3013 }
3014 }
3015
3016 // Finish any activities that are scheduled to do so but have been
3017 // waiting for the next one to start.
3018 for (i=0; i<NF; i++) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003019 ActivityRecord r = finishes.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 synchronized (mService) {
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003021 activityRemoved = destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 }
3023 }
3024
3025 // Report back to any thumbnail receivers.
3026 for (i=0; i<NT; i++) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003027 ActivityRecord r = thumbnails.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028 mService.sendPendingThumbnail(r, null, null, null, true);
3029 }
3030
3031 if (booting) {
3032 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003033 } else if (startingUsers != null) {
3034 for (i=0; i<startingUsers.size(); i++) {
3035 mService.finishUserSwitch(startingUsers.get(i));
3036 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 }
3038
3039 mService.trimApplications();
3040 //dump();
3041 //mWindowManager.dump();
3042
3043 if (enableScreen) {
3044 mService.enableScreenAfterBoot();
3045 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003046
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003047 if (activityRemoved) {
3048 resumeTopActivityLocked(null);
3049 }
3050
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003051 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 }
3053
3054 /**
3055 * @return Returns true if the activity is being finished, false if for
3056 * some reason it is being left as-is.
3057 */
3058 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003059 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003060 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07003061 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003062 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003063 + ", result=" + resultCode + ", data=" + resultData
3064 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003065 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 return false;
3067 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003068
Craig Mautnerd44711d2013-02-23 11:24:36 -08003069 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 return true;
3071 }
3072
Craig Mautnerd2328952013-03-05 12:46:26 -08003073 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003074 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3075 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3076 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3077 ActivityRecord r = activities.get(activityNdx);
3078 if (r.resultTo == self && r.requestCode == requestCode) {
3079 if ((r.resultWho == null && resultWho == null) ||
3080 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3081 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3082 false);
3083 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003084 }
3085 }
3086 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003087 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003088 }
3089
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003090 final void finishTopRunningActivityLocked(ProcessRecord app) {
3091 ActivityRecord r = topRunningActivityLocked(null);
3092 if (r != null && r.app == app) {
3093 // If the top running activity is from this crashing
3094 // process, then terminate it to avoid getting in a loop.
3095 Slog.w(TAG, " Force finishing activity "
3096 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08003097 int taskNdx = mTaskHistory.indexOf(r.task);
3098 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003099 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003100 // Also terminate any activities below it that aren't yet
3101 // stopped, to avoid a situation where one will get
3102 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003103 --activityNdx;
3104 if (activityNdx < 0) {
3105 do {
3106 --taskNdx;
3107 if (taskNdx < 0) {
3108 break;
3109 }
3110 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3111 } while (activityNdx < 0);
3112 }
3113 if (activityNdx >= 0) {
3114 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003115 if (r.state == ActivityState.RESUMED
3116 || r.state == ActivityState.PAUSING
3117 || r.state == ActivityState.PAUSED) {
3118 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
3119 Slog.w(TAG, " Force finishing activity "
3120 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003121 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003122 }
3123 }
3124 }
3125 }
3126 }
3127
Craig Mautnerd2328952013-03-05 12:46:26 -08003128 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003129 ArrayList<ActivityRecord> activities = r.task.mActivities;
3130 for (int index = activities.indexOf(r); index >= 0; --index) {
3131 ActivityRecord cur = activities.get(index);
3132 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003133 break;
3134 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003135 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity",
3136 true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003137 }
3138 return true;
3139 }
3140
Dianne Hackborn5c607432012-02-28 14:44:19 -08003141 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3142 // send the result
3143 ActivityRecord resultTo = r.resultTo;
3144 if (resultTo != null) {
3145 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
3146 + " who=" + r.resultWho + " req=" + r.requestCode
3147 + " res=" + resultCode + " data=" + resultData);
3148 if (r.info.applicationInfo.uid > 0) {
3149 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3150 resultTo.packageName, resultData,
3151 resultTo.getUriPermissionsLocked());
3152 }
3153 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3154 resultData);
3155 r.resultTo = null;
3156 }
3157 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
3158
3159 // Make sure this HistoryRecord is not holding on to other resources,
3160 // because clients have remote IPC references to this object so we
3161 // can't assume that will go away and want to avoid circular IPC refs.
3162 r.results = null;
3163 r.pendingResults = null;
3164 r.newIntents = null;
3165 r.icicle = null;
3166 }
3167
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 /**
3169 * @return Returns true if this activity has been removed from the history
3170 * list, or false if it is still in the list and will be removed later.
3171 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08003172 final boolean finishActivityLocked(ActivityRecord r, int resultCode,
3173 Intent resultData, String reason, boolean oomAdj) {
3174 return finishActivityLocked(r, resultCode, resultData, reason, false, oomAdj);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003175 }
3176
3177 /**
3178 * @return Returns true if this activity has been removed from the history
3179 * list, or false if it is still in the list and will be removed later.
3180 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08003181 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3182 String reason, boolean immediate, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 if (r.finishing) {
3184 Slog.w(TAG, "Duplicate finish request for " + r);
3185 return false;
3186 }
3187
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08003188 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003190 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003192 final ArrayList<ActivityRecord> activities = r.task.mActivities;
3193 final int index = activities.indexOf(r);
3194 if (index < (activities.size() - 1)) {
3195 ActivityRecord next = activities.get(index+1);
3196 if (r.frontOfTask) {
3197 // The next activity is now the front of the task.
3198 next.frontOfTask = true;
3199 }
3200 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3201 // If the caller asked that this activity (and all above it)
3202 // be cleared when the task is reset, don't lose that information,
3203 // but propagate it up to the next activity.
3204 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 }
3206 }
3207
3208 r.pauseKeyDispatchingLocked();
Craig Mautner2219a1b2013-03-25 09:44:30 -07003209 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003210 if (mService.mFocusedActivity == r) {
3211 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3212 }
3213 }
3214
Dianne Hackborn5c607432012-02-28 14:44:19 -08003215 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003216
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 if (mService.mPendingThumbnails.size() > 0) {
3218 // There are clients waiting to receive thumbnails so, in case
3219 // this is an activity that someone is waiting for, add it
3220 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08003221 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 }
3223
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003224 if (immediate) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003225 return finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, oomAdj) == null;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003226 } else if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003227 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 if (DEBUG_TRANSITION) Slog.v(TAG,
3229 "Prepare close transition: finishing " + r);
3230 mService.mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003231 ? AppTransition.TRANSIT_TASK_CLOSE
3232 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08003235 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003237 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003238 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
3239 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
3240 startPausingLocked(false, false);
3241 }
3242
3243 } else if (r.state != ActivityState.PAUSING) {
3244 // If the activity is PAUSING, we will complete the finish once
3245 // it is done pausing; else we can just directly finish it here.
3246 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003247 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 } else {
3249 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
3250 }
3251
3252 return false;
3253 }
3254
3255 private static final int FINISH_IMMEDIATELY = 0;
3256 private static final int FINISH_AFTER_PAUSE = 1;
3257 private static final int FINISH_AFTER_VISIBLE = 2;
3258
3259 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003260 int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 // First things first: if this activity is currently visible,
3262 // and the resumed activity is not yet visible, then hold off on
3263 // finishing until the resumed one becomes visible.
3264 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
3265 if (!mStoppingActivities.contains(r)) {
3266 mStoppingActivities.add(r);
3267 if (mStoppingActivities.size() > 3) {
3268 // If we already have a few activities waiting to stop,
3269 // then give up on things going idle and start clearing
3270 // them out.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003271 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003272 } else {
3273 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003274 }
3275 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003276 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3277 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003279 if (oomAdj) {
3280 mService.updateOomAdjLocked();
3281 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 return r;
3283 }
3284
3285 // make sure the record is cleaned out of other places.
3286 mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003287 mGoingToSleepActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288 mWaitingVisibleActivities.remove(r);
3289 if (mResumedActivity == r) {
3290 mResumedActivity = null;
3291 }
3292 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003293 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003294 r.state = ActivityState.FINISHING;
3295
3296 if (mode == FINISH_IMMEDIATELY
3297 || prevState == ActivityState.STOPPED
3298 || prevState == ActivityState.INITIALIZING) {
3299 // If this activity is already stopped, we can just finish
3300 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003301 boolean activityRemoved = destroyActivityLocked(r, true,
3302 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003303 if (activityRemoved) {
3304 resumeTopActivityLocked(null);
3305 }
3306 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003307 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003308
3309 // Need to go through the full pause cycle to get this
3310 // activity into the stopped state and then finish it.
3311 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
3312 mFinishingActivities.add(r);
3313 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003314 return r;
3315 }
3316
Craig Mautnerd2328952013-03-05 12:46:26 -08003317 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003318 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003319 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08003320 final TaskRecord task = srec.task;
3321 final ArrayList<ActivityRecord> activities = task.mActivities;
3322 final int start = activities.indexOf(srec);
3323 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003324 return false;
3325 }
3326 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003327 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003328 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003329 final ComponentName dest = destIntent.getComponent();
3330 if (start > 0 && dest != null) {
3331 for (int i = finishTo; i >= 0; i--) {
3332 ActivityRecord r = activities.get(i);
3333 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003334 r.info.name.equals(dest.getClassName())) {
3335 finishTo = i;
3336 parent = r;
3337 foundParentInTask = true;
3338 break;
3339 }
3340 }
3341 }
3342
3343 IActivityController controller = mService.mController;
3344 if (controller != null) {
3345 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3346 if (next != null) {
3347 // ask watcher if this is allowed
3348 boolean resumeOK = true;
3349 try {
3350 resumeOK = controller.activityResuming(next.packageName);
3351 } catch (RemoteException e) {
3352 mService.mController = null;
3353 }
3354
3355 if (!resumeOK) {
3356 return false;
3357 }
3358 }
3359 }
3360 final long origId = Binder.clearCallingIdentity();
3361 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003362 ActivityRecord r = activities.get(i);
3363 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003364 // Only return the supplied result for the first activity finished
3365 resultCode = Activity.RESULT_CANCELED;
3366 resultData = null;
3367 }
3368
3369 if (parent != null && foundParentInTask) {
3370 final int parentLaunchMode = parent.info.launchMode;
3371 final int destIntentFlags = destIntent.getFlags();
3372 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3373 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3374 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3375 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
3376 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
3377 } else {
3378 try {
3379 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3380 destIntent.getComponent(), 0, srec.userId);
3381 int res = startActivityLocked(srec.app.thread, destIntent,
3382 null, aInfo, parent.appToken, null,
3383 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
3384 0, null, true, null);
3385 foundParentInTask = res == ActivityManager.START_SUCCESS;
3386 } catch (RemoteException e) {
3387 foundParentInTask = false;
3388 }
3389 requestFinishActivityLocked(parent.appToken, resultCode,
3390 resultData, "navigate-up", true);
3391 }
3392 }
3393 Binder.restoreCallingIdentity(origId);
3394 return foundParentInTask;
3395 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396 /**
3397 * Perform the common clean-up of an activity record. This is called both
3398 * as part of destroyActivityLocked() (when destroying the client-side
3399 * representation) and cleaning things up as a result of its hosting
3400 * processing going away, in which case there is no remaining client-side
3401 * state to destroy so only the cleanup here is needed.
3402 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003403 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3404 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003405 if (mResumedActivity == r) {
3406 mResumedActivity = null;
3407 }
3408 if (mService.mFocusedActivity == r) {
3409 mService.mFocusedActivity = null;
3410 }
3411
3412 r.configDestroy = false;
3413 r.frozenBeforeDestroy = false;
3414
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003415 if (setState) {
3416 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3417 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003418 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003419 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003420 }
3421
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003422 // Make sure this record is no longer in the pending finishes list.
3423 // This could happen, for example, if we are trimming activities
3424 // down to the max limit while they are still waiting to finish.
3425 mFinishingActivities.remove(r);
3426 mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003427
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003428 // Remove any pending results.
3429 if (r.finishing && r.pendingResults != null) {
3430 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3431 PendingIntentRecord rec = apr.get();
3432 if (rec != null) {
3433 mService.cancelIntentSenderLocked(rec, false);
3434 }
3435 }
3436 r.pendingResults = null;
3437 }
3438
3439 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003440 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003441 }
3442
3443 if (mService.mPendingThumbnails.size() > 0) {
3444 // There are clients waiting to receive thumbnails so, in case
3445 // this is an activity that someone is waiting for, add it
3446 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08003447 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 }
3449
3450 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003451 removeTimeoutsForActivityLocked(r);
3452 }
3453
3454 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003455 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003456 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003458 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003459 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003460 }
3461
Dianne Hackborn5c607432012-02-28 14:44:19 -08003462 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003463 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3464 r.makeFinishing();
3465 if (DEBUG_ADD_REMOVE) {
3466 RuntimeException here = new RuntimeException("here");
3467 here.fillInStackTrace();
3468 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003469 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003470 if (r.task != null) {
3471 removeActivity(r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003472 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003473 r.takeFromHistory();
3474 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003475 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003476 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003477 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003478 r.app = null;
3479 mService.mWindowManager.removeAppToken(r.appToken);
3480 if (VALIDATE_TOKENS) {
3481 validateAppTokensLocked();
3482 }
3483 cleanUpActivityServicesLocked(r);
3484 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003485 }
3486
3487 /**
3488 * Perform clean-up of service connections in an activity record.
3489 */
3490 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3491 // Throw away any services that have been bound by this activity.
3492 if (r.connections != null) {
3493 Iterator<ConnectionRecord> it = r.connections.iterator();
3494 while (it.hasNext()) {
3495 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003496 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003497 }
3498 r.connections = null;
3499 }
3500 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003501
3502 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
3503 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
3504 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
3505 mHandler.sendMessage(msg);
3506 }
3507
Dianne Hackborn28695e02011-11-02 21:59:51 -07003508 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003509 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003510 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003511 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3512 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3513 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3514 final ActivityRecord r = activities.get(activityNdx);
3515 if (r.finishing) {
3516 continue;
3517 }
3518 if (r.fullscreen) {
3519 lastIsOpaque = true;
3520 }
3521 if (owner != null && r.app != owner) {
3522 continue;
3523 }
3524 if (!lastIsOpaque) {
3525 continue;
3526 }
3527 // We can destroy this one if we have its icicle saved and
3528 // it is not in the process of pausing/stopping/finishing.
3529 if (r.app != null && r != mResumedActivity && r != mPausingActivity
3530 && r.haveState && !r.visible && r.stopped
3531 && r.state != ActivityState.DESTROYING
3532 && r.state != ActivityState.DESTROYED) {
3533 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3534 + " resumed=" + mResumedActivity
3535 + " pausing=" + mPausingActivity);
3536 if (destroyActivityLocked(r, true, oomAdj, reason)) {
3537 activityRemoved = true;
3538 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003539 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003540 }
3541 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003542 if (activityRemoved) {
3543 resumeTopActivityLocked(null);
3544 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003545 }
3546
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003547 /**
3548 * Destroy the current CLIENT SIDE instance of an activity. This may be
3549 * called both when actually finishing an activity, or when performing
3550 * a configuration switch where we destroy the current client-side object
3551 * but then create a new client-side object for this same HistoryRecord.
3552 */
3553 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07003554 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003555 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003556 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3558 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003559 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003560 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561
3562 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003563
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003564 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003565
3566 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 if (hadApp) {
3569 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003570 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003571 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3572 mService.mHeavyWeightProcess = null;
3573 mService.mHandler.sendEmptyMessage(
3574 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3575 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003577 // No longer have activities, so update oom adj.
3578 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003579 }
3580 }
3581
3582 boolean skipDestroy = false;
3583
3584 try {
3585 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003586 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003587 r.configChangeFlags);
3588 } catch (Exception e) {
3589 // We can just ignore exceptions here... if the process
3590 // has crashed, our death notification will clean things
3591 // up.
3592 //Slog.w(TAG, "Exception thrown during finish", e);
3593 if (r.finishing) {
3594 removeActivityFromHistoryLocked(r);
3595 removedFromHistory = true;
3596 skipDestroy = true;
3597 }
3598 }
3599
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003600 r.nowVisible = false;
3601
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003602 // If the activity is finishing, we need to wait on removing it
3603 // from the list to give it a chance to do its cleanup. During
3604 // that time it may make calls back with its token so we need to
3605 // be able to find it on the list and so we don't want to remove
3606 // it from the list yet. Otherwise, we can just immediately put
3607 // it in the destroyed state since we are not removing it from the
3608 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003609 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003610 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3611 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003612 r.state = ActivityState.DESTROYING;
3613 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
3614 msg.obj = r;
3615 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3616 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003617 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3618 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003620 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003621 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003622 }
3623 } else {
3624 // remove this record from the history.
3625 if (r.finishing) {
3626 removeActivityFromHistoryLocked(r);
3627 removedFromHistory = true;
3628 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003629 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3630 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003631 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003632 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003633 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003634 }
3635 }
3636
3637 r.configChangeFlags = 0;
3638
3639 if (!mLRUActivities.remove(r) && hadApp) {
3640 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3641 }
3642
3643 return removedFromHistory;
3644 }
3645
Craig Mautnerd2328952013-03-05 12:46:26 -08003646 final void activityDestroyedLocked(IBinder token) {
3647 final long origId = Binder.clearCallingIdentity();
3648 try {
3649 ActivityRecord r = ActivityRecord.forToken(token);
3650 if (r != null) {
3651 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003652 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003653
3654 if (isInStackLocked(token) != null) {
3655 if (r.state == ActivityState.DESTROYING) {
3656 cleanUpActivityLocked(r, true, false);
3657 removeActivityFromHistoryLocked(r);
3658 }
3659 }
3660 resumeTopActivityLocked(null);
3661 } finally {
3662 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003663 }
3664 }
3665
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003666 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3667 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003668 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003669 if (DEBUG_CLEANUP) Slog.v(
3670 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003671 + " with " + i + " entries");
3672 while (i > 0) {
3673 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003674 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003675 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003677 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003678 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003679 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003680 }
3681 }
3682 }
3683
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003684 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3685 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
3686 removeHistoryRecordsForAppLocked(mStoppingActivities, app, "mStoppingActivities");
3687 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
3688 removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app,
3689 "mWaitingVisibleActivities");
3690 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
3691
3692 boolean hasVisibleActivities = false;
3693
3694 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003695 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003696 if (DEBUG_CLEANUP) Slog.v(
3697 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003698 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3699 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3700 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3701 final ActivityRecord r = activities.get(activityNdx);
3702 --i;
3703 if (DEBUG_CLEANUP) Slog.v(
3704 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3705 if (r.app == app) {
3706 boolean remove;
3707 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3708 // Don't currently have state for the activity, or
3709 // it is finishing -- always remove it.
3710 remove = true;
3711 } else if (r.launchCount > 2 &&
3712 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3713 // We have launched this activity too many times since it was
3714 // able to run, so give up and remove it.
3715 remove = true;
3716 } else {
3717 // The process may be gone, but the activity lives on!
3718 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003719 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003720 if (remove) {
3721 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
3722 RuntimeException here = new RuntimeException("here");
3723 here.fillInStackTrace();
3724 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3725 + ": haveState=" + r.haveState
3726 + " stateNotNeeded=" + r.stateNotNeeded
3727 + " finishing=" + r.finishing
3728 + " state=" + r.state, here);
3729 }
3730 if (!r.finishing) {
3731 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3732 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3733 r.userId, System.identityHashCode(r),
3734 r.task.taskId, r.shortComponentName,
3735 "proc died without state saved");
3736 }
3737 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003738
Craig Mautner0247fc82013-02-28 14:32:06 -08003739 } else {
3740 // We have the current state for this activity, so
3741 // it can be restarted later when needed.
3742 if (localLOGV) Slog.v(
3743 TAG, "Keeping entry, setting app to null");
3744 if (r.visible) {
3745 hasVisibleActivities = true;
3746 }
3747 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3748 + r);
3749 r.app = null;
3750 r.nowVisible = false;
3751 if (!r.haveState) {
3752 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
3753 "App died, clearing saved state of " + r);
3754 r.icicle = null;
3755 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003756 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003757
Craig Mautnerd2328952013-03-05 12:46:26 -08003758 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003759 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003760 }
3761 }
3762
3763 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003765
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003766 /**
3767 * Move the current home activity's task (if one exists) to the front
3768 * of the stack.
3769 */
3770 final void moveHomeToFrontLocked() {
Craig Mautner0247fc82013-02-28 14:32:06 -08003771 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3772 final TaskRecord task = mTaskHistory.get(taskNdx);
3773 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner11bf9a52013-02-19 14:08:51 -08003774 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3775 final ActivityRecord r = activities.get(activityNdx);
3776 if (r.isHomeActivity) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003777 moveTaskToFrontLocked(task, null, null);
3778 return;
Craig Mautner11bf9a52013-02-19 14:08:51 -08003779 }
3780 }
3781 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003782 }
3783
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003784 final void updateTransitLocked(int transit, Bundle options) {
3785 if (options != null) {
3786 ActivityRecord r = topRunningActivityLocked(null);
3787 if (r != null && r.state != ActivityState.RESUMED) {
3788 r.updateOptionsLocked(options);
3789 } else {
3790 ActivityOptions.abort(options);
3791 }
3792 }
3793 mService.mWindowManager.prepareAppTransition(transit, false);
3794 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003795
Craig Mautnercae015f2013-02-08 14:31:27 -08003796 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003797 final TaskRecord task = taskForIdLocked(taskId);
3798 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003799 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
3800 mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003801 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003802 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3803 // Caller wants the home activity moved with it. To accomplish this,
3804 // we'll just move the home task to the top first.
3805 moveHomeToFrontLocked();
3806 }
3807 moveTaskToFrontLocked(task, null, options);
3808 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003809 }
3810 return false;
3811 }
3812
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003813 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003814 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003815
Craig Mautner11bf9a52013-02-19 14:08:51 -08003816 final int numTasks = mTaskHistory.size();
3817 final int index = mTaskHistory.indexOf(tr);
3818 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
3819 // nothing to do!
3820 if (reason != null &&
3821 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3822 ActivityOptions.abort(options);
3823 } else {
3824 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3825 }
3826 return;
3827 }
3828
3829 // Shift all activities with this task up to the top
3830 // of the stack, keeping them in the same internal order.
3831 mTaskHistory.remove(tr);
3832 mTaskHistory.add(tr);
3833
3834 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003835 if (reason != null &&
3836 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003837 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003838 ActivityRecord r = topRunningActivityLocked(null);
3839 if (r != null) {
3840 mNoAnimActivities.add(r);
3841 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003842 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003843 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003844 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003845 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003846
Craig Mautner58547802013-03-05 08:23:53 -08003847 mService.mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003848
Craig Mautner58547802013-03-05 08:23:53 -08003849 resumeTopActivityLocked(null);
3850 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003851
3852 if (VALIDATE_TOKENS) {
3853 validateAppTokensLocked();
3854 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003855 }
3856
3857 /**
3858 * Worker method for rearranging history stack. Implements the function of moving all
3859 * activities for a specific task (gathering them if disjoint) into a single group at the
3860 * bottom of the stack.
3861 *
3862 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3863 * to premeptively cancel the move.
3864 *
3865 * @param task The taskId to collect and move to the bottom.
3866 * @return Returns true if the move completed, false if not.
3867 */
3868 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3869 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003870
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003871 // If we have a watcher, preflight the move before committing to it. First check
3872 // for *other* available tasks, but if none are available, then try again allowing the
3873 // current task to be selected.
Craig Mautner2219a1b2013-03-25 09:44:30 -07003874 if (mStackSupervisor.isMainStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003875 ActivityRecord next = topRunningActivityLocked(null, task);
3876 if (next == null) {
3877 next = topRunningActivityLocked(null, 0);
3878 }
3879 if (next != null) {
3880 // ask watcher if this is allowed
3881 boolean moveOK = true;
3882 try {
3883 moveOK = mService.mController.activityResuming(next.packageName);
3884 } catch (RemoteException e) {
3885 mService.mController = null;
3886 }
3887 if (!moveOK) {
3888 return false;
3889 }
3890 }
3891 }
3892
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003893 if (DEBUG_TRANSITION) Slog.v(TAG,
3894 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003895
Craig Mautnerd2328952013-03-05 12:46:26 -08003896 final TaskRecord tr = taskForIdLocked(task);
3897 if (tr == null) {
3898 return false;
3899 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003900 mTaskHistory.remove(tr);
3901 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902
3903 if (reason != null &&
3904 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003905 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003906 ActivityRecord r = topRunningActivityLocked(null);
3907 if (r != null) {
3908 mNoAnimActivities.add(r);
3909 }
3910 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08003911 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003912 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003914 mService.mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003915
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003916 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003917 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003918 }
3919
Craig Mautner58547802013-03-05 08:23:53 -08003920 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003921 return true;
3922 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003923
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003924 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003925 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003926 ActivityRecord resumed = mResumedActivity;
3927 if (resumed != null && resumed.thumbHolder == tr) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003928 info.mainThumbnail = screenshotActivities(resumed);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003929 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003930 if (info.mainThumbnail == null) {
3931 info.mainThumbnail = tr.lastThumbnail;
3932 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003933 return info;
3934 }
3935
Dianne Hackborn15491c62012-09-19 10:59:14 -07003936 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
3937 ActivityRecord resumed = mResumedActivity;
3938 if (resumed != null && resumed.task == tr) {
3939 // This task is the current resumed task, we just need to take
3940 // a screenshot of it and return that.
Craig Mautnerd2328952013-03-05 12:46:26 -08003941 return screenshotActivities(resumed);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003942 }
3943 // Return the information about the task, to figure out the top
3944 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003945 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003946 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003947 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003948 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003949 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003950 }
3951
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003952 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3953 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003954 final TaskRecord task = taskForIdLocked(taskId);
3955 if (task == null) {
3956 return null;
3957 }
Craig Mautner16030772013-02-26 16:05:47 -08003958 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003959 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003960 if (taskRequired) {
3961 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3962 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003963 return null;
3964 }
3965
3966 if (subTaskIndex < 0) {
3967 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003968 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003969 return info.root;
3970 }
3971
3972 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003973 if (taskRequired) {
3974 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3975 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003976 return null;
3977 }
3978
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003979 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003980 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003981 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003982 return subtask.activity;
3983 }
3984
Craig Mautner16030772013-02-26 16:05:47 -08003985 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003986 final TaskAccessInfo thumbs = new TaskAccessInfo();
3987 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003988 final ArrayList<ActivityRecord> activities = task.mActivities;
3989 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003990 int j = 0;
3991 ThumbnailHolder holder = null;
3992 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003993 ActivityRecord ar = activities.get(j);
3994 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003995 thumbs.root = ar;
3996 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003997 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003998 if (holder != null) {
3999 thumbs.mainThumbnail = holder.lastThumbnail;
4000 }
4001 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004002 break;
4003 }
4004 j++;
4005 }
4006
4007 if (j >= NA) {
4008 return thumbs;
4009 }
4010
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004011 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
4012 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004013 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08004014 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004015 j++;
4016 if (ar.finishing) {
4017 continue;
4018 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004019 if (ar.thumbHolder != holder && holder != null) {
4020 thumbs.numSubThumbbails++;
4021 holder = ar.thumbHolder;
4022 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07004023 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004024 sub.activity = ar;
4025 sub.index = j-1;
4026 subtasks.add(sub);
4027 }
4028 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004029 if (thumbs.numSubThumbbails > 0) {
4030 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004031 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004032 public Bitmap getThumbnail(int index) {
4033 if (index < 0 || index >= thumbs.subtasks.size()) {
4034 return null;
4035 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004036 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
4037 ActivityRecord resumed = mResumedActivity;
4038 if (resumed != null && resumed.thumbHolder == sub.holder) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004039 return screenshotActivities(resumed);
Dianne Hackborn15491c62012-09-19 10:59:14 -07004040 }
4041 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004042 }
4043 };
4044 }
4045 return thumbs;
4046 }
4047
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004048 private final void logStartActivity(int tag, ActivityRecord r,
4049 TaskRecord task) {
4050 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004051 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004052 r.shortComponentName, r.intent.getAction(),
4053 r.intent.getType(), r.intent.getDataString(),
4054 r.intent.getFlags());
4055 }
4056
4057 /**
4058 * Make sure the given activity matches the current configuration. Returns
4059 * false if the activity had to be destroyed. Returns true if the
4060 * configuration is the same, or the activity will remain running as-is
4061 * for whatever reason. Ensures the HistoryRecord is updated with the
4062 * correct configuration and all other bookkeeping is handled.
4063 */
4064 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
4065 int globalChanges) {
4066 if (mConfigWillChange) {
4067 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4068 "Skipping config check (will change): " + r);
4069 return true;
4070 }
4071
4072 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4073 "Ensuring correct configuration: " + r);
4074
4075 // Short circuit: if the two configurations are the exact same
4076 // object (the common case), then there is nothing to do.
4077 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004078 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004079 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4080 "Configuration unchanged in " + r);
4081 return true;
4082 }
4083
4084 // We don't worry about activities that are finishing.
4085 if (r.finishing) {
4086 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4087 "Configuration doesn't matter in finishing " + r);
4088 r.stopFreezingScreenLocked(false);
4089 return true;
4090 }
4091
4092 // Okay we now are going to make this activity have the new config.
4093 // But then we need to figure out how it needs to deal with that.
4094 Configuration oldConfig = r.configuration;
4095 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004096
4097 // Determine what has changed. May be nothing, if this is a config
4098 // that has come back from the app after going idle. In that case
4099 // we just want to leave the official config object now in the
4100 // activity and do nothing else.
4101 final int changes = oldConfig.diff(newConfig);
4102 if (changes == 0 && !r.forceNewConfig) {
4103 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4104 "Configuration no differences in " + r);
4105 return true;
4106 }
4107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004108 // If the activity isn't currently running, just leave the new
4109 // configuration and it will pick that up next time it starts.
4110 if (r.app == null || r.app.thread == null) {
4111 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4112 "Configuration doesn't matter not running " + r);
4113 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004114 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004115 return true;
4116 }
4117
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004118 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004119 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
4120 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
4121 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07004122 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004123 + ", newConfig=" + newConfig);
4124 }
Dianne Hackborne6676352011-06-01 16:51:20 -07004125 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004126 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4127 r.configChangeFlags |= changes;
4128 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004129 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004130 if (r.app == null || r.app.thread == null) {
4131 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004132 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07004133 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004134 } else if (r.state == ActivityState.PAUSING) {
4135 // A little annoying: we are waiting for this activity to
4136 // finish pausing. Let's not do anything now, but just
4137 // flag that it needs to be restarted when done pausing.
4138 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004139 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004140 r.configDestroy = true;
4141 return true;
4142 } else if (r.state == ActivityState.RESUMED) {
4143 // Try to optimize this case: the configuration is changing
4144 // and we need to restart the top, resumed activity.
4145 // Instead of doing the normal handshaking, just say
4146 // "restart!".
4147 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004148 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004149 relaunchActivityLocked(r, r.configChangeFlags, true);
4150 r.configChangeFlags = 0;
4151 } else {
4152 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004153 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004154 relaunchActivityLocked(r, r.configChangeFlags, false);
4155 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004157
4158 // All done... tell the caller we weren't able to keep this
4159 // activity around.
4160 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004161 }
4162
4163 // Default case: the activity can handle this new configuration, so
4164 // hand it over. Note that we don't need to give it the new
4165 // configuration, since we always send configuration changes to all
4166 // process when they happen so it can just use whatever configuration
4167 // it last got.
4168 if (r.app != null && r.app.thread != null) {
4169 try {
4170 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004171 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004172 } catch (RemoteException e) {
4173 // If process died, whatever.
4174 }
4175 }
4176 r.stopFreezingScreenLocked(false);
4177
4178 return true;
4179 }
4180
4181 private final boolean relaunchActivityLocked(ActivityRecord r,
4182 int changes, boolean andResume) {
4183 List<ResultInfo> results = null;
4184 List<Intent> newIntents = null;
4185 if (andResume) {
4186 results = r.results;
4187 newIntents = r.newIntents;
4188 }
4189 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
4190 + " with results=" + results + " newIntents=" + newIntents
4191 + " andResume=" + andResume);
4192 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004193 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004194 r.task.taskId, r.shortComponentName);
4195
4196 r.startFreezingScreenLocked(r.app, 0);
4197
4198 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004199 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
4200 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
4201 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004202 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08004203 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004204 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004205 // Note: don't need to call pauseIfSleepingLocked() here, because
4206 // the caller will only pass in 'andResume' if this activity is
4207 // currently resumed, which implies we aren't sleeping.
4208 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004209 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 }
4211
4212 if (andResume) {
4213 r.results = null;
4214 r.newIntents = null;
Craig Mautner2219a1b2013-03-25 09:44:30 -07004215 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004216 mService.reportResumedActivityLocked(r);
4217 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004218 r.state = ActivityState.RESUMED;
4219 } else {
4220 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4221 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004222 }
4223
4224 return true;
4225 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004226
4227 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004228 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4229 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4230 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4231 final ActivityRecord r = activities.get(activityNdx);
4232 if (r.appToken == token) {
4233 return true;
4234 }
4235 if (r.fullscreen && !r.finishing) {
4236 return false;
4237 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004238 }
4239 }
4240 return true;
4241 }
4242
4243 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004244 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4245 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4246 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4247 final ActivityRecord r = activities.get(activityNdx);
4248 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004249 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004250 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004251 }
4252 }
4253 }
4254
4255 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
4256 boolean didSomething = false;
4257 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004258 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4259 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4260 int numActivities = activities.size();
4261 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4262 ActivityRecord r = activities.get(activityNdx);
4263 final boolean samePackage = r.packageName.equals(name)
4264 || (name == null && r.userId == userId);
4265 if ((userId == UserHandle.USER_ALL || r.userId == userId)
4266 && (samePackage || r.task == lastTask)
4267 && (r.app == null || evenPersistent || !r.app.persistent)) {
4268 if (!doit) {
4269 if (r.finishing) {
4270 // If this activity is just finishing, then it is not
4271 // interesting as far as something to stop.
4272 continue;
4273 }
4274 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004275 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004276 didSomething = true;
4277 Slog.i(TAG, " Force finishing activity " + r);
4278 if (samePackage) {
4279 if (r.app != null) {
4280 r.app.removed = true;
4281 }
4282 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004283 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004284 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08004285 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08004286 }
4287 }
4288 }
4289 return didSomething;
4290 }
4291
4292 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08004293 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004294 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08004295 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
4296 --maxNum, --taskNdx) {
4297 final TaskRecord task = mTaskHistory.get(taskNdx);
4298 ActivityRecord r = null;
4299 ActivityRecord top = null;
4300 int numActivities = 0;
4301 int numRunning = 0;
4302 final ArrayList<ActivityRecord> activities = task.mActivities;
4303 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4304 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004305
Craig Mautneraab647e2013-02-28 16:31:36 -08004306 // Initialize state for next task if needed.
4307 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4308 top = r;
4309 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004310 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004311
4312 // Add 'r' into the current task.
4313 numActivities++;
4314 if (r.app != null && r.app.thread != null) {
4315 numRunning++;
4316 }
4317
4318 if (localLOGV) Slog.v(
4319 TAG, r.intent.getComponent().flattenToShortString()
4320 + ": task=" + r.task);
4321 }
4322
4323 RunningTaskInfo ci = new RunningTaskInfo();
4324 ci.id = task.taskId;
4325 ci.baseActivity = r.intent.getComponent();
4326 ci.topActivity = top.intent.getComponent();
4327 if (top.thumbHolder != null) {
4328 ci.description = top.thumbHolder.lastDescription;
4329 }
4330 ci.numActivities = numActivities;
4331 ci.numRunning = numRunning;
4332 //System.out.println(
4333 // "#" + maxNum + ": " + " descr=" + ci.description);
4334 if (receiver != null) {
4335 if (localLOGV) Slog.v(
4336 TAG, "State=" + top.state + "Idle=" + top.idle
4337 + " app=" + top.app
4338 + " thr=" + (top.app != null ? top.app.thread : null));
4339 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
4340 if (top.idle && top.app != null && top.app.thread != null) {
4341 topRecord = top;
4342 } else {
4343 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004344 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004345 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004346 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08004347 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004348 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004349 }
4350 return topRecord;
4351 }
4352
4353 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004354 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004355 if (DEBUG_SWITCH) Slog.d(
4356 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004357 if (top >= 0) {
4358 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4359 int activityTop = activities.size() - 1;
4360 if (activityTop > 0) {
4361 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4362 "unhandled-back", true);
4363 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004364 }
4365 }
4366
4367 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004368 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4369 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4370 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4371 final ActivityRecord r = activities.get(activityNdx);
4372 if (r.app == app) {
4373 Slog.w(TAG, " Force finishing activity "
4374 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08004375 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004376 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004377 }
4378 }
4379 }
4380
4381 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4382 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4384 final TaskRecord task = mTaskHistory.get(taskNdx);
4385 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004386 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08004387 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
4388 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004389 }
4390
4391 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4392 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4393
4394 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004395 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4396 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004397 }
4398 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004399 final int top = mTaskHistory.size() - 1;
4400 if (top >= 0) {
4401 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4402 int listTop = list.size() - 1;
4403 if (listTop >= 0) {
4404 activities.add(list.get(listTop));
4405 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004406 }
4407 } else {
4408 ItemMatcher matcher = new ItemMatcher();
4409 matcher.build(name);
4410
Craig Mautneraab647e2013-02-28 16:31:36 -08004411 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4412 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4413 if (matcher.match(r1, r1.intent.getComponent())) {
4414 activities.add(r1);
4415 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004416 }
4417 }
4418 }
4419
4420 return activities;
4421 }
4422
4423 ActivityRecord restartPackage(String packageName) {
4424 ActivityRecord starting = topRunningActivityLocked(null);
4425
4426 // All activities that came from the package must be
4427 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004428 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4429 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4430 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4431 final ActivityRecord a = activities.get(activityNdx);
4432 if (a.info.packageName.equals(packageName)) {
4433 a.forceNewConfig = true;
4434 if (starting != null && a == starting && a.visible) {
4435 a.startFreezingScreenLocked(starting.app,
4436 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4437 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004438 }
4439 }
4440 }
4441
4442 return starting;
4443 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004444
Craig Mautner0247fc82013-02-28 14:32:06 -08004445 private void removeActivity(ActivityRecord r) {
4446 final TaskRecord task = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08004447 // TODO: use ActivityManagerService.removeTask to do this.
Craig Mautner0247fc82013-02-28 14:32:06 -08004448 if (task.removeActivity(r)) {
4449 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "removeActivity: Removing from history, task="
4450 + task);
4451 mTaskHistory.remove(task);
Craig Mautner0247fc82013-02-28 14:32:06 -08004452 }
4453 }
4454
4455 private void setTask(ActivityRecord r, TaskRecord newTask, ThumbnailHolder newThumbHolder,
4456 boolean isRoot) {
4457 if (r.task != null) {
4458 removeActivity(r);
4459 }
4460 r.setTask(newTask, newThumbHolder, isRoot);
4461 }
4462
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004463 private TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4464 boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004465 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004466 if (toTop) {
4467 mTaskHistory.add(task);
4468 } else {
4469 mTaskHistory.add(0, task);
4470 }
4471 return task;
4472 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004473
4474 ArrayList<TaskRecord> getAllTasks() {
4475 return new ArrayList<TaskRecord>(mTaskHistory);
4476 }
4477
4478 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004479 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004480 if (task == null) {
4481 return;
4482 }
4483 task.stack.mTaskHistory.remove(task);
4484 task.stack = this;
4485 if (toTop) {
4486 mTaskHistory.add(task);
4487 } else {
4488 mTaskHistory.add(0, task);
4489 }
4490 }
4491
4492 public int getStackId() {
4493 return mStackId;
4494 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004495}