blob: a943842adf52e401bc47ce7532aaaea88b630646 [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;
45import android.content.pm.ApplicationInfo;
46import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080048import android.content.res.Resources;
49import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080050import android.graphics.Bitmap.Config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070052import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.os.Handler;
54import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090055import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.os.Message;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070057import android.os.ParcelFileDescriptor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.os.PowerManager;
59import android.os.RemoteException;
60import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070061import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.util.EventLog;
63import android.util.Log;
64import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070065import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066
Craig Mautnercae015f2013-02-08 14:31:27 -080067import java.io.FileDescriptor;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070068import java.io.IOException;
Craig Mautnercae015f2013-02-08 14:31:27 -080069import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import java.lang.ref.WeakReference;
71import java.util.ArrayList;
72import java.util.Iterator;
73import java.util.List;
74
75/**
76 * State and management of a single stack of activities.
77 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070078final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070080 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
82 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
83 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
84 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
85 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
86 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
87 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
88 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070089 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautner9658b312013-02-28 10:55:59 -080090
Dianne Hackbornce86ba82011-07-13 19:33:41 -070091 static final boolean DEBUG_STATES = false;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070092 static final boolean DEBUG_ADD_REMOVE = false;
93 static final boolean DEBUG_SAVED_STATE = false;
Dianne Hackborn07981492013-01-28 11:36:23 -080094 static final boolean DEBUG_APP = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070095
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080097
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070098 // How long we wait until giving up on the last activity telling us it
99 // is idle.
100 static final int IDLE_TIMEOUT = 10*1000;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700101
102 // Ticks during which we check progress while waiting for an app to launch.
103 static final int LAUNCH_TICK = 500;
104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105 // How long we wait until giving up on the last activity to pause. This
106 // is short because it directly impacts the responsiveness of starting the
107 // next activity.
108 static final int PAUSE_TIMEOUT = 500;
109
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700110 // How long we wait for the activity to tell us it has stopped before
111 // giving up. This is a good amount of time because we really need this
112 // from the application in order to get its saved state.
113 static final int STOP_TIMEOUT = 10*1000;
114
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800115 // How long we can hold the sleep wake lock before giving up.
116 static final int SLEEP_TIMEOUT = 5*1000;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we can hold the launch wake lock before giving up.
119 static final int LAUNCH_TIMEOUT = 10*1000;
120
121 // How long we wait until giving up on an activity telling us it has
122 // finished destroying itself.
123 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800126 // disabled.
127 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800128
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700129 // How long between activity launches that we consider safe to not warn
130 // the user about an unexpected activity being launched on top.
131 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133 // Set to false to disable the preview that is shown while a new activity
134 // is being started.
135 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800136
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700137 enum ActivityState {
138 INITIALIZING,
139 RESUMED,
140 PAUSING,
141 PAUSED,
142 STOPPING,
143 STOPPED,
144 FINISHING,
145 DESTROYING,
146 DESTROYED
147 }
148
149 final ActivityManagerService mService;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700151 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700153 /**
154 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800155 * running) activities. It contains #TaskRecord objects.
156 */
157 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
158
159 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800160 * Used for validating app tokens with window manager.
161 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800162 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 /**
165 * List of running activities, sorted by recent usage.
166 * The first entry in the list is the least recently used.
167 * It contains HistoryRecord objects.
168 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700169 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170
171 /**
172 * List of activities that are waiting for a new activity
173 * to become visible before completing whatever operation they are
174 * supposed to do.
175 */
176 final ArrayList<ActivityRecord> mWaitingVisibleActivities
177 = new ArrayList<ActivityRecord>();
178
179 /**
180 * List of activities that are ready to be stopped, but waiting
181 * for the next activity to settle down before doing so. It contains
182 * HistoryRecord objects.
183 */
184 final ArrayList<ActivityRecord> mStoppingActivities
185 = new ArrayList<ActivityRecord>();
186
187 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800188 * List of activities that are in the process of going to sleep.
189 */
190 final ArrayList<ActivityRecord> mGoingToSleepActivities
191 = new ArrayList<ActivityRecord>();
192
193 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 * Animations that for the current transition have requested not to
195 * be considered for the transition animation.
196 */
197 final ArrayList<ActivityRecord> mNoAnimActivities
198 = new ArrayList<ActivityRecord>();
199
200 /**
201 * List of activities that are ready to be finished, but waiting
202 * for the previous activity to settle down before doing so. It contains
203 * HistoryRecord objects.
204 */
205 final ArrayList<ActivityRecord> mFinishingActivities
206 = new ArrayList<ActivityRecord>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800207
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700208 /**
209 * List of people waiting to find out about the next launched activity.
210 */
211 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
212 = new ArrayList<IActivityManager.WaitResult>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800213
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700214 /**
215 * List of people waiting to find out about the next visible activity.
216 */
217 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
218 = new ArrayList<IActivityManager.WaitResult>();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700219
220 final ArrayList<UserStartedState> mStartingUsers
221 = new ArrayList<UserStartedState>();
222
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700223 /**
224 * Set when the system is going to sleep, until we have
225 * successfully paused the current activity and released our wake lock.
226 * At that point the system is allowed to actually sleep.
227 */
228 final PowerManager.WakeLock mGoingToSleep;
229
230 /**
231 * We don't want to allow the device to go to sleep while in the process
232 * of launching an activity. This is primarily to allow alarm intent
233 * receivers to launch an activity and get that to run before the device
234 * goes back to sleep.
235 */
236 final PowerManager.WakeLock mLaunchingActivity;
237
238 /**
239 * When we are in the process of pausing an activity, before starting the
240 * next one, this variable holds the activity that is currently being paused.
241 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800242 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700243
244 /**
245 * This is the last activity that we put into the paused state. This is
246 * used to determine if we need to do an activity transition while sleeping,
247 * when we normally hold the top activity paused.
248 */
249 ActivityRecord mLastPausedActivity = null;
250
251 /**
252 * Current activity that is resumed, or null if there is none.
253 */
254 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700257 * This is the last activity that has been started. It is only used to
258 * identify when multiple activities are started at once so that the user
259 * can be warned they may not be in the activity they think they are.
260 */
261 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800262
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700263 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700264 * Set when we know we are going to be calling updateConfiguration()
265 * soon, so want to skip intermediate config checks.
266 */
267 boolean mConfigWillChange;
268
269 /**
270 * Set to indicate whether to issue an onUserLeaving callback when a
271 * newly launched activity is being brought in front of us.
272 */
273 boolean mUserLeaving = false;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800276
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800277 /**
278 * Set when we have taken too long waiting to go to sleep.
279 */
280 boolean mSleepTimeout = false;
281
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700282 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800283 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
284 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
285 */
286 private ActivityRecord mLastScreenshotActivity = null;
287 private Bitmap mLastScreenshotBitmap = null;
288
Craig Mautnercae015f2013-02-08 14:31:27 -0800289 /**
290 * List of ActivityRecord objects that have been finished and must
291 * still report back to a pending thumbnail receiver.
292 */
293 private final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>();
294
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800295 int mThumbnailWidth = -1;
296 int mThumbnailHeight = -1;
297
Amith Yamasani742a6712011-05-04 14:49:28 -0700298 private int mCurrentUser;
299
Craig Mautnerc00204b2013-03-05 15:02:14 -0800300 final int mStackId;
301
Craig Mautner27084302013-03-25 08:05:25 -0700302 /** Run all ActivityStacks through this */
303 final ActivityStackSupervisor mStackSupervisor;
304
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800305 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
306 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
307 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
308 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
309 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
310 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
311 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700312 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700313 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700314 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
315
316 static class ScheduleDestroyArgs {
317 final ProcessRecord mOwner;
318 final boolean mOomAdj;
319 final String mReason;
320 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
321 mOwner = owner;
322 mOomAdj = oomAdj;
323 mReason = reason;
324 }
325 }
326
Zoran Marcetaf958b322012-08-09 20:27:12 +0900327 final Handler mHandler;
328
329 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700330 //public Handler() {
331 // if (localLOGV) Slog.v(TAG, "Handler started!");
332 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900333 public ActivityStackHandler(Looper looper) {
334 super(looper);
335 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336
Zoran Marcetaf958b322012-08-09 20:27:12 +0900337 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700338 public void handleMessage(Message msg) {
339 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800340 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700341 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700342 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700343 Slog.w(TAG, "Sleep timeout! Sleeping now.");
344 mSleepTimeout = true;
345 checkReadyForSleepLocked();
346 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800347 }
348 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700349 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800350 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700351 // We don't at this point know if the activity is fullscreen,
352 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800353 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700354 synchronized (mService) {
355 if (r.app != null) {
356 mService.logAppTooSlow(r.app, r.pauseTime,
357 "pausing " + r);
358 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700359
Craig Mautnerd2328952013-03-05 12:46:26 -0800360 activityPausedLocked(r != null ? r.appToken : null, true);
361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362 } break;
363 case IDLE_TIMEOUT_MSG: {
364 if (mService.mDidDexOpt) {
365 mService.mDidDexOpt = false;
366 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
367 nmsg.obj = msg.obj;
368 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
369 return;
370 }
371 // We don't at this point know if the activity is fullscreen,
372 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800373 ActivityRecord r = (ActivityRecord)msg.obj;
374 Slog.w(TAG, "Activity idle timeout for " + r);
375 activityIdleInternal(r != null ? r.appToken : null, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700377 case LAUNCH_TICK_MSG: {
378 ActivityRecord r = (ActivityRecord)msg.obj;
379 synchronized (mService) {
380 if (r.continueLaunchTickingLocked()) {
381 mService.logAppTooSlow(r.app, r.launchTickTime,
382 "launching " + r);
383 }
384 }
385 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800387 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 // We don't at this point know if the activity is fullscreen,
389 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800390 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800391 synchronized (mService) {
392 activityDestroyedLocked(r != null ? r.appToken : null);
393 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 } break;
395 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800396 ActivityRecord r = (ActivityRecord)msg.obj;
397 activityIdleInternal(r != null ? r.appToken : null, false, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700398 } break;
399 case LAUNCH_TIMEOUT_MSG: {
400 if (mService.mDidDexOpt) {
401 mService.mDidDexOpt = false;
402 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
403 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
404 return;
405 }
406 synchronized (mService) {
407 if (mLaunchingActivity.isHeld()) {
408 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
409 mLaunchingActivity.release();
410 }
411 }
412 } break;
413 case RESUME_TOP_ACTIVITY_MSG: {
414 synchronized (mService) {
415 resumeTopActivityLocked(null);
416 }
417 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700418 case STOP_TIMEOUT_MSG: {
419 ActivityRecord r = (ActivityRecord)msg.obj;
420 // We don't at this point know if the activity is fullscreen,
421 // so we need to be conservative and assume it isn't.
422 Slog.w(TAG, "Activity stop timeout for " + r);
423 synchronized (mService) {
424 if (r.isInHistory()) {
425 activityStoppedLocked(r, null, null, null);
426 }
427 }
428 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700429 case DESTROY_ACTIVITIES_MSG: {
430 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
431 synchronized (mService) {
432 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
433 }
434 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 }
436 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800437 }
438
Craig Mautner000f0022013-02-26 15:04:29 -0800439 private int numActivities() {
440 int count = 0;
441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 count += mTaskHistory.get(taskNdx).mActivities.size();
443 }
444 return count;
445 }
446
Craig Mautner2219a1b2013-03-25 09:44:30 -0700447 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId,
448 ActivityStackSupervisor supervisor) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900449 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700450 mService = service;
451 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700452 PowerManager pm =
453 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
454 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
455 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
456 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800457 mStackId = stackId;
Craig Mautner27084302013-03-25 08:05:25 -0700458 mStackSupervisor = supervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700459 }
Craig Mautner5962b122012-10-05 14:45:52 -0700460
461 private boolean okToShow(ActivityRecord r) {
462 return r.userId == mCurrentUser
463 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
464 }
465
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700466 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800467 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
468 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800469 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800470 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
471 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800472 if (!r.finishing && r != notTop && okToShow(r)) {
473 return r;
474 }
475 }
476 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700477 return null;
478 }
479
480 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800483 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800484 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
485 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800486 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
487 return r;
488 }
489 }
490 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700491 return null;
492 }
493
494 /**
495 * This is a simplified version of topRunningActivityLocked that provides a number of
496 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800497 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700498 * @param token If non-null, any history records matching this token will be skipped.
499 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800500 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700501 * @return Returns the HistoryRecord of the next activity on the stack.
502 */
503 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800504 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
505 TaskRecord task = mTaskHistory.get(taskNdx);
506 if (task.taskId == taskId) {
507 continue;
508 }
509 ArrayList<ActivityRecord> activities = task.mActivities;
510 for (int i = activities.size() - 1; i >= 0; --i) {
511 final ActivityRecord r = activities.get(i);
512 // Note: the taskId check depends on real taskId fields being non-zero
513 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
514 return r;
515 }
516 }
517 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700518 return null;
519 }
520
Craig Mautnerd2328952013-03-05 12:46:26 -0800521 TaskRecord taskForIdLocked(int id) {
522 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
523 final TaskRecord task = mTaskHistory.get(taskNdx);
524 if (task.taskId == id) {
525 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800526 }
527 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700528 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700529 }
530
Craig Mautnerd2328952013-03-05 12:46:26 -0800531 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800532 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800533 if (r != null) {
534 final TaskRecord task = r.task;
535 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
536 if (task.stack != this) Slog.w(TAG,
537 "Illegal state! task does not point to stack it is in.");
538 return r;
539 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800540 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800541 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800542 }
543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700544 private final boolean updateLRUListLocked(ActivityRecord r) {
545 final boolean hadit = mLRUActivities.remove(r);
546 mLRUActivities.add(r);
547 return hadit;
548 }
549
550 /**
551 * Returns the top activity in any existing task matching the given
552 * Intent. Returns null if no such task is found.
553 */
554 private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
555 ComponentName cls = intent.getComponent();
556 if (info.targetActivity != null) {
557 cls = new ComponentName(info.packageName, info.targetActivity);
558 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700559 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800560
561 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
562 final TaskRecord task = mTaskHistory.get(taskNdx);
563 final ActivityRecord r = task.getTopActivity();
564 if (r == null || r.finishing || r.userId != userId ||
565 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
566 continue;
567 }
568
569 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
570 // + "/aff=" + r.task.affinity + " to new cls="
571 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
572 if (task.affinity != null) {
573 if (task.affinity.equals(info.taskAffinity)) {
574 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 return r;
576 }
Craig Mautner000f0022013-02-26 15:04:29 -0800577 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
578 //Slog.i(TAG, "Found matching class!");
579 //dump();
580 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
581 return r;
582 } else if (task.affinityIntent != null
583 && task.affinityIntent.getComponent().equals(cls)) {
584 //Slog.i(TAG, "Found matching class!");
585 //dump();
586 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
587 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700588 }
589 }
590
591 return null;
592 }
593
594 /**
595 * Returns the first activity (starting from the top of the stack) that
596 * is the same as the given activity. Returns null if no such activity
597 * is found.
598 */
599 private ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
600 ComponentName cls = intent.getComponent();
601 if (info.targetActivity != null) {
602 cls = new ComponentName(info.packageName, info.targetActivity);
603 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700604 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605
Craig Mautner000f0022013-02-26 15:04:29 -0800606 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
607 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
608 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
609 ActivityRecord r = activities.get(activityNdx);
610 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700611 //Slog.i(TAG, "Found matching class!");
612 //dump();
613 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
614 return r;
615 }
616 }
617 }
618
619 return null;
620 }
621
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700622 final void showAskCompatModeDialogLocked(ActivityRecord r) {
623 Message msg = Message.obtain();
624 msg.what = ActivityManagerService.SHOW_COMPAT_MODE_DIALOG_MSG;
625 msg.obj = r.task.askedCompatMode ? null : r;
626 mService.mHandler.sendMessage(msg);
627 }
628
Amith Yamasani742a6712011-05-04 14:49:28 -0700629 /*
630 * Move the activities around in the stack to bring a user to the foreground.
631 * @return whether there are any activities for the specified user.
632 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700633 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800634 if (VALIDATE_TOKENS) {
635 validateAppTokensLocked();
636 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700637 mStartingUsers.add(uss);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800638 if (mCurrentUser == userId) {
639 return true;
640 }
641 mCurrentUser = userId;
642
643 // Move userId's tasks to the top.
644 boolean haveActivities = false;
645 TaskRecord task = null;
646 int index = mTaskHistory.size();
647 for (int i = 0; i < index; ++i) {
648 task = mTaskHistory.get(i);
649 if (task.userId == userId) {
650 haveActivities = true;
651 mTaskHistory.remove(i);
652 mTaskHistory.add(task);
653 --index;
654 }
655 }
656
657 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
658 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800659 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700660 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700661 }
662
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700663 final boolean realStartActivityLocked(ActivityRecord r,
664 ProcessRecord app, boolean andResume, boolean checkConfig)
665 throws RemoteException {
666
667 r.startFreezingScreenLocked(app, 0);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800668 mService.mWindowManager.setAppVisibility(r.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700669
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700670 // schedule launch ticks to collect information about slow apps.
671 r.startLaunchTickingLocked();
672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 // Have the window manager re-evaluate the orientation of
674 // the screen based on the new activity order. Note that
675 // as a result of this, it can call back into the activity
676 // manager with a new orientation. We don't care about that,
677 // because the activity is not currently running so we are
678 // just restarting it anyway.
679 if (checkConfig) {
680 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
681 mService.mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -0800682 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Dianne Hackborn813075a62011-11-14 17:45:19 -0800683 mService.updateConfigurationLocked(config, r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700684 }
685
686 r.app = app;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700687 app.waitingToKill = null;
Dianne Hackborn07981492013-01-28 11:36:23 -0800688 r.launchCount++;
689 r.lastLaunchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700690
691 if (localLOGV) Slog.v(TAG, "Launching: " + r);
692
693 int idx = app.activities.indexOf(r);
694 if (idx < 0) {
695 app.activities.add(r);
696 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700697 mService.updateLruProcessLocked(app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700698
699 try {
700 if (app.thread == null) {
701 throw new RemoteException();
702 }
703 List<ResultInfo> results = null;
704 List<Intent> newIntents = null;
705 if (andResume) {
706 results = r.results;
707 newIntents = r.newIntents;
708 }
709 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
710 + " icicle=" + r.icicle
711 + " with results=" + results + " newIntents=" + newIntents
712 + " andResume=" + andResume);
713 if (andResume) {
714 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700715 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700716 r.task.taskId, r.shortComponentName);
717 }
718 if (r.isHomeActivity) {
719 mService.mHomeProcess = app;
720 }
721 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800722 r.sleeping = false;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400723 r.forceNewConfig = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700724 showAskCompatModeDialogLocked(r);
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700725 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700726 String profileFile = null;
727 ParcelFileDescriptor profileFd = null;
728 boolean profileAutoStop = false;
729 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
730 if (mService.mProfileProc == null || mService.mProfileProc == app) {
731 mService.mProfileProc = app;
732 profileFile = mService.mProfileFile;
733 profileFd = mService.mProfileFd;
734 profileAutoStop = mService.mAutoStopProfiler;
735 }
736 }
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -0700737 app.hasShownUi = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700738 app.pendingUiClean = true;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700739 if (profileFd != null) {
740 try {
741 profileFd = profileFd.dup();
742 } catch (IOException e) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800743 if (profileFd != null) {
744 try {
745 profileFd.close();
746 } catch (IOException o) {
747 }
748 profileFd = null;
749 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700750 }
751 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800752 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Dianne Hackborn813075a62011-11-14 17:45:19 -0800753 System.identityHashCode(r), r.info,
754 new Configuration(mService.mConfiguration),
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700755 r.compat, r.icicle, results, newIntents, !andResume,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700756 mService.isNextTransitionForward(), profileFile, profileFd,
757 profileAutoStop);
Craig Mautner9658b312013-02-28 10:55:59 -0800758
Dianne Hackborn54e570f2010-10-04 18:32:32 -0700759 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700760 // This may be a heavy-weight process! Note that the package
761 // manager will ensure that only activity can run in the main
762 // process of the .apk, which is the only thing that will be
763 // considered heavy-weight.
764 if (app.processName.equals(app.info.packageName)) {
765 if (mService.mHeavyWeightProcess != null
766 && mService.mHeavyWeightProcess != app) {
767 Log.w(TAG, "Starting new heavy weight process " + app
768 + " when already running "
769 + mService.mHeavyWeightProcess);
770 }
771 mService.mHeavyWeightProcess = app;
772 Message msg = mService.mHandler.obtainMessage(
773 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
774 msg.obj = r;
775 mService.mHandler.sendMessage(msg);
776 }
777 }
Craig Mautner9658b312013-02-28 10:55:59 -0800778
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700779 } catch (RemoteException e) {
780 if (r.launchFailed) {
781 // This is the second time we failed -- finish activity
782 // and give up.
783 Slog.e(TAG, "Second failure launching "
784 + r.intent.getComponent().flattenToShortString()
785 + ", giving up", e);
786 mService.appDiedLocked(app, app.pid, app.thread);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800787 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700788 "2nd-crash", false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 return false;
790 }
791
792 // This is the first time we failed -- restart process and
793 // retry.
794 app.activities.remove(r);
795 throw e;
796 }
797
798 r.launchFailed = false;
799 if (updateLRUListLocked(r)) {
800 Slog.w(TAG, "Activity " + r
801 + " being launched, but already in LRU list");
802 }
803
804 if (andResume) {
805 // As part of the process of launching, ActivityThread also performs
806 // a resume.
807 r.state = ActivityState.RESUMED;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700808 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
809 + " (starting new instance)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 r.stopped = false;
811 mResumedActivity = r;
812 r.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -0800813 mService.addRecentTaskLocked(r.task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 completeResumeLocked(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800815 checkReadyForSleepLocked();
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700816 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 } else {
818 // This activity is not starting in the resumed state... which
819 // should look like we asked it to pause+stop (but remain visible),
820 // and it has done so and reported back the current icicle and
821 // other state.
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700822 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
823 + " (starting in stopped state)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700824 r.state = ActivityState.STOPPED;
825 r.stopped = true;
826 }
827
828 // Launch the new version setup screen if needed. We do this -after-
829 // launching the initial activity (that is, home), so that it can have
830 // a chance to initialize itself while in the background, making the
831 // switch back to it faster and look better.
Craig Mautner2219a1b2013-03-25 09:44:30 -0700832 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700833 mService.startSetupActivityLocked();
834 }
Craig Mautner9658b312013-02-28 10:55:59 -0800835
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 return true;
837 }
838
839 private final void startSpecificActivityLocked(ActivityRecord r,
840 boolean andResume, boolean checkConfig) {
841 // Is this activity's application already running?
842 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
843 r.info.applicationInfo.uid);
Dianne Hackborn07981492013-01-28 11:36:23 -0800844
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700845 if (r.launchTime == 0) {
846 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700848 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 }
850 } else if (mInitialStartTime == 0) {
851 mInitialStartTime = SystemClock.uptimeMillis();
852 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800853
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 if (app != null && app.thread != null) {
855 try {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700856 app.addPackage(r.info.packageName);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 realStartActivityLocked(r, app, andResume, checkConfig);
858 return;
859 } catch (RemoteException e) {
860 Slog.w(TAG, "Exception when starting activity "
861 + r.intent.getComponent().flattenToShortString(), e);
862 }
863
864 // If a dead object exception was thrown -- fall through to
865 // restart the application.
866 }
867
868 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800869 "activity", r.intent.getComponent(), false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800871
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800872 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700873 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700874 if (!mGoingToSleep.isHeld()) {
875 mGoingToSleep.acquire();
876 if (mLaunchingActivity.isHeld()) {
877 mLaunchingActivity.release();
878 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
879 }
880 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800881 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
882 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
883 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
884 checkReadyForSleepLocked();
885 }
886 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800888 void awakeFromSleepingLocked() {
889 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
890 mSleepTimeout = false;
891 if (mGoingToSleep.isHeld()) {
892 mGoingToSleep.release();
893 }
894 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800895 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
896 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
897 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
898 activities.get(activityNdx).setSleeping(false);
899 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800900 }
901 mGoingToSleepActivities.clear();
902 }
903
904 void activitySleptLocked(ActivityRecord r) {
905 mGoingToSleepActivities.remove(r);
906 checkReadyForSleepLocked();
907 }
908
909 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700910 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800911 // Do not care.
912 return;
913 }
914
915 if (!mSleepTimeout) {
916 if (mResumedActivity != null) {
917 // Still have something resumed; can't sleep until it is paused.
918 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
920 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800921 return;
922 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800923 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800924 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800925 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800926 return;
927 }
928
929 if (mStoppingActivities.size() > 0) {
930 // Still need to tell some activities to stop; can't sleep yet.
931 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
932 + mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700933 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800934 return;
935 }
936
937 ensureActivitiesVisibleLocked(null, 0);
938
939 // Make sure any stopped but visible activities are now sleeping.
940 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800941 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
942 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
943 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
944 final ActivityRecord r = activities.get(activityNdx);
945 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
946 r.setSleeping(true);
947 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800948 }
949 }
950
951 if (mGoingToSleepActivities.size() > 0) {
952 // Still need to tell some activities to sleep; can't sleep yet.
953 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
954 + mGoingToSleepActivities.size() + " activities");
955 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700956 }
957 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800958
959 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
960
961 if (mGoingToSleep.isHeld()) {
962 mGoingToSleep.release();
963 }
964 if (mService.mShuttingDown) {
965 mService.notifyAll();
966 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967 }
Craig Mautner59c00972012-07-30 12:10:24 -0700968
Dianne Hackbornd2835932010-12-13 16:28:46 -0800969 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800970 if (who.noDisplay) {
971 return null;
972 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800973
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800974 Resources res = mService.mContext.getResources();
975 int w = mThumbnailWidth;
976 int h = mThumbnailHeight;
977 if (w < 0) {
978 mThumbnailWidth = w =
979 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
980 mThumbnailHeight = h =
981 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
982 }
983
984 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800985 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
986 || mLastScreenshotBitmap.getWidth() != w
987 || mLastScreenshotBitmap.getHeight() != h) {
988 mLastScreenshotActivity = who;
989 mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications(
990 who.appToken, Display.DEFAULT_DISPLAY, w, h);
991 }
992 if (mLastScreenshotBitmap != null) {
993 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
994 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800995 }
996 return null;
997 }
998
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700999 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001000 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -07001002 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001003 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001004 ActivityRecord prev = mResumedActivity;
1005 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -07001006 Slog.e(TAG, "Trying to pause when nothing is resumed",
1007 new RuntimeException("here").fillInStackTrace());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001008 resumeTopActivityLocked(null);
1009 return;
1010 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001011 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
1012 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001013 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001014 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015 mLastPausedActivity = prev;
1016 prev.state = ActivityState.PAUSING;
1017 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001018 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001019
1020 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001021
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001022 if (prev.app != null && prev.app.thread != null) {
1023 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
1024 try {
1025 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001026 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001027 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001028 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
1029 userLeaving, prev.configChangeFlags);
Craig Mautner2219a1b2013-03-25 09:44:30 -07001030 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001031 mService.updateUsageStats(prev, false);
1032 }
1033 } catch (Exception e) {
1034 // Ignore exception, if process died other code will cleanup.
1035 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001036 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 mLastPausedActivity = null;
1038 }
1039 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001040 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 mLastPausedActivity = null;
1042 }
1043
1044 // If we are not going to sleep, we want to ensure the device is
1045 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -07001046 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001047 mLaunchingActivity.acquire();
1048 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1049 // To be safe, don't allow the wake lock to be held for too long.
1050 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
1051 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
1052 }
1053 }
1054
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001055 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001056 // Have the window manager pause its key dispatching until the new
1057 // activity has started. If we're pausing the activity just because
1058 // the screen is being turned off and the UI is sleeping, don't interrupt
1059 // key dispatch; the same activity will pick it up again on wakeup.
1060 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001061 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062 } else {
1063 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
1064 }
1065
1066 // Schedule a pause timeout in case the app doesn't respond.
1067 // We don't give it much time because this directly impacts the
1068 // responsiveness seen by the user.
1069 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1070 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001071 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1073 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
1074 } else {
1075 // This activity failed to schedule the
1076 // pause, so just treat it as being paused now.
1077 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001078 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079 }
1080 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001081
Craig Mautnerd2328952013-03-05 12:46:26 -08001082 final void activityResumedLocked(IBinder token) {
1083 final ActivityRecord r = ActivityRecord.forToken(token);
1084 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
1085 r.icicle = null;
1086 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001087 }
1088
Craig Mautnerd2328952013-03-05 12:46:26 -08001089 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001091 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092
Craig Mautnerd2328952013-03-05 12:46:26 -08001093 final ActivityRecord r = isInStackLocked(token);
1094 if (r != null) {
1095 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1096 if (mPausingActivity == r) {
1097 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
1098 + (timeout ? " (due to timeout)" : " (pause complete)"));
1099 r.state = ActivityState.PAUSED;
1100 completePauseLocked();
1101 } else {
1102 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1103 r.userId, System.identityHashCode(r), r.shortComponentName,
1104 mPausingActivity != null
1105 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001106 }
1107 }
1108 }
1109
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001110 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
1111 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001112 if (r.state != ActivityState.STOPPING) {
1113 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1114 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1115 return;
1116 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001117 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001118 if (icicle != null) {
1119 // If icicle is null, this is happening due to a timeout, so we
1120 // haven't really saved the state.
1121 r.icicle = icicle;
1122 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001123 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001124 r.updateThumbnail(thumbnail, description);
1125 }
1126 if (!r.stopped) {
1127 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
1128 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1129 r.stopped = true;
1130 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001131 if (r.finishing) {
1132 r.clearOptionsLocked();
1133 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001134 if (r.configDestroy) {
1135 destroyActivityLocked(r, true, false, "stop-config");
1136 resumeTopActivityLocked(null);
1137 } else {
1138 // Now that this process has stopped, we may want to consider
1139 // it to be the previous app to try to keep around in case
1140 // the user wants to return to it.
1141 ProcessRecord fgApp = null;
1142 if (mResumedActivity != null) {
1143 fgApp = mResumedActivity.app;
1144 } else if (mPausingActivity != null) {
1145 fgApp = mPausingActivity.app;
1146 }
1147 if (r.app != null && fgApp != null && r.app != fgApp
1148 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
1149 && r.app != mService.mHomeProcess) {
1150 mService.mPreviousProcess = r.app;
1151 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1152 }
Dianne Hackborn50685602011-12-01 12:23:37 -08001153 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001154 }
1155 }
1156 }
1157
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001158 private final void completePauseLocked() {
1159 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001161
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001162 if (prev != null) {
1163 if (prev.finishing) {
1164 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001165 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001166 } else if (prev.app != null) {
1167 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
1168 if (prev.waitingVisible) {
1169 prev.waitingVisible = false;
1170 mWaitingVisibleActivities.remove(prev);
1171 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
1172 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001173 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001174 if (prev.configDestroy) {
1175 // The previous is being paused because the configuration
1176 // is changing, which means it is actually stopping...
1177 // To juggle the fact that we are also starting a new
1178 // instance right now, we need to first completely stop
1179 // the current instance before starting the new one.
1180 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
1181 destroyActivityLocked(prev, true, false, "pause-config");
1182 } else {
1183 mStoppingActivities.add(prev);
1184 if (mStoppingActivities.size() > 3) {
1185 // If we already have a few activities waiting to stop,
1186 // then give up on things going idle and start clearing
1187 // them out.
1188 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
1189 scheduleIdleLocked();
1190 } else {
1191 checkReadyForSleepLocked();
1192 }
1193 }
1194 } else {
1195 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1196 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001197 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001198 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001200
Craig Mautnere11f2b72013-04-01 12:37:17 -07001201 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001202 resumeTopActivityLocked(prev);
1203 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001204 checkReadyForSleepLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001205 ActivityRecord top = topRunningActivityLocked(null);
1206 if (top == null || (prev != null && top != prev)) {
1207 // If there are no more activities available to run,
1208 // do resume anyway to start something. Also if the top
1209 // activity on the stack is not the just paused activity,
1210 // we need to go ahead and resume it to ensure we complete
1211 // an in-flight app switch.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001212 resumeTopActivityLocked(null);
1213 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001214 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001215
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001216 if (prev != null) {
1217 prev.resumeKeyDispatchingLocked();
1218 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219
1220 if (prev.app != null && prev.cpuTimeAtResume > 0
1221 && mService.mBatteryStatsService.isOnBattery()) {
1222 long diff = 0;
1223 synchronized (mService.mProcessStatsThread) {
1224 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1225 - prev.cpuTimeAtResume;
1226 }
1227 if (diff > 0) {
1228 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1229 synchronized (bsi) {
1230 BatteryStatsImpl.Uid.Proc ps =
1231 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1232 prev.info.packageName);
1233 if (ps != null) {
1234 ps.addForegroundTimeLocked(diff);
1235 }
1236 }
1237 }
1238 }
1239 prev.cpuTimeAtResume = 0; // reset it
1240 }
1241
1242 /**
1243 * Once we know that we have asked an application to put an activity in
1244 * the resumed state (either by launching it or explicitly telling it),
1245 * this function updates the rest of our state to match that fact.
1246 */
1247 private final void completeResumeLocked(ActivityRecord next) {
1248 next.idle = false;
1249 next.results = null;
1250 next.newIntents = null;
1251
1252 // schedule an idle timeout in case the app doesn't do it for us.
1253 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1254 msg.obj = next;
1255 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1256
1257 if (false) {
1258 // The activity was never told to pause, so just keep
1259 // things going as-is. To maintain our own state,
1260 // we need to emulate it coming back and saying it is
1261 // idle.
1262 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1263 msg.obj = next;
1264 mHandler.sendMessage(msg);
1265 }
1266
Craig Mautner2219a1b2013-03-25 09:44:30 -07001267 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001268 mService.reportResumedActivityLocked(next);
1269 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07001270
Craig Mautner2219a1b2013-03-25 09:44:30 -07001271 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272 mService.setFocusedActivityLocked(next);
1273 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001274 next.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275 ensureActivitiesVisibleLocked(null, 0);
1276 mService.mWindowManager.executeAppTransition();
1277 mNoAnimActivities.clear();
1278
1279 // Mark the point when the activity is resuming
1280 // TODO: To be more accurate, the mark should be before the onCreate,
1281 // not after the onResume. But for subsequent starts, onResume is fine.
1282 if (next.app != null) {
1283 synchronized (mService.mProcessStatsThread) {
1284 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1285 }
1286 } else {
1287 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1288 }
1289 }
1290
1291 /**
1292 * Make sure that all activities that need to be visible (that is, they
1293 * currently can be seen by the user) actually are.
1294 */
1295 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1296 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1297 if (DEBUG_VISBILITY) Slog.v(
1298 TAG, "ensureActivitiesVisible behind " + top
1299 + " configChanges=0x" + Integer.toHexString(configChanges));
1300
1301 // If the top activity is not fullscreen, then we need to
1302 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001303 boolean aboveTop = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001304 boolean behindFullscreen = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001305 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1306 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1307 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1308 final ActivityRecord r = activities.get(activityNdx);
1309 if (r.finishing) {
1310 continue;
1311 }
1312 if (aboveTop && r != top) {
1313 continue;
1314 }
1315 aboveTop = false;
1316 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001318 TAG, "Make visible? " + r + " finishing=" + r.finishing
1319 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001320
Craig Mautnerd44711d2013-02-23 11:24:36 -08001321 final boolean doThisProcess = onlyThisProcess == null
1322 || onlyThisProcess.equals(r.processName);
1323
1324 // First: if this is not the current activity being started, make
1325 // sure it matches the current configuration.
1326 if (r != starting && doThisProcess) {
1327 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001328 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001329
1330 if (r.app == null || r.app.thread == null) {
1331 if (onlyThisProcess == null
1332 || onlyThisProcess.equals(r.processName)) {
1333 // This activity needs to be visible, but isn't even
1334 // running... get it started, but don't resume it
1335 // at this point.
1336 if (DEBUG_VISBILITY) Slog.v(
1337 TAG, "Start and freeze screen for " + r);
1338 if (r != starting) {
1339 r.startFreezingScreenLocked(r.app, configChanges);
1340 }
1341 if (!r.visible) {
1342 if (DEBUG_VISBILITY) Slog.v(
1343 TAG, "Starting and making visible: " + r);
1344 mService.mWindowManager.setAppVisibility(r.appToken, true);
1345 }
1346 if (r != starting) {
1347 startSpecificActivityLocked(r, false, false);
1348 }
1349 }
1350
1351 } else if (r.visible) {
1352 // If this activity is already visible, then there is nothing
1353 // else to do here.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001354 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001355 TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001356 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001357
1358 } else if (onlyThisProcess == null) {
1359 // This activity is not currently visible, but is running.
1360 // Tell it to become visible.
1361 r.visible = true;
1362 if (r.state != ActivityState.RESUMED && r != starting) {
1363 // If this activity is paused, tell it
1364 // to now show its window.
1365 if (DEBUG_VISBILITY) Slog.v(
1366 TAG, "Making visible and scheduling visibility: " + r);
1367 try {
1368 mService.mWindowManager.setAppVisibility(r.appToken, true);
1369 r.sleeping = false;
1370 r.app.pendingUiClean = true;
1371 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1372 r.stopFreezingScreenLocked(false);
1373 } catch (Exception e) {
1374 // Just skip on any failure; we'll make it
1375 // visible when it next restarts.
1376 Slog.w(TAG, "Exception thrown making visibile: "
1377 + r.intent.getComponent(), e);
1378 }
1379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381
Craig Mautnerd44711d2013-02-23 11:24:36 -08001382 // Aggregate current change flags.
1383 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001384
Craig Mautnerd44711d2013-02-23 11:24:36 -08001385 if (r.fullscreen) {
1386 // At this point, nothing else needs to be shown
1387 if (DEBUG_VISBILITY) Slog.v(
1388 TAG, "Stopping: fullscreen at " + r);
1389 behindFullscreen = true;
1390 }
1391 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001392 if (r.visible) {
1393 if (DEBUG_VISBILITY) Slog.v(
1394 TAG, "Making invisible: " + r);
1395 r.visible = false;
1396 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001397 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 if ((r.state == ActivityState.STOPPING
1399 || r.state == ActivityState.STOPPED)
1400 && r.app != null && r.app.thread != null) {
1401 if (DEBUG_VISBILITY) Slog.v(
1402 TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001403 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 }
1405 } catch (Exception e) {
1406 // Just skip on any failure; we'll make it
1407 // visible when it next restarts.
1408 Slog.w(TAG, "Exception thrown making hidden: "
1409 + r.intent.getComponent(), e);
1410 }
1411 } else {
1412 if (DEBUG_VISBILITY) Slog.v(
1413 TAG, "Already invisible: " + r);
1414 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001415 }
1416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417 }
1418 }
1419
1420 /**
1421 * Version of ensureActivitiesVisible that can easily be called anywhere.
1422 */
1423 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1424 int configChanges) {
1425 ActivityRecord r = topRunningActivityLocked(null);
1426 if (r != null) {
1427 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1428 }
1429 }
Craig Mautner58547802013-03-05 08:23:53 -08001430
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 /**
1432 * Ensure that the top activity in the stack is resumed.
1433 *
1434 * @param prev The previously resumed activity, for when in the process
1435 * of pausing; can be null to call from elsewhere.
1436 *
1437 * @return Returns true if something is being resumed, or false if
1438 * nothing happened.
1439 */
1440 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001441 return resumeTopActivityLocked(prev, null);
1442 }
1443
1444 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445 // Find the first activity that is not finishing.
1446 ActivityRecord next = topRunningActivityLocked(null);
1447
1448 // Remember how we'll process this pause/resume situation, and ensure
1449 // that the state is reset however we wind up proceeding.
1450 final boolean userLeaving = mUserLeaving;
1451 mUserLeaving = false;
1452
1453 if (next == null) {
1454 // There are no more activities! Let's just start up the
1455 // Launcher...
Craig Mautner2219a1b2013-03-25 09:44:30 -07001456 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001457 ActivityOptions.abort(options);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001458 return mService.startHomeActivityLocked(mCurrentUser);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 }
1460 }
1461
1462 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001463
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 // If the top activity is the resumed one, nothing to do.
1465 if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
1466 // Make sure we have executed any pending transitions, since there
1467 // should be nothing left to do at this point.
1468 mService.mWindowManager.executeAppTransition();
1469 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001470 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 return false;
1472 }
1473
1474 // If we are sleeping, and there is no resumed activity, and the top
1475 // activity is paused, well that is the state we want.
1476 if ((mService.mSleeping || mService.mShuttingDown)
p13451dbad2872012-04-18 11:39:23 +09001477 && mLastPausedActivity == next
1478 && (next.state == ActivityState.PAUSED
1479 || next.state == ActivityState.STOPPED
1480 || next.state == ActivityState.STOPPING)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 // Make sure we have executed any pending transitions, since there
1482 // should be nothing left to do at this point.
1483 mService.mWindowManager.executeAppTransition();
1484 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001485 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 return false;
1487 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001488
1489 // Make sure that the user who owns this activity is started. If not,
1490 // we will just leave it as is because someone should be bringing
1491 // another user's activities to the top of the stack.
1492 if (mService.mStartedUsers.get(next.userId) == null) {
1493 Slog.w(TAG, "Skipping resume of top activity " + next
1494 + ": user " + next.userId + " is stopped");
1495 return false;
1496 }
1497
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001498 // The activity may be waiting for stop, but that is no longer
1499 // appropriate for it.
1500 mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001501 mGoingToSleepActivities.remove(next);
1502 next.sleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 mWaitingVisibleActivities.remove(next);
1504
Dianne Hackborn84375872012-06-01 19:03:50 -07001505 next.updateOptionsLocked(options);
1506
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1508
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001509 // If we are currently pausing an activity, then don't do anything
1510 // until that is done.
1511 if (mPausingActivity != null) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001512 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG,
1513 "Skip resume: pausing=" + mPausingActivity);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001514 return false;
1515 }
1516
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001517 // Okay we are now going to start a switch, to 'next'. We may first
1518 // have to pause the current activity, but this is an important point
1519 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001520 // XXX "App Redirected" dialog is getting too many false positives
1521 // at this point, so turn off for now.
1522 if (false) {
1523 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1524 long now = SystemClock.uptimeMillis();
1525 final boolean inTime = mLastStartedActivity.startTime != 0
1526 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1527 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1528 final int nextUid = next.info.applicationInfo.uid;
1529 if (inTime && lastUid != nextUid
1530 && lastUid != next.launchedFromUid
1531 && mService.checkPermission(
1532 android.Manifest.permission.STOP_APP_SWITCHES,
1533 -1, next.launchedFromUid)
1534 != PackageManager.PERMISSION_GRANTED) {
1535 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1536 } else {
1537 next.startTime = now;
1538 mLastStartedActivity = next;
1539 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001540 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001541 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001542 mLastStartedActivity = next;
1543 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001544 }
Craig Mautner58547802013-03-05 08:23:53 -08001545
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 // We need to start pausing the current activity so the top one
1547 // can be resumed...
1548 if (mResumedActivity != null) {
1549 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001550 // At this point we want to put the upcoming activity's process
1551 // at the top of the LRU list, since we know we will be needing it
1552 // very soon and it would be a waste to let it get killed if it
1553 // happens to be sitting towards the end.
1554 if (next.app != null && next.app.thread != null) {
1555 // No reason to do full oom adj update here; we'll let that
1556 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001557 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001558 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 startPausingLocked(userLeaving, false);
1560 return true;
1561 }
1562
Christopher Tated3f175c2012-06-14 14:16:54 -07001563 // If the most recent activity was noHistory but was only stopped rather
1564 // than stopped+finished because the device went to sleep, we need to make
1565 // sure to finish it as we're making a new activity topmost.
1566 final ActivityRecord last = mLastPausedActivity;
1567 if (mService.mSleeping && last != null && !last.finishing) {
1568 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1569 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1570 if (DEBUG_STATES) {
1571 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1572 }
1573 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001574 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001575 }
1576 }
1577
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 if (prev != null && prev != next) {
1579 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1580 prev.waitingVisible = true;
1581 mWaitingVisibleActivities.add(prev);
1582 if (DEBUG_SWITCH) Slog.v(
1583 TAG, "Resuming top, waiting visible to hide: " + prev);
1584 } else {
1585 // The next activity is already visible, so hide the previous
1586 // activity's windows right now so we can show the new one ASAP.
1587 // We only do this if the previous is finishing, which should mean
1588 // it is on top of the one being resumed so hiding it quickly
1589 // is good. Otherwise, we want to do the normal route of allowing
1590 // the resumed activity to be shown so we can decide if the
1591 // previous should actually be hidden depending on whether the
1592 // new one is found to be full-screen or not.
1593 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001594 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1596 + prev + ", waitingVisible="
1597 + (prev != null ? prev.waitingVisible : null)
1598 + ", nowVisible=" + next.nowVisible);
1599 } else {
1600 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1601 + prev + ", waitingVisible="
1602 + (prev != null ? prev.waitingVisible : null)
1603 + ", nowVisible=" + next.nowVisible);
1604 }
1605 }
1606 }
1607
Dianne Hackborne7f97212011-02-24 14:40:20 -08001608 // Launching this app's activity, make sure the app is no longer
1609 // considered stopped.
1610 try {
1611 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001612 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001613 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001614 } catch (IllegalArgumentException e) {
1615 Slog.w(TAG, "Failed trying to unstop package "
1616 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001617 }
1618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 // We are starting up the next activity, so tell the window manager
1620 // that the previous one will be hidden soon. This way it can know
1621 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001622 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 if (prev != null) {
1624 if (prev.finishing) {
1625 if (DEBUG_TRANSITION) Slog.v(TAG,
1626 "Prepare close transition: prev=" + prev);
1627 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001628 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001629 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 } else {
1631 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001632 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1633 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001635 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1636 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 } else {
1638 if (DEBUG_TRANSITION) Slog.v(TAG,
1639 "Prepare open transition: prev=" + prev);
1640 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001641 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001642 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001643 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 } else {
1645 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001646 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1647 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001648 }
1649 }
1650 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001651 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1652 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 }
Craig Mautner000f0022013-02-26 15:04:29 -08001654 } else if (numActivities() > 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 if (DEBUG_TRANSITION) Slog.v(TAG,
1656 "Prepare open transition: no previous");
1657 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001658 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001659 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001660 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001662 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001663 AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001664 }
1665 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001666 if (!noAnim) {
1667 next.applyOptionsLocked();
1668 } else {
1669 next.clearOptionsLocked();
1670 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671
1672 if (next.app != null && next.app.thread != null) {
1673 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1674
1675 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001676 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001678 // schedule launch ticks to collect information about slow apps.
1679 next.startLaunchTickingLocked();
1680
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001681 ActivityRecord lastResumedActivity = mResumedActivity;
1682 ActivityState lastState = next.state;
1683
1684 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001685
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001686 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 next.state = ActivityState.RESUMED;
1688 mResumedActivity = next;
1689 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001690 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001691 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 updateLRUListLocked(next);
1693
1694 // Have the window manager re-evaluate the orientation of
1695 // the screen based on the new activity order.
1696 boolean updated = false;
Craig Mautner2219a1b2013-03-25 09:44:30 -07001697 if (mStackSupervisor.isMainStack(this)) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001698 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1699 mService.mConfiguration,
1700 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1701 if (config != null) {
1702 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001704 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 if (!updated) {
1708 // The configuration update wasn't able to keep the existing
1709 // instance of the activity, and instead started a new one.
1710 // We should be all done, but let's just make sure our activity
1711 // is still at the top and schedule another run if something
1712 // weird happened.
1713 ActivityRecord nextNext = topRunningActivityLocked(null);
1714 if (DEBUG_SWITCH) Slog.i(TAG,
1715 "Activity config changed during resume: " + next
1716 + ", new next: " + nextNext);
1717 if (nextNext != next) {
1718 // Do over!
1719 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1720 }
Craig Mautner2219a1b2013-03-25 09:44:30 -07001721 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 mService.setFocusedActivityLocked(next);
1723 }
1724 ensureActivitiesVisibleLocked(null, 0);
1725 mService.mWindowManager.executeAppTransition();
1726 mNoAnimActivities.clear();
1727 return true;
1728 }
Craig Mautner58547802013-03-05 08:23:53 -08001729
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 try {
1731 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001732 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 if (a != null) {
1734 final int N = a.size();
1735 if (!next.finishing && N > 0) {
1736 if (DEBUG_RESULTS) Slog.v(
1737 TAG, "Delivering results to " + next
1738 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001739 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 }
1741 }
1742
1743 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001744 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745 }
1746
1747 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001748 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001750
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001751 next.sleeping = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001752 showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001753 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001754 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001756
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001757 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758
1759 } catch (Exception e) {
1760 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001761 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1762 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001763 next.state = lastState;
1764 mResumedActivity = lastResumedActivity;
1765 Slog.i(TAG, "Restarting because process died: " + next);
1766 if (!next.hasBeenLaunched) {
1767 next.hasBeenLaunched = true;
1768 } else {
Craig Mautner2219a1b2013-03-25 09:44:30 -07001769 if (SHOW_APP_STARTING_PREVIEW && mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001771 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001772 mService.compatibilityInfoForPackageLocked(
1773 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001775 next.labelRes, next.icon, next.windowFlags,
1776 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 }
1778 }
1779 startSpecificActivityLocked(next, true, false);
1780 return true;
1781 }
1782
1783 // From this point on, if something goes wrong there is no way
1784 // to recover the activity.
1785 try {
1786 next.visible = true;
1787 completeResumeLocked(next);
1788 } catch (Exception e) {
1789 // If any exception gets thrown, toss away this
1790 // activity and try the next one.
1791 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001792 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001793 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 return true;
1795 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001796 next.stopped = false;
1797
1798 } else {
1799 // Whoops, need to restart this activity!
1800 if (!next.hasBeenLaunched) {
1801 next.hasBeenLaunched = true;
1802 } else {
1803 if (SHOW_APP_STARTING_PREVIEW) {
1804 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001805 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001806 mService.compatibilityInfoForPackageLocked(
1807 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001809 next.labelRes, next.icon, next.windowFlags,
1810 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811 }
1812 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1813 }
1814 startSpecificActivityLocked(next, true, true);
1815 }
1816
1817 return true;
1818 }
1819
Craig Mautner11bf9a52013-02-19 14:08:51 -08001820
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821 private final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001822 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001823 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001824 TaskRecord rTask = r.task;
1825 final int taskId = rTask.taskId;
1826 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001827 // Last activity in task had been removed or ActivityManagerService is reusing task.
1828 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001829 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001830 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001831 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001832 mTaskHistory.add(rTask);
1833 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001834 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 if (!newTask) {
1836 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001837 boolean startIt = true;
1838 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1839 task = mTaskHistory.get(taskNdx);
1840 if (task == r.task) {
1841 // Here it is! Now, if this is not yet visible to the
1842 // user, then just add it without starting; it will
1843 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001844 if (!startIt) {
1845 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1846 + task, new RuntimeException("here").fillInStackTrace());
1847 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001848 r.putInHistory();
Craig Mautner70a86932013-02-28 22:37:44 -08001849 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001850 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation,
1851 r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001852 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001853 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001854 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001855 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001856 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 return;
1858 }
1859 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001860 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861 startIt = false;
1862 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001863 }
1864 }
1865
1866 // Place a new activity at top of stack, so it is next to interact
1867 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001868
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 // If we are not placing the new activity frontmost, we do not want
1870 // to deliver the onUserLeaving callback to the actual frontmost
1871 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001872 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001873 mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001874 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1875 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001876 }
Craig Mautner70a86932013-02-28 22:37:44 -08001877
1878 task = r.task;
1879
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001881 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001882 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001883 task.addActivityToTop(r);
1884
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001885 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001886 r.frontOfTask = newTask;
Craig Mautner70a86932013-02-28 22:37:44 -08001887 if (numActivities() > 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001888 // We want to show the starting preview window if we are
1889 // switching to a new task, or the next activity's process is
1890 // not currently running.
1891 boolean showStartingIcon = newTask;
1892 ProcessRecord proc = r.app;
1893 if (proc == null) {
1894 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1895 }
1896 if (proc == null || proc.thread == null) {
1897 showStartingIcon = true;
1898 }
1899 if (DEBUG_TRANSITION) Slog.v(TAG,
1900 "Prepare open transition: starting " + r);
1901 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001902 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001903 AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001904 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 } else {
1906 mService.mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001907 ? AppTransition.TRANSIT_TASK_OPEN
1908 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 mNoAnimActivities.remove(r);
1910 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001911 r.updateOptionsLocked(options);
Craig Mautner70a86932013-02-28 22:37:44 -08001912 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001913 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001914 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915 boolean doShow = true;
1916 if (newTask) {
1917 // Even though this activity is starting fresh, we still need
1918 // to reset it to make sure we apply affinities to move any
1919 // existing activities from other tasks in to it.
1920 // If the caller has requested that the target task be
1921 // reset, then do so.
1922 if ((r.intent.getFlags()
1923 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1924 resetTaskIfNeededLocked(r, r);
1925 doShow = topRunningNonDelayedActivityLocked(null) == r;
1926 }
1927 }
1928 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1929 // Figure out if we are transitioning from another activity that is
1930 // "has the same starting icon" as the next one. This allows the
1931 // window manager to keep the previous window it had previously
1932 // created, if it still had one.
1933 ActivityRecord prev = mResumedActivity;
1934 if (prev != null) {
1935 // We don't want to reuse the previous starting preview if:
1936 // (1) The current activity is in a different task.
1937 if (prev.task != r.task) prev = null;
1938 // (2) The current activity is already displayed.
1939 else if (prev.nowVisible) prev = null;
1940 }
1941 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001942 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001943 mService.compatibilityInfoForPackageLocked(
1944 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001945 r.labelRes, r.icon, r.windowFlags,
1946 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947 }
1948 } else {
1949 // If this is the first activity, don't do any fancy animations,
1950 // because there is nothing for it to animate on top of.
Craig Mautner70a86932013-02-28 22:37:44 -08001951 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001952 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001953 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001954 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 }
1956 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001957 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 }
1959
1960 if (doResume) {
1961 resumeTopActivityLocked(null);
1962 }
1963 }
1964
Dianne Hackbornbe707852011-11-11 14:32:10 -08001965 final void validateAppTokensLocked() {
1966 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001967 mValidateAppTokens.ensureCapacity(numActivities());
1968 final int numTasks = mTaskHistory.size();
1969 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1970 TaskRecord task = mTaskHistory.get(taskNdx);
1971 final ArrayList<ActivityRecord> activities = task.mActivities;
1972 if (activities.size() == 0) {
1973 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001974 }
Craig Mautner000f0022013-02-26 15:04:29 -08001975 TaskGroup group = new TaskGroup();
1976 group.taskId = task.taskId;
1977 mValidateAppTokens.add(group);
1978 final int numActivities = activities.size();
1979 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1980 final ActivityRecord r = activities.get(activityNdx);
1981 group.tokens.add(r.appToken);
1982 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001983 }
Craig Mautner00af9fe2013-03-25 09:13:41 -07001984 mService.mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001985 }
1986
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001987 /**
1988 * Perform a reset of the given task, if needed as part of launching it.
1989 * Returns the new HistoryRecord at the top of the task.
1990 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001991 /**
1992 * Helper method for #resetTaskIfNeededLocked.
1993 * We are inside of the task being reset... we'll either finish this activity, push it out
1994 * for another task, or leave it as-is.
1995 * @param task The task containing the Activity (taskTop) that might be reset.
1996 * @param forceReset
1997 * @return An ActivityOptions that needs to be processed.
1998 */
1999 private final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task,
2000 boolean forceReset) {
2001 ActivityOptions topOptions = null;
2002
2003 int replyChainEnd = -1;
2004 boolean canMoveOptions = true;
2005
2006 // We only do this for activities that are not the root of the task (since if we finish
2007 // the root, we may no longer have the task!).
2008 final ArrayList<ActivityRecord> activities = task.mActivities;
2009 final int numActivities = activities.size();
2010 for (int i = numActivities - 1; i > 0; --i ) {
2011 ActivityRecord target = activities.get(i);
2012
2013 final int flags = target.info.flags;
2014 final boolean finishOnTaskLaunch =
2015 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2016 final boolean allowTaskReparenting =
2017 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2018 final boolean clearWhenTaskReset =
2019 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2020
2021 if (!finishOnTaskLaunch
2022 && !clearWhenTaskReset
2023 && target.resultTo != null) {
2024 // If this activity is sending a reply to a previous
2025 // activity, we can't do anything with it now until
2026 // we reach the start of the reply chain.
2027 // XXX note that we are assuming the result is always
2028 // to the previous activity, which is almost always
2029 // the case but we really shouldn't count on.
2030 if (replyChainEnd < 0) {
2031 replyChainEnd = i;
2032 }
2033 } else if (!finishOnTaskLaunch
2034 && !clearWhenTaskReset
2035 && allowTaskReparenting
2036 && target.taskAffinity != null
2037 && !target.taskAffinity.equals(task.affinity)) {
2038 // If this activity has an affinity for another
2039 // task, then we need to move it out of here. We will
2040 // move it as far out of the way as possible, to the
2041 // bottom of the activity stack. This also keeps it
2042 // correctly ordered with any activities we previously
2043 // moved.
2044 TaskRecord bottomTask = mTaskHistory.get(0);
2045 ActivityRecord p = bottomTask.mActivities.get(0);
2046 if (target.taskAffinity != null
2047 && target.taskAffinity.equals(p.task.affinity)) {
2048 // If the activity currently at the bottom has the
2049 // same task affinity as the one we are moving,
2050 // then merge it into the same task.
Craig Mautner0247fc82013-02-28 14:32:06 -08002051 setTask(target, p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002052 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2053 + " out to bottom task " + p.task);
2054 } else {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002055 setTask(target, createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
2056 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002057 target.task.affinityIntent = target.intent;
2058 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2059 + " out to new task " + target.task);
2060 }
2061
2062 final TaskRecord targetTask = target.task;
2063 final int targetTaskId = targetTask.taskId;
2064 mService.mWindowManager.setAppGroupId(target.appToken, targetTaskId);
2065
2066 ThumbnailHolder curThumbHolder = target.thumbHolder;
2067 boolean gotOptions = !canMoveOptions;
2068
2069 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2070 for (int srcPos = start; srcPos >= i; --srcPos) {
2071 p = activities.get(srcPos);
2072 if (p.finishing) {
2073 continue;
2074 }
2075
2076 curThumbHolder = p.thumbHolder;
2077 canMoveOptions = false;
2078 if (!gotOptions && topOptions == null) {
2079 topOptions = p.takeOptionsLocked();
2080 if (topOptions != null) {
2081 gotOptions = true;
2082 }
2083 }
2084 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
2085 + task + " adding to task=" + targetTask,
2086 new RuntimeException("here").fillInStackTrace());
2087 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2088 + " out to target's task " + target.task);
Craig Mautner0247fc82013-02-28 14:32:06 -08002089 setTask(p, targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002090 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002091
Craig Mautnere3a74d52013-02-22 14:14:58 -08002092 mService.mWindowManager.setAppGroupId(p.appToken, targetTaskId);
2093 }
2094
2095 mService.mWindowManager.moveTaskToBottom(targetTaskId);
2096 if (VALIDATE_TOKENS) {
2097 validateAppTokensLocked();
2098 }
2099
2100 replyChainEnd = -1;
2101 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2102 // If the activity should just be removed -- either
2103 // because it asks for it, or the task should be
2104 // cleared -- then finish it and anything that is
2105 // part of its reply chain.
2106 int end;
2107 if (clearWhenTaskReset) {
2108 // In this case, we want to finish this activity
2109 // and everything above it, so be sneaky and pretend
2110 // like these are all in the reply chain.
2111 end = numActivities - 1;
2112 } else if (replyChainEnd < 0) {
2113 end = i;
2114 } else {
2115 end = replyChainEnd;
2116 }
2117 ActivityRecord p = null;
2118 boolean gotOptions = !canMoveOptions;
2119 for (int srcPos = i; srcPos <= end; srcPos++) {
2120 p = activities.get(srcPos);
2121 if (p.finishing) {
2122 continue;
2123 }
2124 canMoveOptions = false;
2125 if (!gotOptions && topOptions == null) {
2126 topOptions = p.takeOptionsLocked();
2127 if (topOptions != null) {
2128 gotOptions = true;
2129 }
2130 }
Craig Mautner58547802013-03-05 08:23:53 -08002131 if (DEBUG_TASKS) Slog.w(TAG,
2132 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002133 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002134 end--;
2135 srcPos--;
2136 }
2137 }
2138 replyChainEnd = -1;
2139 } else {
2140 // If we were in the middle of a chain, well the
2141 // activity that started it all doesn't want anything
2142 // special, so leave it all as-is.
2143 replyChainEnd = -1;
2144 }
2145 }
2146
2147 return topOptions;
2148 }
2149
2150 /**
2151 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2152 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2153 * @param affinityTask The task we are looking for an affinity to.
2154 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2155 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2156 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2157 */
Craig Mautner77878772013-03-04 19:46:24 -08002158 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
2159 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002160 int replyChainEnd = -1;
2161 final int taskId = task.taskId;
2162 final String taskAffinity = task.affinity;
2163
2164 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2165 final int numActivities = activities.size();
2166 // Do not operate on the root Activity.
2167 for (int i = numActivities - 1; i > 0; --i) {
2168 ActivityRecord target = activities.get(i);
2169
2170 final int flags = target.info.flags;
2171 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2172 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2173
2174 if (target.resultTo != null) {
2175 // If this activity is sending a reply to a previous
2176 // activity, we can't do anything with it now until
2177 // we reach the start of the reply chain.
2178 // XXX note that we are assuming the result is always
2179 // to the previous activity, which is almost always
2180 // the case but we really shouldn't count on.
2181 if (replyChainEnd < 0) {
2182 replyChainEnd = i;
2183 }
2184 } else if (topTaskIsHigher
2185 && allowTaskReparenting
2186 && taskAffinity != null
2187 && taskAffinity.equals(target.taskAffinity)) {
2188 // This activity has an affinity for our task. Either remove it if we are
2189 // clearing or move it over to our task. Note that
2190 // we currently punt on the case where we are resetting a
2191 // task that is not at the top but who has activities above
2192 // with an affinity to it... this is really not a normal
2193 // case, and we will need to later pull that task to the front
2194 // and usually at that point we will do the reset and pick
2195 // up those remaining activities. (This only happens if
2196 // someone starts an activity in a new task from an activity
2197 // in a task that is not currently on top.)
2198 if (forceReset || finishOnTaskLaunch) {
2199 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2200 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2201 for (int srcPos = start; srcPos >= i; --srcPos) {
2202 final ActivityRecord p = activities.get(srcPos);
2203 if (p.finishing) {
2204 continue;
2205 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002206 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002207 }
2208 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002209 if (taskInsertionPoint < 0) {
2210 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002211
Craig Mautner77878772013-03-04 19:46:24 -08002212 }
Craig Mautner77878772013-03-04 19:46:24 -08002213
2214 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2215 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2216 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2217 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002218 final ActivityRecord p = activities.get(srcPos);
Craig Mautner0247fc82013-02-28 14:32:06 -08002219 setTask(p, task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002220 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002221
Craig Mautnere3a74d52013-02-22 14:14:58 -08002222 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2223 + " to stack at " + task,
2224 new RuntimeException("here").fillInStackTrace());
2225 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2226 + " in to resetting task " + task);
2227 mService.mWindowManager.setAppGroupId(p.appToken, taskId);
2228 }
2229 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002230 if (VALIDATE_TOKENS) {
2231 validateAppTokensLocked();
2232 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002233
2234 // Now we've moved it in to place... but what if this is
2235 // a singleTop activity and we have put it on top of another
2236 // instance of the same activity? Then we drop the instance
2237 // below so it remains singleTop.
2238 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2239 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2240 boolean found = false;
2241 int targetNdx = taskActivities.indexOf(target);
2242 if (targetNdx > 0) {
2243 ActivityRecord p = taskActivities.get(targetNdx - 1);
2244 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002245 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2246 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002247 }
2248 }
2249 }
2250 }
2251
2252 replyChainEnd = -1;
2253 }
2254 }
Craig Mautner77878772013-03-04 19:46:24 -08002255 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002256 }
2257
2258 private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
2259 ActivityRecord newActivity) {
2260 boolean forceReset =
2261 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2262 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2263 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2264 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2265 forceReset = true;
2266 }
2267 }
2268
2269 final TaskRecord task = taskTop.task;
2270
2271 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2272 * for remaining tasks. Used for later tasks to reparent to task. */
2273 boolean taskFound = false;
2274
2275 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2276 ActivityOptions topOptions = null;
2277
Craig Mautner77878772013-03-04 19:46:24 -08002278 // Preserve the location for reparenting in the new task.
2279 int reparentInsertionPoint = -1;
2280
Craig Mautnere3a74d52013-02-22 14:14:58 -08002281 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2282 final TaskRecord targetTask = mTaskHistory.get(i);
2283
2284 if (targetTask == task) {
2285 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2286 taskFound = true;
2287 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002288 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2289 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002290 }
2291 }
2292
Craig Mautner70a86932013-02-28 22:37:44 -08002293 int taskNdx = mTaskHistory.indexOf(task);
2294 do {
2295 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2296 } while (taskTop == null && taskNdx >= 0);
2297
Craig Mautnere3a74d52013-02-22 14:14:58 -08002298 if (topOptions != null) {
2299 // If we got some ActivityOptions from an activity on top that
2300 // was removed from the task, propagate them to the new real top.
2301 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002302 taskTop.updateOptionsLocked(topOptions);
2303 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002304 topOptions.abort();
2305 }
2306 }
2307
2308 return taskTop;
2309 }
2310
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002311 /**
2312 * Perform clear operation as requested by
2313 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
2314 * stack to the given task, then look for
2315 * an instance of that activity in the stack and, if found, finish all
2316 * activities on top of it and return the instance.
2317 *
2318 * @param newR Description of the new activity being started.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002319 * @return Returns the old activity that should be continued to be used,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 * or null if none was found.
2321 */
Craig Mautner16030772013-02-26 16:05:47 -08002322 private final ActivityRecord performClearTaskLocked(TaskRecord task,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002323 ActivityRecord newR, int launchFlags) {
Craig Mautner16030772013-02-26 16:05:47 -08002324
2325 final ArrayList<ActivityRecord> activities = task.mActivities;
2326 int numActivities = activities.size();
2327 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
2328 ActivityRecord r = activities.get(activityNdx);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 if (r.finishing) {
2330 continue;
2331 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002332 if (r.realActivity.equals(newR.realActivity)) {
2333 // Here it is! Now finish everything in front...
2334 ActivityRecord ret = r;
Craig Mautner16030772013-02-26 16:05:47 -08002335
2336 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
2337 r = activities.get(activityNdx);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002338 if (r.finishing) {
2339 continue;
2340 }
Dianne Hackborn9622ca42012-10-23 18:56:33 -07002341 ActivityOptions opts = r.takeOptionsLocked();
2342 if (opts != null) {
2343 ret.updateOptionsLocked(opts);
2344 }
Craig Mautner16030772013-02-26 16:05:47 -08002345 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear", false)) {
Craig Mautner9658b312013-02-28 10:55:59 -08002346 --activityNdx;
Craig Mautner16030772013-02-26 16:05:47 -08002347 --numActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 }
2349 }
Craig Mautner16030772013-02-26 16:05:47 -08002350
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 // Finally, if this is a normal launch mode (that is, not
2352 // expecting onNewIntent()), then we will finish the current
2353 // instance of the activity so a new fresh one can be started.
2354 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Craig Mautner16030772013-02-26 16:05:47 -08002355 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 if (!ret.finishing) {
Craig Mautner16030772013-02-26 16:05:47 -08002357 if (activities.contains(ret)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002358 finishActivityLocked(ret, Activity.RESULT_CANCELED, null,
2359 "clear", false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 }
2361 return null;
2362 }
2363 }
Craig Mautner16030772013-02-26 16:05:47 -08002364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 return ret;
2366 }
2367 }
2368
2369 return null;
2370 }
2371
2372 /**
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002373 * Completely remove all activities associated with an existing
2374 * task starting at a specified index.
2375 */
Craig Mautner16030772013-02-26 16:05:47 -08002376 private final void performClearTaskAtIndexLocked(TaskRecord task, int activityNdx) {
2377 final ArrayList<ActivityRecord> activities = task.mActivities;
2378 int numActivities = activities.size();
2379 for ( ; activityNdx < numActivities; ++activityNdx) {
2380 final ActivityRecord r = activities.get(activityNdx);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002381 if (r.finishing) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002382 continue;
2383 }
Craig Mautner16030772013-02-26 16:05:47 -08002384 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear", false)) {
2385 --activityNdx;
2386 --numActivities;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002387 }
2388 }
2389 }
2390
2391 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002392 * Completely remove all activities associated with an existing task.
2393 */
Craig Mautner16030772013-02-26 16:05:47 -08002394 private final void performClearTaskLocked(TaskRecord task) {
2395 performClearTaskAtIndexLocked(task, 0);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002396 }
2397
2398 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 * Find the activity in the history stack within the given task. Returns
2400 * the index within the history at which it's found, or < 0 if not found.
2401 */
Craig Mautner56f52db2013-02-25 10:03:01 -08002402 private final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
2403 final ComponentName realActivity = r.realActivity;
2404 ArrayList<ActivityRecord> activities = task.mActivities;
2405 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2406 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002407 if (candidate.finishing) {
2408 continue;
2409 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002410 if (candidate.realActivity.equals(realActivity)) {
2411 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002412 }
2413 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002414 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002415 }
2416
2417 /**
2418 * Reorder the history stack so that the activity at the given index is
2419 * brought to the front.
2420 */
Craig Mautner56f52db2013-02-25 10:03:01 -08002421 private final void moveActivityToFrontLocked(ActivityRecord newTop) {
2422 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop
2423 + " to stack at top", new RuntimeException("here").fillInStackTrace());
2424
2425 final TaskRecord task = newTop.task;
2426 task.getTopActivity().frontOfTask = false;
2427 task.mActivities.remove(newTop);
2428 task.mActivities.add(newTop);
2429 newTop.frontOfTask = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002430 }
2431
2432 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002433 Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 String resultWho, int requestCode,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08002435 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002436 boolean componentSpecified, ActivityRecord[] outActivity) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07002437 int err = ActivityManager.START_SUCCESS;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002438
2439 ProcessRecord callerApp = null;
2440 if (caller != null) {
2441 callerApp = mService.getRecordForAppLocked(caller);
2442 if (callerApp != null) {
2443 callingPid = callerApp.pid;
2444 callingUid = callerApp.info.uid;
2445 } else {
2446 Slog.w(TAG, "Unable to find app for caller " + caller
2447 + " (pid=" + callingPid + ") when starting: "
2448 + intent.toString());
Dianne Hackborna4972e92012-03-14 10:38:05 -07002449 err = ActivityManager.START_PERMISSION_DENIED;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002450 }
2451 }
2452
Dianne Hackborna4972e92012-03-14 10:38:05 -07002453 if (err == ActivityManager.START_SUCCESS) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002454 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -07002455 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
2456 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid));
Dianne Hackbornefb58102010-10-14 16:47:34 -07002457 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458
2459 ActivityRecord sourceRecord = null;
2460 ActivityRecord resultRecord = null;
2461 if (resultTo != null) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002462 sourceRecord = isInStackLocked(resultTo);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 if (DEBUG_RESULTS) Slog.v(
Craig Mautner56f52db2013-02-25 10:03:01 -08002464 TAG, "Will send result to " + resultTo + " " + sourceRecord);
2465 if (sourceRecord != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 if (requestCode >= 0 && !sourceRecord.finishing) {
2467 resultRecord = sourceRecord;
2468 }
2469 }
2470 }
2471
2472 int launchFlags = intent.getFlags();
2473
2474 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
2475 && sourceRecord != null) {
2476 // Transfer the result target from the source activity to the new
2477 // one being started, including any failures.
2478 if (requestCode >= 0) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002479 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002480 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 }
2482 resultRecord = sourceRecord.resultTo;
2483 resultWho = sourceRecord.resultWho;
2484 requestCode = sourceRecord.requestCode;
2485 sourceRecord.resultTo = null;
2486 if (resultRecord != null) {
2487 resultRecord.removeResultsLocked(
2488 sourceRecord, resultWho, requestCode);
2489 }
2490 }
2491
Dianne Hackborna4972e92012-03-14 10:38:05 -07002492 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 // We couldn't find a class that can handle the given Intent.
2494 // That's the end of that!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002495 err = ActivityManager.START_INTENT_NOT_RESOLVED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 }
2497
Dianne Hackborna4972e92012-03-14 10:38:05 -07002498 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002499 // We couldn't find the specific class specified in the Intent.
2500 // Also the end of the line.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002501 err = ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002502 }
2503
Dianne Hackborna4972e92012-03-14 10:38:05 -07002504 if (err != ActivityManager.START_SUCCESS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 if (resultRecord != null) {
2506 sendActivityResultLocked(-1,
2507 resultRecord, resultWho, requestCode,
2508 Activity.RESULT_CANCELED, null);
2509 }
Craig Mautner27084302013-03-25 08:05:25 -07002510 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002511 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512 return err;
2513 }
2514
Jeff Sharkey35be7562012-04-18 19:16:15 -07002515 final int startAnyPerm = mService.checkPermission(
2516 START_ANY_ACTIVITY, callingPid, callingUid);
2517 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002518 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
Jeff Sharkey35be7562012-04-18 19:16:15 -07002519 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002520 if (resultRecord != null) {
2521 sendActivityResultLocked(-1,
2522 resultRecord, resultWho, requestCode,
2523 Activity.RESULT_CANCELED, null);
2524 }
Craig Mautner27084302013-03-25 08:05:25 -07002525 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002526 String msg;
2527 if (!aInfo.exported) {
2528 msg = "Permission Denial: starting " + intent.toString()
2529 + " from " + callerApp + " (pid=" + callingPid
2530 + ", uid=" + callingUid + ")"
2531 + " not exported from uid " + aInfo.applicationInfo.uid;
2532 } else {
2533 msg = "Permission Denial: starting " + intent.toString()
2534 + " from " + callerApp + " (pid=" + callingPid
2535 + ", uid=" + callingUid + ")"
2536 + " requires " + aInfo.permission;
2537 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 Slog.w(TAG, msg);
2539 throw new SecurityException(msg);
2540 }
2541
Craig Mautner2219a1b2013-03-25 09:44:30 -07002542 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 if (mService.mController != null) {
2544 boolean abort = false;
2545 try {
2546 // The Intent we give to the watcher has the extra data
2547 // stripped off, since it can contain private information.
2548 Intent watchIntent = intent.cloneFilter();
2549 abort = !mService.mController.activityStarting(watchIntent,
2550 aInfo.applicationInfo.packageName);
2551 } catch (RemoteException e) {
2552 mService.mController = null;
2553 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002554
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 if (abort) {
2556 if (resultRecord != null) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002557 sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 Activity.RESULT_CANCELED, null);
2559 }
2560 // We pretend to the caller that it was really started, but
2561 // they will just get a cancel result.
Craig Mautner27084302013-03-25 08:05:25 -07002562 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002563 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002564 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565 }
2566 }
2567 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002568
Craig Mautnerd2328952013-03-05 12:46:26 -08002569 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 intent, resolvedType, aInfo, mService.mConfiguration,
2571 resultRecord, resultWho, requestCode, componentSpecified);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002572 if (outActivity != null) {
2573 outActivity[0] = r;
2574 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575
Craig Mautner2219a1b2013-03-25 09:44:30 -07002576 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002577 if (mResumedActivity == null
2578 || mResumedActivity.info.applicationInfo.uid != callingUid) {
2579 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
Craig Mautner64f2b172013-03-11 11:40:31 -07002580 PendingActivityLaunch pal =
2581 new PendingActivityLaunch(r, sourceRecord, startFlags, this);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002582 mService.mPendingActivityLaunches.add(pal);
Craig Mautner27084302013-03-25 08:05:25 -07002583 mStackSupervisor.setDismissKeyguard(false);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002584 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002585 return ActivityManager.START_SWITCHES_CANCELED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 }
2587 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002588
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002589 if (mService.mDidAppSwitch) {
2590 // This is the second allowed switch since we stopped switches,
2591 // so now just generally allow switches. Use case: user presses
2592 // home (switches disabled, switch to home, mDidAppSwitch now true);
2593 // user taps a home icon (coming from home so allowed, we hit here
2594 // and now allow anyone to switch again).
2595 mService.mAppSwitchesAllowedTime = 0;
2596 } else {
2597 mService.mDidAppSwitch = true;
2598 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002600
Craig Mautner8d341ef2013-03-26 09:03:27 -07002601 mService.doPendingActivityLaunchesLocked(false);
2602
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002603 err = startActivityUncheckedLocked(r, sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002604 startFlags, true, options);
Craig Mautner27084302013-03-25 08:05:25 -07002605 if (mPausingActivity == null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002606 // Someone asked to have the keyguard dismissed on the next
2607 // activity start, but we are not actually doing an activity
2608 // switch... just dismiss the keyguard now, because we
2609 // probably want to see whatever is behind it.
Craig Mautner27084302013-03-25 08:05:25 -07002610 mStackSupervisor.dismissKeyguard();
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002611 }
2612 return err;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002613 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002614
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002615 final void moveHomeToFrontFromLaunchLocked(int launchFlags) {
2616 if ((launchFlags &
2617 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2618 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2619 // Caller wants to appear on home activity, so before starting
2620 // their own activity we will bring home to the front.
2621 moveHomeToFrontLocked();
2622 }
2623 }
2624
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002626 ActivityRecord sourceRecord, int startFlags, boolean doResume,
2627 Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002628 final Intent intent = r.intent;
2629 final int callingUid = r.launchedFromUid;
Amith Yamasani742a6712011-05-04 14:49:28 -07002630
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002631 int launchFlags = intent.getFlags();
Craig Mautnerd2328952013-03-05 12:46:26 -08002632
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 // We'll invoke onUserLeaving before onPause only if the launching
2634 // activity did not explicitly state that this is an automated launch.
2635 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2636 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2637 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautnerd2328952013-03-05 12:46:26 -08002638
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002639 // If the caller has asked not to resume at this point, we make note
2640 // of this in the record so that we can skip it when trying to find
2641 // the top running activity.
2642 if (!doResume) {
2643 r.delayedResume = true;
2644 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002645
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
2647 != 0 ? r : null;
2648
2649 // If the onlyIfNeeded flag is set, then we can do this if the activity
2650 // being launched is the same as the one making the call... or, as
2651 // a special case, if we do not know the caller then we count the
2652 // current top activity as the caller.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002653 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 ActivityRecord checkedCaller = sourceRecord;
2655 if (checkedCaller == null) {
2656 checkedCaller = topRunningNonDelayedActivityLocked(notTop);
2657 }
2658 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2659 // Caller is not the same as launcher, so always needed.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002660 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 }
2662 }
2663
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002664 if (sourceRecord == null) {
2665 // This activity is not being started from another... in this
2666 // case we -always- start a new task.
2667 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2668 Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: "
2669 + intent);
2670 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2671 }
2672 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2673 // The original activity who is starting us is running as a single
2674 // instance... this new activity it is starting must go on its
2675 // own task.
2676 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2677 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
2678 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2679 // The activity being started is a single instance... it always
2680 // gets launched into its own task.
2681 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2682 }
2683
2684 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2685 // For whatever reason this activity is being launched into a new
2686 // task... yet the caller has requested a result back. Well, that
2687 // is pretty messed up, so instead immediately send back a cancel
2688 // and let the new task continue launched as normal without a
2689 // dependency on its originator.
2690 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2691 sendActivityResultLocked(-1,
2692 r.resultTo, r.resultWho, r.requestCode,
2693 Activity.RESULT_CANCELED, null);
2694 r.resultTo = null;
2695 }
2696
2697 boolean addingToTask = false;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002698 boolean movedHome = false;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002699 TaskRecord reuseTask = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002700 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2701 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2702 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2703 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2704 // If bring to front is requested, and no result is requested, and
2705 // we can find a task that was started with this same
2706 // component, then instead of launching bring that one to the front.
2707 if (r.resultTo == null) {
2708 // See if there is a task to bring to the front. If this is
2709 // a SINGLE_INSTANCE activity, there can be one and only one
2710 // instance of it in the history, and it is always in its own
2711 // unique task, so we do a special search.
2712 ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
2713 ? findTaskLocked(intent, r.info)
2714 : findActivityLocked(intent, r.info);
2715 if (taskTop != null) {
2716 if (taskTop.task.intent == null) {
2717 // This task was started because of movement of
2718 // the activity based on affinity... now that we
2719 // are actually launching it, we can assign the
2720 // base intent.
2721 taskTop.task.setIntent(intent, r.info);
2722 }
2723 // If the target task is not in the front, then we need
2724 // to bring it to the front... except... well, with
2725 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2726 // to have the same behavior as if a new instance was
2727 // being started, which means not bringing it to the front
2728 // if the caller is not itself in the front.
2729 ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
Jean-Baptiste Queru66a5d692010-10-25 17:27:16 -07002730 if (curTop != null && curTop.task != taskTop.task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2732 boolean callerAtFront = sourceRecord == null
2733 || curTop.task == sourceRecord.task;
2734 if (callerAtFront) {
2735 // We really do want to push this one into the
2736 // user's face, right now.
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002737 movedHome = true;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002738 moveHomeToFrontFromLaunchLocked(launchFlags);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002739 moveTaskToFrontLocked(taskTop.task, r, options);
Dianne Hackborn84375872012-06-01 19:03:50 -07002740 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002741 }
2742 }
2743 // If the caller has requested that the target task be
2744 // reset, then do so.
2745 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2746 taskTop = resetTaskIfNeededLocked(taskTop, r);
2747 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002748 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 // We don't need to start a new activity, and
2750 // the client said not to do anything if that
2751 // is the case, so this is it! And for paranoia, make
2752 // sure we have correctly resumed the top activity.
2753 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002754 resumeTopActivityLocked(null, options);
2755 } else {
2756 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002757 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002758 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002759 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002760 if ((launchFlags &
2761 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
2762 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
2763 // The caller has requested to completely replace any
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002764 // existing task with its new activity. Well that should
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002765 // not be too hard...
2766 reuseTask = taskTop.task;
Craig Mautner16030772013-02-26 16:05:47 -08002767 performClearTaskLocked(taskTop.task);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002768 reuseTask.setIntent(r.intent, r.info);
2769 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2771 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2772 // In this situation we want to remove all activities
2773 // from the task up to the one being started. In most
2774 // cases this means we are resetting the task to its
2775 // initial state.
Craig Mautner16030772013-02-26 16:05:47 -08002776 ActivityRecord top = performClearTaskLocked(taskTop.task, r, launchFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002777 if (top != null) {
2778 if (top.frontOfTask) {
2779 // Activity aliases may mean we use different
2780 // intents for the top activity, so make sure
2781 // the task now has the identity of the new
2782 // intent.
2783 top.task.setIntent(r.intent, r.info);
2784 }
2785 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002786 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 } else {
2788 // A special case: we need to
2789 // start the activity because it is not currently
2790 // running, and the caller has asked to clear the
2791 // current task to have this activity at the top.
2792 addingToTask = true;
2793 // Now pretend like this activity is being started
2794 // by the top of its task, so it is put in the
2795 // right place.
2796 sourceRecord = taskTop;
2797 }
2798 } else if (r.realActivity.equals(taskTop.task.realActivity)) {
2799 // In this case the top activity on the task is the
2800 // same as the one being launched, so we take that
2801 // as a request to bring the task to the foreground.
2802 // If the top activity in the task is the root
2803 // activity, deliver this new intent to it if it
2804 // desires.
Johan Viktorssonf363dfd2012-02-16 17:05:16 +01002805 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2806 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 && taskTop.realActivity.equals(r.realActivity)) {
2808 logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task);
2809 if (taskTop.frontOfTask) {
2810 taskTop.task.setIntent(r.intent, r.info);
2811 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002812 taskTop.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 } else if (!r.intent.filterEquals(taskTop.task.intent)) {
2814 // In this case we are launching the root activity
2815 // of the task, but with a different intent. We
2816 // should start a new instance on top.
2817 addingToTask = true;
2818 sourceRecord = taskTop;
2819 }
2820 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2821 // In this case an activity is being launched in to an
2822 // existing task, without resetting that task. This
2823 // is typically the situation of launching an activity
2824 // from a notification or shortcut. We want to place
2825 // the new activity on top of the current task.
2826 addingToTask = true;
2827 sourceRecord = taskTop;
2828 } else if (!taskTop.task.rootWasReset) {
2829 // In this case we are launching in to an existing task
2830 // that has not yet been started from its front door.
2831 // The current task has been brought to the front.
2832 // Ideally, we'd probably like to place this new task
2833 // at the bottom of its stack, but that's a little hard
2834 // to do with the current organization of the code so
2835 // for now we'll just drop it.
2836 taskTop.task.setIntent(r.intent, r.info);
2837 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002838 if (!addingToTask && reuseTask == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 // We didn't do anything... but it was needed (a.k.a., client
2840 // don't use that intent!) And for paranoia, make
2841 // sure we have correctly resumed the top activity.
2842 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002843 resumeTopActivityLocked(null, options);
2844 } else {
2845 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002847 return ActivityManager.START_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 }
2849 }
2850 }
2851 }
2852
2853 //String uri = r.intent.toURI();
2854 //Intent intent2 = new Intent(uri);
2855 //Slog.i(TAG, "Given intent: " + r.intent);
2856 //Slog.i(TAG, "URI is: " + uri);
2857 //Slog.i(TAG, "To intent: " + intent2);
2858
2859 if (r.packageName != null) {
2860 // If the activity being launched is the same as the one currently
2861 // at the top, then we need to check if it should only be launched
2862 // once.
2863 ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
2864 if (top != null && r.resultTo == null) {
Amith Yamasani742a6712011-05-04 14:49:28 -07002865 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002866 if (top.app != null && top.app.thread != null) {
2867 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2868 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
2869 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2870 logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2871 // For paranoia, make sure we have correctly
2872 // resumed the top activity.
2873 if (doResume) {
2874 resumeTopActivityLocked(null);
2875 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002876 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002877 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 // We don't need to start a new activity, and
2879 // the client said not to do anything if that
2880 // is the case, so this is it!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002881 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002882 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002883 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002884 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 }
2886 }
2887 }
2888 }
2889
2890 } else {
2891 if (r.resultTo != null) {
2892 sendActivityResultLocked(-1,
2893 r.resultTo, r.resultWho, r.requestCode,
2894 Activity.RESULT_CANCELED, null);
2895 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002896 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002897 return ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 }
2899
2900 boolean newTask = false;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002901 boolean keepCurTransition = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902
2903 // Should this be considered a new task?
2904 if (r.resultTo == null && !addingToTask
2905 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002906 if (reuseTask == null) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 setTask(r, createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent,
2908 true), null, true);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002909 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2910 + " in new task " + r.task);
2911 } else {
Craig Mautner0247fc82013-02-28 14:32:06 -08002912 setTask(r, reuseTask, reuseTask, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914 newTask = true;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002915 if (!movedHome) {
2916 moveHomeToFrontFromLaunchLocked(launchFlags);
2917 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002918
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 } else if (sourceRecord != null) {
2920 if (!addingToTask &&
2921 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2922 // In this case, we are adding the activity to an existing
2923 // task, but the caller has asked to clear that task if the
2924 // activity is already running.
Craig Mautner16030772013-02-26 16:05:47 -08002925 ActivityRecord top = performClearTaskLocked(sourceRecord.task, r, launchFlags);
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002926 keepCurTransition = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 if (top != null) {
2928 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002929 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002930 // For paranoia, make sure we have correctly
2931 // resumed the top activity.
2932 if (doResume) {
2933 resumeTopActivityLocked(null);
2934 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002935 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002936 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 }
2938 } else if (!addingToTask &&
2939 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2940 // In this case, we are launching an activity in our own task
2941 // that may already be running somewhere in the history, and
2942 // we want to shuffle it to the front of the stack if so.
Craig Mautner56f52db2013-02-25 10:03:01 -08002943 final ActivityRecord top = findActivityInHistoryLocked(r, sourceRecord.task);
2944 if (top != null) {
2945 moveActivityToFrontLocked(top);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002946 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002947 top.updateOptionsLocked(options);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002948 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 if (doResume) {
2950 resumeTopActivityLocked(null);
2951 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002952 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002953 }
2954 }
2955 // An existing activity is starting this new activity, so we want
2956 // to keep the new one in the same task as the one that is starting
2957 // it.
Craig Mautner0247fc82013-02-28 14:32:06 -08002958 setTask(r, sourceRecord.task, sourceRecord.thumbHolder, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002959 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2960 + " in existing task " + r.task);
2961
2962 } else {
2963 // This not being started from an existing activity, and not part
2964 // of a new task... just put it in the top task, though these days
2965 // this case should never happen.
Craig Mautner9658b312013-02-28 10:55:59 -08002966 ActivityRecord prev = null;
2967 // Iterate to find the first non-empty task stack. Note that this code can
2968 // go away once we stop storing tasks with empty mActivities lists.
2969 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2970 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2971 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2972 prev = activities.get(activityNdx);
2973 break;
2974 }
2975 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002976 setTask(r, prev != null
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002977 ? prev.task
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 : createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent, true),
2979 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2981 + " in new guessed " + r.task);
2982 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002983
Dianne Hackborn39792d22010-08-19 18:01:52 -07002984 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002985 intent, r.getUriPermissionsLocked());
Dianne Hackborn39792d22010-08-19 18:01:52 -07002986
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987 if (newTask) {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002988 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 }
2990 logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002991 startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002992 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 }
2994
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002995 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
2996 long thisTime, long totalTime) {
2997 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
2998 WaitResult w = mWaitingActivityLaunched.get(i);
2999 w.timeout = timeout;
3000 if (r != null) {
3001 w.who = new ComponentName(r.info.packageName, r.info.name);
3002 }
3003 w.thisTime = thisTime;
3004 w.totalTime = totalTime;
3005 }
3006 mService.notifyAll();
3007 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003008
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 void reportActivityVisibleLocked(ActivityRecord r) {
3010 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
3011 WaitResult w = mWaitingActivityVisible.get(i);
3012 w.timeout = false;
3013 if (r != null) {
3014 w.who = new ComponentName(r.info.packageName, r.info.name);
3015 }
3016 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
3017 w.thisTime = w.totalTime;
3018 }
3019 mService.notifyAll();
Craig Mautner27084302013-03-25 08:05:25 -07003020 mStackSupervisor.dismissKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
3022
3023 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3024 String resultWho, int requestCode, int resultCode, Intent data) {
3025
3026 if (callingUid > 0) {
3027 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07003028 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 }
3030
3031 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3032 + " : who=" + resultWho + " req=" + requestCode
3033 + " res=" + resultCode + " data=" + data);
3034 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3035 try {
3036 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3037 list.add(new ResultInfo(resultWho, requestCode,
3038 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003039 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 return;
3041 } catch (Exception e) {
3042 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3043 }
3044 }
3045
3046 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3047 }
3048
3049 private final void stopActivityLocked(ActivityRecord r) {
3050 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
3051 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3052 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3053 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07003054 if (!mService.mSleeping) {
3055 if (DEBUG_STATES) {
3056 Slog.d(TAG, "no-history finish of " + r);
3057 }
3058 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003059 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07003060 } else {
3061 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
3062 + " on stop because we're just sleeping");
3063 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003065 }
3066
3067 if (r.app != null && r.app.thread != null) {
Craig Mautner2219a1b2013-03-25 09:44:30 -07003068 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 if (mService.mFocusedActivity == r) {
3070 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3071 }
3072 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003073 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074 try {
3075 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003076 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3077 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 r.state = ActivityState.STOPPING;
3079 if (DEBUG_VISBILITY) Slog.v(
3080 TAG, "Stopping visible=" + r.visible + " for " + r);
3081 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003082 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08003084 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003085 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003086 r.setSleeping(true);
3087 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003088 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
3089 msg.obj = r;
3090 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003091 } catch (Exception e) {
3092 // Maybe just ignore exceptions here... if the process
3093 // has crashed, our death notification will clean things
3094 // up.
3095 Slog.w(TAG, "Exception thrown during pause", e);
3096 // Just in case, assume it to be stopped.
3097 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003098 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 r.state = ActivityState.STOPPED;
3100 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07003101 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 }
3103 }
3104 }
3105 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(
3108 boolean remove) {
3109 int N = mStoppingActivities.size();
3110 if (N <= 0) return null;
3111
3112 ArrayList<ActivityRecord> stops = null;
3113
3114 final boolean nowVisible = mResumedActivity != null
3115 && mResumedActivity.nowVisible
3116 && !mResumedActivity.waitingVisible;
3117 for (int i=0; i<N; i++) {
3118 ActivityRecord s = mStoppingActivities.get(i);
3119 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
3120 + nowVisible + " waitingVisible=" + s.waitingVisible
3121 + " finishing=" + s.finishing);
3122 if (s.waitingVisible && nowVisible) {
3123 mWaitingVisibleActivities.remove(s);
3124 s.waitingVisible = false;
3125 if (s.finishing) {
3126 // If this activity is finishing, it is sitting on top of
3127 // everyone else but we now know it is no longer needed...
3128 // so get rid of it. Otherwise, we need to go through the
3129 // normal flow and hide it once we determine that it is
3130 // hidden by the activities in front of it.
3131 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003132 mService.mWindowManager.setAppVisibility(s.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 }
3134 }
Craig Mautnere11f2b72013-04-01 12:37:17 -07003135 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003136 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3137 if (stops == null) {
3138 stops = new ArrayList<ActivityRecord>();
3139 }
3140 stops.add(s);
3141 mStoppingActivities.remove(i);
3142 N--;
3143 i--;
3144 }
3145 }
3146
3147 return stops;
3148 }
3149
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003150 final void scheduleIdleLocked() {
3151 Message msg = Message.obtain();
3152 msg.what = IDLE_NOW_MSG;
3153 mHandler.sendMessage(msg);
3154 }
3155
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003156 final ActivityRecord activityIdleInternal(IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003157 Configuration config) {
3158 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
3159
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003160 ActivityRecord res = null;
3161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 ArrayList<ActivityRecord> stops = null;
3163 ArrayList<ActivityRecord> finishes = null;
3164 ArrayList<ActivityRecord> thumbnails = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003165 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 int NS = 0;
3167 int NF = 0;
3168 int NT = 0;
3169 IApplicationThread sendThumbnail = null;
3170 boolean booting = false;
3171 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003172 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173
3174 synchronized (mService) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003175 ActivityRecord r = ActivityRecord.forToken(token);
3176 if (r != null) {
3177 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003178 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003179 }
3180
3181 // Get the activity record.
Craig Mautnerd44711d2013-02-23 11:24:36 -08003182 if (isInStackLocked(token) != null) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003183 res = r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184
3185 if (fromTimeout) {
3186 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
3187 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003188
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 // This is a hack to semi-deal with a race condition
3190 // in the client where it can be constructed with a
3191 // newer configuration from when we asked it to launch.
3192 // We'll update with whatever configuration it now says
3193 // it used to launch.
3194 if (config != null) {
3195 r.configuration = config;
3196 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198 // No longer need to keep the device awake.
3199 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
3200 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
3201 mLaunchingActivity.release();
3202 }
3203
3204 // We are now idle. If someone is waiting for a thumbnail from
3205 // us, we can now deliver.
3206 r.idle = true;
3207 mService.scheduleAppGcsLocked();
3208 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
3209 sendThumbnail = r.app.thread;
3210 r.thumbnailNeeded = false;
3211 }
3212
3213 // If this activity is fullscreen, set up to hide those under it.
3214
3215 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
3216 ensureActivitiesVisibleLocked(null, 0);
3217
3218 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Craig Mautner2219a1b2013-03-25 09:44:30 -07003219 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07003220 if (!mService.mBooted) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221 mService.mBooted = true;
3222 enableScreen = true;
3223 }
3224 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 } else if (fromTimeout) {
3226 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
3227 }
3228
3229 // Atomically retrieve all of the other things to do.
3230 stops = processStoppingActivitiesLocked(true);
3231 NS = stops != null ? stops.size() : 0;
3232 if ((NF=mFinishingActivities.size()) > 0) {
3233 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
3234 mFinishingActivities.clear();
3235 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003236 if ((NT=mCancelledThumbnails.size()) > 0) {
3237 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
3238 mCancelledThumbnails.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003239 }
3240
Craig Mautner2219a1b2013-03-25 09:44:30 -07003241 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 booting = mService.mBooting;
3243 mService.mBooting = false;
3244 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003245 if (mStartingUsers.size() > 0) {
3246 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
3247 mStartingUsers.clear();
3248 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 }
3250
3251 int i;
3252
3253 // Send thumbnail if requested.
3254 if (sendThumbnail != null) {
3255 try {
3256 sendThumbnail.requestThumbnail(token);
3257 } catch (Exception e) {
3258 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
3259 mService.sendPendingThumbnail(null, token, null, null, true);
3260 }
3261 }
3262
3263 // Stop any activities that are scheduled to do so but have been
3264 // waiting for the next one to start.
3265 for (i=0; i<NS; i++) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003266 ActivityRecord r = stops.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267 synchronized (mService) {
3268 if (r.finishing) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003269 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 } else {
3271 stopActivityLocked(r);
3272 }
3273 }
3274 }
3275
3276 // Finish any activities that are scheduled to do so but have been
3277 // waiting for the next one to start.
3278 for (i=0; i<NF; i++) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003279 ActivityRecord r = finishes.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003280 synchronized (mService) {
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003281 activityRemoved = destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 }
3283 }
3284
3285 // Report back to any thumbnail receivers.
3286 for (i=0; i<NT; i++) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003287 ActivityRecord r = thumbnails.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288 mService.sendPendingThumbnail(r, null, null, null, true);
3289 }
3290
3291 if (booting) {
3292 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003293 } else if (startingUsers != null) {
3294 for (i=0; i<startingUsers.size(); i++) {
3295 mService.finishUserSwitch(startingUsers.get(i));
3296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297 }
3298
3299 mService.trimApplications();
3300 //dump();
3301 //mWindowManager.dump();
3302
3303 if (enableScreen) {
3304 mService.enableScreenAfterBoot();
3305 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003306
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003307 if (activityRemoved) {
3308 resumeTopActivityLocked(null);
3309 }
3310
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003311 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 }
3313
3314 /**
3315 * @return Returns true if the activity is being finished, false if for
3316 * some reason it is being left as-is.
3317 */
3318 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003319 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003320 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07003321 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003322 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003323 + ", result=" + resultCode + ", data=" + resultData
3324 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003325 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003326 return false;
3327 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328
Craig Mautnerd44711d2013-02-23 11:24:36 -08003329 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 return true;
3331 }
3332
Craig Mautnerd2328952013-03-05 12:46:26 -08003333 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003334 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3335 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3336 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3337 ActivityRecord r = activities.get(activityNdx);
3338 if (r.resultTo == self && r.requestCode == requestCode) {
3339 if ((r.resultWho == null && resultWho == null) ||
3340 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3341 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3342 false);
3343 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003344 }
3345 }
3346 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003347 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003348 }
3349
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003350 final void finishTopRunningActivityLocked(ProcessRecord app) {
3351 ActivityRecord r = topRunningActivityLocked(null);
3352 if (r != null && r.app == app) {
3353 // If the top running activity is from this crashing
3354 // process, then terminate it to avoid getting in a loop.
3355 Slog.w(TAG, " Force finishing activity "
3356 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08003357 int taskNdx = mTaskHistory.indexOf(r.task);
3358 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003359 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003360 // Also terminate any activities below it that aren't yet
3361 // stopped, to avoid a situation where one will get
3362 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003363 --activityNdx;
3364 if (activityNdx < 0) {
3365 do {
3366 --taskNdx;
3367 if (taskNdx < 0) {
3368 break;
3369 }
3370 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3371 } while (activityNdx < 0);
3372 }
3373 if (activityNdx >= 0) {
3374 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003375 if (r.state == ActivityState.RESUMED
3376 || r.state == ActivityState.PAUSING
3377 || r.state == ActivityState.PAUSED) {
3378 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
3379 Slog.w(TAG, " Force finishing activity "
3380 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003381 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003382 }
3383 }
3384 }
3385 }
3386 }
3387
Craig Mautnerd2328952013-03-05 12:46:26 -08003388 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003389 ArrayList<ActivityRecord> activities = r.task.mActivities;
3390 for (int index = activities.indexOf(r); index >= 0; --index) {
3391 ActivityRecord cur = activities.get(index);
3392 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003393 break;
3394 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003395 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity",
3396 true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003397 }
3398 return true;
3399 }
3400
Dianne Hackborn5c607432012-02-28 14:44:19 -08003401 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3402 // send the result
3403 ActivityRecord resultTo = r.resultTo;
3404 if (resultTo != null) {
3405 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
3406 + " who=" + r.resultWho + " req=" + r.requestCode
3407 + " res=" + resultCode + " data=" + resultData);
3408 if (r.info.applicationInfo.uid > 0) {
3409 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3410 resultTo.packageName, resultData,
3411 resultTo.getUriPermissionsLocked());
3412 }
3413 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3414 resultData);
3415 r.resultTo = null;
3416 }
3417 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
3418
3419 // Make sure this HistoryRecord is not holding on to other resources,
3420 // because clients have remote IPC references to this object so we
3421 // can't assume that will go away and want to avoid circular IPC refs.
3422 r.results = null;
3423 r.pendingResults = null;
3424 r.newIntents = null;
3425 r.icicle = null;
3426 }
3427
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003428 /**
3429 * @return Returns true if this activity has been removed from the history
3430 * list, or false if it is still in the list and will be removed later.
3431 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08003432 final boolean finishActivityLocked(ActivityRecord r, int resultCode,
3433 Intent resultData, String reason, boolean oomAdj) {
3434 return finishActivityLocked(r, resultCode, resultData, reason, false, oomAdj);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003435 }
3436
3437 /**
3438 * @return Returns true if this activity has been removed from the history
3439 * list, or false if it is still in the list and will be removed later.
3440 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08003441 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3442 String reason, boolean immediate, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003443 if (r.finishing) {
3444 Slog.w(TAG, "Duplicate finish request for " + r);
3445 return false;
3446 }
3447
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08003448 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003449 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003450 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003452 final ArrayList<ActivityRecord> activities = r.task.mActivities;
3453 final int index = activities.indexOf(r);
3454 if (index < (activities.size() - 1)) {
3455 ActivityRecord next = activities.get(index+1);
3456 if (r.frontOfTask) {
3457 // The next activity is now the front of the task.
3458 next.frontOfTask = true;
3459 }
3460 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3461 // If the caller asked that this activity (and all above it)
3462 // be cleared when the task is reset, don't lose that information,
3463 // but propagate it up to the next activity.
3464 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003465 }
3466 }
3467
3468 r.pauseKeyDispatchingLocked();
Craig Mautner2219a1b2013-03-25 09:44:30 -07003469 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003470 if (mService.mFocusedActivity == r) {
3471 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3472 }
3473 }
3474
Dianne Hackborn5c607432012-02-28 14:44:19 -08003475 finishActivityResultsLocked(r, resultCode, resultData);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003476
3477 if (mService.mPendingThumbnails.size() > 0) {
3478 // There are clients waiting to receive thumbnails so, in case
3479 // this is an activity that someone is waiting for, add it
3480 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08003481 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003482 }
3483
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003484 if (immediate) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003485 return finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, oomAdj) == null;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003486 } else if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003487 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003488 if (DEBUG_TRANSITION) Slog.v(TAG,
3489 "Prepare close transition: finishing " + r);
3490 mService.mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003491 ? AppTransition.TRANSIT_TASK_CLOSE
3492 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003493
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003494 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08003495 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003496
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003497 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
3499 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
3500 startPausingLocked(false, false);
3501 }
3502
3503 } else if (r.state != ActivityState.PAUSING) {
3504 // If the activity is PAUSING, we will complete the finish once
3505 // it is done pausing; else we can just directly finish it here.
3506 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003507 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 } else {
3509 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
3510 }
3511
3512 return false;
3513 }
3514
3515 private static final int FINISH_IMMEDIATELY = 0;
3516 private static final int FINISH_AFTER_PAUSE = 1;
3517 private static final int FINISH_AFTER_VISIBLE = 2;
3518
Craig Mautnerd44711d2013-02-23 11:24:36 -08003519
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003521 int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 // First things first: if this activity is currently visible,
3523 // and the resumed activity is not yet visible, then hold off on
3524 // finishing until the resumed one becomes visible.
3525 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
3526 if (!mStoppingActivities.contains(r)) {
3527 mStoppingActivities.add(r);
3528 if (mStoppingActivities.size() > 3) {
3529 // If we already have a few activities waiting to stop,
3530 // then give up on things going idle and start clearing
3531 // them out.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003532 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003533 } else {
3534 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003535 }
3536 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003537 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3538 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003539 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003540 if (oomAdj) {
3541 mService.updateOomAdjLocked();
3542 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 return r;
3544 }
3545
3546 // make sure the record is cleaned out of other places.
3547 mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003548 mGoingToSleepActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003549 mWaitingVisibleActivities.remove(r);
3550 if (mResumedActivity == r) {
3551 mResumedActivity = null;
3552 }
3553 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003554 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003555 r.state = ActivityState.FINISHING;
3556
3557 if (mode == FINISH_IMMEDIATELY
3558 || prevState == ActivityState.STOPPED
3559 || prevState == ActivityState.INITIALIZING) {
3560 // If this activity is already stopped, we can just finish
3561 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003562 boolean activityRemoved = destroyActivityLocked(r, true,
3563 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003564 if (activityRemoved) {
3565 resumeTopActivityLocked(null);
3566 }
3567 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003569
3570 // Need to go through the full pause cycle to get this
3571 // activity into the stopped state and then finish it.
3572 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
3573 mFinishingActivities.add(r);
3574 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003575 return r;
3576 }
3577
Craig Mautnerd2328952013-03-05 12:46:26 -08003578 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003579 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003580 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08003581 final TaskRecord task = srec.task;
3582 final ArrayList<ActivityRecord> activities = task.mActivities;
3583 final int start = activities.indexOf(srec);
3584 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003585 return false;
3586 }
3587 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003588 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003589 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003590 final ComponentName dest = destIntent.getComponent();
3591 if (start > 0 && dest != null) {
3592 for (int i = finishTo; i >= 0; i--) {
3593 ActivityRecord r = activities.get(i);
3594 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003595 r.info.name.equals(dest.getClassName())) {
3596 finishTo = i;
3597 parent = r;
3598 foundParentInTask = true;
3599 break;
3600 }
3601 }
3602 }
3603
3604 IActivityController controller = mService.mController;
3605 if (controller != null) {
3606 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3607 if (next != null) {
3608 // ask watcher if this is allowed
3609 boolean resumeOK = true;
3610 try {
3611 resumeOK = controller.activityResuming(next.packageName);
3612 } catch (RemoteException e) {
3613 mService.mController = null;
3614 }
3615
3616 if (!resumeOK) {
3617 return false;
3618 }
3619 }
3620 }
3621 final long origId = Binder.clearCallingIdentity();
3622 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003623 ActivityRecord r = activities.get(i);
3624 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003625 // Only return the supplied result for the first activity finished
3626 resultCode = Activity.RESULT_CANCELED;
3627 resultData = null;
3628 }
3629
3630 if (parent != null && foundParentInTask) {
3631 final int parentLaunchMode = parent.info.launchMode;
3632 final int destIntentFlags = destIntent.getFlags();
3633 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3634 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3635 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3636 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
3637 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
3638 } else {
3639 try {
3640 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3641 destIntent.getComponent(), 0, srec.userId);
3642 int res = startActivityLocked(srec.app.thread, destIntent,
3643 null, aInfo, parent.appToken, null,
3644 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
3645 0, null, true, null);
3646 foundParentInTask = res == ActivityManager.START_SUCCESS;
3647 } catch (RemoteException e) {
3648 foundParentInTask = false;
3649 }
3650 requestFinishActivityLocked(parent.appToken, resultCode,
3651 resultData, "navigate-up", true);
3652 }
3653 }
3654 Binder.restoreCallingIdentity(origId);
3655 return foundParentInTask;
3656 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657 /**
3658 * Perform the common clean-up of an activity record. This is called both
3659 * as part of destroyActivityLocked() (when destroying the client-side
3660 * representation) and cleaning things up as a result of its hosting
3661 * processing going away, in which case there is no remaining client-side
3662 * state to destroy so only the cleanup here is needed.
3663 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003664 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3665 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 if (mResumedActivity == r) {
3667 mResumedActivity = null;
3668 }
3669 if (mService.mFocusedActivity == r) {
3670 mService.mFocusedActivity = null;
3671 }
3672
3673 r.configDestroy = false;
3674 r.frozenBeforeDestroy = false;
3675
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003676 if (setState) {
3677 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3678 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003679 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003680 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003681 }
3682
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003683 // Make sure this record is no longer in the pending finishes list.
3684 // This could happen, for example, if we are trimming activities
3685 // down to the max limit while they are still waiting to finish.
3686 mFinishingActivities.remove(r);
3687 mWaitingVisibleActivities.remove(r);
3688
3689 // Remove any pending results.
3690 if (r.finishing && r.pendingResults != null) {
3691 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3692 PendingIntentRecord rec = apr.get();
3693 if (rec != null) {
3694 mService.cancelIntentSenderLocked(rec, false);
3695 }
3696 }
3697 r.pendingResults = null;
3698 }
3699
3700 if (cleanServices) {
3701 cleanUpActivityServicesLocked(r);
3702 }
3703
3704 if (mService.mPendingThumbnails.size() > 0) {
3705 // There are clients waiting to receive thumbnails so, in case
3706 // this is an activity that someone is waiting for, add it
3707 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08003708 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003709 }
3710
3711 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003712 removeTimeoutsForActivityLocked(r);
3713 }
3714
3715 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003716 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003717 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003718 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003719 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003720 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003721 }
3722
Dianne Hackborn5c607432012-02-28 14:44:19 -08003723 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003724 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3725 r.makeFinishing();
3726 if (DEBUG_ADD_REMOVE) {
3727 RuntimeException here = new RuntimeException("here");
3728 here.fillInStackTrace();
3729 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003730 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003731 if (r.task != null) {
3732 removeActivity(r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003733 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003734 r.takeFromHistory();
3735 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003736 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003737 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003738 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003739 r.app = null;
3740 mService.mWindowManager.removeAppToken(r.appToken);
3741 if (VALIDATE_TOKENS) {
3742 validateAppTokensLocked();
3743 }
3744 cleanUpActivityServicesLocked(r);
3745 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003746 }
3747
3748 /**
3749 * Perform clean-up of service connections in an activity record.
3750 */
3751 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3752 // Throw away any services that have been bound by this activity.
3753 if (r.connections != null) {
3754 Iterator<ConnectionRecord> it = r.connections.iterator();
3755 while (it.hasNext()) {
3756 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003757 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003758 }
3759 r.connections = null;
3760 }
3761 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003762
3763 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
3764 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
3765 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
3766 mHandler.sendMessage(msg);
3767 }
3768
Dianne Hackborn28695e02011-11-02 21:59:51 -07003769 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003770 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003771 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003772 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3773 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3774 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3775 final ActivityRecord r = activities.get(activityNdx);
3776 if (r.finishing) {
3777 continue;
3778 }
3779 if (r.fullscreen) {
3780 lastIsOpaque = true;
3781 }
3782 if (owner != null && r.app != owner) {
3783 continue;
3784 }
3785 if (!lastIsOpaque) {
3786 continue;
3787 }
3788 // We can destroy this one if we have its icicle saved and
3789 // it is not in the process of pausing/stopping/finishing.
3790 if (r.app != null && r != mResumedActivity && r != mPausingActivity
3791 && r.haveState && !r.visible && r.stopped
3792 && r.state != ActivityState.DESTROYING
3793 && r.state != ActivityState.DESTROYED) {
3794 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3795 + " resumed=" + mResumedActivity
3796 + " pausing=" + mPausingActivity);
3797 if (destroyActivityLocked(r, true, oomAdj, reason)) {
3798 activityRemoved = true;
3799 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003800 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003801 }
3802 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003803 if (activityRemoved) {
3804 resumeTopActivityLocked(null);
3805 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003806 }
3807
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 /**
3809 * Destroy the current CLIENT SIDE instance of an activity. This may be
3810 * called both when actually finishing an activity, or when performing
3811 * a configuration switch where we destroy the current client-side object
3812 * but then create a new client-side object for this same HistoryRecord.
3813 */
3814 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07003815 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003816 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003817 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003818 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3819 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003820 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003821 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822
3823 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003824
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003825 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003826
3827 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003828
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003829 if (hadApp) {
3830 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003831 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003832 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3833 mService.mHeavyWeightProcess = null;
3834 mService.mHandler.sendEmptyMessage(
3835 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3836 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003837 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003838 // No longer have activities, so update oom adj.
3839 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003840 }
3841 }
3842
3843 boolean skipDestroy = false;
3844
3845 try {
3846 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003847 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003848 r.configChangeFlags);
3849 } catch (Exception e) {
3850 // We can just ignore exceptions here... if the process
3851 // has crashed, our death notification will clean things
3852 // up.
3853 //Slog.w(TAG, "Exception thrown during finish", e);
3854 if (r.finishing) {
3855 removeActivityFromHistoryLocked(r);
3856 removedFromHistory = true;
3857 skipDestroy = true;
3858 }
3859 }
3860
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 r.nowVisible = false;
3862
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003863 // If the activity is finishing, we need to wait on removing it
3864 // from the list to give it a chance to do its cleanup. During
3865 // that time it may make calls back with its token so we need to
3866 // be able to find it on the list and so we don't want to remove
3867 // it from the list yet. Otherwise, we can just immediately put
3868 // it in the destroyed state since we are not removing it from the
3869 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003870 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003871 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3872 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003873 r.state = ActivityState.DESTROYING;
3874 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
3875 msg.obj = r;
3876 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3877 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003878 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3879 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003881 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003882 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003883 }
3884 } else {
3885 // remove this record from the history.
3886 if (r.finishing) {
3887 removeActivityFromHistoryLocked(r);
3888 removedFromHistory = true;
3889 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003890 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3891 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003892 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003893 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003894 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003895 }
3896 }
3897
3898 r.configChangeFlags = 0;
3899
3900 if (!mLRUActivities.remove(r) && hadApp) {
3901 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3902 }
3903
3904 return removedFromHistory;
3905 }
3906
Craig Mautnerd2328952013-03-05 12:46:26 -08003907 final void activityDestroyedLocked(IBinder token) {
3908 final long origId = Binder.clearCallingIdentity();
3909 try {
3910 ActivityRecord r = ActivityRecord.forToken(token);
3911 if (r != null) {
3912 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003914
3915 if (isInStackLocked(token) != null) {
3916 if (r.state == ActivityState.DESTROYING) {
3917 cleanUpActivityLocked(r, true, false);
3918 removeActivityFromHistoryLocked(r);
3919 }
3920 }
3921 resumeTopActivityLocked(null);
3922 } finally {
3923 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003924 }
3925 }
3926
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003927 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3928 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003929 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003930 if (DEBUG_CLEANUP) Slog.v(
3931 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003932 + " with " + i + " entries");
3933 while (i > 0) {
3934 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003935 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003936 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003937 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003938 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003939 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003940 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003941 }
3942 }
3943 }
3944
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003945 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3946 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
3947 removeHistoryRecordsForAppLocked(mStoppingActivities, app, "mStoppingActivities");
3948 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
3949 removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app,
3950 "mWaitingVisibleActivities");
3951 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
3952
3953 boolean hasVisibleActivities = false;
3954
3955 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003956 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003957 if (DEBUG_CLEANUP) Slog.v(
3958 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003959 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3960 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3961 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3962 final ActivityRecord r = activities.get(activityNdx);
3963 --i;
3964 if (DEBUG_CLEANUP) Slog.v(
3965 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3966 if (r.app == app) {
3967 boolean remove;
3968 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3969 // Don't currently have state for the activity, or
3970 // it is finishing -- always remove it.
3971 remove = true;
3972 } else if (r.launchCount > 2 &&
3973 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3974 // We have launched this activity too many times since it was
3975 // able to run, so give up and remove it.
3976 remove = true;
3977 } else {
3978 // The process may be gone, but the activity lives on!
3979 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003980 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003981 if (remove) {
3982 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
3983 RuntimeException here = new RuntimeException("here");
3984 here.fillInStackTrace();
3985 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3986 + ": haveState=" + r.haveState
3987 + " stateNotNeeded=" + r.stateNotNeeded
3988 + " finishing=" + r.finishing
3989 + " state=" + r.state, here);
3990 }
3991 if (!r.finishing) {
3992 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3993 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3994 r.userId, System.identityHashCode(r),
3995 r.task.taskId, r.shortComponentName,
3996 "proc died without state saved");
3997 }
3998 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003999
Craig Mautner0247fc82013-02-28 14:32:06 -08004000 } else {
4001 // We have the current state for this activity, so
4002 // it can be restarted later when needed.
4003 if (localLOGV) Slog.v(
4004 TAG, "Keeping entry, setting app to null");
4005 if (r.visible) {
4006 hasVisibleActivities = true;
4007 }
4008 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
4009 + r);
4010 r.app = null;
4011 r.nowVisible = false;
4012 if (!r.haveState) {
4013 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
4014 "App died, clearing saved state of " + r);
4015 r.icicle = null;
4016 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004017 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004018
Craig Mautnerd2328952013-03-05 12:46:26 -08004019 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08004020 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004021 }
4022 }
4023
4024 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004025 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004026
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004027 /**
4028 * Move the current home activity's task (if one exists) to the front
4029 * of the stack.
4030 */
4031 final void moveHomeToFrontLocked() {
Craig Mautner0247fc82013-02-28 14:32:06 -08004032 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4033 final TaskRecord task = mTaskHistory.get(taskNdx);
4034 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004035 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4036 final ActivityRecord r = activities.get(activityNdx);
4037 if (r.isHomeActivity) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004038 moveTaskToFrontLocked(task, null, null);
4039 return;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004040 }
4041 }
4042 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004043 }
4044
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004045 final void updateTransitLocked(int transit, Bundle options) {
4046 if (options != null) {
4047 ActivityRecord r = topRunningActivityLocked(null);
4048 if (r != null && r.state != ActivityState.RESUMED) {
4049 r.updateOptionsLocked(options);
4050 } else {
4051 ActivityOptions.abort(options);
4052 }
4053 }
4054 mService.mWindowManager.prepareAppTransition(transit, false);
4055 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004056
Craig Mautnercae015f2013-02-08 14:31:27 -08004057 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004058 final TaskRecord task = taskForIdLocked(taskId);
4059 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004060 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
4061 mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004062 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004063 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
4064 // Caller wants the home activity moved with it. To accomplish this,
4065 // we'll just move the home task to the top first.
4066 moveHomeToFrontLocked();
4067 }
4068 moveTaskToFrontLocked(task, null, options);
4069 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004070 }
4071 return false;
4072 }
4073
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004074 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004075 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004076
Craig Mautner11bf9a52013-02-19 14:08:51 -08004077 final int numTasks = mTaskHistory.size();
4078 final int index = mTaskHistory.indexOf(tr);
4079 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
4080 // nothing to do!
4081 if (reason != null &&
4082 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
4083 ActivityOptions.abort(options);
4084 } else {
4085 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4086 }
4087 return;
4088 }
4089
4090 // Shift all activities with this task up to the top
4091 // of the stack, keeping them in the same internal order.
4092 mTaskHistory.remove(tr);
4093 mTaskHistory.add(tr);
4094
4095 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004096 if (reason != null &&
4097 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004098 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004099 ActivityRecord r = topRunningActivityLocked(null);
4100 if (r != null) {
4101 mNoAnimActivities.add(r);
4102 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004103 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004104 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004105 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004106 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004107
Craig Mautner58547802013-03-05 08:23:53 -08004108 mService.mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004109
Craig Mautner58547802013-03-05 08:23:53 -08004110 resumeTopActivityLocked(null);
4111 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004112
4113 if (VALIDATE_TOKENS) {
4114 validateAppTokensLocked();
4115 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004116 }
4117
4118 /**
4119 * Worker method for rearranging history stack. Implements the function of moving all
4120 * activities for a specific task (gathering them if disjoint) into a single group at the
4121 * bottom of the stack.
4122 *
4123 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4124 * to premeptively cancel the move.
4125 *
4126 * @param task The taskId to collect and move to the bottom.
4127 * @return Returns true if the move completed, false if not.
4128 */
4129 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
4130 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004132 // If we have a watcher, preflight the move before committing to it. First check
4133 // for *other* available tasks, but if none are available, then try again allowing the
4134 // current task to be selected.
Craig Mautner2219a1b2013-03-25 09:44:30 -07004135 if (mStackSupervisor.isMainStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004136 ActivityRecord next = topRunningActivityLocked(null, task);
4137 if (next == null) {
4138 next = topRunningActivityLocked(null, 0);
4139 }
4140 if (next != null) {
4141 // ask watcher if this is allowed
4142 boolean moveOK = true;
4143 try {
4144 moveOK = mService.mController.activityResuming(next.packageName);
4145 } catch (RemoteException e) {
4146 mService.mController = null;
4147 }
4148 if (!moveOK) {
4149 return false;
4150 }
4151 }
4152 }
4153
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004154 if (DEBUG_TRANSITION) Slog.v(TAG,
4155 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156
Craig Mautnerd2328952013-03-05 12:46:26 -08004157 final TaskRecord tr = taskForIdLocked(task);
4158 if (tr == null) {
4159 return false;
4160 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004161 mTaskHistory.remove(tr);
4162 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004163
4164 if (reason != null &&
4165 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004166 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004167 ActivityRecord r = topRunningActivityLocked(null);
4168 if (r != null) {
4169 mNoAnimActivities.add(r);
4170 }
4171 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08004172 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08004173 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004174 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004175 mService.mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004176
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004177 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004178 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004179 }
4180
Craig Mautner58547802013-03-05 08:23:53 -08004181 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004182 return true;
4183 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004184
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004185 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08004186 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004187 ActivityRecord resumed = mResumedActivity;
4188 if (resumed != null && resumed.thumbHolder == tr) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004189 info.mainThumbnail = screenshotActivities(resumed);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004190 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07004191 if (info.mainThumbnail == null) {
4192 info.mainThumbnail = tr.lastThumbnail;
4193 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004194 return info;
4195 }
4196
Dianne Hackborn15491c62012-09-19 10:59:14 -07004197 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
4198 ActivityRecord resumed = mResumedActivity;
4199 if (resumed != null && resumed.task == tr) {
4200 // This task is the current resumed task, we just need to take
4201 // a screenshot of it and return that.
Craig Mautnerd2328952013-03-05 12:46:26 -08004202 return screenshotActivities(resumed);
Dianne Hackborn15491c62012-09-19 10:59:14 -07004203 }
4204 // Return the information about the task, to figure out the top
4205 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08004206 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07004207 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07004208 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07004209 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004210 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07004211 }
4212
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004213 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
4214 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004215 final TaskRecord task = taskForIdLocked(taskId);
4216 if (task == null) {
4217 return null;
4218 }
Craig Mautner16030772013-02-26 16:05:47 -08004219 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004220 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004221 if (taskRequired) {
4222 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
4223 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004224 return null;
4225 }
4226
4227 if (subTaskIndex < 0) {
4228 // Just remove the entire task.
Craig Mautner16030772013-02-26 16:05:47 -08004229 performClearTaskAtIndexLocked(task, info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004230 return info.root;
4231 }
4232
4233 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004234 if (taskRequired) {
4235 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
4236 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004237 return null;
4238 }
4239
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004240 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004241 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautner16030772013-02-26 16:05:47 -08004242 performClearTaskAtIndexLocked(task, subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004243 return subtask.activity;
4244 }
4245
Craig Mautner16030772013-02-26 16:05:47 -08004246 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004247 final TaskAccessInfo thumbs = new TaskAccessInfo();
4248 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08004249 final ArrayList<ActivityRecord> activities = task.mActivities;
4250 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004251 int j = 0;
4252 ThumbnailHolder holder = null;
4253 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08004254 ActivityRecord ar = activities.get(j);
4255 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07004256 thumbs.root = ar;
4257 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004258 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07004259 if (holder != null) {
4260 thumbs.mainThumbnail = holder.lastThumbnail;
4261 }
4262 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004263 break;
4264 }
4265 j++;
4266 }
4267
4268 if (j >= NA) {
4269 return thumbs;
4270 }
4271
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004272 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
4273 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004274 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08004275 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004276 j++;
4277 if (ar.finishing) {
4278 continue;
4279 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004280 if (ar.thumbHolder != holder && holder != null) {
4281 thumbs.numSubThumbbails++;
4282 holder = ar.thumbHolder;
4283 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07004284 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004285 sub.activity = ar;
4286 sub.index = j-1;
4287 subtasks.add(sub);
4288 }
4289 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004290 if (thumbs.numSubThumbbails > 0) {
4291 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004292 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004293 public Bitmap getThumbnail(int index) {
4294 if (index < 0 || index >= thumbs.subtasks.size()) {
4295 return null;
4296 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004297 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
4298 ActivityRecord resumed = mResumedActivity;
4299 if (resumed != null && resumed.thumbHolder == sub.holder) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004300 return screenshotActivities(resumed);
Dianne Hackborn15491c62012-09-19 10:59:14 -07004301 }
4302 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004303 }
4304 };
4305 }
4306 return thumbs;
4307 }
4308
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004309 private final void logStartActivity(int tag, ActivityRecord r,
4310 TaskRecord task) {
4311 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004312 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004313 r.shortComponentName, r.intent.getAction(),
4314 r.intent.getType(), r.intent.getDataString(),
4315 r.intent.getFlags());
4316 }
4317
4318 /**
4319 * Make sure the given activity matches the current configuration. Returns
4320 * false if the activity had to be destroyed. Returns true if the
4321 * configuration is the same, or the activity will remain running as-is
4322 * for whatever reason. Ensures the HistoryRecord is updated with the
4323 * correct configuration and all other bookkeeping is handled.
4324 */
4325 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
4326 int globalChanges) {
4327 if (mConfigWillChange) {
4328 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4329 "Skipping config check (will change): " + r);
4330 return true;
4331 }
4332
4333 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4334 "Ensuring correct configuration: " + r);
4335
4336 // Short circuit: if the two configurations are the exact same
4337 // object (the common case), then there is nothing to do.
4338 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004339 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004340 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4341 "Configuration unchanged in " + r);
4342 return true;
4343 }
4344
4345 // We don't worry about activities that are finishing.
4346 if (r.finishing) {
4347 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4348 "Configuration doesn't matter in finishing " + r);
4349 r.stopFreezingScreenLocked(false);
4350 return true;
4351 }
4352
4353 // Okay we now are going to make this activity have the new config.
4354 // But then we need to figure out how it needs to deal with that.
4355 Configuration oldConfig = r.configuration;
4356 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004357
4358 // Determine what has changed. May be nothing, if this is a config
4359 // that has come back from the app after going idle. In that case
4360 // we just want to leave the official config object now in the
4361 // activity and do nothing else.
4362 final int changes = oldConfig.diff(newConfig);
4363 if (changes == 0 && !r.forceNewConfig) {
4364 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4365 "Configuration no differences in " + r);
4366 return true;
4367 }
4368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004369 // If the activity isn't currently running, just leave the new
4370 // configuration and it will pick that up next time it starts.
4371 if (r.app == null || r.app.thread == null) {
4372 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4373 "Configuration doesn't matter not running " + r);
4374 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004375 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004376 return true;
4377 }
4378
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004379 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004380 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
4381 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
4382 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07004383 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004384 + ", newConfig=" + newConfig);
4385 }
Dianne Hackborne6676352011-06-01 16:51:20 -07004386 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004387 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4388 r.configChangeFlags |= changes;
4389 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004390 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004391 if (r.app == null || r.app.thread == null) {
4392 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004393 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07004394 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004395 } else if (r.state == ActivityState.PAUSING) {
4396 // A little annoying: we are waiting for this activity to
4397 // finish pausing. Let's not do anything now, but just
4398 // flag that it needs to be restarted when done pausing.
4399 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004400 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004401 r.configDestroy = true;
4402 return true;
4403 } else if (r.state == ActivityState.RESUMED) {
4404 // Try to optimize this case: the configuration is changing
4405 // and we need to restart the top, resumed activity.
4406 // Instead of doing the normal handshaking, just say
4407 // "restart!".
4408 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004409 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004410 relaunchActivityLocked(r, r.configChangeFlags, true);
4411 r.configChangeFlags = 0;
4412 } else {
4413 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004414 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004415 relaunchActivityLocked(r, r.configChangeFlags, false);
4416 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004417 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004418
4419 // All done... tell the caller we weren't able to keep this
4420 // activity around.
4421 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004422 }
4423
4424 // Default case: the activity can handle this new configuration, so
4425 // hand it over. Note that we don't need to give it the new
4426 // configuration, since we always send configuration changes to all
4427 // process when they happen so it can just use whatever configuration
4428 // it last got.
4429 if (r.app != null && r.app.thread != null) {
4430 try {
4431 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004432 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004433 } catch (RemoteException e) {
4434 // If process died, whatever.
4435 }
4436 }
4437 r.stopFreezingScreenLocked(false);
4438
4439 return true;
4440 }
4441
4442 private final boolean relaunchActivityLocked(ActivityRecord r,
4443 int changes, boolean andResume) {
4444 List<ResultInfo> results = null;
4445 List<Intent> newIntents = null;
4446 if (andResume) {
4447 results = r.results;
4448 newIntents = r.newIntents;
4449 }
4450 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
4451 + " with results=" + results + " newIntents=" + newIntents
4452 + " andResume=" + andResume);
4453 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004454 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004455 r.task.taskId, r.shortComponentName);
4456
4457 r.startFreezingScreenLocked(r.app, 0);
4458
4459 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004460 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
4461 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
4462 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004463 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08004464 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004465 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004466 // Note: don't need to call pauseIfSleepingLocked() here, because
4467 // the caller will only pass in 'andResume' if this activity is
4468 // currently resumed, which implies we aren't sleeping.
4469 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004470 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004471 }
4472
4473 if (andResume) {
4474 r.results = null;
4475 r.newIntents = null;
Craig Mautner2219a1b2013-03-25 09:44:30 -07004476 if (mStackSupervisor.isMainStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004477 mService.reportResumedActivityLocked(r);
4478 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004479 r.state = ActivityState.RESUMED;
4480 } else {
4481 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4482 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004483 }
4484
4485 return true;
4486 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004487
4488 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004489 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4490 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4491 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4492 final ActivityRecord r = activities.get(activityNdx);
4493 if (r.appToken == token) {
4494 return true;
4495 }
4496 if (r.fullscreen && !r.finishing) {
4497 return false;
4498 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004499 }
4500 }
4501 return true;
4502 }
4503
4504 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004505 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4506 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4507 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4508 final ActivityRecord r = activities.get(activityNdx);
4509 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004510 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004511 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004512 }
4513 }
4514 }
4515
4516 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
4517 boolean didSomething = false;
4518 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004519 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4520 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4521 int numActivities = activities.size();
4522 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4523 ActivityRecord r = activities.get(activityNdx);
4524 final boolean samePackage = r.packageName.equals(name)
4525 || (name == null && r.userId == userId);
4526 if ((userId == UserHandle.USER_ALL || r.userId == userId)
4527 && (samePackage || r.task == lastTask)
4528 && (r.app == null || evenPersistent || !r.app.persistent)) {
4529 if (!doit) {
4530 if (r.finishing) {
4531 // If this activity is just finishing, then it is not
4532 // interesting as far as something to stop.
4533 continue;
4534 }
4535 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004536 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004537 didSomething = true;
4538 Slog.i(TAG, " Force finishing activity " + r);
4539 if (samePackage) {
4540 if (r.app != null) {
4541 r.app.removed = true;
4542 }
4543 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004544 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004545 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08004546 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08004547 }
4548 }
4549 }
4550 return didSomething;
4551 }
4552
4553 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08004554 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004555 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08004556 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
4557 --maxNum, --taskNdx) {
4558 final TaskRecord task = mTaskHistory.get(taskNdx);
4559 ActivityRecord r = null;
4560 ActivityRecord top = null;
4561 int numActivities = 0;
4562 int numRunning = 0;
4563 final ArrayList<ActivityRecord> activities = task.mActivities;
4564 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4565 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004566
Craig Mautneraab647e2013-02-28 16:31:36 -08004567 // Initialize state for next task if needed.
4568 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4569 top = r;
4570 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004571 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004572
4573 // Add 'r' into the current task.
4574 numActivities++;
4575 if (r.app != null && r.app.thread != null) {
4576 numRunning++;
4577 }
4578
4579 if (localLOGV) Slog.v(
4580 TAG, r.intent.getComponent().flattenToShortString()
4581 + ": task=" + r.task);
4582 }
4583
4584 RunningTaskInfo ci = new RunningTaskInfo();
4585 ci.id = task.taskId;
4586 ci.baseActivity = r.intent.getComponent();
4587 ci.topActivity = top.intent.getComponent();
4588 if (top.thumbHolder != null) {
4589 ci.description = top.thumbHolder.lastDescription;
4590 }
4591 ci.numActivities = numActivities;
4592 ci.numRunning = numRunning;
4593 //System.out.println(
4594 // "#" + maxNum + ": " + " descr=" + ci.description);
4595 if (receiver != null) {
4596 if (localLOGV) Slog.v(
4597 TAG, "State=" + top.state + "Idle=" + top.idle
4598 + " app=" + top.app
4599 + " thr=" + (top.app != null ? top.app.thread : null));
4600 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
4601 if (top.idle && top.app != null && top.app.thread != null) {
4602 topRecord = top;
4603 } else {
4604 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004605 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004606 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004607 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08004608 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004609 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004610 }
4611 return topRecord;
4612 }
4613
4614 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004615 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004616 if (DEBUG_SWITCH) Slog.d(
4617 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004618 if (top >= 0) {
4619 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4620 int activityTop = activities.size() - 1;
4621 if (activityTop > 0) {
4622 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4623 "unhandled-back", true);
4624 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004625 }
4626 }
4627
4628 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004629 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4630 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4631 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4632 final ActivityRecord r = activities.get(activityNdx);
4633 if (r.app == app) {
4634 Slog.w(TAG, " Force finishing activity "
4635 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08004636 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004637 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004638 }
4639 }
4640 }
4641
4642 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4643 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004644 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4645 final TaskRecord task = mTaskHistory.get(taskNdx);
4646 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004647 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08004648 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
4649 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004650 }
4651
4652 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4653 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4654
4655 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4657 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004658 }
4659 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004660 final int top = mTaskHistory.size() - 1;
4661 if (top >= 0) {
4662 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4663 int listTop = list.size() - 1;
4664 if (listTop >= 0) {
4665 activities.add(list.get(listTop));
4666 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004667 }
4668 } else {
4669 ItemMatcher matcher = new ItemMatcher();
4670 matcher.build(name);
4671
Craig Mautneraab647e2013-02-28 16:31:36 -08004672 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4673 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4674 if (matcher.match(r1, r1.intent.getComponent())) {
4675 activities.add(r1);
4676 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004677 }
4678 }
4679 }
4680
4681 return activities;
4682 }
4683
4684 ActivityRecord restartPackage(String packageName) {
4685 ActivityRecord starting = topRunningActivityLocked(null);
4686
4687 // All activities that came from the package must be
4688 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004689 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4690 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4691 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4692 final ActivityRecord a = activities.get(activityNdx);
4693 if (a.info.packageName.equals(packageName)) {
4694 a.forceNewConfig = true;
4695 if (starting != null && a == starting && a.visible) {
4696 a.startFreezingScreenLocked(starting.app,
4697 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4698 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004699 }
4700 }
4701 }
4702
4703 return starting;
4704 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004705
Craig Mautner0247fc82013-02-28 14:32:06 -08004706 private void removeActivity(ActivityRecord r) {
4707 final TaskRecord task = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08004708 // TODO: use ActivityManagerService.removeTask to do this.
Craig Mautner0247fc82013-02-28 14:32:06 -08004709 if (task.removeActivity(r)) {
4710 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "removeActivity: Removing from history, task="
4711 + task);
4712 mTaskHistory.remove(task);
Craig Mautner0247fc82013-02-28 14:32:06 -08004713 }
4714 }
4715
4716 private void setTask(ActivityRecord r, TaskRecord newTask, ThumbnailHolder newThumbHolder,
4717 boolean isRoot) {
4718 if (r.task != null) {
4719 removeActivity(r);
4720 }
4721 r.setTask(newTask, newThumbHolder, isRoot);
4722 }
4723
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004724 private TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4725 boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004726 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004727 if (toTop) {
4728 mTaskHistory.add(task);
4729 } else {
4730 mTaskHistory.add(0, task);
4731 }
4732 return task;
4733 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004734
4735 ArrayList<TaskRecord> getAllTasks() {
4736 return new ArrayList<TaskRecord>(mTaskHistory);
4737 }
4738
4739 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004740 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004741 if (task == null) {
4742 return;
4743 }
4744 task.stack.mTaskHistory.remove(task);
4745 task.stack = this;
4746 if (toTop) {
4747 mTaskHistory.add(task);
4748 } else {
4749 mTaskHistory.add(0, task);
4750 }
4751 }
4752
4753 public int getStackId() {
4754 return mStackId;
4755 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004756}