blob: a95710bbc473bbbeebc1235f611fe4aef2cf630f [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 Hackborn91097de2014-04-04 18:02:06 -070039import android.service.voice.IVoiceInteractionSession;
40import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070042import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080043import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080044import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080045import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080046import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048
49import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070050import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.content.res.Resources;
62import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080063import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070065import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080066import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.Handler;
68import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090069import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070077import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import java.lang.ref.WeakReference;
82import java.util.ArrayList;
83import java.util.Iterator;
84import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080085import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
87/**
88 * State and management of a single stack of activities.
89 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070090final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080091
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070092 // Ticks during which we check progress while waiting for an app to launch.
93 static final int LAUNCH_TICK = 500;
94
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095 // How long we wait until giving up on the last activity to pause. This
96 // is short because it directly impacts the responsiveness of starting the
97 // next activity.
98 static final int PAUSE_TIMEOUT = 500;
99
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700100 // How long we wait for the activity to tell us it has stopped before
101 // giving up. This is a good amount of time because we really need this
102 // from the application in order to get its saved state.
103 static final int STOP_TIMEOUT = 10*1000;
104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105 // How long we wait until giving up on an activity telling us it has
106 // finished destroying itself.
107 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800110 // disabled.
111 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800112
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700113 // How long between activity launches that we consider safe to not warn
114 // the user about an unexpected activity being launched on top.
115 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // Set to false to disable the preview that is shown while a new activity
118 // is being started.
119 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800120
Craig Mautner5eda9b32013-07-02 11:58:16 -0700121 // How long to wait for all background Activities to redraw following a call to
122 // convertToTranslucent().
123 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
124
Craig Mautnera61bc652013-10-28 15:43:18 -0700125 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700126
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
Amith Yamasani734983f2014-03-04 16:48:05 -0800343 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100344 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800345 */
Kenny Guy2a764942014-04-02 13:29:20 +0100346 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100347 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100348 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
349 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800350 }
351 return false;
352 }
353
354 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100355 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700356 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
357 }
358
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700359 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800360 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700361 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
362 if (r != null) {
363 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800364 }
365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 return null;
367 }
368
369 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800370 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
371 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800372 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800373 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
374 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800375 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800376 return r;
377 }
378 }
379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 return null;
381 }
382
383 /**
384 * This is a simplified version of topRunningActivityLocked that provides a number of
385 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800386 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 * @param token If non-null, any history records matching this token will be skipped.
388 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800389 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700390 * @return Returns the HistoryRecord of the next activity on the stack.
391 */
392 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
394 TaskRecord task = mTaskHistory.get(taskNdx);
395 if (task.taskId == taskId) {
396 continue;
397 }
398 ArrayList<ActivityRecord> activities = task.mActivities;
399 for (int i = activities.size() - 1; i >= 0; --i) {
400 final ActivityRecord r = activities.get(i);
401 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800402 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800403 return r;
404 }
405 }
406 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700407 return null;
408 }
409
Craig Mautner8849a5e2013-04-02 16:41:03 -0700410 final ActivityRecord topActivity() {
411 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700412 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
414 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700415 final int topActivityNdx = activities.size() - 1;
416 if (topActivityNdx >= 0) {
417 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700418 }
419 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700420 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700421 }
422
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700423 final TaskRecord topTask() {
424 final int size = mTaskHistory.size();
425 if (size > 0) {
426 return mTaskHistory.get(size - 1);
427 }
428 return null;
429 }
430
Craig Mautnerd2328952013-03-05 12:46:26 -0800431 TaskRecord taskForIdLocked(int id) {
432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
433 final TaskRecord task = mTaskHistory.get(taskNdx);
434 if (task.taskId == id) {
435 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800436 }
437 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700438 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700439 }
440
Craig Mautnerd2328952013-03-05 12:46:26 -0800441 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800442 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800443 if (r != null) {
444 final TaskRecord task = r.task;
445 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
446 if (task.stack != this) Slog.w(TAG,
447 "Illegal state! task does not point to stack it is in.");
448 return r;
449 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800450 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800451 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800452 }
453
Craig Mautner2420ead2013-04-01 17:13:20 -0700454 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 final boolean hadit = mLRUActivities.remove(r);
456 mLRUActivities.add(r);
457 return hadit;
458 }
459
Craig Mautnerde4ef022013-04-07 19:01:33 -0700460 final boolean isHomeStack() {
461 return mStackId == HOME_STACK_ID;
462 }
463
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 final boolean isOnHomeDisplay() {
465 return isAttached() &&
466 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
467 }
468
469 final void moveToFront() {
470 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800471 if (isOnHomeDisplay()) {
472 mStackSupervisor.moveHomeStack(isHomeStack());
473 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800474 mStacks.remove(this);
475 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800477 }
478
479 final boolean isAttached() {
480 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800481 }
482
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700483 /**
484 * Returns the top activity in any existing task matching the given
485 * Intent. Returns null if no such task is found.
486 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700487 ActivityRecord findTaskLocked(ActivityRecord target) {
488 Intent intent = target.intent;
489 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700490 ComponentName cls = intent.getComponent();
491 if (info.targetActivity != null) {
492 cls = new ComponentName(info.packageName, info.targetActivity);
493 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700494 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700495 boolean isDocument = intent != null & intent.isDocument();
496 // If documentData is non-null then it must match the existing task data.
497 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800498
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700499 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800500 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
501 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700502 if (task.voiceSession != null) {
503 // We never match voice sessions; those always run independently.
504 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
505 continue;
506 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700507 if (task.userId != userId) {
508 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700509 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700510 continue;
511 }
Craig Mautner000f0022013-02-26 15:04:29 -0800512 final ActivityRecord r = task.getTopActivity();
513 if (r == null || r.finishing || r.userId != userId ||
514 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700515 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800516 continue;
517 }
518
Craig Mautnerd00f4742014-03-12 14:17:26 -0700519 final Intent taskIntent = task.intent;
520 final Intent affinityIntent = task.affinityIntent;
521 final boolean taskIsDocument;
522 final Uri taskDocumentData;
523 if (taskIntent != null && taskIntent.isDocument()) {
524 taskIsDocument = true;
525 taskDocumentData = taskIntent.getData();
526 } else if (affinityIntent != null && affinityIntent.isDocument()) {
527 taskIsDocument = true;
528 taskDocumentData = affinityIntent.getData();
529 } else {
530 taskIsDocument = false;
531 taskDocumentData = null;
532 }
533
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700534 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700535 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700536 + "/aff=" + r.task.affinity + " to new cls="
537 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700538 if (!isDocument && !taskIsDocument && task.affinity != null) {
539 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700540 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700541 return r;
542 }
Craig Mautnerd00f4742014-03-12 14:17:26 -0700543 } else if (taskIntent != null && taskIntent.getComponent().equals(cls) &&
544 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700545 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800546 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700547 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
548 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800549 return r;
Craig Mautnerd00f4742014-03-12 14:17:26 -0700550 } else if (affinityIntent != null && affinityIntent.getComponent().equals(cls) &&
551 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700552 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800553 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700554 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
555 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800556 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700557 } else if (DEBUG_TASKS) {
558 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700559 }
560 }
561
562 return null;
563 }
564
565 /**
566 * Returns the first activity (starting from the top of the stack) that
567 * is the same as the given activity. Returns null if no such activity
568 * is found.
569 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700570 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700571 ComponentName cls = intent.getComponent();
572 if (info.targetActivity != null) {
573 cls = new ComponentName(info.packageName, info.targetActivity);
574 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700575 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700576
Craig Mautner000f0022013-02-26 15:04:29 -0800577 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700578 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100579 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700580 return null;
581 }
582 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800583 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
584 ActivityRecord r = activities.get(activityNdx);
585 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700586 //Slog.i(TAG, "Found matching class!");
587 //dump();
588 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
589 return r;
590 }
591 }
592 }
593
594 return null;
595 }
596
Amith Yamasani742a6712011-05-04 14:49:28 -0700597 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700598 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700599 */
Craig Mautner93529a42013-10-04 15:03:13 -0700600 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800601 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700602 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800603 }
604 mCurrentUser = userId;
605
606 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800607 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800608 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700609 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100610 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700611 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
612 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800613 mTaskHistory.remove(i);
614 mTaskHistory.add(task);
615 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800616 // Use same value for i.
617 } else {
618 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800619 }
620 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700621 if (VALIDATE_TOKENS) {
622 validateAppTokensLocked();
623 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700624 }
625
Craig Mautner2420ead2013-04-01 17:13:20 -0700626 void minimalResumeActivityLocked(ActivityRecord r) {
627 r.state = ActivityState.RESUMED;
628 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
629 + " (starting new instance)");
630 r.stopped = false;
631 mResumedActivity = r;
632 r.task.touchActiveTime();
633 mService.addRecentTaskLocked(r.task);
634 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700635 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700636 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700637 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700638 }
639
Dianne Hackborncee04b52013-07-03 17:01:28 -0700640 private void startLaunchTraces() {
641 if (mFullyDrawnStartTime != 0) {
642 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
643 }
644 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
645 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
646 }
647
648 private void stopFullyDrawnTraceIfNeeded() {
649 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
650 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
651 mFullyDrawnStartTime = 0;
652 }
653 }
654
Craig Mautnere79d42682013-04-01 19:01:53 -0700655 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700656 if (r.displayStartTime == 0) {
657 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
658 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700659 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700660 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700661 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700662 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700663 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700664 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700665 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700666 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800667
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700668 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700669 // Make sure that there is no activity waiting for this to launch.
670 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
671 r.displayStartTime = r.fullyDrawnStartTime = 0;
672 } else {
673 mStackSupervisor.removeTimeoutsForActivityLocked(r);
674 mStackSupervisor.scheduleIdleTimeoutLocked(r);
675 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700676 }
677
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800678 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800679 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800680 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
681 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
682 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
683 activities.get(activityNdx).setSleeping(false);
684 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800685 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800686 }
687
Craig Mautner0eea92c2013-05-16 13:35:39 -0700688 /**
689 * @return true if something must be done before going to sleep.
690 */
691 boolean checkReadyForSleepLocked() {
692 if (mResumedActivity != null) {
693 // Still have something resumed; can't sleep until it is paused.
694 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
695 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
696 startPausingLocked(false, true);
697 return true;
698 }
699 if (mPausingActivity != null) {
700 // Still waiting for something to pause; can't sleep yet.
701 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
702 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800703 }
704
Craig Mautner0eea92c2013-05-16 13:35:39 -0700705 return false;
706 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800707
Craig Mautner0eea92c2013-05-16 13:35:39 -0700708 void goToSleep() {
709 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800710
Craig Mautner0eea92c2013-05-16 13:35:39 -0700711 // Make sure any stopped but visible activities are now sleeping.
712 // This ensures that the activity's onStop() is called.
713 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
714 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
715 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
716 final ActivityRecord r = activities.get(activityNdx);
717 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
718 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800719 }
720 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800721 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722 }
Craig Mautner59c00972012-07-30 12:10:24 -0700723
Dianne Hackbornd2835932010-12-13 16:28:46 -0800724 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800725 if (who.noDisplay) {
726 return null;
727 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800728
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700729 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700730 if (mService.getMostRecentTask() != tr && tr.intent != null &&
731 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700732 // If this task is being excluded from recents, we don't want to take
733 // the expense of capturing a thumbnail, since we will never show it.
734 return null;
735 }
736
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800737 Resources res = mService.mContext.getResources();
738 int w = mThumbnailWidth;
739 int h = mThumbnailHeight;
740 if (w < 0) {
Winson Chungf9f23142014-04-21 14:45:27 -0700741 Configuration config = res.getConfiguration();
742 boolean useAlternateRecents = (config.smallestScreenWidthDp < 600);
743 if (useAlternateRecents) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800744 mThumbnailWidth = w =
745 res.getDimensionPixelSize(com.android.internal.R.dimen.recents_thumbnail_width);
746 mThumbnailHeight = h =
747 res.getDimensionPixelSize(com.android.internal.R.dimen.recents_thumbnail_height);
748 } else {
749 mThumbnailWidth = w =
750 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
751 mThumbnailHeight = h =
752 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
753 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800754 }
755
756 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800757 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700758 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800759 || mLastScreenshotBitmap.getWidth() != w
760 || mLastScreenshotBitmap.getHeight() != h) {
761 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700762 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700763 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800764 }
765 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700766 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800767 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800768 }
769 return null;
770 }
771
Craig Mautnercf910b02013-04-23 11:23:27 -0700772 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800773 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800774 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700775 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800776 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700777 ActivityRecord prev = mResumedActivity;
778 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700779 Slog.e(TAG, "Trying to pause when nothing is resumed",
780 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700781 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 return;
783 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800784
785 if (mActivityContainer.mParentActivity == null) {
786 // Top level stack, not a child. Look for child stacks.
787 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
788 }
789
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700790 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
791 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700792 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800793 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700794 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700795 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
796 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700797 prev.state = ActivityState.PAUSING;
798 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700799 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700800 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Craig Mautnerf6ead5d62014-04-24 10:19:20 -0700801 if (next == null || next.noDisplay || next.task != prev.task) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700802 prev.updateThumbnail(screenshotActivities(prev), null);
803 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700804 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700805
806 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800807
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 if (prev.app != null && prev.app.thread != null) {
809 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
810 try {
811 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700812 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700813 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700814 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800815 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
816 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 } catch (Exception e) {
818 // Ignore exception, if process died other code will cleanup.
819 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800820 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700822 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 }
824 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800825 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700827 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828 }
829
830 // If we are not going to sleep, we want to ensure the device is
831 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700832 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700833 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 }
835
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800836 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 // Have the window manager pause its key dispatching until the new
838 // activity has started. If we're pausing the activity just because
839 // the screen is being turned off and the UI is sleeping, don't interrupt
840 // key dispatch; the same activity will pick it up again on wakeup.
841 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800842 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700843 } else {
844 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
845 }
846
847 // Schedule a pause timeout in case the app doesn't respond.
848 // We don't give it much time because this directly impacts the
849 // responsiveness seen by the user.
850 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
851 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700852 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
854 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
855 } else {
856 // This activity failed to schedule the
857 // pause, so just treat it as being paused now.
858 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700859 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 }
861 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700862
Craig Mautnerd2328952013-03-05 12:46:26 -0800863 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800865 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866
Craig Mautnerd2328952013-03-05 12:46:26 -0800867 final ActivityRecord r = isInStackLocked(token);
868 if (r != null) {
869 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
870 if (mPausingActivity == r) {
871 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
872 + (timeout ? " (due to timeout)" : " (pause complete)"));
873 r.state = ActivityState.PAUSED;
874 completePauseLocked();
875 } else {
876 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
877 r.userId, System.identityHashCode(r), r.shortComponentName,
878 mPausingActivity != null
879 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 }
881 }
882 }
883
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700884 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
885 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700886 if (r.state != ActivityState.STOPPING) {
887 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
888 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
889 return;
890 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700891 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700892 if (icicle != null) {
893 // If icicle is null, this is happening due to a timeout, so we
894 // haven't really saved the state.
895 r.icicle = icicle;
896 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800897 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700898 r.updateThumbnail(thumbnail, description);
899 }
900 if (!r.stopped) {
901 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
902 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
903 r.stopped = true;
904 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700905 if (r.finishing) {
906 r.clearOptionsLocked();
907 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700908 if (r.configDestroy) {
909 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700910 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700911 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700912 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800913 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700914 }
915 }
916 }
917
Craig Mautnerc8143c62013-09-03 12:15:57 -0700918 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800919 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800921
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800922 if (prev != null) {
923 if (prev.finishing) {
924 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700925 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800926 } else if (prev.app != null) {
927 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
928 if (prev.waitingVisible) {
929 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700930 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800931 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
932 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800933 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800934 if (prev.configDestroy) {
935 // The previous is being paused because the configuration
936 // is changing, which means it is actually stopping...
937 // To juggle the fact that we are also starting a new
938 // instance right now, we need to first completely stop
939 // the current instance before starting the new one.
940 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
941 destroyActivityLocked(prev, true, false, "pause-config");
942 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700943 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700944 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
945 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800946 // If we already have a few activities waiting to stop,
947 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700948 // them out. Or if r is the last of activity of the last task the stack
949 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800950 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700951 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800952 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700953 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800954 }
955 }
956 } else {
957 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
958 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800959 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800960 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700961 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800962
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700963 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700964 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700965 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800966 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700967 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700968 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700969 if (top == null || (prev != null && top != prev)) {
970 // If there are no more activities available to run,
971 // do resume anyway to start something. Also if the top
972 // activity on the stack is not the just paused activity,
973 // we need to go ahead and resume it to ensure we complete
974 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700975 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700976 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700977 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800978
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800979 if (prev != null) {
980 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700981
Craig Mautner525f3d92013-05-07 14:01:50 -0700982 if (prev.app != null && prev.cpuTimeAtResume > 0
983 && mService.mBatteryStatsService.isOnBattery()) {
984 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700985 synchronized (mService.mProcessCpuThread) {
986 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700987 - prev.cpuTimeAtResume;
988 }
989 if (diff > 0) {
990 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
991 synchronized (bsi) {
992 BatteryStatsImpl.Uid.Proc ps =
993 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700994 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700995 if (ps != null) {
996 ps.addForegroundTimeLocked(diff);
997 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 }
999 }
1000 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001001 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001003 }
1004
1005 /**
1006 * Once we know that we have asked an application to put an activity in
1007 * the resumed state (either by launching it or explicitly telling it),
1008 * this function updates the rest of our state to match that fact.
1009 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001010 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001011 next.idle = false;
1012 next.results = null;
1013 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -07001014 if (next.nowVisible) {
1015 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1016 mStackSupervisor.dismissKeyguard();
1017 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018
1019 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001020 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001022 mStackSupervisor.reportResumedActivityLocked(next);
1023
1024 next.resumeKeyDispatchingLocked();
1025 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026
1027 // Mark the point when the activity is resuming
1028 // TODO: To be more accurate, the mark should be before the onCreate,
1029 // not after the onResume. But for subsequent starts, onResume is fine.
1030 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001031 synchronized (mService.mProcessCpuThread) {
1032 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001033 }
1034 } else {
1035 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1036 }
1037 }
1038
Craig Mautner580ea812013-04-25 12:58:38 -07001039 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001040 * Determine if home should be visible below the passed record.
1041 * @param record activity we are querying for.
1042 * @return true if home is visible below the passed activity, false otherwise.
1043 */
1044 boolean isActivityOverHome(ActivityRecord record) {
1045 // Start at record and go down, look for either home or a visible fullscreen activity.
1046 final TaskRecord recordTask = record.task;
1047 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
1048 TaskRecord task = mTaskHistory.get(taskNdx);
1049 final ArrayList<ActivityRecord> activities = task.mActivities;
1050 final int startNdx =
1051 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
1052 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
1053 final ActivityRecord r = activities.get(activityNdx);
1054 if (r.isHomeActivity()) {
1055 return true;
1056 }
Craig Mautner76be9d22013-11-04 16:01:22 -08001057 if (!r.finishing && r.fullscreen) {
1058 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001059 return false;
1060 }
1061 }
1062 if (task.mOnTopOfHome) {
1063 // Got to the bottom of a task on top of home without finding a visible fullscreen
1064 // activity. Home is visible.
1065 return true;
1066 }
1067 }
1068 // Got to the bottom of this stack and still don't know. If this is over the home stack
1069 // then record is over home. May not work if we ever get more than two layers.
1070 return mStackSupervisor.isFrontStack(this);
1071 }
1072
Craig Mautnere3a00d72014-04-16 08:31:19 -07001073 private void setVisibile(ActivityRecord r, boolean visible) {
1074 r.visible = visible;
1075 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001076 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001077 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001078 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001079 container.setVisible(visible);
1080 }
1081 }
1082
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001083 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001084 * Version of ensureActivitiesVisible that can easily be called anywhere.
1085 */
1086 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1087 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1088 }
1089
1090 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1091 boolean forceHomeShown) {
1092 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001093 return r != null &&
1094 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001095 }
1096
Jose Lima7ba71252014-04-30 12:59:27 -07001097 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1098 // If so, this stack is hidden, otherwise it is visible.
1099 private boolean isStackVisible() {
1100 if (!isAttached()) {
1101 return false;
1102 }
1103
1104 if (mStackSupervisor.isFrontStack(this)) {
1105 return true;
1106 }
1107
1108 // Start at the task above this one and go up, looking for a visible
1109 // fullscreen activity, or a translucent activity that requested the
1110 // wallpaper to be shown behind it.
1111 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1112 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1113 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
1114 final ArrayList<ActivityRecord> activities = tasks.get(taskNdx).mActivities;
1115 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1116 final ActivityRecord r = activities.get(activityNdx);
1117 if (!r.finishing && r.visible && r.fullscreen) {
1118 return false;
1119 }
1120 }
1121 }
1122 }
1123
1124 return true;
1125 }
1126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 /**
1128 * Make sure that all activities that need to be visible (that is, they
1129 * currently can be seen by the user) actually are.
1130 */
Craig Mautner580ea812013-04-25 12:58:38 -07001131 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1132 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001133 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 TAG, "ensureActivitiesVisible behind " + top
1135 + " configChanges=0x" + Integer.toHexString(configChanges));
1136
Craig Mautner5eda9b32013-07-02 11:58:16 -07001137 if (mTranslucentActivityWaiting != top) {
1138 mUndrawnActivitiesBelowTopTranslucent.clear();
1139 if (mTranslucentActivityWaiting != null) {
1140 // Call the callback with a timeout indication.
1141 notifyActivityDrawnLocked(null);
1142 mTranslucentActivityWaiting = null;
1143 }
1144 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1145 }
1146
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001147 // If the top activity is not fullscreen, then we need to
1148 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001149 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001150 boolean showHomeBehindStack = false;
Jose Lima7ba71252014-04-30 12:59:27 -07001151 boolean behindFullscreen = !isStackVisible();
1152
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001153 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001154 final TaskRecord task = mTaskHistory.get(taskNdx);
1155 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001156 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1157 final ActivityRecord r = activities.get(activityNdx);
1158 if (r.finishing) {
1159 continue;
1160 }
1161 if (aboveTop && r != top) {
1162 continue;
1163 }
1164 aboveTop = false;
1165 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001166 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001167 TAG, "Make visible? " + r + " finishing=" + r.finishing
1168 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001169
Craig Mautnerd44711d2013-02-23 11:24:36 -08001170 final boolean doThisProcess = onlyThisProcess == null
1171 || onlyThisProcess.equals(r.processName);
1172
1173 // First: if this is not the current activity being started, make
1174 // sure it matches the current configuration.
1175 if (r != starting && doThisProcess) {
1176 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001177 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001178
1179 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001180 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001181 // This activity needs to be visible, but isn't even
1182 // running... get it started, but don't resume it
1183 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001184 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001185 if (r != starting) {
1186 r.startFreezingScreenLocked(r.app, configChanges);
1187 }
1188 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001189 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001190 TAG, "Starting and making visible: " + r);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001191 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001192 }
1193 if (r != starting) {
George Mount2c92c972014-03-20 09:38:23 -07001194 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001195 }
1196 }
1197
1198 } else if (r.visible) {
1199 // If this activity is already visible, then there is nothing
1200 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001201 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001202 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001203
1204 } else if (onlyThisProcess == null) {
1205 // This activity is not currently visible, but is running.
1206 // Tell it to become visible.
1207 r.visible = true;
1208 if (r.state != ActivityState.RESUMED && r != starting) {
1209 // If this activity is paused, tell it
1210 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001211 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001212 TAG, "Making visible and scheduling visibility: " + r);
1213 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001214 if (mTranslucentActivityWaiting != null) {
1215 mUndrawnActivitiesBelowTopTranslucent.add(r);
1216 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001217 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001218 r.sleeping = false;
1219 r.app.pendingUiClean = true;
1220 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1221 r.stopFreezingScreenLocked(false);
1222 } catch (Exception e) {
1223 // Just skip on any failure; we'll make it
1224 // visible when it next restarts.
1225 Slog.w(TAG, "Exception thrown making visibile: "
1226 + r.intent.getComponent(), e);
1227 }
1228 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001229 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001230
Craig Mautnerd44711d2013-02-23 11:24:36 -08001231 // Aggregate current change flags.
1232 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001233
Craig Mautnerd44711d2013-02-23 11:24:36 -08001234 if (r.fullscreen) {
1235 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001236 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1237 behindFullscreen = true;
Craig Mautner723f7bb2014-03-27 20:14:13 +00001238 showHomeBehindStack = false;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001239 } else if (isActivityOverHome(r)) {
1240 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1241 showHomeBehindStack = true;
riddle_hsu446ef1d2014-01-09 20:24:34 +08001242 behindFullscreen = !isHomeStack() && r.frontOfTask && task.mOnTopOfHome;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001243 }
1244 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001245 if (DEBUG_VISBILITY) Slog.v(
1246 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1247 + " state=" + r.state
1248 + " behindFullscreen=" + behindFullscreen);
1249 // Now for any activities that aren't visible to the user, make
1250 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001251 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001252 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001254 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001255 switch (r.state) {
1256 case STOPPING:
1257 case STOPPED:
1258 if (r.app != null && r.app.thread != null) {
1259 if (DEBUG_VISBILITY) Slog.v(
1260 TAG, "Scheduling invisibility: " + r);
1261 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1262 }
1263 break;
1264
1265 case INITIALIZING:
1266 case RESUMED:
1267 case PAUSING:
1268 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001269 // This case created for transitioning activities from
1270 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001271 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1272 mStackSupervisor.mStoppingActivities.add(r);
1273 }
1274 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001275 break;
1276
1277 default:
1278 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001279 }
1280 } catch (Exception e) {
1281 // Just skip on any failure; we'll make it
1282 // visible when it next restarts.
1283 Slog.w(TAG, "Exception thrown making hidden: "
1284 + r.intent.getComponent(), e);
1285 }
1286 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001287 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001288 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001289 }
1290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291 }
Craig Mautner580ea812013-04-25 12:58:38 -07001292 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001293 }
Craig Mautner58547802013-03-05 08:23:53 -08001294
Craig Mautner5eda9b32013-07-02 11:58:16 -07001295 void convertToTranslucent(ActivityRecord r) {
1296 mTranslucentActivityWaiting = r;
1297 mUndrawnActivitiesBelowTopTranslucent.clear();
1298 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1299 }
1300
1301 /**
1302 * Called as activities below the top translucent activity are redrawn. When the last one is
1303 * redrawn notify the top activity by calling
1304 * {@link Activity#onTranslucentConversionComplete}.
1305 *
1306 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1307 * occurred and the activity will be notified immediately.
1308 */
1309 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001310 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001311 if ((r == null)
1312 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1313 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1314 // The last undrawn activity below the top has just been drawn. If there is an
1315 // opaque activity at the top, notify it that it can become translucent safely now.
1316 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1317 mTranslucentActivityWaiting = null;
1318 mUndrawnActivitiesBelowTopTranslucent.clear();
1319 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1320
Craig Mautner71dd1b62014-02-18 15:48:52 -08001321 if (waitingActivity != null) {
1322 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1323 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1324 try {
1325 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1326 waitingActivity.appToken, r != null);
1327 } catch (RemoteException e) {
1328 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001329 }
1330 }
1331 }
1332 }
1333
Craig Mautnera61bc652013-10-28 15:43:18 -07001334 /** If any activities below the top running one are in the INITIALIZING state and they have a
1335 * starting window displayed then remove that starting window. It is possible that the activity
1336 * in this state will never resumed in which case that starting window will be orphaned. */
1337 void cancelInitializingActivities() {
1338 final ActivityRecord topActivity = topRunningActivityLocked(null);
1339 boolean aboveTop = true;
1340 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1341 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1342 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1343 final ActivityRecord r = activities.get(activityNdx);
1344 if (aboveTop) {
1345 if (r == topActivity) {
1346 aboveTop = false;
1347 }
1348 continue;
1349 }
1350
1351 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1352 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1353 r.mStartingWindowShown = false;
1354 mWindowManager.removeAppStartingWindow(r.appToken);
1355 }
1356 }
1357 }
1358 }
1359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 /**
1361 * Ensure that the top activity in the stack is resumed.
1362 *
1363 * @param prev The previously resumed activity, for when in the process
1364 * of pausing; can be null to call from elsewhere.
1365 *
1366 * @return Returns true if something is being resumed, or false if
1367 * nothing happened.
1368 */
1369 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001370 return resumeTopActivityLocked(prev, null);
1371 }
1372
1373 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001374 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1375
Craig Mautnerdf88d732014-01-27 09:21:32 -08001376 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001377 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautner6985bad2014-04-21 15:22:06 -07001378 !mActivityContainer.isAttached()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001379 // Do not resume this stack if its parent is not resumed.
1380 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1381 return false;
1382 }
1383
Craig Mautnera61bc652013-10-28 15:43:18 -07001384 cancelInitializingActivities();
1385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386 // Find the first activity that is not finishing.
1387 ActivityRecord next = topRunningActivityLocked(null);
1388
1389 // Remember how we'll process this pause/resume situation, and ensure
1390 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001391 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1392 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001393
1394 if (next == null) {
1395 // There are no more activities! Let's just start up the
1396 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001397 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001398 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001399 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001400 // Only resume home if on home display
1401 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 }
1403
1404 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001405
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001406 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001407 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1408 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 // Make sure we have executed any pending transitions, since there
1410 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001411 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001412 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001413 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001414 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001415 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 return false;
1417 }
1418
Craig Mautner525f3d92013-05-07 14:01:50 -07001419 final TaskRecord nextTask = next.task;
1420 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001421 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001422 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001423 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001424 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001425 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001426 // This task is going away but it was supposed to return to the home task.
1427 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001428 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001429 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001430 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001431 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1432 "resumeTopActivityLocked: Launching home next");
1433 // Only resume home if on home display
1434 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001435 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001436 }
1437
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438 // If we are sleeping, and there is no resumed activity, and the top
1439 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001440 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001441 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001442 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001443 // Make sure we have executed any pending transitions, since there
1444 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001445 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001447 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001448 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001449 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001450 return false;
1451 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001452
1453 // Make sure that the user who owns this activity is started. If not,
1454 // we will just leave it as is because someone should be bringing
1455 // another user's activities to the top of the stack.
1456 if (mService.mStartedUsers.get(next.userId) == null) {
1457 Slog.w(TAG, "Skipping resume of top activity " + next
1458 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001459 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001460 return false;
1461 }
1462
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 // The activity may be waiting for stop, but that is no longer
1464 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001465 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001466 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001467 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001468 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469
Dianne Hackborn84375872012-06-01 19:03:50 -07001470 next.updateOptionsLocked(options);
1471
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001472 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1473
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001474 // If we are currently pausing an activity, then don't do anything
1475 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001476 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001477 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1478 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001479 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001480 return false;
1481 }
1482
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001483 // Okay we are now going to start a switch, to 'next'. We may first
1484 // have to pause the current activity, but this is an important point
1485 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001486 // XXX "App Redirected" dialog is getting too many false positives
1487 // at this point, so turn off for now.
1488 if (false) {
1489 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1490 long now = SystemClock.uptimeMillis();
1491 final boolean inTime = mLastStartedActivity.startTime != 0
1492 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1493 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1494 final int nextUid = next.info.applicationInfo.uid;
1495 if (inTime && lastUid != nextUid
1496 && lastUid != next.launchedFromUid
1497 && mService.checkPermission(
1498 android.Manifest.permission.STOP_APP_SWITCHES,
1499 -1, next.launchedFromUid)
1500 != PackageManager.PERMISSION_GRANTED) {
1501 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1502 } else {
1503 next.startTime = now;
1504 mLastStartedActivity = next;
1505 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001506 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001507 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001508 mLastStartedActivity = next;
1509 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001510 }
Craig Mautner58547802013-03-05 08:23:53 -08001511
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 // We need to start pausing the current activity so the top one
1513 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001514 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001515 if (mResumedActivity != null) {
1516 pausing = true;
1517 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001518 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001519 }
1520 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001521 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1522 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001523 // At this point we want to put the upcoming activity's process
1524 // at the top of the LRU list, since we know we will be needing it
1525 // very soon and it would be a waste to let it get killed if it
1526 // happens to be sitting towards the end.
1527 if (next.app != null && next.app.thread != null) {
1528 // No reason to do full oom adj update here; we'll let that
1529 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001530 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001531 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001532 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 return true;
1534 }
1535
Christopher Tated3f175c2012-06-14 14:16:54 -07001536 // If the most recent activity was noHistory but was only stopped rather
1537 // than stopped+finished because the device went to sleep, we need to make
1538 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001539 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001540 !mLastNoHistoryActivity.finishing) {
1541 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1542 " on new resume");
1543 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1544 null, "no-history", false);
1545 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001546 }
1547
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001548 if (prev != null && prev != next) {
1549 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1550 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001551 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001552 if (DEBUG_SWITCH) Slog.v(
1553 TAG, "Resuming top, waiting visible to hide: " + prev);
1554 } else {
1555 // The next activity is already visible, so hide the previous
1556 // activity's windows right now so we can show the new one ASAP.
1557 // We only do this if the previous is finishing, which should mean
1558 // it is on top of the one being resumed so hiding it quickly
1559 // is good. Otherwise, we want to do the normal route of allowing
1560 // the resumed activity to be shown so we can decide if the
1561 // previous should actually be hidden depending on whether the
1562 // new one is found to be full-screen or not.
1563 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001564 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1566 + prev + ", waitingVisible="
1567 + (prev != null ? prev.waitingVisible : null)
1568 + ", nowVisible=" + next.nowVisible);
1569 } else {
1570 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1571 + prev + ", waitingVisible="
1572 + (prev != null ? prev.waitingVisible : null)
1573 + ", nowVisible=" + next.nowVisible);
1574 }
1575 }
1576 }
1577
Dianne Hackborne7f97212011-02-24 14:40:20 -08001578 // Launching this app's activity, make sure the app is no longer
1579 // considered stopped.
1580 try {
1581 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001582 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001583 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001584 } catch (IllegalArgumentException e) {
1585 Slog.w(TAG, "Failed trying to unstop package "
1586 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001587 }
1588
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 // We are starting up the next activity, so tell the window manager
1590 // that the previous one will be hidden soon. This way it can know
1591 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001592 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 if (prev != null) {
1594 if (prev.finishing) {
1595 if (DEBUG_TRANSITION) Slog.v(TAG,
1596 "Prepare close transition: prev=" + prev);
1597 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001598 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001599 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001600 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001601 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001602 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1603 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001604 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001605 mWindowManager.setAppWillBeHidden(prev.appToken);
1606 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001608 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001609 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001610 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001611 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001613 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001614 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1615 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 }
1617 }
1618 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001619 mWindowManager.setAppWillBeHidden(prev.appToken);
1620 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 }
Craig Mautner967212c2013-04-13 21:10:58 -07001622 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001623 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001625 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001626 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001627 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001628 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001629 }
1630 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001631
1632 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001633 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001634 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1635 if (opts != null) {
1636 resumeAnimOptions = opts.toBundle();
1637 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001638 next.applyOptionsLocked();
1639 } else {
1640 next.clearOptionsLocked();
1641 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001642
Craig Mautnercf910b02013-04-23 11:23:27 -07001643 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 if (next.app != null && next.app.thread != null) {
1645 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1646
1647 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001648 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001650 // schedule launch ticks to collect information about slow apps.
1651 next.startLaunchTickingLocked();
1652
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001653 ActivityRecord lastResumedActivity =
1654 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 ActivityState lastState = next.state;
1656
1657 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001658
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001659 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 next.state = ActivityState.RESUMED;
1661 mResumedActivity = next;
1662 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001663 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001664 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001666 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667
1668 // Have the window manager re-evaluate the orientation of
1669 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001670 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001671 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001672 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001673 mService.mConfiguration,
1674 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1675 if (config != null) {
1676 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001678 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001680
Craig Mautner525f3d92013-05-07 14:01:50 -07001681 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001682 // The configuration update wasn't able to keep the existing
1683 // instance of the activity, and instead started a new one.
1684 // We should be all done, but let's just make sure our activity
1685 // is still at the top and schedule another run if something
1686 // weird happened.
1687 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001688 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001689 "Activity config changed during resume: " + next
1690 + ", new next: " + nextNext);
1691 if (nextNext != next) {
1692 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001693 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001695 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001696 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001697 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001698 return true;
1699 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001700 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001701 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 }
Craig Mautner58547802013-03-05 08:23:53 -08001703
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 try {
1705 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001706 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 if (a != null) {
1708 final int N = a.size();
1709 if (!next.finishing && N > 0) {
1710 if (DEBUG_RESULTS) Slog.v(
1711 TAG, "Delivering results to " + next
1712 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001713 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001714 }
1715 }
1716
1717 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001718 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 }
1720
1721 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001722 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001724
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001725 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001726 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001727 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001728 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001729 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001730 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001731 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001732
Craig Mautner0eea92c2013-05-16 13:35:39 -07001733 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734
Craig Mautnerac6f8432013-07-17 13:24:59 -07001735 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 } catch (Exception e) {
1737 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001738 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1739 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001741 if (lastStack != null) {
1742 lastStack.mResumedActivity = lastResumedActivity;
1743 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 Slog.i(TAG, "Restarting because process died: " + next);
1745 if (!next.hasBeenLaunched) {
1746 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001747 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1748 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001749 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001750 next.appToken, next.packageName, next.theme,
1751 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001752 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1753 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 }
George Mount2c92c972014-03-20 09:38:23 -07001755 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001756 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 return true;
1758 }
1759
1760 // From this point on, if something goes wrong there is no way
1761 // to recover the activity.
1762 try {
1763 next.visible = true;
1764 completeResumeLocked(next);
1765 } catch (Exception e) {
1766 // If any exception gets thrown, toss away this
1767 // activity and try the next one.
1768 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001769 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001770 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001771 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 return true;
1773 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 next.stopped = false;
1775
1776 } else {
1777 // Whoops, need to restart this activity!
1778 if (!next.hasBeenLaunched) {
1779 next.hasBeenLaunched = true;
1780 } else {
1781 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001782 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001783 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001784 mService.compatibilityInfoForPackageLocked(
1785 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001787 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001788 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 }
1790 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1791 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001792 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001793 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 }
1795
Craig Mautnercf910b02013-04-23 11:23:27 -07001796 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 return true;
1798 }
1799
Craig Mautnerac6f8432013-07-17 13:24:59 -07001800 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001801 // If this is being moved to the top by another activity or being launched from the home
1802 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001803 if (isOnHomeDisplay()) {
1804 ActivityStack lastStack = mStackSupervisor.getLastStack();
1805 final boolean fromHome = lastStack.isHomeStack();
1806 if (!isHomeStack() && (fromHome || topTask() != task)) {
1807 task.mOnTopOfHome = fromHome;
1808 }
1809 } else {
1810 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001811 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001812
Craig Mautnerac6f8432013-07-17 13:24:59 -07001813 mTaskHistory.remove(task);
1814 // Now put task at top.
1815 int stackNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001816 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001817 // Put non-current user tasks below current user tasks.
1818 while (--stackNdx >= 0) {
Kenny Guy2a764942014-04-02 13:29:20 +01001819 if (!isCurrentProfileLocked(mTaskHistory.get(stackNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001820 break;
1821 }
1822 }
1823 ++stackNdx;
1824 }
1825 mTaskHistory.add(stackNdx, task);
1826 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001827
Craig Mautner8849a5e2013-04-02 16:41:03 -07001828 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001829 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001830 TaskRecord rTask = r.task;
1831 final int taskId = rTask.taskId;
1832 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001833 // Last activity in task had been removed or ActivityManagerService is reusing task.
1834 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001835 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001836 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001837 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001838 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001839 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001840 if (!newTask) {
1841 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001842 boolean startIt = true;
1843 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1844 task = mTaskHistory.get(taskNdx);
1845 if (task == r.task) {
1846 // Here it is! Now, if this is not yet visible to the
1847 // user, then just add it without starting; it will
1848 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001849 if (!startIt) {
1850 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1851 + task, new RuntimeException("here").fillInStackTrace());
1852 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001853 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001854 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1855 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001856 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001857 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001858 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001859 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001861 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 return;
1863 }
1864 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001865 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001866 startIt = false;
1867 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 }
1869 }
1870
1871 // Place a new activity at top of stack, so it is next to interact
1872 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001873
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 // If we are not placing the new activity frontmost, we do not want
1875 // to deliver the onUserLeaving callback to the actual frontmost
1876 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001877 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001878 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001879 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1880 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 }
Craig Mautner70a86932013-02-28 22:37:44 -08001882
1883 task = r.task;
1884
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001886 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001887 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001888 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001889 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001890
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001891 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001892 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001893 // We want to show the starting preview window if we are
1894 // switching to a new task, or the next activity's process is
1895 // not currently running.
1896 boolean showStartingIcon = newTask;
1897 ProcessRecord proc = r.app;
1898 if (proc == null) {
1899 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1900 }
1901 if (proc == null || proc.thread == null) {
1902 showStartingIcon = true;
1903 }
1904 if (DEBUG_TRANSITION) Slog.v(TAG,
1905 "Prepare open transition: starting " + r);
1906 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001907 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001908 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001910 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001911 ? AppTransition.TRANSIT_TASK_OPEN
1912 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001913 mNoAnimActivities.remove(r);
1914 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001915 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001916 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001917 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001918 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1919 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001920 boolean doShow = true;
1921 if (newTask) {
1922 // Even though this activity is starting fresh, we still need
1923 // to reset it to make sure we apply affinities to move any
1924 // existing activities from other tasks in to it.
1925 // If the caller has requested that the target task be
1926 // reset, then do so.
1927 if ((r.intent.getFlags()
Craig Mautnerd00f4742014-03-12 14:17:26 -07001928 & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001929 resetTaskIfNeededLocked(r, r);
1930 doShow = topRunningNonDelayedActivityLocked(null) == r;
1931 }
1932 }
1933 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1934 // Figure out if we are transitioning from another activity that is
1935 // "has the same starting icon" as the next one. This allows the
1936 // window manager to keep the previous window it had previously
1937 // created, if it still had one.
1938 ActivityRecord prev = mResumedActivity;
1939 if (prev != null) {
1940 // We don't want to reuse the previous starting preview if:
1941 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001942 if (prev.task != r.task) {
1943 prev = null;
1944 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001945 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001946 else if (prev.nowVisible) {
1947 prev = null;
1948 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001950 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001951 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001952 mService.compatibilityInfoForPackageLocked(
1953 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001954 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001955 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07001956 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 }
1958 } else {
1959 // If this is the first activity, don't do any fancy animations,
1960 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001961 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001962 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001963 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1964 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001965 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 }
1967 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001968 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001969 }
1970
1971 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001972 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001973 }
1974 }
1975
Dianne Hackbornbe707852011-11-11 14:32:10 -08001976 final void validateAppTokensLocked() {
1977 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001978 mValidateAppTokens.ensureCapacity(numActivities());
1979 final int numTasks = mTaskHistory.size();
1980 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1981 TaskRecord task = mTaskHistory.get(taskNdx);
1982 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001983 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001984 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001985 }
Craig Mautner000f0022013-02-26 15:04:29 -08001986 TaskGroup group = new TaskGroup();
1987 group.taskId = task.taskId;
1988 mValidateAppTokens.add(group);
1989 final int numActivities = activities.size();
1990 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1991 final ActivityRecord r = activities.get(activityNdx);
1992 group.tokens.add(r.appToken);
1993 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001994 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001995 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001996 }
1997
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 /**
1999 * Perform a reset of the given task, if needed as part of launching it.
2000 * Returns the new HistoryRecord at the top of the task.
2001 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002002 /**
2003 * Helper method for #resetTaskIfNeededLocked.
2004 * We are inside of the task being reset... we'll either finish this activity, push it out
2005 * for another task, or leave it as-is.
2006 * @param task The task containing the Activity (taskTop) that might be reset.
2007 * @param forceReset
2008 * @return An ActivityOptions that needs to be processed.
2009 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002010 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002011 ActivityOptions topOptions = null;
2012
2013 int replyChainEnd = -1;
2014 boolean canMoveOptions = true;
2015
2016 // We only do this for activities that are not the root of the task (since if we finish
2017 // the root, we may no longer have the task!).
2018 final ArrayList<ActivityRecord> activities = task.mActivities;
2019 final int numActivities = activities.size();
2020 for (int i = numActivities - 1; i > 0; --i ) {
2021 ActivityRecord target = activities.get(i);
2022
2023 final int flags = target.info.flags;
2024 final boolean finishOnTaskLaunch =
2025 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2026 final boolean allowTaskReparenting =
2027 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2028 final boolean clearWhenTaskReset =
2029 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2030
2031 if (!finishOnTaskLaunch
2032 && !clearWhenTaskReset
2033 && target.resultTo != null) {
2034 // If this activity is sending a reply to a previous
2035 // activity, we can't do anything with it now until
2036 // we reach the start of the reply chain.
2037 // XXX note that we are assuming the result is always
2038 // to the previous activity, which is almost always
2039 // the case but we really shouldn't count on.
2040 if (replyChainEnd < 0) {
2041 replyChainEnd = i;
2042 }
2043 } else if (!finishOnTaskLaunch
2044 && !clearWhenTaskReset
2045 && allowTaskReparenting
2046 && target.taskAffinity != null
2047 && !target.taskAffinity.equals(task.affinity)) {
2048 // If this activity has an affinity for another
2049 // task, then we need to move it out of here. We will
2050 // move it as far out of the way as possible, to the
2051 // bottom of the activity stack. This also keeps it
2052 // correctly ordered with any activities we previously
2053 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002054 final ThumbnailHolder newThumbHolder;
2055 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002056 final ActivityRecord bottom =
2057 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002058 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002059 if (bottom != null && target.taskAffinity != null
2060 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002061 // If the activity currently at the bottom has the
2062 // same task affinity as the one we are moving,
2063 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002064 targetTask = bottom.task;
2065 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002066 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002067 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002068 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002069 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002070 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002071 newThumbHolder = targetTask;
2072 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002073 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2074 + " out to new task " + target.task);
2075 }
2076
Craig Mautnerd00f4742014-03-12 14:17:26 -07002077 target.thumbHolder = newThumbHolder;
Craig Mautner329f4122013-11-07 09:10:42 -08002078
Craig Mautnere3a74d52013-02-22 14:14:58 -08002079 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002080 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002081
Craig Mautner525f3d92013-05-07 14:01:50 -07002082 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002083 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2084 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002085 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002086 if (p.finishing) {
2087 continue;
2088 }
2089
Craig Mautner525f3d92013-05-07 14:01:50 -07002090 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002091 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002092 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002093 topOptions = p.takeOptionsLocked();
2094 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002095 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002096 }
2097 }
2098 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002099 + task + " adding to task=" + targetTask
2100 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002101 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2102 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002103 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002105
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002106 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002107 }
2108
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002109 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 if (VALIDATE_TOKENS) {
2111 validateAppTokensLocked();
2112 }
2113
2114 replyChainEnd = -1;
2115 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2116 // If the activity should just be removed -- either
2117 // because it asks for it, or the task should be
2118 // cleared -- then finish it and anything that is
2119 // part of its reply chain.
2120 int end;
2121 if (clearWhenTaskReset) {
2122 // In this case, we want to finish this activity
2123 // and everything above it, so be sneaky and pretend
2124 // like these are all in the reply chain.
2125 end = numActivities - 1;
2126 } else if (replyChainEnd < 0) {
2127 end = i;
2128 } else {
2129 end = replyChainEnd;
2130 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002131 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002132 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002133 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002134 if (p.finishing) {
2135 continue;
2136 }
2137 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002138 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002139 topOptions = p.takeOptionsLocked();
2140 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002141 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002142 }
2143 }
Craig Mautner58547802013-03-05 08:23:53 -08002144 if (DEBUG_TASKS) Slog.w(TAG,
2145 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002146 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002147 end--;
2148 srcPos--;
2149 }
2150 }
2151 replyChainEnd = -1;
2152 } else {
2153 // If we were in the middle of a chain, well the
2154 // activity that started it all doesn't want anything
2155 // special, so leave it all as-is.
2156 replyChainEnd = -1;
2157 }
2158 }
2159
2160 return topOptions;
2161 }
2162
2163 /**
2164 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2165 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2166 * @param affinityTask The task we are looking for an affinity to.
2167 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2168 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2169 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2170 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002171 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002172 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002173 int replyChainEnd = -1;
2174 final int taskId = task.taskId;
2175 final String taskAffinity = task.affinity;
2176
2177 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2178 final int numActivities = activities.size();
2179 // Do not operate on the root Activity.
2180 for (int i = numActivities - 1; i > 0; --i) {
2181 ActivityRecord target = activities.get(i);
2182
2183 final int flags = target.info.flags;
2184 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2185 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2186
2187 if (target.resultTo != null) {
2188 // If this activity is sending a reply to a previous
2189 // activity, we can't do anything with it now until
2190 // we reach the start of the reply chain.
2191 // XXX note that we are assuming the result is always
2192 // to the previous activity, which is almost always
2193 // the case but we really shouldn't count on.
2194 if (replyChainEnd < 0) {
2195 replyChainEnd = i;
2196 }
2197 } else if (topTaskIsHigher
2198 && allowTaskReparenting
2199 && taskAffinity != null
2200 && taskAffinity.equals(target.taskAffinity)) {
2201 // This activity has an affinity for our task. Either remove it if we are
2202 // clearing or move it over to our task. Note that
2203 // we currently punt on the case where we are resetting a
2204 // task that is not at the top but who has activities above
2205 // with an affinity to it... this is really not a normal
2206 // case, and we will need to later pull that task to the front
2207 // and usually at that point we will do the reset and pick
2208 // up those remaining activities. (This only happens if
2209 // someone starts an activity in a new task from an activity
2210 // in a task that is not currently on top.)
2211 if (forceReset || finishOnTaskLaunch) {
2212 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2213 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2214 for (int srcPos = start; srcPos >= i; --srcPos) {
2215 final ActivityRecord p = activities.get(srcPos);
2216 if (p.finishing) {
2217 continue;
2218 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002219 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002220 }
2221 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002222 if (taskInsertionPoint < 0) {
2223 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002224
Craig Mautner77878772013-03-04 19:46:24 -08002225 }
Craig Mautner77878772013-03-04 19:46:24 -08002226
2227 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2228 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2229 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2230 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002231 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002232 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002233 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002234
Craig Mautnere3a74d52013-02-22 14:14:58 -08002235 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2236 + " to stack at " + task,
2237 new RuntimeException("here").fillInStackTrace());
2238 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2239 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002240 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002241 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002242 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002243 if (VALIDATE_TOKENS) {
2244 validateAppTokensLocked();
2245 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002246
2247 // Now we've moved it in to place... but what if this is
2248 // a singleTop activity and we have put it on top of another
2249 // instance of the same activity? Then we drop the instance
2250 // below so it remains singleTop.
2251 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2252 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002253 int targetNdx = taskActivities.indexOf(target);
2254 if (targetNdx > 0) {
2255 ActivityRecord p = taskActivities.get(targetNdx - 1);
2256 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002257 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2258 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002259 }
2260 }
2261 }
2262 }
2263
2264 replyChainEnd = -1;
2265 }
2266 }
Craig Mautner77878772013-03-04 19:46:24 -08002267 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002268 }
2269
Craig Mautner8849a5e2013-04-02 16:41:03 -07002270 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002271 ActivityRecord newActivity) {
2272 boolean forceReset =
2273 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2274 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2275 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2276 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2277 forceReset = true;
2278 }
2279 }
2280
2281 final TaskRecord task = taskTop.task;
2282
2283 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2284 * for remaining tasks. Used for later tasks to reparent to task. */
2285 boolean taskFound = false;
2286
2287 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2288 ActivityOptions topOptions = null;
2289
Craig Mautner77878772013-03-04 19:46:24 -08002290 // Preserve the location for reparenting in the new task.
2291 int reparentInsertionPoint = -1;
2292
Craig Mautnere3a74d52013-02-22 14:14:58 -08002293 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2294 final TaskRecord targetTask = mTaskHistory.get(i);
2295
2296 if (targetTask == task) {
2297 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2298 taskFound = true;
2299 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002300 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2301 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002302 }
2303 }
2304
Craig Mautner70a86932013-02-28 22:37:44 -08002305 int taskNdx = mTaskHistory.indexOf(task);
2306 do {
2307 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2308 } while (taskTop == null && taskNdx >= 0);
2309
Craig Mautnere3a74d52013-02-22 14:14:58 -08002310 if (topOptions != null) {
2311 // If we got some ActivityOptions from an activity on top that
2312 // was removed from the task, propagate them to the new real top.
2313 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002314 taskTop.updateOptionsLocked(topOptions);
2315 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 topOptions.abort();
2317 }
2318 }
2319
2320 return taskTop;
2321 }
2322
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2324 String resultWho, int requestCode, int resultCode, Intent data) {
2325
2326 if (callingUid > 0) {
2327 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002328 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 }
2330
2331 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2332 + " : who=" + resultWho + " req=" + requestCode
2333 + " res=" + resultCode + " data=" + data);
2334 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2335 try {
2336 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2337 list.add(new ResultInfo(resultWho, requestCode,
2338 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002339 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 return;
2341 } catch (Exception e) {
2342 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2343 }
2344 }
2345
2346 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2347 }
2348
Craig Mautner04f0b702013-10-22 12:31:01 -07002349 private void adjustFocusedActivityLocked(ActivityRecord r) {
2350 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2351 ActivityRecord next = topRunningActivityLocked(null);
2352 if (next != r) {
2353 final TaskRecord task = r.task;
2354 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2355 mStackSupervisor.moveHomeToTop();
2356 }
2357 }
Winson Chung648c83b2014-04-28 15:11:56 -07002358 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2359 if (top != null) {
2360 mService.setFocusedActivityLocked(top);
2361 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002362 }
2363 }
2364
Craig Mautnerf3333272013-04-22 10:55:53 -07002365 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2367 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2368 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2369 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002370 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002371 if (DEBUG_STATES) {
2372 Slog.d(TAG, "no-history finish of " + r);
2373 }
2374 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002375 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002376 } else {
2377 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2378 + " on stop because we're just sleeping");
2379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002380 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002381 }
2382
2383 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002384 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002385 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 try {
2387 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002388 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2389 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 r.state = ActivityState.STOPPING;
2391 if (DEBUG_VISBILITY) Slog.v(
2392 TAG, "Stopping visible=" + r.visible + " for " + r);
2393 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002394 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002395 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002396 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002397 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002398 r.setSleeping(true);
2399 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002400 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002401 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002402 } catch (Exception e) {
2403 // Maybe just ignore exceptions here... if the process
2404 // has crashed, our death notification will clean things
2405 // up.
2406 Slog.w(TAG, "Exception thrown during pause", e);
2407 // Just in case, assume it to be stopped.
2408 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002409 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002410 r.state = ActivityState.STOPPED;
2411 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002412 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002413 }
2414 }
2415 }
2416 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002417
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002418 /**
2419 * @return Returns true if the activity is being finished, false if for
2420 * some reason it is being left as-is.
2421 */
2422 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002423 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002424 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002425 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002426 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002427 + ", result=" + resultCode + ", data=" + resultData
2428 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002429 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002430 return false;
2431 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432
Craig Mautnerd44711d2013-02-23 11:24:36 -08002433 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 return true;
2435 }
2436
Craig Mautnerd2328952013-03-05 12:46:26 -08002437 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002438 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2439 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2440 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2441 ActivityRecord r = activities.get(activityNdx);
2442 if (r.resultTo == self && r.requestCode == requestCode) {
2443 if ((r.resultWho == null && resultWho == null) ||
2444 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2445 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2446 false);
2447 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002448 }
2449 }
2450 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002451 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002452 }
2453
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002454 final void finishTopRunningActivityLocked(ProcessRecord app) {
2455 ActivityRecord r = topRunningActivityLocked(null);
2456 if (r != null && r.app == app) {
2457 // If the top running activity is from this crashing
2458 // process, then terminate it to avoid getting in a loop.
2459 Slog.w(TAG, " Force finishing activity "
2460 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002461 int taskNdx = mTaskHistory.indexOf(r.task);
2462 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002463 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002464 // Also terminate any activities below it that aren't yet
2465 // stopped, to avoid a situation where one will get
2466 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002467 --activityNdx;
2468 if (activityNdx < 0) {
2469 do {
2470 --taskNdx;
2471 if (taskNdx < 0) {
2472 break;
2473 }
2474 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2475 } while (activityNdx < 0);
2476 }
2477 if (activityNdx >= 0) {
2478 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002479 if (r.state == ActivityState.RESUMED
2480 || r.state == ActivityState.PAUSING
2481 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002482 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002483 Slog.w(TAG, " Force finishing activity "
2484 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002485 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002486 }
2487 }
2488 }
2489 }
2490 }
2491
Craig Mautnerd2328952013-03-05 12:46:26 -08002492 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002493 ArrayList<ActivityRecord> activities = r.task.mActivities;
2494 for (int index = activities.indexOf(r); index >= 0; --index) {
2495 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002496 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002497 break;
2498 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002499 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002500 }
2501 return true;
2502 }
2503
Dianne Hackborn5c607432012-02-28 14:44:19 -08002504 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2505 // send the result
2506 ActivityRecord resultTo = r.resultTo;
2507 if (resultTo != null) {
2508 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2509 + " who=" + r.resultWho + " req=" + r.requestCode
2510 + " res=" + resultCode + " data=" + resultData);
2511 if (r.info.applicationInfo.uid > 0) {
2512 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2513 resultTo.packageName, resultData,
2514 resultTo.getUriPermissionsLocked());
2515 }
2516 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2517 resultData);
2518 r.resultTo = null;
2519 }
2520 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2521
2522 // Make sure this HistoryRecord is not holding on to other resources,
2523 // because clients have remote IPC references to this object so we
2524 // can't assume that will go away and want to avoid circular IPC refs.
2525 r.results = null;
2526 r.pendingResults = null;
2527 r.newIntents = null;
2528 r.icicle = null;
2529 }
2530
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 /**
2532 * @return Returns true if this activity has been removed from the history
2533 * list, or false if it is still in the list and will be removed later.
2534 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002535 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2536 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002537 if (r.finishing) {
2538 Slog.w(TAG, "Duplicate finish request for " + r);
2539 return false;
2540 }
2541
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002542 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002543 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002545 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002546 task.taskId, r.shortComponentName, reason);
2547 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002548 final int index = activities.indexOf(r);
2549 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002550 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002551 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002552 // If the caller asked that this activity (and all above it)
2553 // be cleared when the task is reset, don't lose that information,
2554 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002555 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002556 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557 }
2558 }
2559
2560 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002561
2562 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002563
Dianne Hackborn5c607432012-02-28 14:44:19 -08002564 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002565
Craig Mautnerde4ef022013-04-07 19:01:33 -07002566 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002567 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002568 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002570 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002571 ? AppTransition.TRANSIT_TASK_CLOSE
2572 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002573
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002574 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002575 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002576
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002577 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2579 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2580 startPausingLocked(false, false);
2581 }
2582
Craig Mautneraea74a52014-03-08 14:23:10 -08002583 if (endTask) {
2584 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2585 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 } else if (r.state != ActivityState.PAUSING) {
2587 // If the activity is PAUSING, we will complete the finish once
2588 // it is done pausing; else we can just directly finish it here.
2589 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002590 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002591 } else {
2592 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2593 }
2594
2595 return false;
2596 }
2597
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 static final int FINISH_IMMEDIATELY = 0;
2599 static final int FINISH_AFTER_PAUSE = 1;
2600 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002601
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002603 // First things first: if this activity is currently visible,
2604 // and the resumed activity is not yet visible, then hold off on
2605 // finishing until the resumed one becomes visible.
2606 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002607 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2608 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002609 if (mStackSupervisor.mStoppingActivities.size() > 3
2610 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611 // If we already have a few activities waiting to stop,
2612 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002613 // them out. Or if r is the last of activity of the last task the stack
2614 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002615 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002616 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002617 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002618 }
2619 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002620 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2621 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002622 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002623 if (oomAdj) {
2624 mService.updateOomAdjLocked();
2625 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002626 return r;
2627 }
2628
2629 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002630 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002631 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002632 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 if (mResumedActivity == r) {
2634 mResumedActivity = null;
2635 }
2636 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002637 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002638 r.state = ActivityState.FINISHING;
2639
2640 if (mode == FINISH_IMMEDIATELY
2641 || prevState == ActivityState.STOPPED
2642 || prevState == ActivityState.INITIALIZING) {
2643 // If this activity is already stopped, we can just finish
2644 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002645 boolean activityRemoved = destroyActivityLocked(r, true,
2646 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002647 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002648 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002649 }
2650 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002651 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002652
2653 // Need to go through the full pause cycle to get this
2654 // activity into the stopped state and then finish it.
2655 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002656 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002657 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002658 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002659 return r;
2660 }
2661
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002662 void finishAllActivitiesLocked() {
2663 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2664 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2665 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2666 final ActivityRecord r = activities.get(activityNdx);
2667 if (r.finishing) {
2668 continue;
2669 }
2670 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
2671 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2672 }
2673 }
2674 }
2675
Craig Mautnerd2328952013-03-05 12:46:26 -08002676 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002677 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002678 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002679 final TaskRecord task = srec.task;
2680 final ArrayList<ActivityRecord> activities = task.mActivities;
2681 final int start = activities.indexOf(srec);
2682 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002683 return false;
2684 }
2685 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002686 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002687 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002688 final ComponentName dest = destIntent.getComponent();
2689 if (start > 0 && dest != null) {
2690 for (int i = finishTo; i >= 0; i--) {
2691 ActivityRecord r = activities.get(i);
2692 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002693 r.info.name.equals(dest.getClassName())) {
2694 finishTo = i;
2695 parent = r;
2696 foundParentInTask = true;
2697 break;
2698 }
2699 }
2700 }
2701
2702 IActivityController controller = mService.mController;
2703 if (controller != null) {
2704 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2705 if (next != null) {
2706 // ask watcher if this is allowed
2707 boolean resumeOK = true;
2708 try {
2709 resumeOK = controller.activityResuming(next.packageName);
2710 } catch (RemoteException e) {
2711 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002712 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002713 }
2714
2715 if (!resumeOK) {
2716 return false;
2717 }
2718 }
2719 }
2720 final long origId = Binder.clearCallingIdentity();
2721 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002722 ActivityRecord r = activities.get(i);
2723 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002724 // Only return the supplied result for the first activity finished
2725 resultCode = Activity.RESULT_CANCELED;
2726 resultData = null;
2727 }
2728
2729 if (parent != null && foundParentInTask) {
2730 final int parentLaunchMode = parent.info.launchMode;
2731 final int destIntentFlags = destIntent.getFlags();
2732 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2733 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2734 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2735 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2736 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2737 } else {
2738 try {
2739 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2740 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002741 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002742 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002743 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002744 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002745 foundParentInTask = res == ActivityManager.START_SUCCESS;
2746 } catch (RemoteException e) {
2747 foundParentInTask = false;
2748 }
2749 requestFinishActivityLocked(parent.appToken, resultCode,
2750 resultData, "navigate-up", true);
2751 }
2752 }
2753 Binder.restoreCallingIdentity(origId);
2754 return foundParentInTask;
2755 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002756 /**
2757 * Perform the common clean-up of an activity record. This is called both
2758 * as part of destroyActivityLocked() (when destroying the client-side
2759 * representation) and cleaning things up as a result of its hosting
2760 * processing going away, in which case there is no remaining client-side
2761 * state to destroy so only the cleanup here is needed.
2762 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002763 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2764 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002765 if (mResumedActivity == r) {
2766 mResumedActivity = null;
2767 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002768 if (mPausingActivity == r) {
2769 mPausingActivity = null;
2770 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002771 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772
2773 r.configDestroy = false;
2774 r.frozenBeforeDestroy = false;
2775
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002776 if (setState) {
2777 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2778 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002779 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002780 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002781 }
2782
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002783 // Make sure this record is no longer in the pending finishes list.
2784 // This could happen, for example, if we are trimming activities
2785 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002786 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002787 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002788
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 // Remove any pending results.
2790 if (r.finishing && r.pendingResults != null) {
2791 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2792 PendingIntentRecord rec = apr.get();
2793 if (rec != null) {
2794 mService.cancelIntentSenderLocked(rec, false);
2795 }
2796 }
2797 r.pendingResults = null;
2798 }
2799
2800 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002801 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002802 }
2803
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002804 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002805 removeTimeoutsForActivityLocked(r);
2806 }
2807
2808 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002809 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002811 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002812 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002813 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 }
2815
Craig Mautner04a0ea62014-01-13 12:51:26 -08002816 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002817 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002818 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2819 r.makeFinishing();
2820 if (DEBUG_ADD_REMOVE) {
2821 RuntimeException here = new RuntimeException("here");
2822 here.fillInStackTrace();
2823 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002825 r.takeFromHistory();
2826 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002827 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002828 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002829 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002830 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002831 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002832 if (VALIDATE_TOKENS) {
2833 validateAppTokensLocked();
2834 }
Craig Mautner312ba862014-02-10 17:55:01 -08002835 final TaskRecord task = r.task;
2836 if (task != null && task.removeActivity(r)) {
2837 if (DEBUG_STACK) Slog.i(TAG,
2838 "removeActivityFromHistoryLocked: last activity removed from " + this);
2839 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
2840 mStackSupervisor.moveHomeToTop();
2841 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002842 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08002843 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002844 cleanUpActivityServicesLocked(r);
2845 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002847
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 /**
2849 * Perform clean-up of service connections in an activity record.
2850 */
2851 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2852 // Throw away any services that have been bound by this activity.
2853 if (r.connections != null) {
2854 Iterator<ConnectionRecord> it = r.connections.iterator();
2855 while (it.hasNext()) {
2856 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002857 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858 }
2859 r.connections = null;
2860 }
2861 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002862
2863 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2864 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2865 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2866 mHandler.sendMessage(msg);
2867 }
2868
Dianne Hackborn28695e02011-11-02 21:59:51 -07002869 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002870 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002871 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002872 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2873 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2874 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2875 final ActivityRecord r = activities.get(activityNdx);
2876 if (r.finishing) {
2877 continue;
2878 }
2879 if (r.fullscreen) {
2880 lastIsOpaque = true;
2881 }
2882 if (owner != null && r.app != owner) {
2883 continue;
2884 }
2885 if (!lastIsOpaque) {
2886 continue;
2887 }
2888 // We can destroy this one if we have its icicle saved and
2889 // it is not in the process of pausing/stopping/finishing.
2890 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2891 && r.haveState && !r.visible && r.stopped
2892 && r.state != ActivityState.DESTROYING
2893 && r.state != ActivityState.DESTROYED) {
2894 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2895 + " resumed=" + mResumedActivity
2896 + " pausing=" + mPausingActivity);
2897 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2898 activityRemoved = true;
2899 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002900 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002901 }
2902 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002903 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002904 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002905 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002906 }
2907
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002908 /**
2909 * Destroy the current CLIENT SIDE instance of an activity. This may be
2910 * called both when actually finishing an activity, or when performing
2911 * a configuration switch where we destroy the current client-side object
2912 * but then create a new client-side object for this same HistoryRecord.
2913 */
2914 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002915 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002916 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002917 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002918 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2919 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002920 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002921 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002922
2923 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002924
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002925 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002926
2927 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002928
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 if (hadApp) {
2930 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002931 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002932 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2933 mService.mHeavyWeightProcess = null;
2934 mService.mHandler.sendEmptyMessage(
2935 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2936 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002937 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002938 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002939 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002940 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941 }
2942 }
2943
2944 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002945
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002946 try {
2947 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002948 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 r.configChangeFlags);
2950 } catch (Exception e) {
2951 // We can just ignore exceptions here... if the process
2952 // has crashed, our death notification will clean things
2953 // up.
2954 //Slog.w(TAG, "Exception thrown during finish", e);
2955 if (r.finishing) {
2956 removeActivityFromHistoryLocked(r);
2957 removedFromHistory = true;
2958 skipDestroy = true;
2959 }
2960 }
2961
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002963
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002964 // If the activity is finishing, we need to wait on removing it
2965 // from the list to give it a chance to do its cleanup. During
2966 // that time it may make calls back with its token so we need to
2967 // be able to find it on the list and so we don't want to remove
2968 // it from the list yet. Otherwise, we can just immediately put
2969 // it in the destroyed state since we are not removing it from the
2970 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002972 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2973 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002975 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2977 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002978 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002980 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002981 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 }
2983 } else {
2984 // remove this record from the history.
2985 if (r.finishing) {
2986 removeActivityFromHistoryLocked(r);
2987 removedFromHistory = true;
2988 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002989 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002990 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002991 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002992 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 }
2994 }
2995
2996 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002997
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 if (!mLRUActivities.remove(r) && hadApp) {
2999 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3000 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003001
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 return removedFromHistory;
3003 }
3004
Craig Mautnerd2328952013-03-05 12:46:26 -08003005 final void activityDestroyedLocked(IBinder token) {
3006 final long origId = Binder.clearCallingIdentity();
3007 try {
3008 ActivityRecord r = ActivityRecord.forToken(token);
3009 if (r != null) {
3010 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003012
3013 if (isInStackLocked(token) != null) {
3014 if (r.state == ActivityState.DESTROYING) {
3015 cleanUpActivityLocked(r, true, false);
3016 removeActivityFromHistoryLocked(r);
3017 }
3018 }
Craig Mautner05d29032013-05-03 13:40:13 -07003019 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003020 } finally {
3021 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 }
3023 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003024
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003025 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3026 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003028 if (DEBUG_CLEANUP) Slog.v(
3029 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003030 + " with " + i + " entries");
3031 while (i > 0) {
3032 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003033 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003034 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003036 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003038 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003039 }
3040 }
3041 }
3042
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003043 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3044 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003045 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3046 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003047 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3048 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003049 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003050 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003051 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3052 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003053
3054 boolean hasVisibleActivities = false;
3055
3056 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003057 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003058 if (DEBUG_CLEANUP) Slog.v(
3059 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003060 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3061 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3062 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3063 final ActivityRecord r = activities.get(activityNdx);
3064 --i;
3065 if (DEBUG_CLEANUP) Slog.v(
3066 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3067 if (r.app == app) {
3068 boolean remove;
3069 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3070 // Don't currently have state for the activity, or
3071 // it is finishing -- always remove it.
3072 remove = true;
3073 } else if (r.launchCount > 2 &&
3074 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3075 // We have launched this activity too many times since it was
3076 // able to run, so give up and remove it.
3077 remove = true;
3078 } else {
3079 // The process may be gone, but the activity lives on!
3080 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003081 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003082 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003083 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003084 RuntimeException here = new RuntimeException("here");
3085 here.fillInStackTrace();
3086 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3087 + ": haveState=" + r.haveState
3088 + " stateNotNeeded=" + r.stateNotNeeded
3089 + " finishing=" + r.finishing
3090 + " state=" + r.state, here);
3091 }
3092 if (!r.finishing) {
3093 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3094 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3095 r.userId, System.identityHashCode(r),
3096 r.task.taskId, r.shortComponentName,
3097 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003098 if (r.state == ActivityState.RESUMED) {
3099 mService.updateUsageStats(r, false);
3100 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003101 }
3102 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003103
Craig Mautner0247fc82013-02-28 14:32:06 -08003104 } else {
3105 // We have the current state for this activity, so
3106 // it can be restarted later when needed.
3107 if (localLOGV) Slog.v(
3108 TAG, "Keeping entry, setting app to null");
3109 if (r.visible) {
3110 hasVisibleActivities = true;
3111 }
3112 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3113 + r);
3114 r.app = null;
3115 r.nowVisible = false;
3116 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003117 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003118 "App died, clearing saved state of " + r);
3119 r.icicle = null;
3120 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003121 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003122
Craig Mautnerd2328952013-03-05 12:46:26 -08003123 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003124 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003125 }
3126 }
3127
3128 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003130
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003131 final void updateTransitLocked(int transit, Bundle options) {
3132 if (options != null) {
3133 ActivityRecord r = topRunningActivityLocked(null);
3134 if (r != null && r.state != ActivityState.RESUMED) {
3135 r.updateOptionsLocked(options);
3136 } else {
3137 ActivityOptions.abort(options);
3138 }
3139 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003140 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003141 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003142
Craig Mautnera82aa092013-09-13 15:34:08 -07003143 void moveHomeTaskToTop() {
3144 final int top = mTaskHistory.size() - 1;
3145 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3146 final TaskRecord task = mTaskHistory.get(taskNdx);
3147 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003148 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003149 mTaskHistory.remove(taskNdx);
3150 mTaskHistory.add(top, task);
3151 mWindowManager.moveTaskToTop(task.taskId);
3152 return;
3153 }
3154 }
3155 }
3156
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003157 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003158 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003159
Craig Mautner11bf9a52013-02-19 14:08:51 -08003160 final int numTasks = mTaskHistory.size();
3161 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003162 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003163 // nothing to do!
3164 if (reason != null &&
3165 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3166 ActivityOptions.abort(options);
3167 } else {
3168 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3169 }
3170 return;
3171 }
3172
Craig Mautnere0a38842013-12-16 16:14:02 -08003173 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003174
Craig Mautner11bf9a52013-02-19 14:08:51 -08003175 // Shift all activities with this task up to the top
3176 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003177 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003178
3179 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 if (reason != null &&
3181 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003182 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 ActivityRecord r = topRunningActivityLocked(null);
3184 if (r != null) {
3185 mNoAnimActivities.add(r);
3186 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003187 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003189 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003190 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003191
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003192 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193
Craig Mautner05d29032013-05-03 13:40:13 -07003194 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003195 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003196
3197 if (VALIDATE_TOKENS) {
3198 validateAppTokensLocked();
3199 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 }
3201
3202 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003203 * Worker method for rearranging history stack. Implements the function of moving all
3204 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003206 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003207 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3208 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003209 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003210 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 * @return Returns true if the move completed, false if not.
3212 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003213 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003214 final TaskRecord tr = taskForIdLocked(taskId);
3215 if (tr == null) {
3216 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3217 return false;
3218 }
3219
3220 Slog.i(TAG, "moveTaskToBack: " + tr);
3221
3222 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003223
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 // If we have a watcher, preflight the move before committing to it. First check
3225 // for *other* available tasks, but if none are available, then try again allowing the
3226 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003227 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003228 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 if (next == null) {
3230 next = topRunningActivityLocked(null, 0);
3231 }
3232 if (next != null) {
3233 // ask watcher if this is allowed
3234 boolean moveOK = true;
3235 try {
3236 moveOK = mService.mController.activityResuming(next.packageName);
3237 } catch (RemoteException e) {
3238 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003239 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003240 }
3241 if (!moveOK) {
3242 return false;
3243 }
3244 }
3245 }
3246
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003248 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249
Craig Mautner11bf9a52013-02-19 14:08:51 -08003250 mTaskHistory.remove(tr);
3251 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003252
Craig Mautnerc8143c62013-09-03 12:15:57 -07003253 // There is an assumption that moving a task to the back moves it behind the home activity.
3254 // We make sure here that some activity in the stack will launch home.
3255 ActivityRecord lastActivity = null;
3256 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003257 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3258 final TaskRecord task = mTaskHistory.get(taskNdx);
3259 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003260 break;
3261 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003262 if (taskNdx == 1) {
3263 // Set the last task before tr to go to home.
3264 task.mOnTopOfHome = true;
3265 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003266 }
3267
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003269 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3270 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003271 ActivityRecord r = topRunningActivityLocked(null);
3272 if (r != null) {
3273 mNoAnimActivities.add(r);
3274 }
3275 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003276 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003277 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003278 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003279
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003280 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003281 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 }
3283
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003284 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003285 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003286 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003287 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003288 }
3289
Craig Mautner05d29032013-05-03 13:40:13 -07003290 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003291 return true;
3292 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003293
Craig Mautner8849a5e2013-04-02 16:41:03 -07003294 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003296 final Uri data = r.intent.getData();
3297 final String strData = data != null ? data.toSafeString() : null;
3298
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003300 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003301 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003302 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 }
3304
3305 /**
3306 * Make sure the given activity matches the current configuration. Returns
3307 * false if the activity had to be destroyed. Returns true if the
3308 * configuration is the same, or the activity will remain running as-is
3309 * for whatever reason. Ensures the HistoryRecord is updated with the
3310 * correct configuration and all other bookkeeping is handled.
3311 */
3312 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3313 int globalChanges) {
3314 if (mConfigWillChange) {
3315 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3316 "Skipping config check (will change): " + r);
3317 return true;
3318 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003319
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3321 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003322
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003323 // Short circuit: if the two configurations are the exact same
3324 // object (the common case), then there is nothing to do.
3325 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003326 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003327 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3328 "Configuration unchanged in " + r);
3329 return true;
3330 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003331
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003332 // We don't worry about activities that are finishing.
3333 if (r.finishing) {
3334 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3335 "Configuration doesn't matter in finishing " + r);
3336 r.stopFreezingScreenLocked(false);
3337 return true;
3338 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003340 // Okay we now are going to make this activity have the new config.
3341 // But then we need to figure out how it needs to deal with that.
3342 Configuration oldConfig = r.configuration;
3343 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003344
3345 // Determine what has changed. May be nothing, if this is a config
3346 // that has come back from the app after going idle. In that case
3347 // we just want to leave the official config object now in the
3348 // activity and do nothing else.
3349 final int changes = oldConfig.diff(newConfig);
3350 if (changes == 0 && !r.forceNewConfig) {
3351 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3352 "Configuration no differences in " + r);
3353 return true;
3354 }
3355
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003356 // If the activity isn't currently running, just leave the new
3357 // configuration and it will pick that up next time it starts.
3358 if (r.app == null || r.app.thread == null) {
3359 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3360 "Configuration doesn't matter not running " + r);
3361 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003362 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003363 return true;
3364 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003365
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003366 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003367 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3368 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3369 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003370 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003371 + ", newConfig=" + newConfig);
3372 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003373 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003374 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3375 r.configChangeFlags |= changes;
3376 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003377 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003378 if (r.app == null || r.app.thread == null) {
3379 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003380 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003381 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003382 } else if (r.state == ActivityState.PAUSING) {
3383 // A little annoying: we are waiting for this activity to
3384 // finish pausing. Let's not do anything now, but just
3385 // flag that it needs to be restarted when done pausing.
3386 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003387 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003388 r.configDestroy = true;
3389 return true;
3390 } else if (r.state == ActivityState.RESUMED) {
3391 // Try to optimize this case: the configuration is changing
3392 // and we need to restart the top, resumed activity.
3393 // Instead of doing the normal handshaking, just say
3394 // "restart!".
3395 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003396 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003397 relaunchActivityLocked(r, r.configChangeFlags, true);
3398 r.configChangeFlags = 0;
3399 } else {
3400 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003401 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003402 relaunchActivityLocked(r, r.configChangeFlags, false);
3403 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003404 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003405
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003406 // All done... tell the caller we weren't able to keep this
3407 // activity around.
3408 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003409 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003410
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003411 // Default case: the activity can handle this new configuration, so
3412 // hand it over. Note that we don't need to give it the new
3413 // configuration, since we always send configuration changes to all
3414 // process when they happen so it can just use whatever configuration
3415 // it last got.
3416 if (r.app != null && r.app.thread != null) {
3417 try {
3418 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003419 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003420 } catch (RemoteException e) {
3421 // If process died, whatever.
3422 }
3423 }
3424 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003425
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003426 return true;
3427 }
3428
Craig Mautnerc8143c62013-09-03 12:15:57 -07003429 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003430 int changes, boolean andResume) {
3431 List<ResultInfo> results = null;
3432 List<Intent> newIntents = null;
3433 if (andResume) {
3434 results = r.results;
3435 newIntents = r.newIntents;
3436 }
3437 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3438 + " with results=" + results + " newIntents=" + newIntents
3439 + " andResume=" + andResume);
3440 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003441 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003442 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003443
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003444 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003445
Craig Mautner34b73df2014-01-12 21:11:08 -08003446 mStackSupervisor.removeChildActivityContainers(r);
3447
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003449 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3450 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3451 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003452 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003453 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003454 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003455 // Note: don't need to call pauseIfSleepingLocked() here, because
3456 // the caller will only pass in 'andResume' if this activity is
3457 // currently resumed, which implies we aren't sleeping.
3458 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003459 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003460 }
3461
3462 if (andResume) {
3463 r.results = null;
3464 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003465 r.state = ActivityState.RESUMED;
3466 } else {
3467 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3468 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003469 }
3470
3471 return true;
3472 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003473
3474 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003475 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3476 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3477 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3478 final ActivityRecord r = activities.get(activityNdx);
3479 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003480 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003481 }
3482 if (r.fullscreen && !r.finishing) {
3483 return false;
3484 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003485 }
3486 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003487 final ActivityRecord r = ActivityRecord.forToken(token);
3488 if (r == null) {
3489 return false;
3490 }
3491 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3492 + " would have returned true for r=" + r);
3493 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003494 }
3495
3496 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003497 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3498 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3499 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3500 final ActivityRecord r = activities.get(activityNdx);
3501 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003502 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003503 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003504 }
3505 }
3506 }
3507
3508 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3509 boolean didSomething = false;
3510 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003511 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3512 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3513 int numActivities = activities.size();
3514 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3515 ActivityRecord r = activities.get(activityNdx);
3516 final boolean samePackage = r.packageName.equals(name)
3517 || (name == null && r.userId == userId);
3518 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3519 && (samePackage || r.task == lastTask)
3520 && (r.app == null || evenPersistent || !r.app.persistent)) {
3521 if (!doit) {
3522 if (r.finishing) {
3523 // If this activity is just finishing, then it is not
3524 // interesting as far as something to stop.
3525 continue;
3526 }
3527 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003528 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003529 didSomething = true;
3530 Slog.i(TAG, " Force finishing activity " + r);
3531 if (samePackage) {
3532 if (r.app != null) {
3533 r.app.removed = true;
3534 }
3535 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003536 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003537 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003538 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3539 true)) {
3540 // r has been deleted from mActivities, accommodate.
3541 --numActivities;
3542 --activityNdx;
3543 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003544 }
3545 }
3546 }
3547 return didSomething;
3548 }
3549
Dianne Hackborn09233282014-04-30 11:33:59 -07003550 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003551 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003552 final TaskRecord task = mTaskHistory.get(taskNdx);
3553 ActivityRecord r = null;
3554 ActivityRecord top = null;
3555 int numActivities = 0;
3556 int numRunning = 0;
3557 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003558 if (activities.isEmpty()) {
3559 continue;
3560 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003561 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3562 continue;
3563 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003564 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3565 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003566
Craig Mautneraab647e2013-02-28 16:31:36 -08003567 // Initialize state for next task if needed.
3568 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3569 top = r;
3570 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003571 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003572
3573 // Add 'r' into the current task.
3574 numActivities++;
3575 if (r.app != null && r.app.thread != null) {
3576 numRunning++;
3577 }
3578
3579 if (localLOGV) Slog.v(
3580 TAG, r.intent.getComponent().flattenToShortString()
3581 + ": task=" + r.task);
3582 }
3583
3584 RunningTaskInfo ci = new RunningTaskInfo();
3585 ci.id = task.taskId;
3586 ci.baseActivity = r.intent.getComponent();
3587 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003588 ci.lastActiveTime = task.lastActiveTime;
3589
Craig Mautneraab647e2013-02-28 16:31:36 -08003590 if (top.thumbHolder != null) {
3591 ci.description = top.thumbHolder.lastDescription;
3592 }
3593 ci.numActivities = numActivities;
3594 ci.numRunning = numRunning;
3595 //System.out.println(
3596 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003597 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003598 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003599 }
3600
3601 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003602 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003603 if (DEBUG_SWITCH) Slog.d(
3604 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003605 if (top >= 0) {
3606 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3607 int activityTop = activities.size() - 1;
3608 if (activityTop > 0) {
3609 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3610 "unhandled-back", true);
3611 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003612 }
3613 }
3614
Craig Mautner6b74cb52013-09-27 17:02:21 -07003615 /**
3616 * Reset local parameters because an app's activity died.
3617 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003618 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003619 */
3620 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003621 if (mPausingActivity != null && mPausingActivity.app == app) {
3622 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3623 "App died while pausing: " + mPausingActivity);
3624 mPausingActivity = null;
3625 }
3626 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3627 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003628 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003629 }
3630
Craig Mautner19091252013-10-05 00:03:53 -07003631 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003632 }
3633
Craig Mautnercae015f2013-02-08 14:31:27 -08003634 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003635 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3636 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3637 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3638 final ActivityRecord r = activities.get(activityNdx);
3639 if (r.app == app) {
3640 Slog.w(TAG, " Force finishing activity "
3641 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003642 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003643 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003644 }
3645 }
3646 }
3647
Dianne Hackborn390517b2013-05-30 15:03:32 -07003648 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003649 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003650 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003651 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3652 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003653 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3654 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003655 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003656 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003657 if (printed) {
3658 header = null;
3659 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003660 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003661 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003662 }
3663
3664 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3665 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3666
3667 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003668 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3669 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003670 }
3671 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003672 final int top = mTaskHistory.size() - 1;
3673 if (top >= 0) {
3674 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3675 int listTop = list.size() - 1;
3676 if (listTop >= 0) {
3677 activities.add(list.get(listTop));
3678 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003679 }
3680 } else {
3681 ItemMatcher matcher = new ItemMatcher();
3682 matcher.build(name);
3683
Craig Mautneraab647e2013-02-28 16:31:36 -08003684 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3685 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3686 if (matcher.match(r1, r1.intent.getComponent())) {
3687 activities.add(r1);
3688 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003689 }
3690 }
3691 }
3692
3693 return activities;
3694 }
3695
3696 ActivityRecord restartPackage(String packageName) {
3697 ActivityRecord starting = topRunningActivityLocked(null);
3698
3699 // All activities that came from the package must be
3700 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003701 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3702 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3703 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3704 final ActivityRecord a = activities.get(activityNdx);
3705 if (a.info.packageName.equals(packageName)) {
3706 a.forceNewConfig = true;
3707 if (starting != null && a == starting && a.visible) {
3708 a.startFreezingScreenLocked(starting.app,
3709 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3710 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003711 }
3712 }
3713 }
3714
3715 return starting;
3716 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003717
Craig Mautner41db4a72014-05-07 17:20:56 -07003718 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003719 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003720 mWindowManager.removeTask(task.taskId);
3721 final ActivityRecord r = mResumedActivity;
3722 if (r != null && r.task == task) {
3723 mResumedActivity = null;
3724 }
3725
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003726 final int taskNdx = mTaskHistory.indexOf(task);
3727 final int topTaskNdx = mTaskHistory.size() - 1;
3728 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3729 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3730 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003731 mTaskHistory.remove(task);
Craig Mautner41db4a72014-05-07 17:20:56 -07003732
3733 if (task.mActivities.isEmpty()) {
3734 final boolean isVoiceSession = task.voiceSession != null;
3735 if (isVoiceSession) {
3736 try {
3737 task.voiceSession.taskFinished(task.intent, task.taskId);
3738 } catch (RemoteException e) {
3739 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003740 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003741 if (task.autoRemoveFromRecents() || isVoiceSession) {
3742 // Task creator asked to remove this when done, or this task was a voice
3743 // interaction, so it should not remain on the recent tasks list.
3744 mService.mRecentTasks.remove(task);
3745 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003746 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003747
3748 if (mTaskHistory.isEmpty()) {
3749 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3750 if (isOnHomeDisplay()) {
3751 mStackSupervisor.moveHomeStack(!isHomeStack());
3752 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003753 if (mStacks != null) {
3754 mStacks.remove(this);
3755 mStacks.add(0, this);
3756 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003757 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003758 }
3759
Dianne Hackborn91097de2014-04-04 18:02:06 -07003760 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
3761 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
3762 boolean toTop) {
3763 TaskRecord task = new TaskRecord(taskId, info, intent, voiceSession, voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003764 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003765 return task;
3766 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003767
3768 ArrayList<TaskRecord> getAllTasks() {
3769 return new ArrayList<TaskRecord>(mTaskHistory);
3770 }
3771
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003772 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003773 task.stack = this;
3774 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003775 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003776 } else {
3777 mTaskHistory.add(0, task);
3778 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003779 if (!moving && task.voiceSession != null) {
3780 try {
3781 task.voiceSession.taskStarted(task.intent, task.taskId);
3782 } catch (RemoteException e) {
3783 }
3784 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003785 }
3786
3787 public int getStackId() {
3788 return mStackId;
3789 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003790
3791 @Override
3792 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003793 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3794 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003795 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796}