blob: 859c81fb5a79f3ebae58de03ed5bda67813a57c0 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
34import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
35import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070037import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070038import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
39
Dianne Hackborn91097de2014-04-04 18:02:06 -070040import android.service.voice.IVoiceInteractionSession;
41import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070043import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080044import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080045import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080046import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080047import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049
50import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070051import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070052import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080054import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080056import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.content.res.Resources;
63import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080064import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070066import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080067import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Handler;
69import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090070import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070072import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.RemoteException;
74import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080075import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070076import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070079import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080082import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import java.lang.ref.WeakReference;
84import java.util.ArrayList;
85import java.util.Iterator;
86import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080087import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
89/**
90 * State and management of a single stack of activities.
91 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070092final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070094 // Ticks during which we check progress while waiting for an app to launch.
95 static final int LAUNCH_TICK = 500;
96
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070097 // How long we wait until giving up on the last activity to pause. This
98 // is short because it directly impacts the responsiveness of starting the
99 // next activity.
100 static final int PAUSE_TIMEOUT = 500;
101
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700102 // How long we wait for the activity to tell us it has stopped before
103 // giving up. This is a good amount of time because we really need this
104 // from the application in order to get its saved state.
105 static final int STOP_TIMEOUT = 10*1000;
106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700107 // How long we wait until giving up on an activity telling us it has
108 // finished destroying itself.
109 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700111 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800112 // disabled.
113 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800114
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700115 // How long between activity launches that we consider safe to not warn
116 // the user about an unexpected activity being launched on top.
117 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119 // Set to false to disable the preview that is shown while a new activity
120 // is being started.
121 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800122
Craig Mautner5eda9b32013-07-02 11:58:16 -0700123 // How long to wait for all background Activities to redraw following a call to
124 // convertToTranslucent().
125 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
126
Craig Mautnera61bc652013-10-28 15:43:18 -0700127 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 enum ActivityState {
130 INITIALIZING,
131 RESUMED,
132 PAUSING,
133 PAUSED,
134 STOPPING,
135 STOPPED,
136 FINISHING,
137 DESTROYING,
138 DESTROYED
139 }
140
141 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700142 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700144 /**
145 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800146 * running) activities. It contains #TaskRecord objects.
147 */
148 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
149
150 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800151 * Used for validating app tokens with window manager.
152 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800153 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800154
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700155 /**
156 * List of running activities, sorted by recent usage.
157 * The first entry in the list is the least recently used.
158 * It contains HistoryRecord objects.
159 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700160 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161
162 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700163 * Animations that for the current transition have requested not to
164 * be considered for the transition animation.
165 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700166 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * When we are in the process of pausing an activity, before starting the
170 * next one, this variable holds the activity that is currently being paused.
171 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800172 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
175 * This is the last activity that we put into the paused state. This is
176 * used to determine if we need to do an activity transition while sleeping,
177 * when we normally hold the top activity paused.
178 */
179 ActivityRecord mLastPausedActivity = null;
180
181 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700182 * Activities that specify No History must be removed once the user navigates away from them.
183 * If the device goes to sleep with such an activity in the paused state then we save it here
184 * and finish it later if another activity replaces it on wakeup.
185 */
186 ActivityRecord mLastNoHistoryActivity = null;
187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * Current activity that is resumed, or null if there is none.
190 */
191 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700194 * This is the last activity that has been started. It is only used to
195 * identify when multiple activities are started at once so that the user
196 * can be warned they may not be in the activity they think they are.
197 */
198 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800199
Craig Mautner5eda9b32013-07-02 11:58:16 -0700200 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
201 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
202 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
203 // Activity in mTranslucentActivityWaiting is notified via
204 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
205 // background activity being drawn then the same call will be made with a true value.
206 ActivityRecord mTranslucentActivityWaiting = null;
207 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
208 new ArrayList<ActivityRecord>();
Craig Mautner233ceee2014-05-09 17:05:11 -0700209 // Options passed from the caller of the convertToTranslucent to the activity that will
210 // appear below it.
211 ActivityOptions mReturningActivityOptions = null;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700212
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700213 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700214 * Set when we know we are going to be calling updateConfiguration()
215 * soon, so want to skip intermediate config checks.
216 */
217 boolean mConfigWillChange;
218
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700219 long mLaunchStartTime = 0;
220 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800221
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800222 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800223 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
224 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
225 */
226 private ActivityRecord mLastScreenshotActivity = null;
227 private Bitmap mLastScreenshotBitmap = null;
228
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800229 int mThumbnailWidth = -1;
230 int mThumbnailHeight = -1;
231
Craig Mautner858d8a62013-04-23 17:08:34 -0700232 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700233
Craig Mautnerc00204b2013-03-05 15:02:14 -0800234 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800236 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
237 ArrayList<ActivityStack> mStacks;
238 /** The attached Display's unique identifier, or -1 if detached */
239 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240
Craig Mautner27084302013-03-25 08:05:25 -0700241 /** Run all ActivityStacks through this */
242 final ActivityStackSupervisor mStackSupervisor;
243
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700244 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700245 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
246 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
247 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
248 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700249 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700250
251 static class ScheduleDestroyArgs {
252 final ProcessRecord mOwner;
253 final boolean mOomAdj;
254 final String mReason;
255 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
256 mOwner = owner;
257 mOomAdj = oomAdj;
258 mReason = reason;
259 }
260 }
261
Zoran Marcetaf958b322012-08-09 20:27:12 +0900262 final Handler mHandler;
263
264 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700265 //public Handler() {
266 // if (localLOGV) Slog.v(TAG, "Handler started!");
267 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700268 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900269 super(looper);
270 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700271
Zoran Marcetaf958b322012-08-09 20:27:12 +0900272 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273 public void handleMessage(Message msg) {
274 switch (msg.what) {
275 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800276 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700277 // We don't at this point know if the activity is fullscreen,
278 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700280 synchronized (mService) {
281 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700282 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 }
Craig Mautnera0026042014-04-23 11:45:37 -0700284 activityPausedLocked(r.appToken, true, r.persistentState);
Craig Mautnerd2328952013-03-05 12:46:26 -0800285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700287 case LAUNCH_TICK_MSG: {
288 ActivityRecord r = (ActivityRecord)msg.obj;
289 synchronized (mService) {
290 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700291 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700292 }
293 }
294 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800296 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 // We don't at this point know if the activity is fullscreen,
298 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800299 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800300 synchronized (mService) {
301 activityDestroyedLocked(r != null ? r.appToken : null);
302 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700303 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700304 case STOP_TIMEOUT_MSG: {
305 ActivityRecord r = (ActivityRecord)msg.obj;
306 // We don't at this point know if the activity is fullscreen,
307 // so we need to be conservative and assume it isn't.
308 Slog.w(TAG, "Activity stop timeout for " + r);
309 synchronized (mService) {
310 if (r.isInHistory()) {
311 activityStoppedLocked(r, null, null, null);
312 }
313 }
314 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700315 case DESTROY_ACTIVITIES_MSG: {
316 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
317 synchronized (mService) {
318 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
319 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700320 } break;
321 case TRANSLUCENT_TIMEOUT_MSG: {
322 synchronized (mService) {
323 notifyActivityDrawnLocked(null);
324 }
325 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 }
327 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800328 }
329
Craig Mautner34b73df2014-01-12 21:11:08 -0800330 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800331 int count = 0;
332 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
333 count += mTaskHistory.get(taskNdx).mActivities.size();
334 }
335 return count;
336 }
337
Craig Mautner4a1cb222013-12-04 16:14:06 -0800338 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
339 mActivityContainer = activityContainer;
340 mStackSupervisor = activityContainer.getOuter();
341 mService = mStackSupervisor.mService;
342 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
343 mWindowManager = mService.mWindowManager;
344 mStackId = activityContainer.mStackId;
345 mCurrentUser = mService.mCurrentUserId;
Winson Chung376543b2014-05-21 17:43:28 -0700346 // Get the activity screenshot thumbnail dimensions
347 Resources res = mService.mContext.getResources();
348 mThumbnailWidth =
349 res.getDimensionPixelSize(com.android.internal.R.dimen.recents_thumbnail_width);
350 mThumbnailHeight =
351 res.getDimensionPixelSize(com.android.internal.R.dimen.recents_thumbnail_height);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700352 }
Craig Mautner5962b122012-10-05 14:45:52 -0700353
Amith Yamasani734983f2014-03-04 16:48:05 -0800354 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100355 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800356 */
Kenny Guy2a764942014-04-02 13:29:20 +0100357 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100358 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100359 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
360 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800361 }
362 return false;
363 }
364
365 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100366 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700367 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
368 }
369
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700370 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800371 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700372 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
373 if (r != null) {
374 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800375 }
376 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700377 return null;
378 }
379
380 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
382 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800383 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800384 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
385 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800386 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800387 return r;
388 }
389 }
390 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700391 return null;
392 }
393
394 /**
395 * This is a simplified version of topRunningActivityLocked that provides a number of
396 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800397 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700398 * @param token If non-null, any history records matching this token will be skipped.
399 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800400 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 * @return Returns the HistoryRecord of the next activity on the stack.
402 */
403 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800404 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
405 TaskRecord task = mTaskHistory.get(taskNdx);
406 if (task.taskId == taskId) {
407 continue;
408 }
409 ArrayList<ActivityRecord> activities = task.mActivities;
410 for (int i = activities.size() - 1; i >= 0; --i) {
411 final ActivityRecord r = activities.get(i);
412 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800413 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800414 return r;
415 }
416 }
417 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700418 return null;
419 }
420
Craig Mautner8849a5e2013-04-02 16:41:03 -0700421 final ActivityRecord topActivity() {
422 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700423 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
425 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700426 final int topActivityNdx = activities.size() - 1;
427 if (topActivityNdx >= 0) {
428 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700429 }
430 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700431 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700432 }
433
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700434 final TaskRecord topTask() {
435 final int size = mTaskHistory.size();
436 if (size > 0) {
437 return mTaskHistory.get(size - 1);
438 }
439 return null;
440 }
441
Craig Mautnerd2328952013-03-05 12:46:26 -0800442 TaskRecord taskForIdLocked(int id) {
443 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
444 final TaskRecord task = mTaskHistory.get(taskNdx);
445 if (task.taskId == id) {
446 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800447 }
448 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700449 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700450 }
451
Craig Mautnerd2328952013-03-05 12:46:26 -0800452 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800453 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 if (r != null) {
455 final TaskRecord task = r.task;
456 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
457 if (task.stack != this) Slog.w(TAG,
458 "Illegal state! task does not point to stack it is in.");
459 return r;
460 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800461 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800462 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800463 }
464
Craig Mautner2420ead2013-04-01 17:13:20 -0700465 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700466 final boolean hadit = mLRUActivities.remove(r);
467 mLRUActivities.add(r);
468 return hadit;
469 }
470
Craig Mautnerde4ef022013-04-07 19:01:33 -0700471 final boolean isHomeStack() {
472 return mStackId == HOME_STACK_ID;
473 }
474
Craig Mautnere0a38842013-12-16 16:14:02 -0800475 final boolean isOnHomeDisplay() {
476 return isAttached() &&
477 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
478 }
479
480 final void moveToFront() {
481 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800482 if (isOnHomeDisplay()) {
483 mStackSupervisor.moveHomeStack(isHomeStack());
484 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800485 mStacks.remove(this);
486 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800487 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800488 }
489
490 final boolean isAttached() {
491 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800492 }
493
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700494 /**
495 * Returns the top activity in any existing task matching the given
496 * Intent. Returns null if no such task is found.
497 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700498 ActivityRecord findTaskLocked(ActivityRecord target) {
499 Intent intent = target.intent;
500 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700501 ComponentName cls = intent.getComponent();
502 if (info.targetActivity != null) {
503 cls = new ComponentName(info.packageName, info.targetActivity);
504 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700505 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700506 boolean isDocument = intent != null & intent.isDocument();
507 // If documentData is non-null then it must match the existing task data.
508 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800509
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700510 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800511 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
512 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700513 if (task.voiceSession != null) {
514 // We never match voice sessions; those always run independently.
515 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
516 continue;
517 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700518 if (task.userId != userId) {
519 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700520 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521 continue;
522 }
Craig Mautner000f0022013-02-26 15:04:29 -0800523 final ActivityRecord r = task.getTopActivity();
524 if (r == null || r.finishing || r.userId != userId ||
525 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700526 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800527 continue;
528 }
529
Craig Mautnerd00f4742014-03-12 14:17:26 -0700530 final Intent taskIntent = task.intent;
531 final Intent affinityIntent = task.affinityIntent;
532 final boolean taskIsDocument;
533 final Uri taskDocumentData;
534 if (taskIntent != null && taskIntent.isDocument()) {
535 taskIsDocument = true;
536 taskDocumentData = taskIntent.getData();
537 } else if (affinityIntent != null && affinityIntent.isDocument()) {
538 taskIsDocument = true;
539 taskDocumentData = affinityIntent.getData();
540 } else {
541 taskIsDocument = false;
542 taskDocumentData = null;
543 }
544
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700545 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700546 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700547 + "/aff=" + r.task.affinity + " to new cls="
548 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700549 if (!isDocument && !taskIsDocument && task.affinity != null) {
550 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700551 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700552 return r;
553 }
Craig Mautnerd00f4742014-03-12 14:17:26 -0700554 } else if (taskIntent != null && taskIntent.getComponent().equals(cls) &&
555 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700556 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800557 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700558 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
559 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800560 return r;
Craig Mautnerd00f4742014-03-12 14:17:26 -0700561 } else if (affinityIntent != null && affinityIntent.getComponent().equals(cls) &&
562 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700563 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800564 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700565 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
566 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800567 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700568 } else if (DEBUG_TASKS) {
569 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700570 }
571 }
572
573 return null;
574 }
575
576 /**
577 * Returns the first activity (starting from the top of the stack) that
578 * is the same as the given activity. Returns null if no such activity
579 * is found.
580 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700581 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700582 ComponentName cls = intent.getComponent();
583 if (info.targetActivity != null) {
584 cls = new ComponentName(info.packageName, info.targetActivity);
585 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700586 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587
Craig Mautner000f0022013-02-26 15:04:29 -0800588 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700589 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100590 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700591 return null;
592 }
593 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800594 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
595 ActivityRecord r = activities.get(activityNdx);
596 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700597 //Slog.i(TAG, "Found matching class!");
598 //dump();
599 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
600 return r;
601 }
602 }
603 }
604
605 return null;
606 }
607
Amith Yamasani742a6712011-05-04 14:49:28 -0700608 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700609 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700610 */
Craig Mautner93529a42013-10-04 15:03:13 -0700611 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800612 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700613 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800614 }
615 mCurrentUser = userId;
616
617 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800618 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800619 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700620 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100621 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700622 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
623 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800624 mTaskHistory.remove(i);
625 mTaskHistory.add(task);
626 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800627 // Use same value for i.
628 } else {
629 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800630 }
631 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700632 if (VALIDATE_TOKENS) {
633 validateAppTokensLocked();
634 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700635 }
636
Craig Mautner2420ead2013-04-01 17:13:20 -0700637 void minimalResumeActivityLocked(ActivityRecord r) {
638 r.state = ActivityState.RESUMED;
639 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
640 + " (starting new instance)");
641 r.stopped = false;
642 mResumedActivity = r;
643 r.task.touchActiveTime();
644 mService.addRecentTaskLocked(r.task);
645 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700646 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700647 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700648 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700649 }
650
Dianne Hackborncee04b52013-07-03 17:01:28 -0700651 private void startLaunchTraces() {
652 if (mFullyDrawnStartTime != 0) {
653 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
654 }
655 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
656 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
657 }
658
659 private void stopFullyDrawnTraceIfNeeded() {
660 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
661 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
662 mFullyDrawnStartTime = 0;
663 }
664 }
665
Craig Mautnere79d42682013-04-01 19:01:53 -0700666 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700667 if (r.displayStartTime == 0) {
668 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
669 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700670 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700671 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700672 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700673 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700674 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700675 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700676 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800678
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700679 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700680 // Make sure that there is no activity waiting for this to launch.
681 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
682 r.displayStartTime = r.fullyDrawnStartTime = 0;
683 } else {
684 mStackSupervisor.removeTimeoutsForActivityLocked(r);
685 mStackSupervisor.scheduleIdleTimeoutLocked(r);
686 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700687 }
688
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800689 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800690 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800691 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
692 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
693 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
694 activities.get(activityNdx).setSleeping(false);
695 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800696 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800697 }
698
Craig Mautner0eea92c2013-05-16 13:35:39 -0700699 /**
700 * @return true if something must be done before going to sleep.
701 */
702 boolean checkReadyForSleepLocked() {
703 if (mResumedActivity != null) {
704 // Still have something resumed; can't sleep until it is paused.
705 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
706 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
707 startPausingLocked(false, true);
708 return true;
709 }
710 if (mPausingActivity != null) {
711 // Still waiting for something to pause; can't sleep yet.
712 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
713 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800714 }
715
Craig Mautner0eea92c2013-05-16 13:35:39 -0700716 return false;
717 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800718
Craig Mautner0eea92c2013-05-16 13:35:39 -0700719 void goToSleep() {
720 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800721
Craig Mautner0eea92c2013-05-16 13:35:39 -0700722 // Make sure any stopped but visible activities are now sleeping.
723 // This ensures that the activity's onStop() is called.
724 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
725 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
726 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
727 final ActivityRecord r = activities.get(activityNdx);
728 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
729 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800730 }
731 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800732 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700733 }
Craig Mautner59c00972012-07-30 12:10:24 -0700734
Winson Chung376543b2014-05-21 17:43:28 -0700735 /**
736 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
737 * again when requested.
738 */
739 private void invalidateLastScreenshot() {
740 mLastScreenshotActivity = null;
741 if (mLastScreenshotBitmap != null) {
742 mLastScreenshotBitmap.recycle();
743 }
744 mLastScreenshotBitmap = null;
745 }
746
Dianne Hackbornd2835932010-12-13 16:28:46 -0800747 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700748 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800749 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700750 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800751 return null;
752 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800753
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700754 TaskRecord tr = who.task;
Winson Chung376543b2014-05-21 17:43:28 -0700755 if (mService.getMostRecentTask() != tr || isHomeStack()) {
756 // This is an optimization -- since we never show Home or Recents within Recents itself,
757 // we can just go ahead and skip taking the screenshot if this is the home stack. In
758 // the case where the most recent task is not the task that was supplied, then the stack
759 // has changed, so invalidate the last screenshot().
760 invalidateLastScreenshot();
761 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700762 return null;
763 }
764
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800765 int w = mThumbnailWidth;
766 int h = mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800767 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800768 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700769 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800770 || mLastScreenshotBitmap.getWidth() != w
771 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700772 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800773 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700774 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700775 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800776 }
777 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700778 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700779 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800780 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800781 }
Winson Chung376543b2014-05-21 17:43:28 -0700782 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800783 return null;
784 }
785
Craig Mautnercf910b02013-04-23 11:23:27 -0700786 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800787 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800788 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700789 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800790 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700791 ActivityRecord prev = mResumedActivity;
792 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700793 Slog.e(TAG, "Trying to pause when nothing is resumed",
794 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700795 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700796 return;
797 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800798
799 if (mActivityContainer.mParentActivity == null) {
800 // Top level stack, not a child. Look for child stacks.
801 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
802 }
803
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700804 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
805 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700806 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800807 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700809 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
810 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700811 prev.state = ActivityState.PAUSING;
812 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700813 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700814 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Craig Mautnerf6ead5d62014-04-24 10:19:20 -0700815 if (next == null || next.noDisplay || next.task != prev.task) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700816 prev.updateThumbnail(screenshotActivities(prev), null);
817 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700818 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819
820 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800821
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 if (prev.app != null && prev.app.thread != null) {
823 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
824 try {
825 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700826 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700828 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800829 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
830 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 } catch (Exception e) {
832 // Ignore exception, if process died other code will cleanup.
833 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800834 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700836 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 }
838 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800839 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700841 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842 }
843
844 // If we are not going to sleep, we want to ensure the device is
845 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700846 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700847 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848 }
849
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800850 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 // Have the window manager pause its key dispatching until the new
852 // activity has started. If we're pausing the activity just because
853 // the screen is being turned off and the UI is sleeping, don't interrupt
854 // key dispatch; the same activity will pick it up again on wakeup.
855 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 } else {
858 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
859 }
860
861 // Schedule a pause timeout in case the app doesn't respond.
862 // We don't give it much time because this directly impacts the
863 // responsiveness seen by the user.
864 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
865 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700866 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700867 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
868 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
869 } else {
870 // This activity failed to schedule the
871 // pause, so just treat it as being paused now.
872 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700873 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700874 }
875 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700876
Craig Mautnera0026042014-04-23 11:45:37 -0700877 final void activityPausedLocked(IBinder token, boolean timeout,
878 PersistableBundle persistentState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800880 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700881
Craig Mautnerd2328952013-03-05 12:46:26 -0800882 final ActivityRecord r = isInStackLocked(token);
883 if (r != null) {
884 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Craig Mautnera0026042014-04-23 11:45:37 -0700885 r.persistentState = persistentState;
Craig Mautnerd2328952013-03-05 12:46:26 -0800886 if (mPausingActivity == r) {
887 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
888 + (timeout ? " (due to timeout)" : " (pause complete)"));
889 r.state = ActivityState.PAUSED;
890 completePauseLocked();
891 } else {
892 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
893 r.userId, System.identityHashCode(r), r.shortComponentName,
894 mPausingActivity != null
895 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700896 }
897 }
898 }
899
Craig Mautnera0026042014-04-23 11:45:37 -0700900 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
901 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700902 if (r.state != ActivityState.STOPPING) {
903 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
904 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
905 return;
906 }
Craig Mautnera0026042014-04-23 11:45:37 -0700907 r.persistentState = persistentState;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700908 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700909 if (icicle != null) {
910 // If icicle is null, this is happening due to a timeout, so we
911 // haven't really saved the state.
912 r.icicle = icicle;
913 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800914 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700915 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700916 }
917 if (!r.stopped) {
918 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
919 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
920 r.stopped = true;
921 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700922 if (r.finishing) {
923 r.clearOptionsLocked();
924 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700925 if (r.configDestroy) {
926 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700927 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700928 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700929 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800930 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700931 }
932 }
933 }
934
Craig Mautnerc8143c62013-09-03 12:15:57 -0700935 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800936 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800938
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800939 if (prev != null) {
940 if (prev.finishing) {
941 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700942 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800943 } else if (prev.app != null) {
944 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
945 if (prev.waitingVisible) {
946 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700947 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800948 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
949 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800950 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 if (prev.configDestroy) {
952 // The previous is being paused because the configuration
953 // is changing, which means it is actually stopping...
954 // To juggle the fact that we are also starting a new
955 // instance right now, we need to first completely stop
956 // the current instance before starting the new one.
957 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
958 destroyActivityLocked(prev, true, false, "pause-config");
959 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700960 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700961 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
962 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800963 // If we already have a few activities waiting to stop,
964 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700965 // them out. Or if r is the last of activity of the last task the stack
966 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800967 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700968 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800969 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700970 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800971 }
972 }
973 } else {
974 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
975 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800976 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800977 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700978 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800979
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700980 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700981 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700982 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800983 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700984 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700986 if (top == null || (prev != null && top != prev)) {
987 // If there are no more activities available to run,
988 // do resume anyway to start something. Also if the top
989 // activity on the stack is not the just paused activity,
990 // we need to go ahead and resume it to ensure we complete
991 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700992 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700993 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700994 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800995
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800996 if (prev != null) {
997 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998
Craig Mautner525f3d92013-05-07 14:01:50 -0700999 if (prev.app != null && prev.cpuTimeAtResume > 0
1000 && mService.mBatteryStatsService.isOnBattery()) {
1001 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -07001002 synchronized (mService.mProcessCpuThread) {
1003 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -07001004 - prev.cpuTimeAtResume;
1005 }
1006 if (diff > 0) {
1007 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1008 synchronized (bsi) {
1009 BatteryStatsImpl.Uid.Proc ps =
1010 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001011 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001012 if (ps != null) {
1013 ps.addForegroundTimeLocked(diff);
1014 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015 }
1016 }
1017 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001018 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001019 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001020 }
1021
1022 /**
1023 * Once we know that we have asked an application to put an activity in
1024 * the resumed state (either by launching it or explicitly telling it),
1025 * this function updates the rest of our state to match that fact.
1026 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001027 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001028 next.idle = false;
1029 next.results = null;
1030 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -07001031 if (next.nowVisible) {
1032 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1033 mStackSupervisor.dismissKeyguard();
1034 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035
1036 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001037 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001038
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001039 mStackSupervisor.reportResumedActivityLocked(next);
1040
1041 next.resumeKeyDispatchingLocked();
1042 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001043
1044 // Mark the point when the activity is resuming
1045 // TODO: To be more accurate, the mark should be before the onCreate,
1046 // not after the onResume. But for subsequent starts, onResume is fine.
1047 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001048 synchronized (mService.mProcessCpuThread) {
1049 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050 }
1051 } else {
1052 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1053 }
Winson Chung376543b2014-05-21 17:43:28 -07001054
1055 // If we are resuming the activity that we had last screenshotted, then we know it will be
1056 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1057 if (next == mLastScreenshotActivity) {
1058 invalidateLastScreenshot();
1059 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001060 }
1061
Craig Mautner580ea812013-04-25 12:58:38 -07001062 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001063 * Determine if home should be visible below the passed record.
1064 * @param record activity we are querying for.
1065 * @return true if home is visible below the passed activity, false otherwise.
1066 */
1067 boolean isActivityOverHome(ActivityRecord record) {
1068 // Start at record and go down, look for either home or a visible fullscreen activity.
1069 final TaskRecord recordTask = record.task;
1070 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
1071 TaskRecord task = mTaskHistory.get(taskNdx);
1072 final ArrayList<ActivityRecord> activities = task.mActivities;
1073 final int startNdx =
1074 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
1075 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
1076 final ActivityRecord r = activities.get(activityNdx);
1077 if (r.isHomeActivity()) {
1078 return true;
1079 }
Craig Mautner76be9d22013-11-04 16:01:22 -08001080 if (!r.finishing && r.fullscreen) {
1081 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001082 return false;
1083 }
1084 }
1085 if (task.mOnTopOfHome) {
1086 // Got to the bottom of a task on top of home without finding a visible fullscreen
1087 // activity. Home is visible.
1088 return true;
1089 }
1090 }
1091 // Got to the bottom of this stack and still don't know. If this is over the home stack
1092 // then record is over home. May not work if we ever get more than two layers.
1093 return mStackSupervisor.isFrontStack(this);
1094 }
1095
Craig Mautnere3a00d72014-04-16 08:31:19 -07001096 private void setVisibile(ActivityRecord r, boolean visible) {
1097 r.visible = visible;
1098 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001099 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001100 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001101 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001102 container.setVisible(visible);
1103 }
1104 }
1105
Jose Lima7ba71252014-04-30 12:59:27 -07001106 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1107 // If so, this stack is hidden, otherwise it is visible.
1108 private boolean isStackVisible() {
1109 if (!isAttached()) {
1110 return false;
1111 }
1112
1113 if (mStackSupervisor.isFrontStack(this)) {
1114 return true;
1115 }
1116
Jose Lima729cb232014-05-05 15:59:40 -07001117 /**
1118 * Start at the task above this one and go up, looking for a visible
1119 * fullscreen activity, or a translucent activity that requested the
1120 * wallpaper to be shown behind it.
1121 */
Jose Lima7ba71252014-04-30 12:59:27 -07001122 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1123 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1124 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
1125 final ArrayList<ActivityRecord> activities = tasks.get(taskNdx).mActivities;
1126 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1127 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001128
1129 // Conditions for an activity to obscure the stack we're
1130 // examining:
1131 // 1. Not Finishing AND Visible AND:
1132 // 2. Either:
1133 // - Full Screen Activity OR
1134 // - On top of Home and our stack is NOT home
1135 if (!r.finishing && r.visible && (r.fullscreen ||
1136 (!isHomeStack() && r.frontOfTask && tasks.get(taskNdx).mOnTopOfHome))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001137 return false;
1138 }
1139 }
1140 }
1141 }
1142
1143 return true;
1144 }
1145
Jose Lima729cb232014-05-05 15:59:40 -07001146 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1147 ActivityRecord r = topRunningActivityLocked(null);
1148 if (r != null) {
1149 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1150 }
1151 }
1152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 /**
1154 * Make sure that all activities that need to be visible (that is, they
1155 * currently can be seen by the user) actually are.
1156 */
Jose Lima729cb232014-05-05 15:59:40 -07001157 final void ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1158 String onlyThisProcess, int configChanges) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001159 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 TAG, "ensureActivitiesVisible behind " + top
1161 + " configChanges=0x" + Integer.toHexString(configChanges));
1162
Craig Mautner5eda9b32013-07-02 11:58:16 -07001163 if (mTranslucentActivityWaiting != top) {
1164 mUndrawnActivitiesBelowTopTranslucent.clear();
1165 if (mTranslucentActivityWaiting != null) {
1166 // Call the callback with a timeout indication.
1167 notifyActivityDrawnLocked(null);
1168 mTranslucentActivityWaiting = null;
1169 }
1170 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1171 }
1172
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 // If the top activity is not fullscreen, then we need to
1174 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001175 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001176 boolean behindFullscreen = !isStackVisible();
1177
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001178 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001179 final TaskRecord task = mTaskHistory.get(taskNdx);
1180 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001181 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1182 final ActivityRecord r = activities.get(activityNdx);
1183 if (r.finishing) {
1184 continue;
1185 }
1186 if (aboveTop && r != top) {
1187 continue;
1188 }
1189 aboveTop = false;
1190 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001192 TAG, "Make visible? " + r + " finishing=" + r.finishing
1193 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001194
Craig Mautnerd44711d2013-02-23 11:24:36 -08001195 final boolean doThisProcess = onlyThisProcess == null
1196 || onlyThisProcess.equals(r.processName);
1197
1198 // First: if this is not the current activity being started, make
1199 // sure it matches the current configuration.
1200 if (r != starting && doThisProcess) {
1201 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001202 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001203
1204 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001205 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001206 // This activity needs to be visible, but isn't even
1207 // running... get it started, but don't resume it
1208 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001209 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001210 if (r != starting) {
1211 r.startFreezingScreenLocked(r.app, configChanges);
1212 }
1213 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001214 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001215 TAG, "Starting and making visible: " + r);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001216 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001217 }
1218 if (r != starting) {
George Mount2c92c972014-03-20 09:38:23 -07001219 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001220 }
1221 }
1222
1223 } else if (r.visible) {
1224 // If this activity is already visible, then there is nothing
1225 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001226 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001227 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001228
1229 } else if (onlyThisProcess == null) {
1230 // This activity is not currently visible, but is running.
1231 // Tell it to become visible.
1232 r.visible = true;
1233 if (r.state != ActivityState.RESUMED && r != starting) {
1234 // If this activity is paused, tell it
1235 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001236 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001237 TAG, "Making visible and scheduling visibility: " + r);
1238 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001239 if (mTranslucentActivityWaiting != null) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001240 r.updateOptionsLocked(mReturningActivityOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001241 mUndrawnActivitiesBelowTopTranslucent.add(r);
1242 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001243 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001244 r.sleeping = false;
1245 r.app.pendingUiClean = true;
1246 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1247 r.stopFreezingScreenLocked(false);
1248 } catch (Exception e) {
1249 // Just skip on any failure; we'll make it
1250 // visible when it next restarts.
1251 Slog.w(TAG, "Exception thrown making visibile: "
1252 + r.intent.getComponent(), e);
1253 }
1254 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001255 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256
Craig Mautnerd44711d2013-02-23 11:24:36 -08001257 // Aggregate current change flags.
1258 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259
Craig Mautnerd44711d2013-02-23 11:24:36 -08001260 if (r.fullscreen) {
1261 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001262 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1263 behindFullscreen = true;
Jose Lima729cb232014-05-05 15:59:40 -07001264 } else if (!isHomeStack() && r.frontOfTask && task.mOnTopOfHome) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001265 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001266 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001267 }
1268 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001269 if (DEBUG_VISBILITY) Slog.v(
1270 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1271 + " state=" + r.state
1272 + " behindFullscreen=" + behindFullscreen);
1273 // Now for any activities that aren't visible to the user, make
1274 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001276 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001277 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001278 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001279 switch (r.state) {
1280 case STOPPING:
1281 case STOPPED:
1282 if (r.app != null && r.app.thread != null) {
1283 if (DEBUG_VISBILITY) Slog.v(
1284 TAG, "Scheduling invisibility: " + r);
1285 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1286 }
1287 break;
1288
1289 case INITIALIZING:
1290 case RESUMED:
1291 case PAUSING:
1292 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001293 // This case created for transitioning activities from
1294 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001295 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1296 mStackSupervisor.mStoppingActivities.add(r);
1297 }
1298 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001299 break;
1300
1301 default:
1302 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001303 }
1304 } catch (Exception e) {
1305 // Just skip on any failure; we'll make it
1306 // visible when it next restarts.
1307 Slog.w(TAG, "Exception thrown making hidden: "
1308 + r.intent.getComponent(), e);
1309 }
1310 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001311 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 }
1314 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001315 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316 }
Craig Mautner58547802013-03-05 08:23:53 -08001317
Craig Mautner233ceee2014-05-09 17:05:11 -07001318 void convertToTranslucent(ActivityRecord r, ActivityOptions options) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001319 mTranslucentActivityWaiting = r;
1320 mUndrawnActivitiesBelowTopTranslucent.clear();
Craig Mautner233ceee2014-05-09 17:05:11 -07001321 mReturningActivityOptions = options;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001322 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1323 }
1324
1325 /**
1326 * Called as activities below the top translucent activity are redrawn. When the last one is
1327 * redrawn notify the top activity by calling
1328 * {@link Activity#onTranslucentConversionComplete}.
1329 *
1330 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1331 * occurred and the activity will be notified immediately.
1332 */
1333 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001334 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001335 if ((r == null)
1336 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1337 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1338 // The last undrawn activity below the top has just been drawn. If there is an
1339 // opaque activity at the top, notify it that it can become translucent safely now.
1340 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1341 mTranslucentActivityWaiting = null;
1342 mUndrawnActivitiesBelowTopTranslucent.clear();
1343 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1344
Craig Mautner71dd1b62014-02-18 15:48:52 -08001345 if (waitingActivity != null) {
1346 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1347 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1348 try {
1349 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1350 waitingActivity.appToken, r != null);
1351 } catch (RemoteException e) {
1352 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001353 }
1354 }
1355 }
1356 }
1357
Craig Mautnera61bc652013-10-28 15:43:18 -07001358 /** If any activities below the top running one are in the INITIALIZING state and they have a
1359 * starting window displayed then remove that starting window. It is possible that the activity
1360 * in this state will never resumed in which case that starting window will be orphaned. */
1361 void cancelInitializingActivities() {
1362 final ActivityRecord topActivity = topRunningActivityLocked(null);
1363 boolean aboveTop = true;
1364 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1365 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1366 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1367 final ActivityRecord r = activities.get(activityNdx);
1368 if (aboveTop) {
1369 if (r == topActivity) {
1370 aboveTop = false;
1371 }
1372 continue;
1373 }
1374
1375 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1376 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1377 r.mStartingWindowShown = false;
1378 mWindowManager.removeAppStartingWindow(r.appToken);
1379 }
1380 }
1381 }
1382 }
1383
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001384 /**
1385 * Ensure that the top activity in the stack is resumed.
1386 *
1387 * @param prev The previously resumed activity, for when in the process
1388 * of pausing; can be null to call from elsewhere.
1389 *
1390 * @return Returns true if something is being resumed, or false if
1391 * nothing happened.
1392 */
1393 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001394 return resumeTopActivityLocked(prev, null);
1395 }
1396
1397 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001398 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1399
Craig Mautnerdf88d732014-01-27 09:21:32 -08001400 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001401 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautner6985bad2014-04-21 15:22:06 -07001402 !mActivityContainer.isAttached()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001403 // Do not resume this stack if its parent is not resumed.
1404 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1405 return false;
1406 }
1407
Craig Mautnera61bc652013-10-28 15:43:18 -07001408 cancelInitializingActivities();
1409
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001410 // Find the first activity that is not finishing.
1411 ActivityRecord next = topRunningActivityLocked(null);
1412
1413 // Remember how we'll process this pause/resume situation, and ensure
1414 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001415 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1416 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417
1418 if (next == null) {
1419 // There are no more activities! Let's just start up the
1420 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001421 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001422 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001423 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001424 // Only resume home if on home display
1425 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 }
1427
1428 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001429
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001431 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1432 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 // Make sure we have executed any pending transitions, since there
1434 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001435 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001437 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001438 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001439 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001440 return false;
1441 }
1442
Craig Mautner525f3d92013-05-07 14:01:50 -07001443 final TaskRecord nextTask = next.task;
1444 final TaskRecord prevTask = prev != null ? prev.task : null;
bulicccd230712014-05-12 14:34:50 -07001445 if (prevTask != null && prevTask.stack == this &&
1446 prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001447 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001448 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001449 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001450 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001451 // This task is going away but it was supposed to return to the home task.
1452 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001453 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001454 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001455 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001456 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1457 "resumeTopActivityLocked: Launching home next");
1458 // Only resume home if on home display
1459 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001460 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001461 }
1462
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 // If we are sleeping, and there is no resumed activity, and the top
1464 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001465 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001466 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001467 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 // Make sure we have executed any pending transitions, since there
1469 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001470 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001472 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001473 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001474 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001475 return false;
1476 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001477
1478 // Make sure that the user who owns this activity is started. If not,
1479 // we will just leave it as is because someone should be bringing
1480 // another user's activities to the top of the stack.
1481 if (mService.mStartedUsers.get(next.userId) == null) {
1482 Slog.w(TAG, "Skipping resume of top activity " + next
1483 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001484 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001485 return false;
1486 }
1487
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 // The activity may be waiting for stop, but that is no longer
1489 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001490 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001491 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001492 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001493 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494
1495 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1496
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001497 // If we are currently pausing an activity, then don't do anything
1498 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001499 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001500 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1501 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001502 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001503 return false;
1504 }
1505
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001506 // Okay we are now going to start a switch, to 'next'. We may first
1507 // have to pause the current activity, but this is an important point
1508 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001509 // XXX "App Redirected" dialog is getting too many false positives
1510 // at this point, so turn off for now.
1511 if (false) {
1512 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1513 long now = SystemClock.uptimeMillis();
1514 final boolean inTime = mLastStartedActivity.startTime != 0
1515 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1516 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1517 final int nextUid = next.info.applicationInfo.uid;
1518 if (inTime && lastUid != nextUid
1519 && lastUid != next.launchedFromUid
1520 && mService.checkPermission(
1521 android.Manifest.permission.STOP_APP_SWITCHES,
1522 -1, next.launchedFromUid)
1523 != PackageManager.PERMISSION_GRANTED) {
1524 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1525 } else {
1526 next.startTime = now;
1527 mLastStartedActivity = next;
1528 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001529 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001530 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001531 mLastStartedActivity = next;
1532 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001533 }
Craig Mautner58547802013-03-05 08:23:53 -08001534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 // We need to start pausing the current activity so the top one
1536 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001537 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001538 if (mResumedActivity != null) {
1539 pausing = true;
1540 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001541 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001542 }
1543 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001544 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1545 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001546 // At this point we want to put the upcoming activity's process
1547 // at the top of the LRU list, since we know we will be needing it
1548 // very soon and it would be a waste to let it get killed if it
1549 // happens to be sitting towards the end.
1550 if (next.app != null && next.app.thread != null) {
1551 // No reason to do full oom adj update here; we'll let that
1552 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001553 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001554 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001555 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 return true;
1557 }
1558
Christopher Tated3f175c2012-06-14 14:16:54 -07001559 // If the most recent activity was noHistory but was only stopped rather
1560 // than stopped+finished because the device went to sleep, we need to make
1561 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001562 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001563 !mLastNoHistoryActivity.finishing) {
1564 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1565 " on new resume");
1566 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1567 null, "no-history", false);
1568 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001569 }
1570
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 if (prev != null && prev != next) {
1572 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1573 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001574 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 if (DEBUG_SWITCH) Slog.v(
1576 TAG, "Resuming top, waiting visible to hide: " + prev);
1577 } else {
1578 // The next activity is already visible, so hide the previous
1579 // activity's windows right now so we can show the new one ASAP.
1580 // We only do this if the previous is finishing, which should mean
1581 // it is on top of the one being resumed so hiding it quickly
1582 // is good. Otherwise, we want to do the normal route of allowing
1583 // the resumed activity to be shown so we can decide if the
1584 // previous should actually be hidden depending on whether the
1585 // new one is found to be full-screen or not.
1586 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001587 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1589 + prev + ", waitingVisible="
1590 + (prev != null ? prev.waitingVisible : null)
1591 + ", nowVisible=" + next.nowVisible);
1592 } else {
1593 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1594 + prev + ", waitingVisible="
1595 + (prev != null ? prev.waitingVisible : null)
1596 + ", nowVisible=" + next.nowVisible);
1597 }
1598 }
1599 }
1600
Dianne Hackborne7f97212011-02-24 14:40:20 -08001601 // Launching this app's activity, make sure the app is no longer
1602 // considered stopped.
1603 try {
1604 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001605 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001606 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001607 } catch (IllegalArgumentException e) {
1608 Slog.w(TAG, "Failed trying to unstop package "
1609 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001610 }
1611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 // We are starting up the next activity, so tell the window manager
1613 // that the previous one will be hidden soon. This way it can know
1614 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001615 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 if (prev != null) {
1617 if (prev.finishing) {
1618 if (DEBUG_TRANSITION) Slog.v(TAG,
1619 "Prepare close transition: prev=" + prev);
1620 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001621 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001622 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001624 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001625 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1626 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001627 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001628 mWindowManager.setAppWillBeHidden(prev.appToken);
1629 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001631 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001633 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001634 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001636 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001637 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1638 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001639 }
1640 }
1641 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001642 mWindowManager.setAppWillBeHidden(prev.appToken);
1643 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 }
Craig Mautner967212c2013-04-13 21:10:58 -07001645 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001646 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001647 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001648 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001649 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001650 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001651 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 }
1653 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001654
1655 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001656 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001657 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1658 if (opts != null) {
1659 resumeAnimOptions = opts.toBundle();
1660 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001661 next.applyOptionsLocked();
1662 } else {
1663 next.clearOptionsLocked();
1664 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665
Craig Mautnercf910b02013-04-23 11:23:27 -07001666 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 if (next.app != null && next.app.thread != null) {
1668 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1669
1670 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001671 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001672
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001673 // schedule launch ticks to collect information about slow apps.
1674 next.startLaunchTickingLocked();
1675
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001676 ActivityRecord lastResumedActivity =
1677 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001678 ActivityState lastState = next.state;
1679
1680 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001681
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001682 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001683 next.state = ActivityState.RESUMED;
1684 mResumedActivity = next;
1685 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001686 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001687 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001689 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690
1691 // Have the window manager re-evaluate the orientation of
1692 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001693 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001694 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001695 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001696 mService.mConfiguration,
1697 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1698 if (config != null) {
1699 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001701 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001703
Craig Mautner525f3d92013-05-07 14:01:50 -07001704 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 // The configuration update wasn't able to keep the existing
1706 // instance of the activity, and instead started a new one.
1707 // We should be all done, but let's just make sure our activity
1708 // is still at the top and schedule another run if something
1709 // weird happened.
1710 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001711 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001712 "Activity config changed during resume: " + next
1713 + ", new next: " + nextNext);
1714 if (nextNext != next) {
1715 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001716 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001717 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001718 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001719 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001720 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001721 return true;
1722 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001723 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001724 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 }
Craig Mautner58547802013-03-05 08:23:53 -08001726
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 try {
1728 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001729 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 if (a != null) {
1731 final int N = a.size();
1732 if (!next.finishing && N > 0) {
1733 if (DEBUG_RESULTS) Slog.v(
1734 TAG, "Delivering results to " + next
1735 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001736 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001737 }
1738 }
1739
1740 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001741 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 }
1743
1744 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001745 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001747
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001748 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001749 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001750 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001751 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001752 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001753 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001754 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001755
Craig Mautner0eea92c2013-05-16 13:35:39 -07001756 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757
Craig Mautnerac6f8432013-07-17 13:24:59 -07001758 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001759 } 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;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001764 if (lastStack != null) {
1765 lastStack.mResumedActivity = lastResumedActivity;
1766 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 Slog.i(TAG, "Restarting because process died: " + next);
1768 if (!next.hasBeenLaunched) {
1769 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001770 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1771 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001772 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001773 next.appToken, next.packageName, next.theme,
1774 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001775 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1776 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 }
George Mount2c92c972014-03-20 09:38:23 -07001778 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001779 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 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);
Craig Mautnercf910b02013-04-23 11:23:27 -07001794 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795 return true;
1796 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 next.stopped = false;
1798
1799 } else {
1800 // Whoops, need to restart this activity!
1801 if (!next.hasBeenLaunched) {
1802 next.hasBeenLaunched = true;
1803 } else {
1804 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001805 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001806 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001807 mService.compatibilityInfoForPackageLocked(
1808 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001810 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001811 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 }
1813 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1814 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001815 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001816 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 }
1818
Craig Mautnercf910b02013-04-23 11:23:27 -07001819 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 return true;
1821 }
1822
Craig Mautnerac6f8432013-07-17 13:24:59 -07001823 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001824 // If this is being moved to the top by another activity or being launched from the home
1825 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001826 if (isOnHomeDisplay()) {
1827 ActivityStack lastStack = mStackSupervisor.getLastStack();
1828 final boolean fromHome = lastStack.isHomeStack();
1829 if (!isHomeStack() && (fromHome || topTask() != task)) {
1830 task.mOnTopOfHome = fromHome;
1831 }
1832 } else {
1833 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001834 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001835
Craig Mautnerac6f8432013-07-17 13:24:59 -07001836 mTaskHistory.remove(task);
1837 // Now put task at top.
1838 int stackNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001839 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001840 // Put non-current user tasks below current user tasks.
1841 while (--stackNdx >= 0) {
Kenny Guy2a764942014-04-02 13:29:20 +01001842 if (!isCurrentProfileLocked(mTaskHistory.get(stackNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001843 break;
1844 }
1845 }
1846 ++stackNdx;
1847 }
1848 mTaskHistory.add(stackNdx, task);
1849 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001850
Craig Mautner8849a5e2013-04-02 16:41:03 -07001851 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001852 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001853 TaskRecord rTask = r.task;
1854 final int taskId = rTask.taskId;
1855 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001856 // Last activity in task had been removed or ActivityManagerService is reusing task.
1857 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001858 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001859 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001860 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001861 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001862 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001863 if (!newTask) {
1864 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001865 boolean startIt = true;
1866 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1867 task = mTaskHistory.get(taskNdx);
1868 if (task == r.task) {
1869 // Here it is! Now, if this is not yet visible to the
1870 // user, then just add it without starting; it will
1871 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001872 if (!startIt) {
1873 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1874 + task, new RuntimeException("here").fillInStackTrace());
1875 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001876 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001877 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1878 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001879 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001880 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001882 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001884 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 return;
1886 }
1887 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001888 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001889 startIt = false;
1890 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001891 }
1892 }
1893
1894 // Place a new activity at top of stack, so it is next to interact
1895 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001896
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 // If we are not placing the new activity frontmost, we do not want
1898 // to deliver the onUserLeaving callback to the actual frontmost
1899 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001900 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001901 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001902 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1903 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001904 }
Craig Mautner70a86932013-02-28 22:37:44 -08001905
1906 task = r.task;
1907
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001908 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001909 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001910 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001911 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001912 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001913
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001914 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001915 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001916 // We want to show the starting preview window if we are
1917 // switching to a new task, or the next activity's process is
1918 // not currently running.
1919 boolean showStartingIcon = newTask;
1920 ProcessRecord proc = r.app;
1921 if (proc == null) {
1922 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1923 }
1924 if (proc == null || proc.thread == null) {
1925 showStartingIcon = true;
1926 }
1927 if (DEBUG_TRANSITION) Slog.v(TAG,
1928 "Prepare open transition: starting " + r);
1929 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001930 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001931 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001932 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001933 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001934 ? AppTransition.TRANSIT_TASK_OPEN
1935 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001936 mNoAnimActivities.remove(r);
1937 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001938 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001939 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001940 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1941 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001942 boolean doShow = true;
1943 if (newTask) {
1944 // Even though this activity is starting fresh, we still need
1945 // to reset it to make sure we apply affinities to move any
1946 // existing activities from other tasks in to it.
1947 // If the caller has requested that the target task be
1948 // reset, then do so.
1949 if ((r.intent.getFlags()
Craig Mautnerd00f4742014-03-12 14:17:26 -07001950 & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951 resetTaskIfNeededLocked(r, r);
1952 doShow = topRunningNonDelayedActivityLocked(null) == r;
1953 }
1954 }
1955 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1956 // Figure out if we are transitioning from another activity that is
1957 // "has the same starting icon" as the next one. This allows the
1958 // window manager to keep the previous window it had previously
1959 // created, if it still had one.
1960 ActivityRecord prev = mResumedActivity;
1961 if (prev != null) {
1962 // We don't want to reuse the previous starting preview if:
1963 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001964 if (prev.task != r.task) {
1965 prev = null;
1966 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001968 else if (prev.nowVisible) {
1969 prev = null;
1970 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001971 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001972 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001973 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001974 mService.compatibilityInfoForPackageLocked(
1975 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001976 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001977 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07001978 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001979 }
1980 } else {
1981 // If this is the first activity, don't do any fancy animations,
1982 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001983 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001984 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001985 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1986 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001987 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07001988 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989 }
1990 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001991 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001992 }
1993
1994 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001995 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001996 }
1997 }
1998
Dianne Hackbornbe707852011-11-11 14:32:10 -08001999 final void validateAppTokensLocked() {
2000 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002001 mValidateAppTokens.ensureCapacity(numActivities());
2002 final int numTasks = mTaskHistory.size();
2003 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2004 TaskRecord task = mTaskHistory.get(taskNdx);
2005 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002006 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002007 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002008 }
Craig Mautner000f0022013-02-26 15:04:29 -08002009 TaskGroup group = new TaskGroup();
2010 group.taskId = task.taskId;
2011 mValidateAppTokens.add(group);
2012 final int numActivities = activities.size();
2013 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2014 final ActivityRecord r = activities.get(activityNdx);
2015 group.tokens.add(r.appToken);
2016 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002017 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002018 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002019 }
2020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002021 /**
2022 * Perform a reset of the given task, if needed as part of launching it.
2023 * Returns the new HistoryRecord at the top of the task.
2024 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002025 /**
2026 * Helper method for #resetTaskIfNeededLocked.
2027 * We are inside of the task being reset... we'll either finish this activity, push it out
2028 * for another task, or leave it as-is.
2029 * @param task The task containing the Activity (taskTop) that might be reset.
2030 * @param forceReset
2031 * @return An ActivityOptions that needs to be processed.
2032 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002033 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002034 ActivityOptions topOptions = null;
2035
2036 int replyChainEnd = -1;
2037 boolean canMoveOptions = true;
2038
2039 // We only do this for activities that are not the root of the task (since if we finish
2040 // the root, we may no longer have the task!).
2041 final ArrayList<ActivityRecord> activities = task.mActivities;
2042 final int numActivities = activities.size();
2043 for (int i = numActivities - 1; i > 0; --i ) {
2044 ActivityRecord target = activities.get(i);
2045
2046 final int flags = target.info.flags;
2047 final boolean finishOnTaskLaunch =
2048 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2049 final boolean allowTaskReparenting =
2050 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2051 final boolean clearWhenTaskReset =
2052 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2053
2054 if (!finishOnTaskLaunch
2055 && !clearWhenTaskReset
2056 && target.resultTo != null) {
2057 // If this activity is sending a reply to a previous
2058 // activity, we can't do anything with it now until
2059 // we reach the start of the reply chain.
2060 // XXX note that we are assuming the result is always
2061 // to the previous activity, which is almost always
2062 // the case but we really shouldn't count on.
2063 if (replyChainEnd < 0) {
2064 replyChainEnd = i;
2065 }
2066 } else if (!finishOnTaskLaunch
2067 && !clearWhenTaskReset
2068 && allowTaskReparenting
2069 && target.taskAffinity != null
2070 && !target.taskAffinity.equals(task.affinity)) {
2071 // If this activity has an affinity for another
2072 // task, then we need to move it out of here. We will
2073 // move it as far out of the way as possible, to the
2074 // bottom of the activity stack. This also keeps it
2075 // correctly ordered with any activities we previously
2076 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002077 final ThumbnailHolder newThumbHolder;
2078 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002079 final ActivityRecord bottom =
2080 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002081 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002082 if (bottom != null && target.taskAffinity != null
2083 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002084 // If the activity currently at the bottom has the
2085 // same task affinity as the one we are moving,
2086 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002087 targetTask = bottom.task;
2088 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002089 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002090 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002091 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002092 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002093 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002094 newThumbHolder = targetTask;
2095 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002096 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2097 + " out to new task " + target.task);
2098 }
2099
Craig Mautnerd00f4742014-03-12 14:17:26 -07002100 target.thumbHolder = newThumbHolder;
Craig Mautner329f4122013-11-07 09:10:42 -08002101
Craig Mautnere3a74d52013-02-22 14:14:58 -08002102 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002103 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104
Craig Mautner525f3d92013-05-07 14:01:50 -07002105 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002106 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2107 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002108 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002109 if (p.finishing) {
2110 continue;
2111 }
2112
Craig Mautner525f3d92013-05-07 14:01:50 -07002113 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002114 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002115 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002116 topOptions = p.takeOptionsLocked();
2117 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002118 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002119 }
2120 }
2121 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002122 + task + " adding to task=" + targetTask
2123 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002124 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2125 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002126 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002127 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002128
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002129 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002130 }
2131
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002132 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002133 if (VALIDATE_TOKENS) {
2134 validateAppTokensLocked();
2135 }
2136
2137 replyChainEnd = -1;
2138 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2139 // If the activity should just be removed -- either
2140 // because it asks for it, or the task should be
2141 // cleared -- then finish it and anything that is
2142 // part of its reply chain.
2143 int end;
2144 if (clearWhenTaskReset) {
2145 // In this case, we want to finish this activity
2146 // and everything above it, so be sneaky and pretend
2147 // like these are all in the reply chain.
2148 end = numActivities - 1;
2149 } else if (replyChainEnd < 0) {
2150 end = i;
2151 } else {
2152 end = replyChainEnd;
2153 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002154 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002155 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002156 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002157 if (p.finishing) {
2158 continue;
2159 }
2160 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002161 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002162 topOptions = p.takeOptionsLocked();
2163 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002164 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002165 }
2166 }
Craig Mautner58547802013-03-05 08:23:53 -08002167 if (DEBUG_TASKS) Slog.w(TAG,
2168 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002169 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002170 end--;
2171 srcPos--;
2172 }
2173 }
2174 replyChainEnd = -1;
2175 } else {
2176 // If we were in the middle of a chain, well the
2177 // activity that started it all doesn't want anything
2178 // special, so leave it all as-is.
2179 replyChainEnd = -1;
2180 }
2181 }
2182
2183 return topOptions;
2184 }
2185
2186 /**
2187 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2188 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2189 * @param affinityTask The task we are looking for an affinity to.
2190 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2191 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2192 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2193 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002194 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002195 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002196 int replyChainEnd = -1;
2197 final int taskId = task.taskId;
2198 final String taskAffinity = task.affinity;
2199
2200 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2201 final int numActivities = activities.size();
2202 // Do not operate on the root Activity.
2203 for (int i = numActivities - 1; i > 0; --i) {
2204 ActivityRecord target = activities.get(i);
2205
2206 final int flags = target.info.flags;
2207 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2208 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2209
2210 if (target.resultTo != null) {
2211 // If this activity is sending a reply to a previous
2212 // activity, we can't do anything with it now until
2213 // we reach the start of the reply chain.
2214 // XXX note that we are assuming the result is always
2215 // to the previous activity, which is almost always
2216 // the case but we really shouldn't count on.
2217 if (replyChainEnd < 0) {
2218 replyChainEnd = i;
2219 }
2220 } else if (topTaskIsHigher
2221 && allowTaskReparenting
2222 && taskAffinity != null
2223 && taskAffinity.equals(target.taskAffinity)) {
2224 // This activity has an affinity for our task. Either remove it if we are
2225 // clearing or move it over to our task. Note that
2226 // we currently punt on the case where we are resetting a
2227 // task that is not at the top but who has activities above
2228 // with an affinity to it... this is really not a normal
2229 // case, and we will need to later pull that task to the front
2230 // and usually at that point we will do the reset and pick
2231 // up those remaining activities. (This only happens if
2232 // someone starts an activity in a new task from an activity
2233 // in a task that is not currently on top.)
2234 if (forceReset || finishOnTaskLaunch) {
2235 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2236 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2237 for (int srcPos = start; srcPos >= i; --srcPos) {
2238 final ActivityRecord p = activities.get(srcPos);
2239 if (p.finishing) {
2240 continue;
2241 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002242 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002243 }
2244 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002245 if (taskInsertionPoint < 0) {
2246 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002247
Craig Mautner77878772013-03-04 19:46:24 -08002248 }
Craig Mautner77878772013-03-04 19:46:24 -08002249
2250 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2251 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2252 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2253 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002254 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002255 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002256 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002257
Craig Mautnere3a74d52013-02-22 14:14:58 -08002258 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2259 + " to stack at " + task,
2260 new RuntimeException("here").fillInStackTrace());
2261 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2262 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002263 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002264 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002265 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002266 if (VALIDATE_TOKENS) {
2267 validateAppTokensLocked();
2268 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002269
2270 // Now we've moved it in to place... but what if this is
2271 // a singleTop activity and we have put it on top of another
2272 // instance of the same activity? Then we drop the instance
2273 // below so it remains singleTop.
2274 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2275 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002276 int targetNdx = taskActivities.indexOf(target);
2277 if (targetNdx > 0) {
2278 ActivityRecord p = taskActivities.get(targetNdx - 1);
2279 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002280 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2281 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282 }
2283 }
2284 }
2285 }
2286
2287 replyChainEnd = -1;
2288 }
2289 }
Craig Mautner77878772013-03-04 19:46:24 -08002290 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002291 }
2292
Craig Mautner8849a5e2013-04-02 16:41:03 -07002293 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002294 ActivityRecord newActivity) {
2295 boolean forceReset =
2296 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2297 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2298 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2299 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2300 forceReset = true;
2301 }
2302 }
2303
2304 final TaskRecord task = taskTop.task;
2305
2306 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2307 * for remaining tasks. Used for later tasks to reparent to task. */
2308 boolean taskFound = false;
2309
2310 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2311 ActivityOptions topOptions = null;
2312
Craig Mautner77878772013-03-04 19:46:24 -08002313 // Preserve the location for reparenting in the new task.
2314 int reparentInsertionPoint = -1;
2315
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2317 final TaskRecord targetTask = mTaskHistory.get(i);
2318
2319 if (targetTask == task) {
2320 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2321 taskFound = true;
2322 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002323 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2324 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002325 }
2326 }
2327
Craig Mautner70a86932013-02-28 22:37:44 -08002328 int taskNdx = mTaskHistory.indexOf(task);
2329 do {
2330 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2331 } while (taskTop == null && taskNdx >= 0);
2332
Craig Mautnere3a74d52013-02-22 14:14:58 -08002333 if (topOptions != null) {
2334 // If we got some ActivityOptions from an activity on top that
2335 // was removed from the task, propagate them to the new real top.
2336 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002337 taskTop.updateOptionsLocked(topOptions);
2338 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002339 topOptions.abort();
2340 }
2341 }
2342
2343 return taskTop;
2344 }
2345
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2347 String resultWho, int requestCode, int resultCode, Intent data) {
2348
2349 if (callingUid > 0) {
2350 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002351 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 }
2353
2354 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2355 + " : who=" + resultWho + " req=" + requestCode
2356 + " res=" + resultCode + " data=" + data);
2357 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2358 try {
2359 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2360 list.add(new ResultInfo(resultWho, requestCode,
2361 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002362 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002363 return;
2364 } catch (Exception e) {
2365 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2366 }
2367 }
2368
2369 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2370 }
2371
Craig Mautner04f0b702013-10-22 12:31:01 -07002372 private void adjustFocusedActivityLocked(ActivityRecord r) {
2373 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2374 ActivityRecord next = topRunningActivityLocked(null);
2375 if (next != r) {
2376 final TaskRecord task = r.task;
2377 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2378 mStackSupervisor.moveHomeToTop();
2379 }
2380 }
Winson Chung648c83b2014-04-28 15:11:56 -07002381 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2382 if (top != null) {
2383 mService.setFocusedActivityLocked(top);
2384 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002385 }
2386 }
2387
Craig Mautnerf3333272013-04-22 10:55:53 -07002388 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2390 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2391 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2392 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002393 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002394 if (DEBUG_STATES) {
2395 Slog.d(TAG, "no-history finish of " + r);
2396 }
2397 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002398 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002399 } else {
2400 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2401 + " on stop because we're just sleeping");
2402 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002403 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002404 }
2405
2406 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002407 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002408 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 try {
2410 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002411 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2412 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002413 r.state = ActivityState.STOPPING;
2414 if (DEBUG_VISBILITY) Slog.v(
2415 TAG, "Stopping visible=" + r.visible + " for " + r);
2416 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002417 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002418 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002419 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002420 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002421 r.setSleeping(true);
2422 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002423 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002424 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 } catch (Exception e) {
2426 // Maybe just ignore exceptions here... if the process
2427 // has crashed, our death notification will clean things
2428 // up.
2429 Slog.w(TAG, "Exception thrown during pause", e);
2430 // Just in case, assume it to be stopped.
2431 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002432 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 r.state = ActivityState.STOPPED;
2434 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002435 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 }
2437 }
2438 }
2439 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002440
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 /**
2442 * @return Returns true if the activity is being finished, false if for
2443 * some reason it is being left as-is.
2444 */
2445 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002446 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002447 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002448 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002449 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002450 + ", result=" + resultCode + ", data=" + resultData
2451 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002452 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002453 return false;
2454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002455
Craig Mautnerd44711d2013-02-23 11:24:36 -08002456 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 return true;
2458 }
2459
Craig Mautnerd2328952013-03-05 12:46:26 -08002460 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002461 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2462 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2463 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2464 ActivityRecord r = activities.get(activityNdx);
2465 if (r.resultTo == self && r.requestCode == requestCode) {
2466 if ((r.resultWho == null && resultWho == null) ||
2467 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2468 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2469 false);
2470 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002471 }
2472 }
2473 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002474 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002475 }
2476
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002477 final void finishTopRunningActivityLocked(ProcessRecord app) {
2478 ActivityRecord r = topRunningActivityLocked(null);
2479 if (r != null && r.app == app) {
2480 // If the top running activity is from this crashing
2481 // process, then terminate it to avoid getting in a loop.
2482 Slog.w(TAG, " Force finishing activity "
2483 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002484 int taskNdx = mTaskHistory.indexOf(r.task);
2485 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002486 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002487 // Also terminate any activities below it that aren't yet
2488 // stopped, to avoid a situation where one will get
2489 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002490 --activityNdx;
2491 if (activityNdx < 0) {
2492 do {
2493 --taskNdx;
2494 if (taskNdx < 0) {
2495 break;
2496 }
2497 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2498 } while (activityNdx < 0);
2499 }
2500 if (activityNdx >= 0) {
2501 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002502 if (r.state == ActivityState.RESUMED
2503 || r.state == ActivityState.PAUSING
2504 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002505 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002506 Slog.w(TAG, " Force finishing activity "
2507 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002508 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002509 }
2510 }
2511 }
2512 }
2513 }
2514
Craig Mautnerd2328952013-03-05 12:46:26 -08002515 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002516 ArrayList<ActivityRecord> activities = r.task.mActivities;
2517 for (int index = activities.indexOf(r); index >= 0; --index) {
2518 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002519 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002520 break;
2521 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002522 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002523 }
2524 return true;
2525 }
2526
Dianne Hackborn5c607432012-02-28 14:44:19 -08002527 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2528 // send the result
2529 ActivityRecord resultTo = r.resultTo;
2530 if (resultTo != null) {
2531 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2532 + " who=" + r.resultWho + " req=" + r.requestCode
2533 + " res=" + resultCode + " data=" + resultData);
2534 if (r.info.applicationInfo.uid > 0) {
2535 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2536 resultTo.packageName, resultData,
2537 resultTo.getUriPermissionsLocked());
2538 }
2539 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2540 resultData);
2541 r.resultTo = null;
2542 }
2543 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2544
2545 // Make sure this HistoryRecord is not holding on to other resources,
2546 // because clients have remote IPC references to this object so we
2547 // can't assume that will go away and want to avoid circular IPC refs.
2548 r.results = null;
2549 r.pendingResults = null;
2550 r.newIntents = null;
2551 r.icicle = null;
2552 }
2553
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002554 /**
2555 * @return Returns true if this activity has been removed from the history
2556 * list, or false if it is still in the list and will be removed later.
2557 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002558 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2559 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 if (r.finishing) {
2561 Slog.w(TAG, "Duplicate finish request for " + r);
2562 return false;
2563 }
2564
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002565 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002566 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002568 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002569 task.taskId, r.shortComponentName, reason);
2570 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002571 final int index = activities.indexOf(r);
2572 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002573 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002574 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002575 // If the caller asked that this activity (and all above it)
2576 // be cleared when the task is reset, don't lose that information,
2577 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002578 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002579 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002580 }
2581 }
2582
2583 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002584
2585 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586
Dianne Hackborn5c607432012-02-28 14:44:19 -08002587 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002588
Craig Mautnerde4ef022013-04-07 19:01:33 -07002589 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002590 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002591 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002592 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002593 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002594 ? AppTransition.TRANSIT_TASK_CLOSE
2595 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002596
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002598 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002599
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002600 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002601 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2602 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2603 startPausingLocked(false, false);
2604 }
2605
Craig Mautneraea74a52014-03-08 14:23:10 -08002606 if (endTask) {
2607 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2608 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002609 } else if (r.state != ActivityState.PAUSING) {
2610 // If the activity is PAUSING, we will complete the finish once
2611 // it is done pausing; else we can just directly finish it here.
2612 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002613 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 } else {
2615 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2616 }
2617
2618 return false;
2619 }
2620
Craig Mautnerf3333272013-04-22 10:55:53 -07002621 static final int FINISH_IMMEDIATELY = 0;
2622 static final int FINISH_AFTER_PAUSE = 1;
2623 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624
Craig Mautnerf3333272013-04-22 10:55:53 -07002625 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002626 // First things first: if this activity is currently visible,
2627 // and the resumed activity is not yet visible, then hold off on
2628 // finishing until the resumed one becomes visible.
2629 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002630 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2631 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002632 if (mStackSupervisor.mStoppingActivities.size() > 3
2633 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002634 // If we already have a few activities waiting to stop,
2635 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002636 // them out. Or if r is the last of activity of the last task the stack
2637 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002638 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002639 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002640 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 }
2642 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002643 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2644 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002645 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002646 if (oomAdj) {
2647 mService.updateOomAdjLocked();
2648 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002649 return r;
2650 }
2651
2652 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002653 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002654 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002655 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 if (mResumedActivity == r) {
2657 mResumedActivity = null;
2658 }
2659 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002660 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 r.state = ActivityState.FINISHING;
2662
2663 if (mode == FINISH_IMMEDIATELY
2664 || prevState == ActivityState.STOPPED
2665 || prevState == ActivityState.INITIALIZING) {
2666 // If this activity is already stopped, we can just finish
2667 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002668 boolean activityRemoved = destroyActivityLocked(r, true,
2669 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002670 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002671 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002672 }
2673 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002675
2676 // Need to go through the full pause cycle to get this
2677 // activity into the stopped state and then finish it.
2678 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002679 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002680 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002681 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 return r;
2683 }
2684
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002685 void finishAllActivitiesLocked() {
2686 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2687 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2688 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2689 final ActivityRecord r = activities.get(activityNdx);
2690 if (r.finishing) {
2691 continue;
2692 }
2693 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
2694 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2695 }
2696 }
2697 }
2698
Craig Mautnerd2328952013-03-05 12:46:26 -08002699 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002700 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002701 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002702 final TaskRecord task = srec.task;
2703 final ArrayList<ActivityRecord> activities = task.mActivities;
2704 final int start = activities.indexOf(srec);
2705 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002706 return false;
2707 }
2708 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002709 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002710 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002711 final ComponentName dest = destIntent.getComponent();
2712 if (start > 0 && dest != null) {
2713 for (int i = finishTo; i >= 0; i--) {
2714 ActivityRecord r = activities.get(i);
2715 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002716 r.info.name.equals(dest.getClassName())) {
2717 finishTo = i;
2718 parent = r;
2719 foundParentInTask = true;
2720 break;
2721 }
2722 }
2723 }
2724
2725 IActivityController controller = mService.mController;
2726 if (controller != null) {
2727 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2728 if (next != null) {
2729 // ask watcher if this is allowed
2730 boolean resumeOK = true;
2731 try {
2732 resumeOK = controller.activityResuming(next.packageName);
2733 } catch (RemoteException e) {
2734 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002735 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002736 }
2737
2738 if (!resumeOK) {
2739 return false;
2740 }
2741 }
2742 }
2743 final long origId = Binder.clearCallingIdentity();
2744 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002745 ActivityRecord r = activities.get(i);
2746 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002747 // Only return the supplied result for the first activity finished
2748 resultCode = Activity.RESULT_CANCELED;
2749 resultData = null;
2750 }
2751
2752 if (parent != null && foundParentInTask) {
2753 final int parentLaunchMode = parent.info.launchMode;
2754 final int destIntentFlags = destIntent.getFlags();
2755 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2756 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2757 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2758 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2759 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2760 } else {
2761 try {
2762 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2763 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002764 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002765 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002766 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002767 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002768 foundParentInTask = res == ActivityManager.START_SUCCESS;
2769 } catch (RemoteException e) {
2770 foundParentInTask = false;
2771 }
2772 requestFinishActivityLocked(parent.appToken, resultCode,
2773 resultData, "navigate-up", true);
2774 }
2775 }
2776 Binder.restoreCallingIdentity(origId);
2777 return foundParentInTask;
2778 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 /**
2780 * Perform the common clean-up of an activity record. This is called both
2781 * as part of destroyActivityLocked() (when destroying the client-side
2782 * representation) and cleaning things up as a result of its hosting
2783 * processing going away, in which case there is no remaining client-side
2784 * state to destroy so only the cleanup here is needed.
2785 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002786 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2787 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002788 if (mResumedActivity == r) {
2789 mResumedActivity = null;
2790 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002791 if (mPausingActivity == r) {
2792 mPausingActivity = null;
2793 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002794 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795
2796 r.configDestroy = false;
2797 r.frozenBeforeDestroy = false;
2798
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002799 if (setState) {
2800 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2801 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002802 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002803 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002804 }
2805
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 // Make sure this record is no longer in the pending finishes list.
2807 // This could happen, for example, if we are trimming activities
2808 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002809 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002810 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002811
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002812 // Remove any pending results.
2813 if (r.finishing && r.pendingResults != null) {
2814 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2815 PendingIntentRecord rec = apr.get();
2816 if (rec != null) {
2817 mService.cancelIntentSenderLocked(rec, false);
2818 }
2819 }
2820 r.pendingResults = null;
2821 }
2822
2823 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002824 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002825 }
2826
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002828 removeTimeoutsForActivityLocked(r);
2829 }
2830
2831 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002832 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002833 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002834 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002835 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002836 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002837 }
2838
Craig Mautner04a0ea62014-01-13 12:51:26 -08002839 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002840 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002841 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2842 r.makeFinishing();
2843 if (DEBUG_ADD_REMOVE) {
2844 RuntimeException here = new RuntimeException("here");
2845 here.fillInStackTrace();
2846 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002848 r.takeFromHistory();
2849 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002850 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002851 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002852 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002853 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002854 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002855 if (VALIDATE_TOKENS) {
2856 validateAppTokensLocked();
2857 }
Craig Mautner312ba862014-02-10 17:55:01 -08002858 final TaskRecord task = r.task;
2859 if (task != null && task.removeActivity(r)) {
2860 if (DEBUG_STACK) Slog.i(TAG,
2861 "removeActivityFromHistoryLocked: last activity removed from " + this);
2862 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
2863 mStackSupervisor.moveHomeToTop();
2864 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002865 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08002866 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002867 cleanUpActivityServicesLocked(r);
2868 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002869 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002870
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871 /**
2872 * Perform clean-up of service connections in an activity record.
2873 */
2874 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2875 // Throw away any services that have been bound by this activity.
2876 if (r.connections != null) {
2877 Iterator<ConnectionRecord> it = r.connections.iterator();
2878 while (it.hasNext()) {
2879 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002880 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881 }
2882 r.connections = null;
2883 }
2884 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002885
2886 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2887 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2888 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2889 mHandler.sendMessage(msg);
2890 }
2891
Dianne Hackborn28695e02011-11-02 21:59:51 -07002892 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002893 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002894 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002895 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2896 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2897 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2898 final ActivityRecord r = activities.get(activityNdx);
2899 if (r.finishing) {
2900 continue;
2901 }
2902 if (r.fullscreen) {
2903 lastIsOpaque = true;
2904 }
2905 if (owner != null && r.app != owner) {
2906 continue;
2907 }
2908 if (!lastIsOpaque) {
2909 continue;
2910 }
2911 // We can destroy this one if we have its icicle saved and
2912 // it is not in the process of pausing/stopping/finishing.
2913 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2914 && r.haveState && !r.visible && r.stopped
2915 && r.state != ActivityState.DESTROYING
2916 && r.state != ActivityState.DESTROYED) {
2917 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2918 + " resumed=" + mResumedActivity
2919 + " pausing=" + mPausingActivity);
2920 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2921 activityRemoved = true;
2922 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002923 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002924 }
2925 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002926 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002927 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002928 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002929 }
2930
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 /**
2932 * Destroy the current CLIENT SIDE instance of an activity. This may be
2933 * called both when actually finishing an activity, or when performing
2934 * a configuration switch where we destroy the current client-side object
2935 * but then create a new client-side object for this same HistoryRecord.
2936 */
2937 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002938 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002939 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002940 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2942 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002943 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002944 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945
2946 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002947
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002948 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949
2950 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002951
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 if (hadApp) {
2953 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002954 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2956 mService.mHeavyWeightProcess = null;
2957 mService.mHandler.sendEmptyMessage(
2958 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2959 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002960 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002961 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002962 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002963 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964 }
2965 }
2966
2967 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 try {
2970 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002971 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 r.configChangeFlags);
2973 } catch (Exception e) {
2974 // We can just ignore exceptions here... if the process
2975 // has crashed, our death notification will clean things
2976 // up.
2977 //Slog.w(TAG, "Exception thrown during finish", e);
2978 if (r.finishing) {
2979 removeActivityFromHistoryLocked(r);
2980 removedFromHistory = true;
2981 skipDestroy = true;
2982 }
2983 }
2984
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002986
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002987 // If the activity is finishing, we need to wait on removing it
2988 // from the list to give it a chance to do its cleanup. During
2989 // that time it may make calls back with its token so we need to
2990 // be able to find it on the list and so we don't want to remove
2991 // it from the list yet. Otherwise, we can just immediately put
2992 // it in the destroyed state since we are not removing it from the
2993 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002995 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2996 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002997 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002998 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3000 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003001 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003003 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003004 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003005 }
3006 } else {
3007 // remove this record from the history.
3008 if (r.finishing) {
3009 removeActivityFromHistoryLocked(r);
3010 removedFromHistory = true;
3011 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003012 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003014 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003015 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003016 }
3017 }
3018
3019 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 if (!mLRUActivities.remove(r) && hadApp) {
3022 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3023 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003024
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003025 return removedFromHistory;
3026 }
3027
Craig Mautnerd2328952013-03-05 12:46:26 -08003028 final void activityDestroyedLocked(IBinder token) {
3029 final long origId = Binder.clearCallingIdentity();
3030 try {
3031 ActivityRecord r = ActivityRecord.forToken(token);
3032 if (r != null) {
3033 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003034 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003035
3036 if (isInStackLocked(token) != null) {
3037 if (r.state == ActivityState.DESTROYING) {
3038 cleanUpActivityLocked(r, true, false);
3039 removeActivityFromHistoryLocked(r);
3040 }
3041 }
Craig Mautner05d29032013-05-03 13:40:13 -07003042 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003043 } finally {
3044 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 }
3046 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003047
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003048 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3049 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003051 if (DEBUG_CLEANUP) Slog.v(
3052 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003053 + " with " + i + " entries");
3054 while (i > 0) {
3055 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003056 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003057 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003059 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003061 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 }
3063 }
3064 }
3065
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003066 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3067 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003068 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3069 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003070 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3071 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003072 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003073 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003074 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3075 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003076
3077 boolean hasVisibleActivities = false;
3078
3079 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003080 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003081 if (DEBUG_CLEANUP) Slog.v(
3082 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003083 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3084 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3085 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3086 final ActivityRecord r = activities.get(activityNdx);
3087 --i;
3088 if (DEBUG_CLEANUP) Slog.v(
3089 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3090 if (r.app == app) {
3091 boolean remove;
3092 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3093 // Don't currently have state for the activity, or
3094 // it is finishing -- always remove it.
3095 remove = true;
3096 } else if (r.launchCount > 2 &&
3097 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3098 // We have launched this activity too many times since it was
3099 // able to run, so give up and remove it.
3100 remove = true;
3101 } else {
3102 // The process may be gone, but the activity lives on!
3103 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003104 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003105 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003106 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003107 RuntimeException here = new RuntimeException("here");
3108 here.fillInStackTrace();
3109 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3110 + ": haveState=" + r.haveState
3111 + " stateNotNeeded=" + r.stateNotNeeded
3112 + " finishing=" + r.finishing
3113 + " state=" + r.state, here);
3114 }
3115 if (!r.finishing) {
3116 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3117 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3118 r.userId, System.identityHashCode(r),
3119 r.task.taskId, r.shortComponentName,
3120 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003121 if (r.state == ActivityState.RESUMED) {
3122 mService.updateUsageStats(r, false);
3123 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003124 }
3125 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003126
Craig Mautner0247fc82013-02-28 14:32:06 -08003127 } else {
3128 // We have the current state for this activity, so
3129 // it can be restarted later when needed.
3130 if (localLOGV) Slog.v(
3131 TAG, "Keeping entry, setting app to null");
3132 if (r.visible) {
3133 hasVisibleActivities = true;
3134 }
3135 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3136 + r);
3137 r.app = null;
3138 r.nowVisible = false;
3139 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003140 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003141 "App died, clearing saved state of " + r);
3142 r.icicle = null;
3143 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003144 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003145
Craig Mautnerd2328952013-03-05 12:46:26 -08003146 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003147 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003148 }
3149 }
3150
3151 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003153
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003154 final void updateTransitLocked(int transit, Bundle options) {
3155 if (options != null) {
3156 ActivityRecord r = topRunningActivityLocked(null);
3157 if (r != null && r.state != ActivityState.RESUMED) {
3158 r.updateOptionsLocked(options);
3159 } else {
3160 ActivityOptions.abort(options);
3161 }
3162 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003163 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003164 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003165
Craig Mautnera82aa092013-09-13 15:34:08 -07003166 void moveHomeTaskToTop() {
3167 final int top = mTaskHistory.size() - 1;
3168 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3169 final TaskRecord task = mTaskHistory.get(taskNdx);
3170 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003171 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003172 mTaskHistory.remove(taskNdx);
3173 mTaskHistory.add(top, task);
3174 mWindowManager.moveTaskToTop(task.taskId);
3175 return;
3176 }
3177 }
3178 }
3179
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003180 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003181 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003182
Craig Mautner11bf9a52013-02-19 14:08:51 -08003183 final int numTasks = mTaskHistory.size();
3184 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003185 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003186 // nothing to do!
3187 if (reason != null &&
3188 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3189 ActivityOptions.abort(options);
3190 } else {
3191 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3192 }
3193 return;
3194 }
3195
Craig Mautnere0a38842013-12-16 16:14:02 -08003196 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003197
Craig Mautner11bf9a52013-02-19 14:08:51 -08003198 // Shift all activities with this task up to the top
3199 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003200 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003201
3202 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003203 if (reason != null &&
3204 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003205 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003206 ActivityRecord r = topRunningActivityLocked(null);
3207 if (r != null) {
3208 mNoAnimActivities.add(r);
3209 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003210 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003212 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003214
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003215 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216
Craig Mautner05d29032013-05-03 13:40:13 -07003217 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003218 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003219
3220 if (VALIDATE_TOKENS) {
3221 validateAppTokensLocked();
3222 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003223 }
3224
3225 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003226 * Worker method for rearranging history stack. Implements the function of moving all
3227 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003229 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003230 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3231 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003232 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003233 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 * @return Returns true if the move completed, false if not.
3235 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003236 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003237 final TaskRecord tr = taskForIdLocked(taskId);
3238 if (tr == null) {
3239 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3240 return false;
3241 }
3242
3243 Slog.i(TAG, "moveTaskToBack: " + tr);
3244
3245 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003246
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 // If we have a watcher, preflight the move before committing to it. First check
3248 // for *other* available tasks, but if none are available, then try again allowing the
3249 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003250 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003251 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003252 if (next == null) {
3253 next = topRunningActivityLocked(null, 0);
3254 }
3255 if (next != null) {
3256 // ask watcher if this is allowed
3257 boolean moveOK = true;
3258 try {
3259 moveOK = mService.mController.activityResuming(next.packageName);
3260 } catch (RemoteException e) {
3261 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003262 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003263 }
3264 if (!moveOK) {
3265 return false;
3266 }
3267 }
3268 }
3269
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003271 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272
Craig Mautner11bf9a52013-02-19 14:08:51 -08003273 mTaskHistory.remove(tr);
3274 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003275
Craig Mautnerc8143c62013-09-03 12:15:57 -07003276 // There is an assumption that moving a task to the back moves it behind the home activity.
3277 // We make sure here that some activity in the stack will launch home.
3278 ActivityRecord lastActivity = null;
3279 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003280 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3281 final TaskRecord task = mTaskHistory.get(taskNdx);
3282 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003283 break;
3284 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003285 if (taskNdx == 1) {
3286 // Set the last task before tr to go to home.
3287 task.mOnTopOfHome = true;
3288 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003289 }
3290
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003291 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003292 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3293 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003294 ActivityRecord r = topRunningActivityLocked(null);
3295 if (r != null) {
3296 mNoAnimActivities.add(r);
3297 }
3298 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003299 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003301 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003302
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003304 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 }
3306
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003307 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003308 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003309 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003310 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003311 }
3312
Craig Mautner05d29032013-05-03 13:40:13 -07003313 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003314 return true;
3315 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003316
Craig Mautner8849a5e2013-04-02 16:41:03 -07003317 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003319 final Uri data = r.intent.getData();
3320 final String strData = data != null ? data.toSafeString() : null;
3321
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003323 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003325 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003326 }
3327
3328 /**
3329 * Make sure the given activity matches the current configuration. Returns
3330 * false if the activity had to be destroyed. Returns true if the
3331 * configuration is the same, or the activity will remain running as-is
3332 * for whatever reason. Ensures the HistoryRecord is updated with the
3333 * correct configuration and all other bookkeeping is handled.
3334 */
3335 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3336 int globalChanges) {
3337 if (mConfigWillChange) {
3338 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3339 "Skipping config check (will change): " + r);
3340 return true;
3341 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003342
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3344 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003345
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 // Short circuit: if the two configurations are the exact same
3347 // object (the common case), then there is nothing to do.
3348 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003349 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003350 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3351 "Configuration unchanged in " + r);
3352 return true;
3353 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003354
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 // We don't worry about activities that are finishing.
3356 if (r.finishing) {
3357 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3358 "Configuration doesn't matter in finishing " + r);
3359 r.stopFreezingScreenLocked(false);
3360 return true;
3361 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003362
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003363 // Okay we now are going to make this activity have the new config.
3364 // But then we need to figure out how it needs to deal with that.
3365 Configuration oldConfig = r.configuration;
3366 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003367
3368 // Determine what has changed. May be nothing, if this is a config
3369 // that has come back from the app after going idle. In that case
3370 // we just want to leave the official config object now in the
3371 // activity and do nothing else.
3372 final int changes = oldConfig.diff(newConfig);
3373 if (changes == 0 && !r.forceNewConfig) {
3374 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3375 "Configuration no differences in " + r);
3376 return true;
3377 }
3378
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003379 // If the activity isn't currently running, just leave the new
3380 // configuration and it will pick that up next time it starts.
3381 if (r.app == null || r.app.thread == null) {
3382 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3383 "Configuration doesn't matter not running " + r);
3384 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003385 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 return true;
3387 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003388
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003389 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003390 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3391 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3392 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003393 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003394 + ", newConfig=" + newConfig);
3395 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003396 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003397 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3398 r.configChangeFlags |= changes;
3399 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003400 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003401 if (r.app == null || r.app.thread == null) {
3402 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003403 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003404 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003405 } else if (r.state == ActivityState.PAUSING) {
3406 // A little annoying: we are waiting for this activity to
3407 // finish pausing. Let's not do anything now, but just
3408 // flag that it needs to be restarted when done pausing.
3409 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003410 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003411 r.configDestroy = true;
3412 return true;
3413 } else if (r.state == ActivityState.RESUMED) {
3414 // Try to optimize this case: the configuration is changing
3415 // and we need to restart the top, resumed activity.
3416 // Instead of doing the normal handshaking, just say
3417 // "restart!".
3418 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003419 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003420 relaunchActivityLocked(r, r.configChangeFlags, true);
3421 r.configChangeFlags = 0;
3422 } else {
3423 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003424 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003425 relaunchActivityLocked(r, r.configChangeFlags, false);
3426 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003428
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003429 // All done... tell the caller we weren't able to keep this
3430 // activity around.
3431 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003432 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003433
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003434 // Default case: the activity can handle this new configuration, so
3435 // hand it over. Note that we don't need to give it the new
3436 // configuration, since we always send configuration changes to all
3437 // process when they happen so it can just use whatever configuration
3438 // it last got.
3439 if (r.app != null && r.app.thread != null) {
3440 try {
3441 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003442 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003443 } catch (RemoteException e) {
3444 // If process died, whatever.
3445 }
3446 }
3447 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003448
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003449 return true;
3450 }
3451
Craig Mautnerc8143c62013-09-03 12:15:57 -07003452 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003453 int changes, boolean andResume) {
3454 List<ResultInfo> results = null;
3455 List<Intent> newIntents = null;
3456 if (andResume) {
3457 results = r.results;
3458 newIntents = r.newIntents;
3459 }
3460 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3461 + " with results=" + results + " newIntents=" + newIntents
3462 + " andResume=" + andResume);
3463 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003464 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003465 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003466
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003467 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003468
Craig Mautner34b73df2014-01-12 21:11:08 -08003469 mStackSupervisor.removeChildActivityContainers(r);
3470
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003472 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3473 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3474 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003475 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003476 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003477 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003478 // Note: don't need to call pauseIfSleepingLocked() here, because
3479 // the caller will only pass in 'andResume' if this activity is
3480 // currently resumed, which implies we aren't sleeping.
3481 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003482 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003483 }
3484
3485 if (andResume) {
3486 r.results = null;
3487 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003488 r.state = ActivityState.RESUMED;
3489 } else {
3490 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3491 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003492 }
3493
3494 return true;
3495 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003496
3497 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003498 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3499 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3500 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3501 final ActivityRecord r = activities.get(activityNdx);
3502 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003503 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003504 }
3505 if (r.fullscreen && !r.finishing) {
3506 return false;
3507 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003508 }
3509 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003510 final ActivityRecord r = ActivityRecord.forToken(token);
3511 if (r == null) {
3512 return false;
3513 }
3514 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3515 + " would have returned true for r=" + r);
3516 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003517 }
3518
3519 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003520 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3521 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3522 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3523 final ActivityRecord r = activities.get(activityNdx);
3524 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003525 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003526 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003527 }
3528 }
3529 }
3530
3531 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3532 boolean didSomething = false;
3533 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003534 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3535 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3536 int numActivities = activities.size();
3537 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3538 ActivityRecord r = activities.get(activityNdx);
3539 final boolean samePackage = r.packageName.equals(name)
3540 || (name == null && r.userId == userId);
3541 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3542 && (samePackage || r.task == lastTask)
3543 && (r.app == null || evenPersistent || !r.app.persistent)) {
3544 if (!doit) {
3545 if (r.finishing) {
3546 // If this activity is just finishing, then it is not
3547 // interesting as far as something to stop.
3548 continue;
3549 }
3550 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003551 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003552 didSomething = true;
3553 Slog.i(TAG, " Force finishing activity " + r);
3554 if (samePackage) {
3555 if (r.app != null) {
3556 r.app.removed = true;
3557 }
3558 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003559 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003560 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003561 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3562 true)) {
3563 // r has been deleted from mActivities, accommodate.
3564 --numActivities;
3565 --activityNdx;
3566 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003567 }
3568 }
3569 }
3570 return didSomething;
3571 }
3572
Dianne Hackborn09233282014-04-30 11:33:59 -07003573 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003574 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003575 final TaskRecord task = mTaskHistory.get(taskNdx);
3576 ActivityRecord r = null;
3577 ActivityRecord top = null;
3578 int numActivities = 0;
3579 int numRunning = 0;
3580 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003581 if (activities.isEmpty()) {
3582 continue;
3583 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003584 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3585 continue;
3586 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003587 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3588 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003589
Craig Mautneraab647e2013-02-28 16:31:36 -08003590 // Initialize state for next task if needed.
3591 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3592 top = r;
3593 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003594 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003595
3596 // Add 'r' into the current task.
3597 numActivities++;
3598 if (r.app != null && r.app.thread != null) {
3599 numRunning++;
3600 }
3601
3602 if (localLOGV) Slog.v(
3603 TAG, r.intent.getComponent().flattenToShortString()
3604 + ": task=" + r.task);
3605 }
3606
3607 RunningTaskInfo ci = new RunningTaskInfo();
3608 ci.id = task.taskId;
3609 ci.baseActivity = r.intent.getComponent();
3610 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003611 ci.lastActiveTime = task.lastActiveTime;
3612
Craig Mautneraab647e2013-02-28 16:31:36 -08003613 if (top.thumbHolder != null) {
3614 ci.description = top.thumbHolder.lastDescription;
3615 }
3616 ci.numActivities = numActivities;
3617 ci.numRunning = numRunning;
3618 //System.out.println(
3619 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003620 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003621 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003622 }
3623
3624 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003625 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003626 if (DEBUG_SWITCH) Slog.d(
3627 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003628 if (top >= 0) {
3629 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3630 int activityTop = activities.size() - 1;
3631 if (activityTop > 0) {
3632 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3633 "unhandled-back", true);
3634 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003635 }
3636 }
3637
Craig Mautner6b74cb52013-09-27 17:02:21 -07003638 /**
3639 * Reset local parameters because an app's activity died.
3640 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003641 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003642 */
3643 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003644 if (mPausingActivity != null && mPausingActivity.app == app) {
3645 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3646 "App died while pausing: " + mPausingActivity);
3647 mPausingActivity = null;
3648 }
3649 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3650 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003651 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003652 }
3653
Craig Mautner19091252013-10-05 00:03:53 -07003654 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003655 }
3656
Craig Mautnercae015f2013-02-08 14:31:27 -08003657 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003658 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3659 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3660 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3661 final ActivityRecord r = activities.get(activityNdx);
3662 if (r.app == app) {
3663 Slog.w(TAG, " Force finishing activity "
3664 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003665 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003666 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003667 }
3668 }
3669 }
3670
Dianne Hackborn390517b2013-05-30 15:03:32 -07003671 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003672 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003673 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003674 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3675 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003676 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3677 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003678 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003679 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003680 if (printed) {
3681 header = null;
3682 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003683 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003684 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003685 }
3686
3687 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3688 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3689
3690 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003691 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3692 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003693 }
3694 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003695 final int top = mTaskHistory.size() - 1;
3696 if (top >= 0) {
3697 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3698 int listTop = list.size() - 1;
3699 if (listTop >= 0) {
3700 activities.add(list.get(listTop));
3701 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003702 }
3703 } else {
3704 ItemMatcher matcher = new ItemMatcher();
3705 matcher.build(name);
3706
Craig Mautneraab647e2013-02-28 16:31:36 -08003707 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3708 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3709 if (matcher.match(r1, r1.intent.getComponent())) {
3710 activities.add(r1);
3711 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003712 }
3713 }
3714 }
3715
3716 return activities;
3717 }
3718
3719 ActivityRecord restartPackage(String packageName) {
3720 ActivityRecord starting = topRunningActivityLocked(null);
3721
3722 // All activities that came from the package must be
3723 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003724 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3725 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3726 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3727 final ActivityRecord a = activities.get(activityNdx);
3728 if (a.info.packageName.equals(packageName)) {
3729 a.forceNewConfig = true;
3730 if (starting != null && a == starting && a.visible) {
3731 a.startFreezingScreenLocked(starting.app,
3732 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3733 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003734 }
3735 }
3736 }
3737
3738 return starting;
3739 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003740
Craig Mautner41db4a72014-05-07 17:20:56 -07003741 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003742 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003743 mWindowManager.removeTask(task.taskId);
3744 final ActivityRecord r = mResumedActivity;
3745 if (r != null && r.task == task) {
3746 mResumedActivity = null;
3747 }
3748
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003749 final int taskNdx = mTaskHistory.indexOf(task);
3750 final int topTaskNdx = mTaskHistory.size() - 1;
3751 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3752 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3753 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003754 mTaskHistory.remove(task);
Craig Mautner41db4a72014-05-07 17:20:56 -07003755
3756 if (task.mActivities.isEmpty()) {
3757 final boolean isVoiceSession = task.voiceSession != null;
3758 if (isVoiceSession) {
3759 try {
3760 task.voiceSession.taskFinished(task.intent, task.taskId);
3761 } catch (RemoteException e) {
3762 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003763 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003764 if (task.autoRemoveFromRecents() || isVoiceSession) {
3765 // Task creator asked to remove this when done, or this task was a voice
3766 // interaction, so it should not remain on the recent tasks list.
3767 mService.mRecentTasks.remove(task);
3768 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003769 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003770
3771 if (mTaskHistory.isEmpty()) {
3772 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3773 if (isOnHomeDisplay()) {
3774 mStackSupervisor.moveHomeStack(!isHomeStack());
3775 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003776 if (mStacks != null) {
3777 mStacks.remove(this);
3778 mStacks.add(0, this);
3779 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003780 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003781 }
3782
Dianne Hackborn91097de2014-04-04 18:02:06 -07003783 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
3784 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
3785 boolean toTop) {
3786 TaskRecord task = new TaskRecord(taskId, info, intent, voiceSession, voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003787 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003788 return task;
3789 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003790
3791 ArrayList<TaskRecord> getAllTasks() {
3792 return new ArrayList<TaskRecord>(mTaskHistory);
3793 }
3794
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003795 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003796 task.stack = this;
3797 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003798 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003799 } else {
3800 mTaskHistory.add(0, task);
3801 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003802 if (!moving && task.voiceSession != null) {
3803 try {
3804 task.voiceSession.taskStarted(task.intent, task.taskId);
3805 } catch (RemoteException e) {
3806 }
3807 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003808 }
3809
3810 public int getStackId() {
3811 return mStackId;
3812 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003813
3814 @Override
3815 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003816 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3817 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003818 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003819}