blob: 68ef815b7d536e04eeb2af252f4c299b32878301 [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;
36import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070037import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
38
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070039import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070040import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080041import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080042import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080043import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080044import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046
47import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070048import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070049import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080054import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080060import android.content.res.Resources;
61import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.RemoteException;
71import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070076import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077
Craig Mautnercae015f2013-02-08 14:31:27 -080078import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080import java.lang.ref.WeakReference;
81import java.util.ArrayList;
82import java.util.Iterator;
83import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080084import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
86/**
87 * State and management of a single stack of activities.
88 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070089final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080090
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070091 // Ticks during which we check progress while waiting for an app to launch.
92 static final int LAUNCH_TICK = 500;
93
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094 // How long we wait until giving up on the last activity to pause. This
95 // is short because it directly impacts the responsiveness of starting the
96 // next activity.
97 static final int PAUSE_TIMEOUT = 500;
98
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070099 // How long we wait for the activity to tell us it has stopped before
100 // giving up. This is a good amount of time because we really need this
101 // from the application in order to get its saved state.
102 static final int STOP_TIMEOUT = 10*1000;
103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104 // How long we wait until giving up on an activity telling us it has
105 // finished destroying itself.
106 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800109 // disabled.
110 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800111
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700112 // How long between activity launches that we consider safe to not warn
113 // the user about an unexpected activity being launched on top.
114 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // Set to false to disable the preview that is shown while a new activity
117 // is being started.
118 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800119
Craig Mautner5eda9b32013-07-02 11:58:16 -0700120 // How long to wait for all background Activities to redraw following a call to
121 // convertToTranslucent().
122 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
123
John Reck172e87c2013-10-02 16:55:16 -0700124 static final boolean SCREENSHOT_FORCE_565 = ActivityManager
125 .isLowRamDeviceStatic() ? true : false;
126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 enum ActivityState {
128 INITIALIZING,
129 RESUMED,
130 PAUSING,
131 PAUSED,
132 STOPPING,
133 STOPPED,
134 FINISHING,
135 DESTROYING,
136 DESTROYED
137 }
138
139 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 /**
143 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144 * running) activities. It contains #TaskRecord objects.
145 */
146 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
147
148 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800149 * Used for validating app tokens with window manager.
150 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800151 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700153 /**
154 * List of running activities, sorted by recent usage.
155 * The first entry in the list is the least recently used.
156 * It contains HistoryRecord objects.
157 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700158 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159
160 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 * Animations that for the current transition have requested not to
162 * be considered for the transition animation.
163 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700164 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167 * When we are in the process of pausing an activity, before starting the
168 * next one, this variable holds the activity that is currently being paused.
169 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800170 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
173 * This is the last activity that we put into the paused state. This is
174 * used to determine if we need to do an activity transition while sleeping,
175 * when we normally hold the top activity paused.
176 */
177 ActivityRecord mLastPausedActivity = null;
178
179 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700180 * Activities that specify No History must be removed once the user navigates away from them.
181 * If the device goes to sleep with such an activity in the paused state then we save it here
182 * and finish it later if another activity replaces it on wakeup.
183 */
184 ActivityRecord mLastNoHistoryActivity = null;
185
186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * Current activity that is resumed, or null if there is none.
188 */
189 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700192 * This is the last activity that has been started. It is only used to
193 * identify when multiple activities are started at once so that the user
194 * can be warned they may not be in the activity they think they are.
195 */
196 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800197
Craig Mautner5eda9b32013-07-02 11:58:16 -0700198 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
199 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
200 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
201 // Activity in mTranslucentActivityWaiting is notified via
202 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
203 // background activity being drawn then the same call will be made with a true value.
204 ActivityRecord mTranslucentActivityWaiting = null;
205 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
206 new ArrayList<ActivityRecord>();
207
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Set when we know we are going to be calling updateConfiguration()
210 * soon, so want to skip intermediate config checks.
211 */
212 boolean mConfigWillChange;
213
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700214 long mLaunchStartTime = 0;
215 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800216
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800217 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800218 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
219 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
220 */
221 private ActivityRecord mLastScreenshotActivity = null;
222 private Bitmap mLastScreenshotBitmap = null;
223
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800224 int mThumbnailWidth = -1;
225 int mThumbnailHeight = -1;
226
Craig Mautner858d8a62013-04-23 17:08:34 -0700227 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700228
Craig Mautnerc00204b2013-03-05 15:02:14 -0800229 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800230 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800231 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
232 ArrayList<ActivityStack> mStacks;
233 /** The attached Display's unique identifier, or -1 if detached */
234 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235
Craig Mautner27084302013-03-25 08:05:25 -0700236 /** Run all ActivityStacks through this */
237 final ActivityStackSupervisor mStackSupervisor;
238
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700239 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700240 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
241 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
242 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
243 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700244 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700245
246 static class ScheduleDestroyArgs {
247 final ProcessRecord mOwner;
248 final boolean mOomAdj;
249 final String mReason;
250 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
251 mOwner = owner;
252 mOomAdj = oomAdj;
253 mReason = reason;
254 }
255 }
256
Zoran Marcetaf958b322012-08-09 20:27:12 +0900257 final Handler mHandler;
258
259 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700260 //public Handler() {
261 // if (localLOGV) Slog.v(TAG, "Handler started!");
262 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700263 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 super(looper);
265 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700266
Zoran Marcetaf958b322012-08-09 20:27:12 +0900267 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268 public void handleMessage(Message msg) {
269 switch (msg.what) {
270 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800271 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272 // We don't at this point know if the activity is fullscreen,
273 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800274 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700275 synchronized (mService) {
276 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700277 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700278 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700279 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800280 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700281 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700282 case LAUNCH_TICK_MSG: {
283 ActivityRecord r = (ActivityRecord)msg.obj;
284 synchronized (mService) {
285 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700286 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700287 }
288 }
289 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700290 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800291 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 // We don't at this point know if the activity is fullscreen,
293 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800294 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800295 synchronized (mService) {
296 activityDestroyedLocked(r != null ? r.appToken : null);
297 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700299 case STOP_TIMEOUT_MSG: {
300 ActivityRecord r = (ActivityRecord)msg.obj;
301 // We don't at this point know if the activity is fullscreen,
302 // so we need to be conservative and assume it isn't.
303 Slog.w(TAG, "Activity stop timeout for " + r);
304 synchronized (mService) {
305 if (r.isInHistory()) {
306 activityStoppedLocked(r, null, null, null);
307 }
308 }
309 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700310 case DESTROY_ACTIVITIES_MSG: {
311 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
312 synchronized (mService) {
313 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
314 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700315 } break;
316 case TRANSLUCENT_TIMEOUT_MSG: {
317 synchronized (mService) {
318 notifyActivityDrawnLocked(null);
319 }
320 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700321 }
322 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800323 }
324
Craig Mautner34b73df2014-01-12 21:11:08 -0800325 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800326 int count = 0;
327 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
328 count += mTaskHistory.get(taskNdx).mActivities.size();
329 }
330 return count;
331 }
332
Craig Mautner4a1cb222013-12-04 16:14:06 -0800333 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
334 mActivityContainer = activityContainer;
335 mStackSupervisor = activityContainer.getOuter();
336 mService = mStackSupervisor.mService;
337 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
338 mWindowManager = mService.mWindowManager;
339 mStackId = activityContainer.mStackId;
340 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700341 }
Craig Mautner5962b122012-10-05 14:45:52 -0700342
Craig Mautner6b74cb52013-09-27 17:02:21 -0700343 boolean okToShow(ActivityRecord r) {
Craig Mautner5962b122012-10-05 14:45:52 -0700344 return r.userId == mCurrentUser
345 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
346 }
347
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700348 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800349 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700350 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
351 if (r != null) {
352 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800353 }
354 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700355 return null;
356 }
357
358 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800359 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
360 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800361 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800362 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
363 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800364 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
365 return r;
366 }
367 }
368 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700369 return null;
370 }
371
372 /**
373 * This is a simplified version of topRunningActivityLocked that provides a number of
374 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800375 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 * @param token If non-null, any history records matching this token will be skipped.
377 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800378 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700379 * @return Returns the HistoryRecord of the next activity on the stack.
380 */
381 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800382 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
383 TaskRecord task = mTaskHistory.get(taskNdx);
384 if (task.taskId == taskId) {
385 continue;
386 }
387 ArrayList<ActivityRecord> activities = task.mActivities;
388 for (int i = activities.size() - 1; i >= 0; --i) {
389 final ActivityRecord r = activities.get(i);
390 // Note: the taskId check depends on real taskId fields being non-zero
391 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
392 return r;
393 }
394 }
395 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700396 return null;
397 }
398
Craig Mautner8849a5e2013-04-02 16:41:03 -0700399 final ActivityRecord topActivity() {
400 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700401 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700402 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
403 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
404 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700405 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700406 }
407 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700408 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700409 }
410
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700411 final TaskRecord topTask() {
412 final int size = mTaskHistory.size();
413 if (size > 0) {
414 return mTaskHistory.get(size - 1);
415 }
416 return null;
417 }
418
Craig Mautnerd2328952013-03-05 12:46:26 -0800419 TaskRecord taskForIdLocked(int id) {
420 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
421 final TaskRecord task = mTaskHistory.get(taskNdx);
422 if (task.taskId == id) {
423 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800424 }
425 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700426 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700427 }
428
Craig Mautnerd2328952013-03-05 12:46:26 -0800429 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800430 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800431 if (r != null) {
432 final TaskRecord task = r.task;
433 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
434 if (task.stack != this) Slog.w(TAG,
435 "Illegal state! task does not point to stack it is in.");
436 return r;
437 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800438 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800439 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800440 }
441
Craig Mautner2420ead2013-04-01 17:13:20 -0700442 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700443 final boolean hadit = mLRUActivities.remove(r);
444 mLRUActivities.add(r);
445 return hadit;
446 }
447
Craig Mautnerde4ef022013-04-07 19:01:33 -0700448 final boolean isHomeStack() {
449 return mStackId == HOME_STACK_ID;
450 }
451
Craig Mautnere0a38842013-12-16 16:14:02 -0800452 final boolean isOnHomeDisplay() {
453 return isAttached() &&
454 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
455 }
456
457 final void moveToFront() {
458 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800459 if (isOnHomeDisplay()) {
460 mStackSupervisor.moveHomeStack(isHomeStack());
461 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800462 mStacks.remove(this);
463 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800464 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800465 }
466
467 final boolean isAttached() {
468 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800469 }
470
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700471 /**
472 * Returns the top activity in any existing task matching the given
473 * Intent. Returns null if no such task is found.
474 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700475 ActivityRecord findTaskLocked(ActivityRecord target) {
476 Intent intent = target.intent;
477 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700478 ComponentName cls = intent.getComponent();
479 if (info.targetActivity != null) {
480 cls = new ComponentName(info.packageName, info.targetActivity);
481 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700482 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800483
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700484 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800485 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
486 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700487 if (task.userId != userId) {
488 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700489 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700490 continue;
491 }
Craig Mautner000f0022013-02-26 15:04:29 -0800492 final ActivityRecord r = task.getTopActivity();
493 if (r == null || r.finishing || r.userId != userId ||
494 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700495 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800496 continue;
497 }
498
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700499 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
500 + r.task.intent.getComponent().flattenToShortString()
501 + "/aff=" + r.task.affinity + " to new cls="
502 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautner000f0022013-02-26 15:04:29 -0800503 if (task.affinity != null) {
504 if (task.affinity.equals(info.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700505 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700506 return r;
507 }
Craig Mautner000f0022013-02-26 15:04:29 -0800508 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700509 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800510 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700511 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
512 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800513 return r;
514 } else if (task.affinityIntent != null
515 && task.affinityIntent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700516 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800517 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700518 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
519 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800520 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700521 } else if (DEBUG_TASKS) {
522 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700523 }
524 }
525
526 return null;
527 }
528
529 /**
530 * Returns the first activity (starting from the top of the stack) that
531 * is the same as the given activity. Returns null if no such activity
532 * is found.
533 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700534 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700535 ComponentName cls = intent.getComponent();
536 if (info.targetActivity != null) {
537 cls = new ComponentName(info.packageName, info.targetActivity);
538 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700539 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700540
Craig Mautner000f0022013-02-26 15:04:29 -0800541 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700542 TaskRecord task = mTaskHistory.get(taskNdx);
543 if (task.userId != mCurrentUser) {
544 return null;
545 }
546 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800547 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
548 ActivityRecord r = activities.get(activityNdx);
549 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700550 //Slog.i(TAG, "Found matching class!");
551 //dump();
552 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
553 return r;
554 }
555 }
556 }
557
558 return null;
559 }
560
Amith Yamasani742a6712011-05-04 14:49:28 -0700561 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700562 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700563 */
Craig Mautner93529a42013-10-04 15:03:13 -0700564 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800565 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700566 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800567 }
568 mCurrentUser = userId;
569
570 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800571 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800572 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700573 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800574 if (task.userId == userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700575 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
576 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800577 mTaskHistory.remove(i);
578 mTaskHistory.add(task);
579 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800580 // Use same value for i.
581 } else {
582 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800583 }
584 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700585 if (VALIDATE_TOKENS) {
586 validateAppTokensLocked();
587 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700588 }
589
Craig Mautner2420ead2013-04-01 17:13:20 -0700590 void minimalResumeActivityLocked(ActivityRecord r) {
591 r.state = ActivityState.RESUMED;
592 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
593 + " (starting new instance)");
594 r.stopped = false;
595 mResumedActivity = r;
596 r.task.touchActiveTime();
597 mService.addRecentTaskLocked(r.task);
598 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700599 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700600 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700601 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602 }
603
Dianne Hackborncee04b52013-07-03 17:01:28 -0700604 private void startLaunchTraces() {
605 if (mFullyDrawnStartTime != 0) {
606 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
607 }
608 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
609 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
610 }
611
612 private void stopFullyDrawnTraceIfNeeded() {
613 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
614 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
615 mFullyDrawnStartTime = 0;
616 }
617 }
618
Craig Mautnere79d42682013-04-01 19:01:53 -0700619 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700620 if (r.displayStartTime == 0) {
621 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
622 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700623 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700624 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700625 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700626 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700627 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700628 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700630 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800631
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700632 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700633 // Make sure that there is no activity waiting for this to launch.
634 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
635 r.displayStartTime = r.fullyDrawnStartTime = 0;
636 } else {
637 mStackSupervisor.removeTimeoutsForActivityLocked(r);
638 mStackSupervisor.scheduleIdleTimeoutLocked(r);
639 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700640 }
641
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800642 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800643 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800644 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
645 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
646 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
647 activities.get(activityNdx).setSleeping(false);
648 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800649 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800650 }
651
Craig Mautner0eea92c2013-05-16 13:35:39 -0700652 /**
653 * @return true if something must be done before going to sleep.
654 */
655 boolean checkReadyForSleepLocked() {
656 if (mResumedActivity != null) {
657 // Still have something resumed; can't sleep until it is paused.
658 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
659 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
660 startPausingLocked(false, true);
661 return true;
662 }
663 if (mPausingActivity != null) {
664 // Still waiting for something to pause; can't sleep yet.
665 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
666 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800667 }
668
Craig Mautner0eea92c2013-05-16 13:35:39 -0700669 return false;
670 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800671
Craig Mautner0eea92c2013-05-16 13:35:39 -0700672 void goToSleep() {
673 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800674
Craig Mautner0eea92c2013-05-16 13:35:39 -0700675 // Make sure any stopped but visible activities are now sleeping.
676 // This ensures that the activity's onStop() is called.
677 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
678 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
679 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
680 final ActivityRecord r = activities.get(activityNdx);
681 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
682 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800683 }
684 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800685 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700686 }
Craig Mautner59c00972012-07-30 12:10:24 -0700687
Dianne Hackbornd2835932010-12-13 16:28:46 -0800688 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800689 if (who.noDisplay) {
690 return null;
691 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800692
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700693 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700694 if (mService.getMostRecentTask() != tr && tr.intent != null &&
695 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700696 // If this task is being excluded from recents, we don't want to take
697 // the expense of capturing a thumbnail, since we will never show it.
698 return null;
699 }
700
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800701 Resources res = mService.mContext.getResources();
702 int w = mThumbnailWidth;
703 int h = mThumbnailHeight;
704 if (w < 0) {
705 mThumbnailWidth = w =
706 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
707 mThumbnailHeight = h =
708 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
709 }
710
711 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800712 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700713 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800714 || mLastScreenshotBitmap.getWidth() != w
715 || mLastScreenshotBitmap.getHeight() != h) {
716 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700717 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700718 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800719 }
720 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700721 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800722 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800723 }
724 return null;
725 }
726
Craig Mautnercf910b02013-04-23 11:23:27 -0700727 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800728 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800729 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700730 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800731 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700732 ActivityRecord prev = mResumedActivity;
733 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700734 Slog.e(TAG, "Trying to pause when nothing is resumed",
735 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700736 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700737 return;
738 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800739
740 if (mActivityContainer.mParentActivity == null) {
741 // Top level stack, not a child. Look for child stacks.
742 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
743 }
744
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700745 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
746 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700747 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800748 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700749 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700750 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
751 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700752 prev.state = ActivityState.PAUSING;
753 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700754 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700755 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
756 if (next == null || next.task != prev.task) {
757 prev.updateThumbnail(screenshotActivities(prev), null);
758 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700759 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700760
761 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800762
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700763 if (prev.app != null && prev.app.thread != null) {
764 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
765 try {
766 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700767 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700769 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800770 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
771 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700772 } catch (Exception e) {
773 // Ignore exception, if process died other code will cleanup.
774 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800775 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700777 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700778 }
779 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800780 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700781 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700782 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700783 }
784
785 // If we are not going to sleep, we want to ensure the device is
786 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700787 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700788 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 }
790
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800791 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700792 // Have the window manager pause its key dispatching until the new
793 // activity has started. If we're pausing the activity just because
794 // the screen is being turned off and the UI is sleeping, don't interrupt
795 // key dispatch; the same activity will pick it up again on wakeup.
796 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800797 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 } else {
799 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
800 }
801
802 // Schedule a pause timeout in case the app doesn't respond.
803 // We don't give it much time because this directly impacts the
804 // responsiveness seen by the user.
805 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
806 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700807 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
809 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
810 } else {
811 // This activity failed to schedule the
812 // pause, so just treat it as being paused now.
813 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700814 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700815 }
816 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700817
Craig Mautnerd2328952013-03-05 12:46:26 -0800818 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800820 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821
Craig Mautnerd2328952013-03-05 12:46:26 -0800822 final ActivityRecord r = isInStackLocked(token);
823 if (r != null) {
824 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
825 if (mPausingActivity == r) {
826 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
827 + (timeout ? " (due to timeout)" : " (pause complete)"));
828 r.state = ActivityState.PAUSED;
829 completePauseLocked();
830 } else {
831 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
832 r.userId, System.identityHashCode(r), r.shortComponentName,
833 mPausingActivity != null
834 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 }
836 }
837 }
838
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700839 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
840 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700841 if (r.state != ActivityState.STOPPING) {
842 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
843 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
844 return;
845 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700846 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700847 if (icicle != null) {
848 // If icicle is null, this is happening due to a timeout, so we
849 // haven't really saved the state.
850 r.icicle = icicle;
851 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800852 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700853 r.updateThumbnail(thumbnail, description);
854 }
855 if (!r.stopped) {
856 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
857 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
858 r.stopped = true;
859 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700860 if (r.finishing) {
861 r.clearOptionsLocked();
862 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700863 if (r.configDestroy) {
864 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700865 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700866 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700867 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800868 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700869 }
870 }
871 }
872
Craig Mautnerc8143c62013-09-03 12:15:57 -0700873 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800874 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800876
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 if (prev != null) {
878 if (prev.finishing) {
879 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700880 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800881 } else if (prev.app != null) {
882 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
883 if (prev.waitingVisible) {
884 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700885 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800886 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
887 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800888 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800889 if (prev.configDestroy) {
890 // The previous is being paused because the configuration
891 // is changing, which means it is actually stopping...
892 // To juggle the fact that we are also starting a new
893 // instance right now, we need to first completely stop
894 // the current instance before starting the new one.
895 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
896 destroyActivityLocked(prev, true, false, "pause-config");
897 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700898 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700899 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
900 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800901 // If we already have a few activities waiting to stop,
902 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700903 // them out. Or if r is the last of activity of the last task the stack
904 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800905 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700906 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800907 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700908 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800909 }
910 }
911 } else {
912 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
913 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800914 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800915 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800917
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700918 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700919 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700920 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800921 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700922 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700923 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700924 if (top == null || (prev != null && top != prev)) {
925 // If there are no more activities available to run,
926 // do resume anyway to start something. Also if the top
927 // activity on the stack is not the just paused activity,
928 // we need to go ahead and resume it to ensure we complete
929 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700930 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800933
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800934 if (prev != null) {
935 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936
Craig Mautner525f3d92013-05-07 14:01:50 -0700937 if (prev.app != null && prev.cpuTimeAtResume > 0
938 && mService.mBatteryStatsService.isOnBattery()) {
939 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700940 synchronized (mService.mProcessCpuThread) {
941 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700942 - prev.cpuTimeAtResume;
943 }
944 if (diff > 0) {
945 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
946 synchronized (bsi) {
947 BatteryStatsImpl.Uid.Proc ps =
948 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700949 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700950 if (ps != null) {
951 ps.addForegroundTimeLocked(diff);
952 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700953 }
954 }
955 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700956 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700958 }
959
960 /**
961 * Once we know that we have asked an application to put an activity in
962 * the resumed state (either by launching it or explicitly telling it),
963 * this function updates the rest of our state to match that fact.
964 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700965 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700966 next.idle = false;
967 next.results = null;
968 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700969 if (next.nowVisible) {
970 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
971 mStackSupervisor.dismissKeyguard();
972 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700973
974 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700975 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700976
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700977 mStackSupervisor.reportResumedActivityLocked(next);
978
979 next.resumeKeyDispatchingLocked();
980 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700981
982 // Mark the point when the activity is resuming
983 // TODO: To be more accurate, the mark should be before the onCreate,
984 // not after the onResume. But for subsequent starts, onResume is fine.
985 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700986 synchronized (mService.mProcessCpuThread) {
987 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 }
989 } else {
990 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
991 }
992 }
993
Craig Mautner580ea812013-04-25 12:58:38 -0700994 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -0700995 * Determine if home should be visible below the passed record.
996 * @param record activity we are querying for.
997 * @return true if home is visible below the passed activity, false otherwise.
998 */
999 boolean isActivityOverHome(ActivityRecord record) {
1000 // Start at record and go down, look for either home or a visible fullscreen activity.
1001 final TaskRecord recordTask = record.task;
1002 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
1003 TaskRecord task = mTaskHistory.get(taskNdx);
1004 final ArrayList<ActivityRecord> activities = task.mActivities;
1005 final int startNdx =
1006 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
1007 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
1008 final ActivityRecord r = activities.get(activityNdx);
1009 if (r.isHomeActivity()) {
1010 return true;
1011 }
Craig Mautner76be9d22013-11-04 16:01:22 -08001012 if (!r.finishing && r.fullscreen) {
1013 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001014 return false;
1015 }
1016 }
1017 if (task.mOnTopOfHome) {
1018 // Got to the bottom of a task on top of home without finding a visible fullscreen
1019 // activity. Home is visible.
1020 return true;
1021 }
1022 }
1023 // Got to the bottom of this stack and still don't know. If this is over the home stack
1024 // then record is over home. May not work if we ever get more than two layers.
1025 return mStackSupervisor.isFrontStack(this);
1026 }
1027
1028 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001029 * Version of ensureActivitiesVisible that can easily be called anywhere.
1030 */
1031 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1032 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1033 }
1034
1035 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1036 boolean forceHomeShown) {
1037 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001038 return r != null &&
1039 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001040 }
1041
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042 /**
1043 * Make sure that all activities that need to be visible (that is, they
1044 * currently can be seen by the user) actually are.
1045 */
Craig Mautner580ea812013-04-25 12:58:38 -07001046 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1047 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001048 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001049 TAG, "ensureActivitiesVisible behind " + top
1050 + " configChanges=0x" + Integer.toHexString(configChanges));
1051
Craig Mautner5eda9b32013-07-02 11:58:16 -07001052 if (mTranslucentActivityWaiting != top) {
1053 mUndrawnActivitiesBelowTopTranslucent.clear();
1054 if (mTranslucentActivityWaiting != null) {
1055 // Call the callback with a timeout indication.
1056 notifyActivityDrawnLocked(null);
1057 mTranslucentActivityWaiting = null;
1058 }
1059 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1060 }
1061
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062 // If the top activity is not fullscreen, then we need to
1063 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001064 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001065 boolean showHomeBehindStack = false;
1066 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1067 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001068 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001069 final TaskRecord task = mTaskHistory.get(taskNdx);
1070 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001071 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1072 final ActivityRecord r = activities.get(activityNdx);
1073 if (r.finishing) {
1074 continue;
1075 }
1076 if (aboveTop && r != top) {
1077 continue;
1078 }
1079 aboveTop = false;
1080 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001081 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001082 TAG, "Make visible? " + r + " finishing=" + r.finishing
1083 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001084
Craig Mautnerd44711d2013-02-23 11:24:36 -08001085 final boolean doThisProcess = onlyThisProcess == null
1086 || onlyThisProcess.equals(r.processName);
1087
1088 // First: if this is not the current activity being started, make
1089 // sure it matches the current configuration.
1090 if (r != starting && doThisProcess) {
1091 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001093
1094 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001095 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001096 // This activity needs to be visible, but isn't even
1097 // running... get it started, but don't resume it
1098 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001099 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001100 if (r != starting) {
1101 r.startFreezingScreenLocked(r.app, configChanges);
1102 }
1103 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001104 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001105 TAG, "Starting and making visible: " + r);
louis_changd5c91ec2014-01-28 18:38:06 +08001106 r.visible = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001108 }
1109 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001110 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001111 }
1112 }
1113
1114 } else if (r.visible) {
1115 // If this activity is already visible, then there is nothing
1116 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001117 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001118 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001119
1120 } else if (onlyThisProcess == null) {
1121 // This activity is not currently visible, but is running.
1122 // Tell it to become visible.
1123 r.visible = true;
1124 if (r.state != ActivityState.RESUMED && r != starting) {
1125 // If this activity is paused, tell it
1126 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001127 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001128 TAG, "Making visible and scheduling visibility: " + r);
1129 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001130 if (mTranslucentActivityWaiting != null) {
1131 mUndrawnActivitiesBelowTopTranslucent.add(r);
1132 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001133 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001134 r.sleeping = false;
1135 r.app.pendingUiClean = true;
1136 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1137 r.stopFreezingScreenLocked(false);
1138 } catch (Exception e) {
1139 // Just skip on any failure; we'll make it
1140 // visible when it next restarts.
1141 Slog.w(TAG, "Exception thrown making visibile: "
1142 + r.intent.getComponent(), e);
1143 }
1144 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146
Craig Mautnerd44711d2013-02-23 11:24:36 -08001147 // Aggregate current change flags.
1148 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001149
Craig Mautnerd44711d2013-02-23 11:24:36 -08001150 if (r.fullscreen) {
1151 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001152 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1153 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001154 } else if (isActivityOverHome(r)) {
1155 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1156 showHomeBehindStack = true;
riddle_hsu446ef1d2014-01-09 20:24:34 +08001157 behindFullscreen = !isHomeStack() && r.frontOfTask && task.mOnTopOfHome;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001158 }
1159 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001160 if (DEBUG_VISBILITY) Slog.v(
1161 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1162 + " state=" + r.state
1163 + " behindFullscreen=" + behindFullscreen);
1164 // Now for any activities that aren't visible to the user, make
1165 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001166 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001167 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001168 r.visible = false;
1169 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001170 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001171 switch (r.state) {
1172 case STOPPING:
1173 case STOPPED:
1174 if (r.app != null && r.app.thread != null) {
1175 if (DEBUG_VISBILITY) Slog.v(
1176 TAG, "Scheduling invisibility: " + r);
1177 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1178 }
1179 break;
1180
1181 case INITIALIZING:
1182 case RESUMED:
1183 case PAUSING:
1184 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001185 // This case created for transitioning activities from
1186 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001187 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1188 mStackSupervisor.mStoppingActivities.add(r);
1189 }
1190 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001191 break;
1192
1193 default:
1194 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195 }
1196 } catch (Exception e) {
1197 // Just skip on any failure; we'll make it
1198 // visible when it next restarts.
1199 Slog.w(TAG, "Exception thrown making hidden: "
1200 + r.intent.getComponent(), e);
1201 }
1202 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001203 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 }
1206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 }
Craig Mautner580ea812013-04-25 12:58:38 -07001208 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001209 }
Craig Mautner58547802013-03-05 08:23:53 -08001210
Craig Mautner5eda9b32013-07-02 11:58:16 -07001211 void convertToTranslucent(ActivityRecord r) {
1212 mTranslucentActivityWaiting = r;
1213 mUndrawnActivitiesBelowTopTranslucent.clear();
1214 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1215 }
1216
1217 /**
1218 * Called as activities below the top translucent activity are redrawn. When the last one is
1219 * redrawn notify the top activity by calling
1220 * {@link Activity#onTranslucentConversionComplete}.
1221 *
1222 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1223 * occurred and the activity will be notified immediately.
1224 */
1225 void notifyActivityDrawnLocked(ActivityRecord r) {
1226 if ((r == null)
1227 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1228 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1229 // The last undrawn activity below the top has just been drawn. If there is an
1230 // opaque activity at the top, notify it that it can become translucent safely now.
1231 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1232 mTranslucentActivityWaiting = null;
1233 mUndrawnActivitiesBelowTopTranslucent.clear();
1234 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1235
Craig Mautner71dd1b62014-02-18 15:48:52 -08001236 if (waitingActivity != null) {
1237 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1238 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1239 try {
1240 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1241 waitingActivity.appToken, r != null);
1242 } catch (RemoteException e) {
1243 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001244 }
1245 }
1246 }
1247 }
1248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001249 /**
1250 * Ensure that the top activity in the stack is resumed.
1251 *
1252 * @param prev The previously resumed activity, for when in the process
1253 * of pausing; can be null to call from elsewhere.
1254 *
1255 * @return Returns true if something is being resumed, or false if
1256 * nothing happened.
1257 */
1258 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001259 return resumeTopActivityLocked(prev, null);
1260 }
1261
1262 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001263 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1264
Craig Mautnerdf88d732014-01-27 09:21:32 -08001265 ActivityRecord parent = mActivityContainer.mParentActivity;
1266 if (parent != null && parent.state != ActivityState.RESUMED) {
1267 // Do not resume this stack if its parent is not resumed.
1268 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1269 return false;
1270 }
1271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272 // Find the first activity that is not finishing.
1273 ActivityRecord next = topRunningActivityLocked(null);
1274
1275 // Remember how we'll process this pause/resume situation, and ensure
1276 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001277 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1278 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001279
1280 if (next == null) {
1281 // There are no more activities! Let's just start up the
1282 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001283 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001284 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001285 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001286 // Only resume home if on home display
1287 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001288 }
1289
1290 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001291
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001292 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001293 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1294 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001295 // Make sure we have executed any pending transitions, since there
1296 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001297 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001298 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001299 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001300 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001301 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001302 return false;
1303 }
1304
Craig Mautner525f3d92013-05-07 14:01:50 -07001305 final TaskRecord nextTask = next.task;
1306 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001307 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001308 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001309 if (prevTask == nextTask) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001310 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001311 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001312 // This task is going away but it was supposed to return to the home task.
1313 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001314 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001315 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001316 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001317 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1318 "resumeTopActivityLocked: Launching home next");
1319 // Only resume home if on home display
1320 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001321 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001322 }
1323
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001324 // If we are sleeping, and there is no resumed activity, and the top
1325 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001326 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001327 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001328 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001329 // Make sure we have executed any pending transitions, since there
1330 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001331 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001332 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001333 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001334 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001335 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001336 return false;
1337 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001338
1339 // Make sure that the user who owns this activity is started. If not,
1340 // we will just leave it as is because someone should be bringing
1341 // another user's activities to the top of the stack.
1342 if (mService.mStartedUsers.get(next.userId) == null) {
1343 Slog.w(TAG, "Skipping resume of top activity " + next
1344 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001345 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001346 return false;
1347 }
1348
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001349 // The activity may be waiting for stop, but that is no longer
1350 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001351 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001352 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001353 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001354 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001355
Dianne Hackborn84375872012-06-01 19:03:50 -07001356 next.updateOptionsLocked(options);
1357
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001358 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1359
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001360 // If we are currently pausing an activity, then don't do anything
1361 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001362 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001363 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1364 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001365 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001366 return false;
1367 }
1368
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001369 // Okay we are now going to start a switch, to 'next'. We may first
1370 // have to pause the current activity, but this is an important point
1371 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001372 // XXX "App Redirected" dialog is getting too many false positives
1373 // at this point, so turn off for now.
1374 if (false) {
1375 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1376 long now = SystemClock.uptimeMillis();
1377 final boolean inTime = mLastStartedActivity.startTime != 0
1378 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1379 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1380 final int nextUid = next.info.applicationInfo.uid;
1381 if (inTime && lastUid != nextUid
1382 && lastUid != next.launchedFromUid
1383 && mService.checkPermission(
1384 android.Manifest.permission.STOP_APP_SWITCHES,
1385 -1, next.launchedFromUid)
1386 != PackageManager.PERMISSION_GRANTED) {
1387 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1388 } else {
1389 next.startTime = now;
1390 mLastStartedActivity = next;
1391 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001392 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001393 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001394 mLastStartedActivity = next;
1395 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001396 }
Craig Mautner58547802013-03-05 08:23:53 -08001397
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 // We need to start pausing the current activity so the top one
1399 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001400 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001401 if (mResumedActivity != null) {
1402 pausing = true;
1403 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001404 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001405 }
1406 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001407 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1408 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001409 // At this point we want to put the upcoming activity's process
1410 // at the top of the LRU list, since we know we will be needing it
1411 // very soon and it would be a waste to let it get killed if it
1412 // happens to be sitting towards the end.
1413 if (next.app != null && next.app.thread != null) {
1414 // No reason to do full oom adj update here; we'll let that
1415 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001416 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001417 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001418 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 return true;
1420 }
1421
Christopher Tated3f175c2012-06-14 14:16:54 -07001422 // If the most recent activity was noHistory but was only stopped rather
1423 // than stopped+finished because the device went to sleep, we need to make
1424 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001425 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1426 !mLastNoHistoryActivity.finishing) {
1427 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1428 " on new resume");
1429 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1430 null, "no-history", false);
1431 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001432 }
1433
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001434 if (prev != null && prev != next) {
1435 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1436 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001437 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438 if (DEBUG_SWITCH) Slog.v(
1439 TAG, "Resuming top, waiting visible to hide: " + prev);
1440 } else {
1441 // The next activity is already visible, so hide the previous
1442 // activity's windows right now so we can show the new one ASAP.
1443 // We only do this if the previous is finishing, which should mean
1444 // it is on top of the one being resumed so hiding it quickly
1445 // is good. Otherwise, we want to do the normal route of allowing
1446 // the resumed activity to be shown so we can decide if the
1447 // previous should actually be hidden depending on whether the
1448 // new one is found to be full-screen or not.
1449 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001450 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1452 + prev + ", waitingVisible="
1453 + (prev != null ? prev.waitingVisible : null)
1454 + ", nowVisible=" + next.nowVisible);
1455 } else {
1456 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1457 + prev + ", waitingVisible="
1458 + (prev != null ? prev.waitingVisible : null)
1459 + ", nowVisible=" + next.nowVisible);
1460 }
1461 }
1462 }
1463
Dianne Hackborne7f97212011-02-24 14:40:20 -08001464 // Launching this app's activity, make sure the app is no longer
1465 // considered stopped.
1466 try {
1467 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001468 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001469 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001470 } catch (IllegalArgumentException e) {
1471 Slog.w(TAG, "Failed trying to unstop package "
1472 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001473 }
1474
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001475 // We are starting up the next activity, so tell the window manager
1476 // that the previous one will be hidden soon. This way it can know
1477 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001478 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 if (prev != null) {
1480 if (prev.finishing) {
1481 if (DEBUG_TRANSITION) Slog.v(TAG,
1482 "Prepare close transition: prev=" + prev);
1483 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001484 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001485 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001487 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001488 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1489 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001491 mWindowManager.setAppWillBeHidden(prev.appToken);
1492 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001494 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001496 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001497 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001498 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001499 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001500 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1501 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502 }
1503 }
1504 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001505 mWindowManager.setAppWillBeHidden(prev.appToken);
1506 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 }
Craig Mautner967212c2013-04-13 21:10:58 -07001508 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001509 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001511 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001512 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001514 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001515 }
1516 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001517 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001518 next.applyOptionsLocked();
1519 } else {
1520 next.clearOptionsLocked();
1521 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522
Craig Mautnercf910b02013-04-23 11:23:27 -07001523 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 if (next.app != null && next.app.thread != null) {
1525 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1526
1527 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001528 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001530 // schedule launch ticks to collect information about slow apps.
1531 next.startLaunchTickingLocked();
1532
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001533 ActivityRecord lastResumedActivity =
1534 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 ActivityState lastState = next.state;
1536
1537 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001538
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001539 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540 next.state = ActivityState.RESUMED;
1541 mResumedActivity = next;
1542 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001543 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001544 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001546 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547
1548 // Have the window manager re-evaluate the orientation of
1549 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001550 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001551 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001552 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001553 mService.mConfiguration,
1554 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1555 if (config != null) {
1556 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001557 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001558 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001560
Craig Mautner525f3d92013-05-07 14:01:50 -07001561 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001562 // The configuration update wasn't able to keep the existing
1563 // instance of the activity, and instead started a new one.
1564 // We should be all done, but let's just make sure our activity
1565 // is still at the top and schedule another run if something
1566 // weird happened.
1567 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001568 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 "Activity config changed during resume: " + next
1570 + ", new next: " + nextNext);
1571 if (nextNext != next) {
1572 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001573 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001575 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001576 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001577 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001578 return true;
1579 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001580 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001581 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 }
Craig Mautner58547802013-03-05 08:23:53 -08001583
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 try {
1585 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001586 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001587 if (a != null) {
1588 final int N = a.size();
1589 if (!next.finishing && N > 0) {
1590 if (DEBUG_RESULTS) Slog.v(
1591 TAG, "Delivering results to " + next
1592 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001593 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001594 }
1595 }
1596
1597 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001598 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 }
1600
1601 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001602 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001604
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001605 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001606 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001607 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001608 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1609 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001610 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001611
Craig Mautner0eea92c2013-05-16 13:35:39 -07001612 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613
Craig Mautnerac6f8432013-07-17 13:24:59 -07001614 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 } catch (Exception e) {
1616 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001617 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1618 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001620 if (lastStack != null) {
1621 lastStack.mResumedActivity = lastResumedActivity;
1622 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 Slog.i(TAG, "Restarting because process died: " + next);
1624 if (!next.hasBeenLaunched) {
1625 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001626 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1627 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001628 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001629 next.appToken, next.packageName, next.theme,
1630 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001631 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1632 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001634 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001635 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 return true;
1637 }
1638
1639 // From this point on, if something goes wrong there is no way
1640 // to recover the activity.
1641 try {
1642 next.visible = true;
1643 completeResumeLocked(next);
1644 } catch (Exception e) {
1645 // If any exception gets thrown, toss away this
1646 // activity and try the next one.
1647 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001648 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001649 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001650 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 return true;
1652 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 next.stopped = false;
1654
1655 } else {
1656 // Whoops, need to restart this activity!
1657 if (!next.hasBeenLaunched) {
1658 next.hasBeenLaunched = true;
1659 } else {
1660 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001661 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001662 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001663 mService.compatibilityInfoForPackageLocked(
1664 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001666 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001667 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001668 }
1669 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1670 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001671 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001672 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 }
1674
Craig Mautnercf910b02013-04-23 11:23:27 -07001675 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001676 return true;
1677 }
1678
Craig Mautnerac6f8432013-07-17 13:24:59 -07001679 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001680 // If this is being moved to the top by another activity or being launched from the home
1681 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001682 if (isOnHomeDisplay()) {
1683 ActivityStack lastStack = mStackSupervisor.getLastStack();
1684 final boolean fromHome = lastStack.isHomeStack();
1685 if (!isHomeStack() && (fromHome || topTask() != task)) {
1686 task.mOnTopOfHome = fromHome;
1687 }
1688 } else {
1689 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001690 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001691
Craig Mautnerac6f8432013-07-17 13:24:59 -07001692 mTaskHistory.remove(task);
1693 // Now put task at top.
1694 int stackNdx = mTaskHistory.size();
1695 if (task.userId != mCurrentUser) {
1696 // Put non-current user tasks below current user tasks.
1697 while (--stackNdx >= 0) {
1698 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1699 break;
1700 }
1701 }
1702 ++stackNdx;
1703 }
1704 mTaskHistory.add(stackNdx, task);
1705 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001706
Craig Mautner8849a5e2013-04-02 16:41:03 -07001707 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001708 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001709 TaskRecord rTask = r.task;
1710 final int taskId = rTask.taskId;
1711 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001712 // Last activity in task had been removed or ActivityManagerService is reusing task.
1713 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001714 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001715 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001716 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001717 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001718 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 if (!newTask) {
1720 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001721 boolean startIt = true;
1722 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1723 task = mTaskHistory.get(taskNdx);
1724 if (task == r.task) {
1725 // Here it is! Now, if this is not yet visible to the
1726 // user, then just add it without starting; it will
1727 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001728 if (!startIt) {
1729 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1730 + task, new RuntimeException("here").fillInStackTrace());
1731 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001732 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001733 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1734 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001735 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001736 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001737 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001738 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001739 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001740 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741 return;
1742 }
1743 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001744 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745 startIt = false;
1746 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 }
1748 }
1749
1750 // Place a new activity at top of stack, so it is next to interact
1751 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 // If we are not placing the new activity frontmost, we do not want
1754 // to deliver the onUserLeaving callback to the actual frontmost
1755 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001756 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001757 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001758 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1759 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001760 }
Craig Mautner70a86932013-02-28 22:37:44 -08001761
1762 task = r.task;
1763
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001764 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001765 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001766 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001767 task.addActivityToTop(r);
Craig Mautner3b475fe2013-12-16 15:58:31 -08001768 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001769
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001770 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001771 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 // We want to show the starting preview window if we are
1773 // switching to a new task, or the next activity's process is
1774 // not currently running.
1775 boolean showStartingIcon = newTask;
1776 ProcessRecord proc = r.app;
1777 if (proc == null) {
1778 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1779 }
1780 if (proc == null || proc.thread == null) {
1781 showStartingIcon = true;
1782 }
1783 if (DEBUG_TRANSITION) Slog.v(TAG,
1784 "Prepare open transition: starting " + r);
1785 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001786 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001789 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001790 ? AppTransition.TRANSIT_TASK_OPEN
1791 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792 mNoAnimActivities.remove(r);
1793 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001794 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001795 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001796 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001797 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1798 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 boolean doShow = true;
1800 if (newTask) {
1801 // Even though this activity is starting fresh, we still need
1802 // to reset it to make sure we apply affinities to move any
1803 // existing activities from other tasks in to it.
1804 // If the caller has requested that the target task be
1805 // reset, then do so.
1806 if ((r.intent.getFlags()
1807 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1808 resetTaskIfNeededLocked(r, r);
1809 doShow = topRunningNonDelayedActivityLocked(null) == r;
1810 }
1811 }
1812 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1813 // Figure out if we are transitioning from another activity that is
1814 // "has the same starting icon" as the next one. This allows the
1815 // window manager to keep the previous window it had previously
1816 // created, if it still had one.
1817 ActivityRecord prev = mResumedActivity;
1818 if (prev != null) {
1819 // We don't want to reuse the previous starting preview if:
1820 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001821 if (prev.task != r.task) {
1822 prev = null;
1823 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001825 else if (prev.nowVisible) {
1826 prev = null;
1827 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001829 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001830 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001831 mService.compatibilityInfoForPackageLocked(
1832 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001833 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001834 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 }
1836 } else {
1837 // If this is the first activity, don't do any fancy animations,
1838 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001839 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001840 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001841 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1842 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001843 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001844 }
1845 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001846 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001847 }
1848
1849 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001850 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001851 }
1852 }
1853
Dianne Hackbornbe707852011-11-11 14:32:10 -08001854 final void validateAppTokensLocked() {
1855 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001856 mValidateAppTokens.ensureCapacity(numActivities());
1857 final int numTasks = mTaskHistory.size();
1858 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1859 TaskRecord task = mTaskHistory.get(taskNdx);
1860 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001861 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001862 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001863 }
Craig Mautner000f0022013-02-26 15:04:29 -08001864 TaskGroup group = new TaskGroup();
1865 group.taskId = task.taskId;
1866 mValidateAppTokens.add(group);
1867 final int numActivities = activities.size();
1868 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1869 final ActivityRecord r = activities.get(activityNdx);
1870 group.tokens.add(r.appToken);
1871 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001872 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001873 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001874 }
1875
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001876 /**
1877 * Perform a reset of the given task, if needed as part of launching it.
1878 * Returns the new HistoryRecord at the top of the task.
1879 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001880 /**
1881 * Helper method for #resetTaskIfNeededLocked.
1882 * We are inside of the task being reset... we'll either finish this activity, push it out
1883 * for another task, or leave it as-is.
1884 * @param task The task containing the Activity (taskTop) that might be reset.
1885 * @param forceReset
1886 * @return An ActivityOptions that needs to be processed.
1887 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001888 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001889 ActivityOptions topOptions = null;
1890
1891 int replyChainEnd = -1;
1892 boolean canMoveOptions = true;
1893
1894 // We only do this for activities that are not the root of the task (since if we finish
1895 // the root, we may no longer have the task!).
1896 final ArrayList<ActivityRecord> activities = task.mActivities;
1897 final int numActivities = activities.size();
1898 for (int i = numActivities - 1; i > 0; --i ) {
1899 ActivityRecord target = activities.get(i);
1900
1901 final int flags = target.info.flags;
1902 final boolean finishOnTaskLaunch =
1903 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1904 final boolean allowTaskReparenting =
1905 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1906 final boolean clearWhenTaskReset =
1907 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1908
1909 if (!finishOnTaskLaunch
1910 && !clearWhenTaskReset
1911 && target.resultTo != null) {
1912 // If this activity is sending a reply to a previous
1913 // activity, we can't do anything with it now until
1914 // we reach the start of the reply chain.
1915 // XXX note that we are assuming the result is always
1916 // to the previous activity, which is almost always
1917 // the case but we really shouldn't count on.
1918 if (replyChainEnd < 0) {
1919 replyChainEnd = i;
1920 }
1921 } else if (!finishOnTaskLaunch
1922 && !clearWhenTaskReset
1923 && allowTaskReparenting
1924 && target.taskAffinity != null
1925 && !target.taskAffinity.equals(task.affinity)) {
1926 // If this activity has an affinity for another
1927 // task, then we need to move it out of here. We will
1928 // move it as far out of the way as possible, to the
1929 // bottom of the activity stack. This also keeps it
1930 // correctly ordered with any activities we previously
1931 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08001932 final ThumbnailHolder newThumbHolder;
1933 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001934 final ActivityRecord bottom =
1935 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08001936 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001937 if (bottom != null && target.taskAffinity != null
1938 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001939 // If the activity currently at the bottom has the
1940 // same task affinity as the one we are moving,
1941 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08001942 targetTask = bottom.task;
1943 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001944 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001945 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001946 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08001947 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
1948 null, false);
1949 newThumbHolder = targetTask;
1950 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001951 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1952 + " out to new task " + target.task);
1953 }
1954
Craig Mautner329f4122013-11-07 09:10:42 -08001955 if (clearWhenTaskReset) {
1956 // This is the start of a new sub-task.
1957 if (target.thumbHolder == null) {
1958 target.thumbHolder = new ThumbnailHolder();
1959 }
1960 } else {
1961 target.thumbHolder = newThumbHolder;
1962 }
1963
Craig Mautnere3a74d52013-02-22 14:14:58 -08001964 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001965 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001966
Craig Mautner525f3d92013-05-07 14:01:50 -07001967 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001968 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1969 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001970 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001971 if (p.finishing) {
1972 continue;
1973 }
1974
Craig Mautner525f3d92013-05-07 14:01:50 -07001975 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001976 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001977 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001978 topOptions = p.takeOptionsLocked();
1979 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001980 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001981 }
1982 }
1983 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08001984 + task + " adding to task=" + targetTask
1985 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08001986 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1987 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001988 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001989 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001990
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001992 }
1993
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001994 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001995 if (VALIDATE_TOKENS) {
1996 validateAppTokensLocked();
1997 }
1998
1999 replyChainEnd = -1;
2000 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2001 // If the activity should just be removed -- either
2002 // because it asks for it, or the task should be
2003 // cleared -- then finish it and anything that is
2004 // part of its reply chain.
2005 int end;
2006 if (clearWhenTaskReset) {
2007 // In this case, we want to finish this activity
2008 // and everything above it, so be sneaky and pretend
2009 // like these are all in the reply chain.
2010 end = numActivities - 1;
2011 } else if (replyChainEnd < 0) {
2012 end = i;
2013 } else {
2014 end = replyChainEnd;
2015 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002016 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002017 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002018 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002019 if (p.finishing) {
2020 continue;
2021 }
2022 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002023 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002024 topOptions = p.takeOptionsLocked();
2025 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002026 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002027 }
2028 }
Craig Mautner58547802013-03-05 08:23:53 -08002029 if (DEBUG_TASKS) Slog.w(TAG,
2030 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002031 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002032 end--;
2033 srcPos--;
2034 }
2035 }
2036 replyChainEnd = -1;
2037 } else {
2038 // If we were in the middle of a chain, well the
2039 // activity that started it all doesn't want anything
2040 // special, so leave it all as-is.
2041 replyChainEnd = -1;
2042 }
2043 }
2044
2045 return topOptions;
2046 }
2047
2048 /**
2049 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2050 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2051 * @param affinityTask The task we are looking for an affinity to.
2052 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2053 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2054 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2055 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002056 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002057 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002058 int replyChainEnd = -1;
2059 final int taskId = task.taskId;
2060 final String taskAffinity = task.affinity;
2061
2062 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2063 final int numActivities = activities.size();
2064 // Do not operate on the root Activity.
2065 for (int i = numActivities - 1; i > 0; --i) {
2066 ActivityRecord target = activities.get(i);
2067
2068 final int flags = target.info.flags;
2069 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2070 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2071
2072 if (target.resultTo != null) {
2073 // If this activity is sending a reply to a previous
2074 // activity, we can't do anything with it now until
2075 // we reach the start of the reply chain.
2076 // XXX note that we are assuming the result is always
2077 // to the previous activity, which is almost always
2078 // the case but we really shouldn't count on.
2079 if (replyChainEnd < 0) {
2080 replyChainEnd = i;
2081 }
2082 } else if (topTaskIsHigher
2083 && allowTaskReparenting
2084 && taskAffinity != null
2085 && taskAffinity.equals(target.taskAffinity)) {
2086 // This activity has an affinity for our task. Either remove it if we are
2087 // clearing or move it over to our task. Note that
2088 // we currently punt on the case where we are resetting a
2089 // task that is not at the top but who has activities above
2090 // with an affinity to it... this is really not a normal
2091 // case, and we will need to later pull that task to the front
2092 // and usually at that point we will do the reset and pick
2093 // up those remaining activities. (This only happens if
2094 // someone starts an activity in a new task from an activity
2095 // in a task that is not currently on top.)
2096 if (forceReset || finishOnTaskLaunch) {
2097 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2098 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2099 for (int srcPos = start; srcPos >= i; --srcPos) {
2100 final ActivityRecord p = activities.get(srcPos);
2101 if (p.finishing) {
2102 continue;
2103 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002104 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002105 }
2106 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002107 if (taskInsertionPoint < 0) {
2108 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002109
Craig Mautner77878772013-03-04 19:46:24 -08002110 }
Craig Mautner77878772013-03-04 19:46:24 -08002111
2112 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2113 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2114 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2115 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002116 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002117 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002118 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002119
Craig Mautnere3a74d52013-02-22 14:14:58 -08002120 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2121 + " to stack at " + task,
2122 new RuntimeException("here").fillInStackTrace());
2123 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2124 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002125 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002126 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002127 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002128 if (VALIDATE_TOKENS) {
2129 validateAppTokensLocked();
2130 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002131
2132 // Now we've moved it in to place... but what if this is
2133 // a singleTop activity and we have put it on top of another
2134 // instance of the same activity? Then we drop the instance
2135 // below so it remains singleTop.
2136 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2137 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002138 int targetNdx = taskActivities.indexOf(target);
2139 if (targetNdx > 0) {
2140 ActivityRecord p = taskActivities.get(targetNdx - 1);
2141 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002142 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2143 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002144 }
2145 }
2146 }
2147 }
2148
2149 replyChainEnd = -1;
2150 }
2151 }
Craig Mautner77878772013-03-04 19:46:24 -08002152 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002153 }
2154
Craig Mautner8849a5e2013-04-02 16:41:03 -07002155 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002156 ActivityRecord newActivity) {
2157 boolean forceReset =
2158 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2159 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2160 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2161 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2162 forceReset = true;
2163 }
2164 }
2165
2166 final TaskRecord task = taskTop.task;
2167
2168 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2169 * for remaining tasks. Used for later tasks to reparent to task. */
2170 boolean taskFound = false;
2171
2172 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2173 ActivityOptions topOptions = null;
2174
Craig Mautner77878772013-03-04 19:46:24 -08002175 // Preserve the location for reparenting in the new task.
2176 int reparentInsertionPoint = -1;
2177
Craig Mautnere3a74d52013-02-22 14:14:58 -08002178 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2179 final TaskRecord targetTask = mTaskHistory.get(i);
2180
2181 if (targetTask == task) {
2182 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2183 taskFound = true;
2184 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002185 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2186 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002187 }
2188 }
2189
Craig Mautner70a86932013-02-28 22:37:44 -08002190 int taskNdx = mTaskHistory.indexOf(task);
2191 do {
2192 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2193 } while (taskTop == null && taskNdx >= 0);
2194
Craig Mautnere3a74d52013-02-22 14:14:58 -08002195 if (topOptions != null) {
2196 // If we got some ActivityOptions from an activity on top that
2197 // was removed from the task, propagate them to the new real top.
2198 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002199 taskTop.updateOptionsLocked(topOptions);
2200 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002201 topOptions.abort();
2202 }
2203 }
2204
2205 return taskTop;
2206 }
2207
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2209 String resultWho, int requestCode, int resultCode, Intent data) {
2210
2211 if (callingUid > 0) {
2212 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002213 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002214 }
2215
2216 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2217 + " : who=" + resultWho + " req=" + requestCode
2218 + " res=" + resultCode + " data=" + data);
2219 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2220 try {
2221 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2222 list.add(new ResultInfo(resultWho, requestCode,
2223 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002224 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225 return;
2226 } catch (Exception e) {
2227 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2228 }
2229 }
2230
2231 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2232 }
2233
Craig Mautner04f0b702013-10-22 12:31:01 -07002234 private void adjustFocusedActivityLocked(ActivityRecord r) {
2235 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2236 ActivityRecord next = topRunningActivityLocked(null);
2237 if (next != r) {
2238 final TaskRecord task = r.task;
2239 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2240 mStackSupervisor.moveHomeToTop();
2241 }
2242 }
2243 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2244 }
2245 }
2246
Craig Mautnerf3333272013-04-22 10:55:53 -07002247 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2249 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2250 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2251 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002252 if (!mService.mSleeping) {
2253 if (DEBUG_STATES) {
2254 Slog.d(TAG, "no-history finish of " + r);
2255 }
2256 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002257 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002258 } else {
2259 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2260 + " on stop because we're just sleeping");
2261 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002262 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002263 }
2264
2265 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002266 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002267 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 try {
2269 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002270 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2271 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 r.state = ActivityState.STOPPING;
2273 if (DEBUG_VISBILITY) Slog.v(
2274 TAG, "Stopping visible=" + r.visible + " for " + r);
2275 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002276 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002278 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002279 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002280 r.setSleeping(true);
2281 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002282 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002283 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002284 } catch (Exception e) {
2285 // Maybe just ignore exceptions here... if the process
2286 // has crashed, our death notification will clean things
2287 // up.
2288 Slog.w(TAG, "Exception thrown during pause", e);
2289 // Just in case, assume it to be stopped.
2290 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002291 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 r.state = ActivityState.STOPPED;
2293 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002294 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 }
2296 }
2297 }
2298 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002299
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002300 /**
2301 * @return Returns true if the activity is being finished, false if for
2302 * some reason it is being left as-is.
2303 */
2304 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002305 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002306 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002307 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002308 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002309 + ", result=" + resultCode + ", data=" + resultData
2310 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002311 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 return false;
2313 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314
Craig Mautnerd44711d2013-02-23 11:24:36 -08002315 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 return true;
2317 }
2318
Craig Mautnerd2328952013-03-05 12:46:26 -08002319 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002320 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2321 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2322 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2323 ActivityRecord r = activities.get(activityNdx);
2324 if (r.resultTo == self && r.requestCode == requestCode) {
2325 if ((r.resultWho == null && resultWho == null) ||
2326 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2327 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2328 false);
2329 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002330 }
2331 }
2332 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002333 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002334 }
2335
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002336 final void finishTopRunningActivityLocked(ProcessRecord app) {
2337 ActivityRecord r = topRunningActivityLocked(null);
2338 if (r != null && r.app == app) {
2339 // If the top running activity is from this crashing
2340 // process, then terminate it to avoid getting in a loop.
2341 Slog.w(TAG, " Force finishing activity "
2342 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002343 int taskNdx = mTaskHistory.indexOf(r.task);
2344 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002345 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002346 // Also terminate any activities below it that aren't yet
2347 // stopped, to avoid a situation where one will get
2348 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002349 --activityNdx;
2350 if (activityNdx < 0) {
2351 do {
2352 --taskNdx;
2353 if (taskNdx < 0) {
2354 break;
2355 }
2356 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2357 } while (activityNdx < 0);
2358 }
2359 if (activityNdx >= 0) {
2360 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002361 if (r.state == ActivityState.RESUMED
2362 || r.state == ActivityState.PAUSING
2363 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002364 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002365 Slog.w(TAG, " Force finishing activity "
2366 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002367 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002368 }
2369 }
2370 }
2371 }
2372 }
2373
Craig Mautnerd2328952013-03-05 12:46:26 -08002374 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002375 ArrayList<ActivityRecord> activities = r.task.mActivities;
2376 for (int index = activities.indexOf(r); index >= 0; --index) {
2377 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002378 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002379 break;
2380 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002381 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002382 }
2383 return true;
2384 }
2385
Dianne Hackborn5c607432012-02-28 14:44:19 -08002386 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2387 // send the result
2388 ActivityRecord resultTo = r.resultTo;
2389 if (resultTo != null) {
2390 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2391 + " who=" + r.resultWho + " req=" + r.requestCode
2392 + " res=" + resultCode + " data=" + resultData);
2393 if (r.info.applicationInfo.uid > 0) {
2394 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2395 resultTo.packageName, resultData,
2396 resultTo.getUriPermissionsLocked());
2397 }
2398 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2399 resultData);
2400 r.resultTo = null;
2401 }
2402 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2403
2404 // Make sure this HistoryRecord is not holding on to other resources,
2405 // because clients have remote IPC references to this object so we
2406 // can't assume that will go away and want to avoid circular IPC refs.
2407 r.results = null;
2408 r.pendingResults = null;
2409 r.newIntents = null;
2410 r.icicle = null;
2411 }
2412
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002413 /**
2414 * @return Returns true if this activity has been removed from the history
2415 * list, or false if it is still in the list and will be removed later.
2416 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002417 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2418 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002419 if (r.finishing) {
2420 Slog.w(TAG, "Duplicate finish request for " + r);
2421 return false;
2422 }
2423
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002424 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002426 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002427 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002428 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2429 final int index = activities.indexOf(r);
2430 if (index < (activities.size() - 1)) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08002431 r.task.setFrontOfTask();
Craig Mautnerd44711d2013-02-23 11:24:36 -08002432 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2433 // If the caller asked that this activity (and all above it)
2434 // be cleared when the task is reset, don't lose that information,
2435 // but propagate it up to the next activity.
Craig Mautner3b475fe2013-12-16 15:58:31 -08002436 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002437 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 }
2439 }
2440
2441 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002442
2443 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444
Dianne Hackborn5c607432012-02-28 14:44:19 -08002445 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002446
Craig Mautnerc8143c62013-09-03 12:15:57 -07002447 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002448 // There are clients waiting to receive thumbnails so, in case
2449 // this is an activity that someone is waiting for, add it
2450 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002451 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002452 }
2453
Craig Mautnerde4ef022013-04-07 19:01:33 -07002454 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002455 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002456 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002458 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002459 ? AppTransition.TRANSIT_TASK_CLOSE
2460 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002461
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002463 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002464
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002465 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2467 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2468 startPausingLocked(false, false);
2469 }
2470
2471 } else if (r.state != ActivityState.PAUSING) {
2472 // If the activity is PAUSING, we will complete the finish once
2473 // it is done pausing; else we can just directly finish it here.
2474 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002475 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 } else {
2477 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2478 }
2479
2480 return false;
2481 }
2482
Craig Mautnerf3333272013-04-22 10:55:53 -07002483 static final int FINISH_IMMEDIATELY = 0;
2484 static final int FINISH_AFTER_PAUSE = 1;
2485 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002486
Craig Mautnerf3333272013-04-22 10:55:53 -07002487 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002488 // First things first: if this activity is currently visible,
2489 // and the resumed activity is not yet visible, then hold off on
2490 // finishing until the resumed one becomes visible.
2491 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002492 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2493 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002494 if (mStackSupervisor.mStoppingActivities.size() > 3
2495 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 // If we already have a few activities waiting to stop,
2497 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002498 // them out. Or if r is the last of activity of the last task the stack
2499 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002500 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002501 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002502 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002503 }
2504 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002505 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2506 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002507 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002508 if (oomAdj) {
2509 mService.updateOomAdjLocked();
2510 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 return r;
2512 }
2513
2514 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002515 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002516 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002517 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002518 if (mResumedActivity == r) {
2519 mResumedActivity = null;
2520 }
2521 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002522 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 r.state = ActivityState.FINISHING;
2524
2525 if (mode == FINISH_IMMEDIATELY
2526 || prevState == ActivityState.STOPPED
2527 || prevState == ActivityState.INITIALIZING) {
2528 // If this activity is already stopped, we can just finish
2529 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002530 boolean activityRemoved = destroyActivityLocked(r, true,
2531 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002532 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002533 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002534 }
2535 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002536 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002537
2538 // Need to go through the full pause cycle to get this
2539 // activity into the stopped state and then finish it.
2540 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002541 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002542 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002543 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 return r;
2545 }
2546
Craig Mautnerd2328952013-03-05 12:46:26 -08002547 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002548 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002549 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002550 final TaskRecord task = srec.task;
2551 final ArrayList<ActivityRecord> activities = task.mActivities;
2552 final int start = activities.indexOf(srec);
2553 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002554 return false;
2555 }
2556 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002557 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002558 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002559 final ComponentName dest = destIntent.getComponent();
2560 if (start > 0 && dest != null) {
2561 for (int i = finishTo; i >= 0; i--) {
2562 ActivityRecord r = activities.get(i);
2563 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002564 r.info.name.equals(dest.getClassName())) {
2565 finishTo = i;
2566 parent = r;
2567 foundParentInTask = true;
2568 break;
2569 }
2570 }
2571 }
2572
2573 IActivityController controller = mService.mController;
2574 if (controller != null) {
2575 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2576 if (next != null) {
2577 // ask watcher if this is allowed
2578 boolean resumeOK = true;
2579 try {
2580 resumeOK = controller.activityResuming(next.packageName);
2581 } catch (RemoteException e) {
2582 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002583 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002584 }
2585
2586 if (!resumeOK) {
2587 return false;
2588 }
2589 }
2590 }
2591 final long origId = Binder.clearCallingIdentity();
2592 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002593 ActivityRecord r = activities.get(i);
2594 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002595 // Only return the supplied result for the first activity finished
2596 resultCode = Activity.RESULT_CANCELED;
2597 resultData = null;
2598 }
2599
2600 if (parent != null && foundParentInTask) {
2601 final int parentLaunchMode = parent.info.launchMode;
2602 final int destIntentFlags = destIntent.getFlags();
2603 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2604 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2605 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2606 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2607 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2608 } else {
2609 try {
2610 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2611 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002612 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002613 null, aInfo, parent.appToken, null,
2614 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002615 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002616 foundParentInTask = res == ActivityManager.START_SUCCESS;
2617 } catch (RemoteException e) {
2618 foundParentInTask = false;
2619 }
2620 requestFinishActivityLocked(parent.appToken, resultCode,
2621 resultData, "navigate-up", true);
2622 }
2623 }
2624 Binder.restoreCallingIdentity(origId);
2625 return foundParentInTask;
2626 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 /**
2628 * Perform the common clean-up of an activity record. This is called both
2629 * as part of destroyActivityLocked() (when destroying the client-side
2630 * representation) and cleaning things up as a result of its hosting
2631 * processing going away, in which case there is no remaining client-side
2632 * state to destroy so only the cleanup here is needed.
2633 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002634 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2635 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002636 if (mResumedActivity == r) {
2637 mResumedActivity = null;
2638 }
2639 if (mService.mFocusedActivity == r) {
2640 mService.mFocusedActivity = null;
2641 }
2642
2643 r.configDestroy = false;
2644 r.frozenBeforeDestroy = false;
2645
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002646 if (setState) {
2647 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2648 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002649 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002650 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002651 }
2652
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 // Make sure this record is no longer in the pending finishes list.
2654 // This could happen, for example, if we are trimming activities
2655 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002656 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002657 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002658
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002659 // Remove any pending results.
2660 if (r.finishing && r.pendingResults != null) {
2661 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2662 PendingIntentRecord rec = apr.get();
2663 if (rec != null) {
2664 mService.cancelIntentSenderLocked(rec, false);
2665 }
2666 }
2667 r.pendingResults = null;
2668 }
2669
2670 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002671 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002672 }
2673
Craig Mautnerc8143c62013-09-03 12:15:57 -07002674 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675 // There are clients waiting to receive thumbnails so, in case
2676 // this is an activity that someone is waiting for, add it
2677 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002678 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002679 }
2680
2681 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002682 removeTimeoutsForActivityLocked(r);
2683 }
2684
2685 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002686 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002687 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002688 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002689 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002690 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002691 }
2692
Craig Mautner04a0ea62014-01-13 12:51:26 -08002693 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002694 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002695 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2696 r.makeFinishing();
2697 if (DEBUG_ADD_REMOVE) {
2698 RuntimeException here = new RuntimeException("here");
2699 here.fillInStackTrace();
2700 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002702 r.takeFromHistory();
2703 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002704 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002705 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002706 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002707 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002708 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002709 if (VALIDATE_TOKENS) {
2710 validateAppTokensLocked();
2711 }
Craig Mautner312ba862014-02-10 17:55:01 -08002712 final TaskRecord task = r.task;
2713 if (task != null && task.removeActivity(r)) {
2714 if (DEBUG_STACK) Slog.i(TAG,
2715 "removeActivityFromHistoryLocked: last activity removed from " + this);
2716 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
2717 mStackSupervisor.moveHomeToTop();
2718 }
2719 removeTask(task);
2720 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002721 cleanUpActivityServicesLocked(r);
2722 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002724
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002725 /**
2726 * Perform clean-up of service connections in an activity record.
2727 */
2728 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2729 // Throw away any services that have been bound by this activity.
2730 if (r.connections != null) {
2731 Iterator<ConnectionRecord> it = r.connections.iterator();
2732 while (it.hasNext()) {
2733 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002734 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735 }
2736 r.connections = null;
2737 }
2738 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002739
2740 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2741 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2742 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2743 mHandler.sendMessage(msg);
2744 }
2745
Dianne Hackborn28695e02011-11-02 21:59:51 -07002746 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002747 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002748 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002749 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2750 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2751 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2752 final ActivityRecord r = activities.get(activityNdx);
2753 if (r.finishing) {
2754 continue;
2755 }
2756 if (r.fullscreen) {
2757 lastIsOpaque = true;
2758 }
2759 if (owner != null && r.app != owner) {
2760 continue;
2761 }
2762 if (!lastIsOpaque) {
2763 continue;
2764 }
2765 // We can destroy this one if we have its icicle saved and
2766 // it is not in the process of pausing/stopping/finishing.
2767 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2768 && r.haveState && !r.visible && r.stopped
2769 && r.state != ActivityState.DESTROYING
2770 && r.state != ActivityState.DESTROYED) {
2771 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2772 + " resumed=" + mResumedActivity
2773 + " pausing=" + mPausingActivity);
2774 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2775 activityRemoved = true;
2776 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002777 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002778 }
2779 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002780 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002781 mStackSupervisor.resumeTopActivitiesLocked();
2782
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002783 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002784 }
2785
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786 /**
2787 * Destroy the current CLIENT SIDE instance of an activity. This may be
2788 * called both when actually finishing an activity, or when performing
2789 * a configuration switch where we destroy the current client-side object
2790 * but then create a new client-side object for this same HistoryRecord.
2791 */
2792 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002793 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002794 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002795 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2797 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002798 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002799 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800
2801 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002802
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002803 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002804
2805 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002806
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 if (hadApp) {
2808 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002809 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2811 mService.mHeavyWeightProcess = null;
2812 mService.mHandler.sendEmptyMessage(
2813 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2814 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002815 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002816 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002817 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002818 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 }
2820 }
2821
2822 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002823
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 try {
2825 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002826 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 r.configChangeFlags);
2828 } catch (Exception e) {
2829 // We can just ignore exceptions here... if the process
2830 // has crashed, our death notification will clean things
2831 // up.
2832 //Slog.w(TAG, "Exception thrown during finish", e);
2833 if (r.finishing) {
2834 removeActivityFromHistoryLocked(r);
2835 removedFromHistory = true;
2836 skipDestroy = true;
2837 }
2838 }
2839
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002840 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002841
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002842 // If the activity is finishing, we need to wait on removing it
2843 // from the list to give it a chance to do its cleanup. During
2844 // that time it may make calls back with its token so we need to
2845 // be able to find it on the list and so we don't want to remove
2846 // it from the list yet. Otherwise, we can just immediately put
2847 // it in the destroyed state since we are not removing it from the
2848 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002850 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2851 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002852 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002853 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2855 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002856 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002858 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002859 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 }
2861 } else {
2862 // remove this record from the history.
2863 if (r.finishing) {
2864 removeActivityFromHistoryLocked(r);
2865 removedFromHistory = true;
2866 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002867 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002869 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002870 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871 }
2872 }
2873
2874 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002875
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002876 if (!mLRUActivities.remove(r) && hadApp) {
2877 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2878 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002879
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002880 return removedFromHistory;
2881 }
2882
Craig Mautnerd2328952013-03-05 12:46:26 -08002883 final void activityDestroyedLocked(IBinder token) {
2884 final long origId = Binder.clearCallingIdentity();
2885 try {
2886 ActivityRecord r = ActivityRecord.forToken(token);
2887 if (r != null) {
2888 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002890
2891 if (isInStackLocked(token) != null) {
2892 if (r.state == ActivityState.DESTROYING) {
2893 cleanUpActivityLocked(r, true, false);
2894 removeActivityFromHistoryLocked(r);
2895 }
2896 }
Craig Mautner05d29032013-05-03 13:40:13 -07002897 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002898 } finally {
2899 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002900 }
2901 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002902
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002903 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2904 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002906 if (DEBUG_CLEANUP) Slog.v(
2907 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002908 + " with " + i + " entries");
2909 while (i > 0) {
2910 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002911 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002912 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002914 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002915 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002916 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 }
2918 }
2919 }
2920
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002921 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2922 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002923 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2924 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002925 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2926 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002927 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002928 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002929 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2930 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002931
2932 boolean hasVisibleActivities = false;
2933
2934 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002935 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002936 if (DEBUG_CLEANUP) Slog.v(
2937 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002938 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2939 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2940 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2941 final ActivityRecord r = activities.get(activityNdx);
2942 --i;
2943 if (DEBUG_CLEANUP) Slog.v(
2944 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2945 if (r.app == app) {
2946 boolean remove;
2947 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2948 // Don't currently have state for the activity, or
2949 // it is finishing -- always remove it.
2950 remove = true;
2951 } else if (r.launchCount > 2 &&
2952 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2953 // We have launched this activity too many times since it was
2954 // able to run, so give up and remove it.
2955 remove = true;
2956 } else {
2957 // The process may be gone, but the activity lives on!
2958 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002959 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002960 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002961 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002962 RuntimeException here = new RuntimeException("here");
2963 here.fillInStackTrace();
2964 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2965 + ": haveState=" + r.haveState
2966 + " stateNotNeeded=" + r.stateNotNeeded
2967 + " finishing=" + r.finishing
2968 + " state=" + r.state, here);
2969 }
2970 if (!r.finishing) {
2971 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2972 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2973 r.userId, System.identityHashCode(r),
2974 r.task.taskId, r.shortComponentName,
2975 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002976 if (r.state == ActivityState.RESUMED) {
2977 mService.updateUsageStats(r, false);
2978 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002979 }
2980 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002981
Craig Mautner0247fc82013-02-28 14:32:06 -08002982 } else {
2983 // We have the current state for this activity, so
2984 // it can be restarted later when needed.
2985 if (localLOGV) Slog.v(
2986 TAG, "Keeping entry, setting app to null");
2987 if (r.visible) {
2988 hasVisibleActivities = true;
2989 }
2990 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2991 + r);
2992 r.app = null;
2993 r.nowVisible = false;
2994 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002996 "App died, clearing saved state of " + r);
2997 r.icicle = null;
2998 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002999 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003000
Craig Mautnerd2328952013-03-05 12:46:26 -08003001 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003002 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003003 }
3004 }
3005
3006 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003008
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003009 final void updateTransitLocked(int transit, Bundle options) {
3010 if (options != null) {
3011 ActivityRecord r = topRunningActivityLocked(null);
3012 if (r != null && r.state != ActivityState.RESUMED) {
3013 r.updateOptionsLocked(options);
3014 } else {
3015 ActivityOptions.abort(options);
3016 }
3017 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003018 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003019 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003020
Craig Mautnera82aa092013-09-13 15:34:08 -07003021 void moveHomeTaskToTop() {
3022 final int top = mTaskHistory.size() - 1;
3023 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3024 final TaskRecord task = mTaskHistory.get(taskNdx);
3025 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003026 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003027 mTaskHistory.remove(taskNdx);
3028 mTaskHistory.add(top, task);
3029 mWindowManager.moveTaskToTop(task.taskId);
3030 return;
3031 }
3032 }
3033 }
3034
Craig Mautnercae015f2013-02-08 14:31:27 -08003035 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003036 final TaskRecord task = taskForIdLocked(taskId);
3037 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003038 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003039 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003040 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003041 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3042 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07003043 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003044 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003045 }
3046 moveTaskToFrontLocked(task, null, options);
3047 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003048 }
3049 return false;
3050 }
3051
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003052 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003053 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054
Craig Mautner11bf9a52013-02-19 14:08:51 -08003055 final int numTasks = mTaskHistory.size();
3056 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003057 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003058 // nothing to do!
3059 if (reason != null &&
3060 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3061 ActivityOptions.abort(options);
3062 } else {
3063 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3064 }
3065 return;
3066 }
3067
Craig Mautnere0a38842013-12-16 16:14:02 -08003068 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003069
Craig Mautner11bf9a52013-02-19 14:08:51 -08003070 // Shift all activities with this task up to the top
3071 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003072 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003073
3074 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 if (reason != null &&
3076 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003077 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 ActivityRecord r = topRunningActivityLocked(null);
3079 if (r != null) {
3080 mNoAnimActivities.add(r);
3081 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003082 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003084 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003086
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003087 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003088
Craig Mautner05d29032013-05-03 13:40:13 -07003089 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003090 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003091
3092 if (VALIDATE_TOKENS) {
3093 validateAppTokensLocked();
3094 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 }
3096
3097 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003098 * Worker method for rearranging history stack. Implements the function of moving all
3099 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003101 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3103 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003104 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003105 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003106 * @return Returns true if the move completed, false if not.
3107 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003108 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3109 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 // If we have a watcher, preflight the move before committing to it. First check
3112 // for *other* available tasks, but if none are available, then try again allowing the
3113 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003114 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003115 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003116 if (next == null) {
3117 next = topRunningActivityLocked(null, 0);
3118 }
3119 if (next != null) {
3120 // ask watcher if this is allowed
3121 boolean moveOK = true;
3122 try {
3123 moveOK = mService.mController.activityResuming(next.packageName);
3124 } catch (RemoteException e) {
3125 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003126 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 }
3128 if (!moveOK) {
3129 return false;
3130 }
3131 }
3132 }
3133
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003134 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003135 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003136
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003137 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003138 if (tr == null) {
3139 return false;
3140 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003141
Craig Mautner11bf9a52013-02-19 14:08:51 -08003142 mTaskHistory.remove(tr);
3143 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144
Craig Mautnerc8143c62013-09-03 12:15:57 -07003145 // There is an assumption that moving a task to the back moves it behind the home activity.
3146 // We make sure here that some activity in the stack will launch home.
3147 ActivityRecord lastActivity = null;
3148 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003149 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3150 final TaskRecord task = mTaskHistory.get(taskNdx);
3151 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003152 break;
3153 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003154 if (taskNdx == 1) {
3155 // Set the last task before tr to go to home.
3156 task.mOnTopOfHome = true;
3157 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003158 }
3159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003161 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3162 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 ActivityRecord r = topRunningActivityLocked(null);
3164 if (r != null) {
3165 mNoAnimActivities.add(r);
3166 }
3167 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003168 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003170 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003171
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003173 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 }
3175
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003176 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003177 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003178 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003179 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003180 }
3181
Craig Mautner05d29032013-05-03 13:40:13 -07003182 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 return true;
3184 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003185
Craig Mautner8849a5e2013-04-02 16:41:03 -07003186 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003187 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003188 final Uri data = r.intent.getData();
3189 final String strData = data != null ? data.toSafeString() : null;
3190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003192 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003194 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 }
3196
3197 /**
3198 * Make sure the given activity matches the current configuration. Returns
3199 * false if the activity had to be destroyed. Returns true if the
3200 * configuration is the same, or the activity will remain running as-is
3201 * for whatever reason. Ensures the HistoryRecord is updated with the
3202 * correct configuration and all other bookkeeping is handled.
3203 */
3204 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3205 int globalChanges) {
3206 if (mConfigWillChange) {
3207 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3208 "Skipping config check (will change): " + r);
3209 return true;
3210 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003211
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003212 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3213 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003214
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 // Short circuit: if the two configurations are the exact same
3216 // object (the common case), then there is nothing to do.
3217 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003218 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003219 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3220 "Configuration unchanged in " + r);
3221 return true;
3222 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003223
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 // We don't worry about activities that are finishing.
3225 if (r.finishing) {
3226 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3227 "Configuration doesn't matter in finishing " + r);
3228 r.stopFreezingScreenLocked(false);
3229 return true;
3230 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003231
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003232 // Okay we now are going to make this activity have the new config.
3233 // But then we need to figure out how it needs to deal with that.
3234 Configuration oldConfig = r.configuration;
3235 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003236
3237 // Determine what has changed. May be nothing, if this is a config
3238 // that has come back from the app after going idle. In that case
3239 // we just want to leave the official config object now in the
3240 // activity and do nothing else.
3241 final int changes = oldConfig.diff(newConfig);
3242 if (changes == 0 && !r.forceNewConfig) {
3243 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3244 "Configuration no differences in " + r);
3245 return true;
3246 }
3247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 // If the activity isn't currently running, just leave the new
3249 // configuration and it will pick that up next time it starts.
3250 if (r.app == null || r.app.thread == null) {
3251 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3252 "Configuration doesn't matter not running " + r);
3253 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003254 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003255 return true;
3256 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003257
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003258 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003259 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3260 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3261 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003262 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003263 + ", newConfig=" + newConfig);
3264 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003265 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003266 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3267 r.configChangeFlags |= changes;
3268 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003269 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003270 if (r.app == null || r.app.thread == null) {
3271 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003272 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003273 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003274 } else if (r.state == ActivityState.PAUSING) {
3275 // A little annoying: we are waiting for this activity to
3276 // finish pausing. Let's not do anything now, but just
3277 // flag that it needs to be restarted when done pausing.
3278 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003279 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003280 r.configDestroy = true;
3281 return true;
3282 } else if (r.state == ActivityState.RESUMED) {
3283 // Try to optimize this case: the configuration is changing
3284 // and we need to restart the top, resumed activity.
3285 // Instead of doing the normal handshaking, just say
3286 // "restart!".
3287 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003288 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003289 relaunchActivityLocked(r, r.configChangeFlags, true);
3290 r.configChangeFlags = 0;
3291 } else {
3292 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003293 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003294 relaunchActivityLocked(r, r.configChangeFlags, false);
3295 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003296 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003297
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003298 // All done... tell the caller we weren't able to keep this
3299 // activity around.
3300 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003301 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003302
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 // Default case: the activity can handle this new configuration, so
3304 // hand it over. Note that we don't need to give it the new
3305 // configuration, since we always send configuration changes to all
3306 // process when they happen so it can just use whatever configuration
3307 // it last got.
3308 if (r.app != null && r.app.thread != null) {
3309 try {
3310 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003311 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 } catch (RemoteException e) {
3313 // If process died, whatever.
3314 }
3315 }
3316 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003317
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 return true;
3319 }
3320
Craig Mautnerc8143c62013-09-03 12:15:57 -07003321 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 int changes, boolean andResume) {
3323 List<ResultInfo> results = null;
3324 List<Intent> newIntents = null;
3325 if (andResume) {
3326 results = r.results;
3327 newIntents = r.newIntents;
3328 }
3329 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3330 + " with results=" + results + " newIntents=" + newIntents
3331 + " andResume=" + andResume);
3332 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003333 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003335
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003337
Craig Mautner34b73df2014-01-12 21:11:08 -08003338 mStackSupervisor.removeChildActivityContainers(r);
3339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003340 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003341 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3342 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3343 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003344 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003345 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003346 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 // Note: don't need to call pauseIfSleepingLocked() here, because
3348 // the caller will only pass in 'andResume' if this activity is
3349 // currently resumed, which implies we aren't sleeping.
3350 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003351 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 }
3353
3354 if (andResume) {
3355 r.results = null;
3356 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003357 r.state = ActivityState.RESUMED;
3358 } else {
3359 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3360 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 }
3362
3363 return true;
3364 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003365
3366 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003367 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3368 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3369 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3370 final ActivityRecord r = activities.get(activityNdx);
3371 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003372 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003373 }
3374 if (r.fullscreen && !r.finishing) {
3375 return false;
3376 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003377 }
3378 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003379 final ActivityRecord r = ActivityRecord.forToken(token);
3380 if (r == null) {
3381 return false;
3382 }
3383 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3384 + " would have returned true for r=" + r);
3385 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003386 }
3387
3388 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003389 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3390 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3391 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3392 final ActivityRecord r = activities.get(activityNdx);
3393 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003394 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003395 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003396 }
3397 }
3398 }
3399
3400 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3401 boolean didSomething = false;
3402 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3404 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3405 int numActivities = activities.size();
3406 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3407 ActivityRecord r = activities.get(activityNdx);
3408 final boolean samePackage = r.packageName.equals(name)
3409 || (name == null && r.userId == userId);
3410 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3411 && (samePackage || r.task == lastTask)
3412 && (r.app == null || evenPersistent || !r.app.persistent)) {
3413 if (!doit) {
3414 if (r.finishing) {
3415 // If this activity is just finishing, then it is not
3416 // interesting as far as something to stop.
3417 continue;
3418 }
3419 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003420 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003421 didSomething = true;
3422 Slog.i(TAG, " Force finishing activity " + r);
3423 if (samePackage) {
3424 if (r.app != null) {
3425 r.app.removed = true;
3426 }
3427 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003428 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003429 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003430 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3431 true)) {
3432 // r has been deleted from mActivities, accommodate.
3433 --numActivities;
3434 --activityNdx;
3435 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003436 }
3437 }
3438 }
3439 return didSomething;
3440 }
3441
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003442 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003443 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003444 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003446 final TaskRecord task = mTaskHistory.get(taskNdx);
3447 ActivityRecord r = null;
3448 ActivityRecord top = null;
3449 int numActivities = 0;
3450 int numRunning = 0;
3451 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003452 if (activities.isEmpty()) {
3453 continue;
3454 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003455 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3456 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003457
Craig Mautneraab647e2013-02-28 16:31:36 -08003458 // Initialize state for next task if needed.
3459 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3460 top = r;
3461 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003462 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003463
3464 // Add 'r' into the current task.
3465 numActivities++;
3466 if (r.app != null && r.app.thread != null) {
3467 numRunning++;
3468 }
3469
3470 if (localLOGV) Slog.v(
3471 TAG, r.intent.getComponent().flattenToShortString()
3472 + ": task=" + r.task);
3473 }
3474
3475 RunningTaskInfo ci = new RunningTaskInfo();
3476 ci.id = task.taskId;
3477 ci.baseActivity = r.intent.getComponent();
3478 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003479 ci.lastActiveTime = task.lastActiveTime;
3480
Craig Mautneraab647e2013-02-28 16:31:36 -08003481 if (top.thumbHolder != null) {
3482 ci.description = top.thumbHolder.lastDescription;
3483 }
3484 ci.numActivities = numActivities;
3485 ci.numRunning = numRunning;
3486 //System.out.println(
3487 // "#" + maxNum + ": " + " descr=" + ci.description);
3488 if (receiver != null) {
3489 if (localLOGV) Slog.v(
3490 TAG, "State=" + top.state + "Idle=" + top.idle
3491 + " app=" + top.app
3492 + " thr=" + (top.app != null ? top.app.thread : null));
3493 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3494 if (top.idle && top.app != null && top.app.thread != null) {
3495 topRecord = top;
3496 } else {
3497 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003498 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003499 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003500 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003501 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003502 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003503 }
3504 return topRecord;
3505 }
3506
3507 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003508 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003509 if (DEBUG_SWITCH) Slog.d(
3510 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003511 if (top >= 0) {
3512 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3513 int activityTop = activities.size() - 1;
3514 if (activityTop > 0) {
3515 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3516 "unhandled-back", true);
3517 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003518 }
3519 }
3520
Craig Mautner6b74cb52013-09-27 17:02:21 -07003521 /**
3522 * Reset local parameters because an app's activity died.
3523 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003524 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003525 */
3526 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003527 if (mPausingActivity != null && mPausingActivity.app == app) {
3528 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3529 "App died while pausing: " + mPausingActivity);
3530 mPausingActivity = null;
3531 }
3532 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3533 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003534 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003535 }
3536
Craig Mautner19091252013-10-05 00:03:53 -07003537 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003538 }
3539
Craig Mautnercae015f2013-02-08 14:31:27 -08003540 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003541 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3542 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3543 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3544 final ActivityRecord r = activities.get(activityNdx);
3545 if (r.app == app) {
3546 Slog.w(TAG, " Force finishing activity "
3547 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003548 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003549 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003550 }
3551 }
3552 }
3553
Dianne Hackborn390517b2013-05-30 15:03:32 -07003554 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003555 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003556 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003557 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3558 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003559 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3560 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003561 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003562 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003563 if (printed) {
3564 header = null;
3565 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003566 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003567 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003568 }
3569
3570 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3571 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3572
3573 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003574 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3575 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003576 }
3577 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003578 final int top = mTaskHistory.size() - 1;
3579 if (top >= 0) {
3580 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3581 int listTop = list.size() - 1;
3582 if (listTop >= 0) {
3583 activities.add(list.get(listTop));
3584 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003585 }
3586 } else {
3587 ItemMatcher matcher = new ItemMatcher();
3588 matcher.build(name);
3589
Craig Mautneraab647e2013-02-28 16:31:36 -08003590 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3591 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3592 if (matcher.match(r1, r1.intent.getComponent())) {
3593 activities.add(r1);
3594 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003595 }
3596 }
3597 }
3598
3599 return activities;
3600 }
3601
3602 ActivityRecord restartPackage(String packageName) {
3603 ActivityRecord starting = topRunningActivityLocked(null);
3604
3605 // All activities that came from the package must be
3606 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003607 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3608 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3609 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3610 final ActivityRecord a = activities.get(activityNdx);
3611 if (a.info.packageName.equals(packageName)) {
3612 a.forceNewConfig = true;
3613 if (starting != null && a == starting && a.visible) {
3614 a.startFreezingScreenLocked(starting.app,
3615 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3616 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003617 }
3618 }
3619 }
3620
3621 return starting;
3622 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003623
Craig Mautner04a0ea62014-01-13 12:51:26 -08003624 void removeTask(TaskRecord task) {
3625 mWindowManager.removeTask(task.taskId);
3626 final ActivityRecord r = mResumedActivity;
3627 if (r != null && r.task == task) {
3628 mResumedActivity = null;
3629 }
3630
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003631 final int taskNdx = mTaskHistory.indexOf(task);
3632 final int topTaskNdx = mTaskHistory.size() - 1;
3633 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3634 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3635 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003636 mTaskHistory.remove(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003637
3638 if (mTaskHistory.isEmpty()) {
3639 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3640 if (isOnHomeDisplay()) {
3641 mStackSupervisor.moveHomeStack(!isHomeStack());
3642 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003643 if (mStacks != null) {
3644 mStacks.remove(this);
3645 mStacks.add(0, this);
3646 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003647 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003648 }
3649
Craig Mautnerde4ef022013-04-07 19:01:33 -07003650 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003651 TaskRecord task = new TaskRecord(taskId, info, intent);
3652 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003653 return task;
3654 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003655
3656 ArrayList<TaskRecord> getAllTasks() {
3657 return new ArrayList<TaskRecord>(mTaskHistory);
3658 }
3659
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003660 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003661 task.stack = this;
3662 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003663 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003664 } else {
3665 mTaskHistory.add(0, task);
3666 }
3667 }
3668
3669 public int getStackId() {
3670 return mStackId;
3671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003672
3673 @Override
3674 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003675 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3676 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003677 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003678}