blob: 4bd86e43bb62212a09133ded17d4c86c7167063e [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
Craig Mautner84984fa2014-06-19 11:19:20 -070033import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070035
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
37import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070041import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070042import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
43
Dianne Hackborn91097de2014-04-04 18:02:06 -070044import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070045import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070046import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080047import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080049import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080050import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070051import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052
53import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070054import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070055import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080057import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080059import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080065import android.content.res.Resources;
66import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080067import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070069import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080070import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Handler;
72import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090073import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070075import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.RemoteException;
77import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080078import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070079import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070080import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070083import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084
Craig Mautnercae015f2013-02-08 14:31:27 -080085import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080086import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087import java.lang.ref.WeakReference;
88import java.util.ArrayList;
89import java.util.Iterator;
90import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080091import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092
93/**
94 * State and management of a single stack of activities.
95 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070096final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080097
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070098 // Ticks during which we check progress while waiting for an app to launch.
99 static final int LAUNCH_TICK = 500;
100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700101 // How long we wait until giving up on the last activity to pause. This
102 // is short because it directly impacts the responsiveness of starting the
103 // next activity.
104 static final int PAUSE_TIMEOUT = 500;
105
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700106 // How long we wait for the activity to tell us it has stopped before
107 // giving up. This is a good amount of time because we really need this
108 // from the application in order to get its saved state.
109 static final int STOP_TIMEOUT = 10*1000;
110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700111 // How long we wait until giving up on an activity telling us it has
112 // finished destroying itself.
113 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700115 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800116 // disabled.
117 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800118
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700119 // How long between activity launches that we consider safe to not warn
120 // the user about an unexpected activity being launched on top.
121 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700123 // Set to false to disable the preview that is shown while a new activity
124 // is being started.
125 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800126
Craig Mautner5eda9b32013-07-02 11:58:16 -0700127 // How long to wait for all background Activities to redraw following a call to
128 // convertToTranslucent().
129 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
130
Craig Mautnera61bc652013-10-28 15:43:18 -0700131 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133 enum ActivityState {
134 INITIALIZING,
135 RESUMED,
136 PAUSING,
137 PAUSED,
138 STOPPING,
139 STOPPED,
140 FINISHING,
141 DESTROYING,
142 DESTROYED
143 }
144
145 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700146 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 /**
149 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800150 * running) activities. It contains #TaskRecord objects.
151 */
152 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
153
154 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800155 * Used for validating app tokens with window manager.
156 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800157 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800158
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159 /**
160 * List of running activities, sorted by recent usage.
161 * The first entry in the list is the least recently used.
162 * It contains HistoryRecord objects.
163 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700164 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167 * Animations that for the current transition have requested not to
168 * be considered for the transition animation.
169 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700170 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173 * When we are in the process of pausing an activity, before starting the
174 * next one, this variable holds the activity that is currently being paused.
175 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800176 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177
178 /**
179 * This is the last activity that we put into the paused state. This is
180 * used to determine if we need to do an activity transition while sleeping,
181 * when we normally hold the top activity paused.
182 */
183 ActivityRecord mLastPausedActivity = null;
184
185 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700186 * Activities that specify No History must be removed once the user navigates away from them.
187 * If the device goes to sleep with such an activity in the paused state then we save it here
188 * and finish it later if another activity replaces it on wakeup.
189 */
190 ActivityRecord mLastNoHistoryActivity = null;
191
192 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 * Current activity that is resumed, or null if there is none.
194 */
195 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800196
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700197 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700198 * This is the last activity that has been started. It is only used to
199 * identify when multiple activities are started at once so that the user
200 * can be warned they may not be in the activity they think they are.
201 */
202 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800203
Craig Mautner5eda9b32013-07-02 11:58:16 -0700204 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
205 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
206 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
207 // Activity in mTranslucentActivityWaiting is notified via
208 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
209 // background activity being drawn then the same call will be made with a true value.
210 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700211 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700212 new ArrayList<ActivityRecord>();
213
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700215 * Set when we know we are going to be calling updateConfiguration()
216 * soon, so want to skip intermediate config checks.
217 */
218 boolean mConfigWillChange;
219
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700220 long mLaunchStartTime = 0;
221 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800222
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800223 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800224 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
225 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
226 */
227 private ActivityRecord mLastScreenshotActivity = null;
228 private Bitmap mLastScreenshotBitmap = null;
229
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800230 int mThumbnailWidth = -1;
231 int mThumbnailHeight = -1;
232
Craig Mautner858d8a62013-04-23 17:08:34 -0700233 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700234
Craig Mautnerc00204b2013-03-05 15:02:14 -0800235 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800236 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800237 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
238 ArrayList<ActivityStack> mStacks;
239 /** The attached Display's unique identifier, or -1 if detached */
240 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800241
Craig Mautner27084302013-03-25 08:05:25 -0700242 /** Run all ActivityStacks through this */
243 final ActivityStackSupervisor mStackSupervisor;
244
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700245 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700246 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
247 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
248 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
249 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700250 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700251 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700252 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700253
254 static class ScheduleDestroyArgs {
255 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700256 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700257 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700258 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700259 mReason = reason;
260 }
261 }
262
Zoran Marcetaf958b322012-08-09 20:27:12 +0900263 final Handler mHandler;
264
265 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700266 //public Handler() {
267 // if (localLOGV) Slog.v(TAG, "Handler started!");
268 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700269 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900270 super(looper);
271 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272
Zoran Marcetaf958b322012-08-09 20:27:12 +0900273 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274 public void handleMessage(Message msg) {
275 switch (msg.what) {
276 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800277 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 // We don't at this point know if the activity is fullscreen,
279 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800280 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700281 synchronized (mService) {
282 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700283 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 }
Craig Mautnera0026042014-04-23 11:45:37 -0700285 activityPausedLocked(r.appToken, true, r.persistentState);
Craig Mautnerd2328952013-03-05 12:46:26 -0800286 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700288 case LAUNCH_TICK_MSG: {
289 ActivityRecord r = (ActivityRecord)msg.obj;
290 synchronized (mService) {
291 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700292 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700293 }
294 }
295 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800297 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 // We don't at this point know if the activity is fullscreen,
299 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800300 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800301 synchronized (mService) {
302 activityDestroyedLocked(r != null ? r.appToken : null);
303 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700305 case STOP_TIMEOUT_MSG: {
306 ActivityRecord r = (ActivityRecord)msg.obj;
307 // We don't at this point know if the activity is fullscreen,
308 // so we need to be conservative and assume it isn't.
309 Slog.w(TAG, "Activity stop timeout for " + r);
310 synchronized (mService) {
311 if (r.isInHistory()) {
312 activityStoppedLocked(r, null, null, null);
313 }
314 }
315 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700316 case DESTROY_ACTIVITIES_MSG: {
317 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
318 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700319 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700320 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700321 } break;
322 case TRANSLUCENT_TIMEOUT_MSG: {
323 synchronized (mService) {
324 notifyActivityDrawnLocked(null);
325 }
326 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700327 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700328 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700329 final ActivityRecord r = getVisibleBehindActivity();
330 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700331 if (r != null) {
332 mService.killAppAtUsersRequest(r.app, null);
333 }
334 }
335 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 }
337 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800338 }
339
Craig Mautner34b73df2014-01-12 21:11:08 -0800340 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800341 int count = 0;
342 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
343 count += mTaskHistory.get(taskNdx).mActivities.size();
344 }
345 return count;
346 }
347
Craig Mautner4a1cb222013-12-04 16:14:06 -0800348 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
349 mActivityContainer = activityContainer;
350 mStackSupervisor = activityContainer.getOuter();
351 mService = mStackSupervisor.mService;
352 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
353 mWindowManager = mService.mWindowManager;
354 mStackId = activityContainer.mStackId;
355 mCurrentUser = mService.mCurrentUserId;
Winson Chung376543b2014-05-21 17:43:28 -0700356 // Get the activity screenshot thumbnail dimensions
357 Resources res = mService.mContext.getResources();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700358 mThumbnailWidth = mService.mThumbnailWidth;
359 mThumbnailHeight = mService.mThumbnailHeight;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700360 }
Craig Mautner5962b122012-10-05 14:45:52 -0700361
Amith Yamasani734983f2014-03-04 16:48:05 -0800362 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100363 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800364 */
Kenny Guy2a764942014-04-02 13:29:20 +0100365 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100366 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100367 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
368 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800369 }
370 return false;
371 }
372
373 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100374 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700375 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
376 }
377
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800379 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700380 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
381 if (r != null) {
382 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800383 }
384 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700385 return null;
386 }
387
388 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800389 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
390 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800391 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800392 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
393 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800394 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800395 return r;
396 }
397 }
398 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700399 return null;
400 }
401
402 /**
403 * This is a simplified version of topRunningActivityLocked that provides a number of
404 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800405 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700406 * @param token If non-null, any history records matching this token will be skipped.
407 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800408 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700409 * @return Returns the HistoryRecord of the next activity on the stack.
410 */
411 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800412 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
413 TaskRecord task = mTaskHistory.get(taskNdx);
414 if (task.taskId == taskId) {
415 continue;
416 }
417 ArrayList<ActivityRecord> activities = task.mActivities;
418 for (int i = activities.size() - 1; i >= 0; --i) {
419 final ActivityRecord r = activities.get(i);
420 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800421 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800422 return r;
423 }
424 }
425 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700426 return null;
427 }
428
Craig Mautner8849a5e2013-04-02 16:41:03 -0700429 final ActivityRecord topActivity() {
430 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700431 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
433 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700434 final int topActivityNdx = activities.size() - 1;
435 if (topActivityNdx >= 0) {
436 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700437 }
438 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700439 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700440 }
441
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700442 final TaskRecord topTask() {
443 final int size = mTaskHistory.size();
444 if (size > 0) {
445 return mTaskHistory.get(size - 1);
446 }
447 return null;
448 }
449
Craig Mautnerd2328952013-03-05 12:46:26 -0800450 TaskRecord taskForIdLocked(int id) {
451 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
452 final TaskRecord task = mTaskHistory.get(taskNdx);
453 if (task.taskId == id) {
454 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800455 }
456 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700457 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700458 }
459
Craig Mautnerd2328952013-03-05 12:46:26 -0800460 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800461 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800462 if (r != null) {
463 final TaskRecord task = r.task;
464 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
465 if (task.stack != this) Slog.w(TAG,
466 "Illegal state! task does not point to stack it is in.");
467 return r;
468 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800469 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800470 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800471 }
472
Craig Mautner2420ead2013-04-01 17:13:20 -0700473 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700474 final boolean hadit = mLRUActivities.remove(r);
475 mLRUActivities.add(r);
476 return hadit;
477 }
478
Craig Mautnerde4ef022013-04-07 19:01:33 -0700479 final boolean isHomeStack() {
480 return mStackId == HOME_STACK_ID;
481 }
482
Craig Mautnere0a38842013-12-16 16:14:02 -0800483 final boolean isOnHomeDisplay() {
484 return isAttached() &&
485 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
486 }
487
488 final void moveToFront() {
489 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800490 if (isOnHomeDisplay()) {
491 mStackSupervisor.moveHomeStack(isHomeStack());
492 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800493 mStacks.remove(this);
494 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800495 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800496 }
497
498 final boolean isAttached() {
499 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800500 }
501
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700502 /**
503 * Returns the top activity in any existing task matching the given
504 * Intent. Returns null if no such task is found.
505 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700506 ActivityRecord findTaskLocked(ActivityRecord target) {
507 Intent intent = target.intent;
508 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700509 ComponentName cls = intent.getComponent();
510 if (info.targetActivity != null) {
511 cls = new ComponentName(info.packageName, info.targetActivity);
512 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700513 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700514 boolean isDocument = intent != null & intent.isDocument();
515 // If documentData is non-null then it must match the existing task data.
516 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800517
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700518 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800519 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
520 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700521 if (task.voiceSession != null) {
522 // We never match voice sessions; those always run independently.
523 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
524 continue;
525 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700526 if (task.userId != userId) {
527 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700528 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700529 continue;
530 }
Craig Mautner000f0022013-02-26 15:04:29 -0800531 final ActivityRecord r = task.getTopActivity();
532 if (r == null || r.finishing || r.userId != userId ||
533 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700534 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800535 continue;
536 }
537
Craig Mautnerd00f4742014-03-12 14:17:26 -0700538 final Intent taskIntent = task.intent;
539 final Intent affinityIntent = task.affinityIntent;
540 final boolean taskIsDocument;
541 final Uri taskDocumentData;
542 if (taskIntent != null && taskIntent.isDocument()) {
543 taskIsDocument = true;
544 taskDocumentData = taskIntent.getData();
545 } else if (affinityIntent != null && affinityIntent.isDocument()) {
546 taskIsDocument = true;
547 taskDocumentData = affinityIntent.getData();
548 } else {
549 taskIsDocument = false;
550 taskDocumentData = null;
551 }
552
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700553 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700554 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700555 + "/aff=" + r.task.affinity + " to new cls="
556 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700557 if (!isDocument && !taskIsDocument && task.affinity != null) {
558 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700559 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700560 return r;
561 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700562 } else if (taskIntent != null && taskIntent.getComponent() != null &&
563 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700564 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700565 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800566 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700567 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
568 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800569 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700570 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
571 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700572 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700573 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800574 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700575 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
576 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800577 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700578 } else if (DEBUG_TASKS) {
579 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700580 }
581 }
582
583 return null;
584 }
585
586 /**
587 * Returns the first activity (starting from the top of the stack) that
588 * is the same as the given activity. Returns null if no such activity
589 * is found.
590 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700591 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700592 ComponentName cls = intent.getComponent();
593 if (info.targetActivity != null) {
594 cls = new ComponentName(info.packageName, info.targetActivity);
595 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700596 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700597
Craig Mautner000f0022013-02-26 15:04:29 -0800598 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700599 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100600 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700601 return null;
602 }
603 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800604 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
605 ActivityRecord r = activities.get(activityNdx);
606 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700607 //Slog.i(TAG, "Found matching class!");
608 //dump();
609 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
610 return r;
611 }
612 }
613 }
614
615 return null;
616 }
617
Amith Yamasani742a6712011-05-04 14:49:28 -0700618 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700619 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700620 */
Craig Mautner93529a42013-10-04 15:03:13 -0700621 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800622 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700623 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800624 }
625 mCurrentUser = userId;
626
627 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800629 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700630 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100631 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700632 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
633 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800634 mTaskHistory.remove(i);
635 mTaskHistory.add(task);
636 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800637 // Use same value for i.
638 } else {
639 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800640 }
641 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700642 if (VALIDATE_TOKENS) {
643 validateAppTokensLocked();
644 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700645 }
646
Craig Mautner2420ead2013-04-01 17:13:20 -0700647 void minimalResumeActivityLocked(ActivityRecord r) {
648 r.state = ActivityState.RESUMED;
649 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
650 + " (starting new instance)");
651 r.stopped = false;
652 mResumedActivity = r;
653 r.task.touchActiveTime();
654 mService.addRecentTaskLocked(r.task);
655 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700656 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700657 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700658 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659 }
660
Dianne Hackborncee04b52013-07-03 17:01:28 -0700661 private void startLaunchTraces() {
662 if (mFullyDrawnStartTime != 0) {
663 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
664 }
665 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
666 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
667 }
668
669 private void stopFullyDrawnTraceIfNeeded() {
670 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
671 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
672 mFullyDrawnStartTime = 0;
673 }
674 }
675
Craig Mautnere79d42682013-04-01 19:01:53 -0700676 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700677 if (r.displayStartTime == 0) {
678 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
679 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700680 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700681 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700683 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700684 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700685 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700686 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700687 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800688
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700689 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700690 // Make sure that there is no activity waiting for this to launch.
691 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
692 r.displayStartTime = r.fullyDrawnStartTime = 0;
693 } else {
694 mStackSupervisor.removeTimeoutsForActivityLocked(r);
695 mStackSupervisor.scheduleIdleTimeoutLocked(r);
696 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700697 }
698
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800699 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800700 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800701 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
702 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
703 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
704 activities.get(activityNdx).setSleeping(false);
705 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800706 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800707 }
708
Craig Mautner0eea92c2013-05-16 13:35:39 -0700709 /**
710 * @return true if something must be done before going to sleep.
711 */
712 boolean checkReadyForSleepLocked() {
713 if (mResumedActivity != null) {
714 // Still have something resumed; can't sleep until it is paused.
715 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
716 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
717 startPausingLocked(false, true);
718 return true;
719 }
720 if (mPausingActivity != null) {
721 // Still waiting for something to pause; can't sleep yet.
722 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
723 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800724 }
725
Craig Mautner0eea92c2013-05-16 13:35:39 -0700726 return false;
727 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800728
Craig Mautner0eea92c2013-05-16 13:35:39 -0700729 void goToSleep() {
730 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800731
Craig Mautner0eea92c2013-05-16 13:35:39 -0700732 // Make sure any stopped but visible activities are now sleeping.
733 // This ensures that the activity's onStop() is called.
734 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
735 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
736 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
737 final ActivityRecord r = activities.get(activityNdx);
738 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
739 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800740 }
741 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800742 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700743 }
Craig Mautner59c00972012-07-30 12:10:24 -0700744
Winson Chung376543b2014-05-21 17:43:28 -0700745 /**
746 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
747 * again when requested.
748 */
749 private void invalidateLastScreenshot() {
750 mLastScreenshotActivity = null;
751 if (mLastScreenshotBitmap != null) {
752 mLastScreenshotBitmap.recycle();
753 }
754 mLastScreenshotBitmap = null;
755 }
756
Dianne Hackbornd2835932010-12-13 16:28:46 -0800757 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700758 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800759 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700760 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800761 return null;
762 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800763
Winson Chung083baf92014-07-11 10:32:42 -0700764 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700765 // This is an optimization -- since we never show Home or Recents within Recents itself,
766 // we can just go ahead and skip taking the screenshot if this is the home stack. In
767 // the case where the most recent task is not the task that was supplied, then the stack
768 // has changed, so invalidate the last screenshot().
769 invalidateLastScreenshot();
770 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700771 return null;
772 }
773
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800774 int w = mThumbnailWidth;
775 int h = mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800776 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800777 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700778 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800779 || mLastScreenshotBitmap.getWidth() != w
780 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700781 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800782 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700783 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700784 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800785 }
786 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700787 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700788 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800789 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800790 }
Winson Chung376543b2014-05-21 17:43:28 -0700791 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800792 return null;
793 }
794
Craig Mautnercf910b02013-04-23 11:23:27 -0700795 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800796 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800797 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700798 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800799 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800 ActivityRecord prev = mResumedActivity;
801 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700802 Slog.e(TAG, "Trying to pause when nothing is resumed",
803 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700804 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700805 return;
806 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800807
808 if (mActivityContainer.mParentActivity == null) {
809 // Top level stack, not a child. Look for child stacks.
810 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
811 }
812
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700813 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
814 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700815 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800816 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700818 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
819 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700820 prev.state = ActivityState.PAUSING;
821 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700822 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700823 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Alan Viverettea2ff6112014-07-14 17:46:27 -0700824 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task)) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700825 prev.updateThumbnail(screenshotActivities(prev), null);
826 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700827 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828
829 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800830
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 if (prev.app != null && prev.app.thread != null) {
832 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
833 try {
834 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700835 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700837 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800838 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
839 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 } catch (Exception e) {
841 // Ignore exception, if process died other code will cleanup.
842 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800843 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700845 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 }
847 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800848 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700850 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 }
852
853 // If we are not going to sleep, we want to ensure the device is
854 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700855 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700856 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 }
858
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800859 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 // Have the window manager pause its key dispatching until the new
861 // activity has started. If we're pausing the activity just because
862 // the screen is being turned off and the UI is sleeping, don't interrupt
863 // key dispatch; the same activity will pick it up again on wakeup.
864 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 } else {
867 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
868 }
869
870 // Schedule a pause timeout in case the app doesn't respond.
871 // We don't give it much time because this directly impacts the
872 // responsiveness seen by the user.
873 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
874 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700875 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700876 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
877 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
878 } else {
879 // This activity failed to schedule the
880 // pause, so just treat it as being paused now.
881 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700882 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 }
884 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700885
Craig Mautnera0026042014-04-23 11:45:37 -0700886 final void activityPausedLocked(IBinder token, boolean timeout,
887 PersistableBundle persistentState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700888 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800889 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700890
Craig Mautnerd2328952013-03-05 12:46:26 -0800891 final ActivityRecord r = isInStackLocked(token);
892 if (r != null) {
893 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Craig Mautner21d24a22014-04-23 11:45:37 -0700894 if (persistentState != null) {
895 r.persistentState = persistentState;
896 mService.notifyTaskPersisterLocked(r.task, false);
897 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800898 if (mPausingActivity == r) {
899 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
900 + (timeout ? " (due to timeout)" : " (pause complete)"));
901 r.state = ActivityState.PAUSED;
902 completePauseLocked();
903 } else {
904 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
905 r.userId, System.identityHashCode(r), r.shortComponentName,
906 mPausingActivity != null
907 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700908 }
909 }
910 }
911
Craig Mautnera0026042014-04-23 11:45:37 -0700912 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
913 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700914 if (r.state != ActivityState.STOPPING) {
915 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
916 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
917 return;
918 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700919 if (persistentState != null) {
920 r.persistentState = persistentState;
921 mService.notifyTaskPersisterLocked(r.task, false);
922 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700923 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700924 if (icicle != null) {
925 // If icicle is null, this is happening due to a timeout, so we
926 // haven't really saved the state.
927 r.icicle = icicle;
928 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800929 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700930 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700931 }
932 if (!r.stopped) {
933 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
934 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
935 r.stopped = true;
936 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700937 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
938 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700939 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700940 if (r.finishing) {
941 r.clearOptionsLocked();
942 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700943 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700944 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700945 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700946 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700947 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800948 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700949 }
950 }
951 }
952
Craig Mautnerc8143c62013-09-03 12:15:57 -0700953 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800954 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800956
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800957 if (prev != null) {
958 if (prev.finishing) {
959 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700960 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800961 } else if (prev.app != null) {
962 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
963 if (prev.waitingVisible) {
964 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700965 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800966 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
967 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800968 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800969 if (prev.configDestroy) {
970 // The previous is being paused because the configuration
971 // is changing, which means it is actually stopping...
972 // To juggle the fact that we are also starting a new
973 // instance right now, we need to first completely stop
974 // the current instance before starting the new one.
975 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700976 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700977 } else if (!hasVisibleBehindActivity()) {
978 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700979 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700980 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700981 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
982 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800983 // If we already have a few activities waiting to stop,
984 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700985 // them out. Or if r is the last of activity of the last task the stack
986 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800987 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700988 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700990 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800991 }
992 }
993 } else {
994 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
995 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800996 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800997 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800999
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001000 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001001 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -07001002 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001003 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001004 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001005 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001006 if (top == null || (prev != null && top != prev)) {
1007 // If there are no more activities available to run,
1008 // do resume anyway to start something. Also if the top
1009 // activity on the stack is not the just paused activity,
1010 // we need to go ahead and resume it to ensure we complete
1011 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -07001012 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001013 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001015
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001016 if (prev != null) {
1017 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018
Craig Mautner525f3d92013-05-07 14:01:50 -07001019 if (prev.app != null && prev.cpuTimeAtResume > 0
1020 && mService.mBatteryStatsService.isOnBattery()) {
1021 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -07001022 synchronized (mService.mProcessCpuThread) {
1023 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -07001024 - prev.cpuTimeAtResume;
1025 }
1026 if (diff > 0) {
1027 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1028 synchronized (bsi) {
1029 BatteryStatsImpl.Uid.Proc ps =
1030 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001031 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001032 if (ps != null) {
1033 ps.addForegroundTimeLocked(diff);
1034 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035 }
1036 }
1037 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001038 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001039 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 }
1041
1042 /**
1043 * Once we know that we have asked an application to put an activity in
1044 * the resumed state (either by launching it or explicitly telling it),
1045 * this function updates the rest of our state to match that fact.
1046 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001047 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048 next.idle = false;
1049 next.results = null;
1050 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001051
1052 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1053 ProcessRecord app = next.task.mActivities.get(0).app;
1054 if (app != null && app != mService.mHomeProcess) {
1055 mService.mHomeProcess = app;
1056 }
1057 }
1058
Craig Mautner07566322013-09-26 16:42:55 -07001059 if (next.nowVisible) {
1060 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001061 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001062 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001063
1064 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001065 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001066
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 mStackSupervisor.reportResumedActivityLocked(next);
1068
1069 next.resumeKeyDispatchingLocked();
1070 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071
1072 // Mark the point when the activity is resuming
1073 // TODO: To be more accurate, the mark should be before the onCreate,
1074 // not after the onResume. But for subsequent starts, onResume is fine.
1075 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001076 synchronized (mService.mProcessCpuThread) {
1077 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 }
1079 } else {
1080 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1081 }
Winson Chung376543b2014-05-21 17:43:28 -07001082
1083 // If we are resuming the activity that we had last screenshotted, then we know it will be
1084 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1085 if (next == mLastScreenshotActivity) {
1086 invalidateLastScreenshot();
1087 }
George Mount6ba042b2014-07-28 11:12:28 -07001088 next.returningOptions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 }
1090
Craig Mautnere3a00d72014-04-16 08:31:19 -07001091 private void setVisibile(ActivityRecord r, boolean visible) {
1092 r.visible = visible;
1093 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001094 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001095 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001096 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001097 container.setVisible(visible);
1098 }
1099 }
1100
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001101 // Find the first visible activity above the passed activity and if it is translucent return it
1102 // otherwise return null;
1103 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1104 TaskRecord task = r.task;
1105 if (task == null) {
1106 return null;
1107 }
1108
1109 ActivityStack stack = task.stack;
1110 if (stack == null) {
1111 return null;
1112 }
1113
1114 int stackNdx = mStacks.indexOf(stack);
1115
1116 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1117 int taskNdx = tasks.indexOf(task);
1118
1119 ArrayList<ActivityRecord> activities = task.mActivities;
1120 int activityNdx = activities.indexOf(r) + 1;
1121
1122 final int numStacks = mStacks.size();
1123 while (stackNdx < numStacks) {
1124 tasks = mStacks.get(stackNdx).mTaskHistory;
1125 final int numTasks = tasks.size();
1126 while (taskNdx < numTasks) {
1127 activities = tasks.get(taskNdx).mActivities;
1128 final int numActivities = activities.size();
1129 while (activityNdx < numActivities) {
1130 final ActivityRecord activity = activities.get(activityNdx);
1131 if (!activity.finishing) {
1132 return activity.fullscreen ? null : activity;
1133 }
1134 ++activityNdx;
1135 }
1136 activityNdx = 0;
1137 ++taskNdx;
1138 }
1139 taskNdx = 0;
1140 ++stackNdx;
1141 }
1142
1143 return null;
1144 }
1145
Jose Lima7ba71252014-04-30 12:59:27 -07001146 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1147 // If so, this stack is hidden, otherwise it is visible.
1148 private boolean isStackVisible() {
1149 if (!isAttached()) {
1150 return false;
1151 }
1152
1153 if (mStackSupervisor.isFrontStack(this)) {
1154 return true;
1155 }
1156
Jose Lima729cb232014-05-05 15:59:40 -07001157 /**
1158 * Start at the task above this one and go up, looking for a visible
1159 * fullscreen activity, or a translucent activity that requested the
1160 * wallpaper to be shown behind it.
1161 */
Jose Lima7ba71252014-04-30 12:59:27 -07001162 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1163 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1164 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001165 final TaskRecord task = tasks.get(taskNdx);
1166 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001167 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1168 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001169
1170 // Conditions for an activity to obscure the stack we're
1171 // examining:
1172 // 1. Not Finishing AND Visible AND:
1173 // 2. Either:
1174 // - Full Screen Activity OR
1175 // - On top of Home and our stack is NOT home
1176 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001177 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001178 return false;
1179 }
1180 }
1181 }
1182 }
1183
1184 return true;
1185 }
1186
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 /**
1188 * Make sure that all activities that need to be visible (that is, they
1189 * currently can be seen by the user) actually are.
1190 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001191 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1192 ActivityRecord top = topRunningActivityLocked(null);
1193 if (top == null) {
1194 return;
1195 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001196 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 TAG, "ensureActivitiesVisible behind " + top
1198 + " configChanges=0x" + Integer.toHexString(configChanges));
1199
Craig Mautner5eda9b32013-07-02 11:58:16 -07001200 if (mTranslucentActivityWaiting != top) {
1201 mUndrawnActivitiesBelowTopTranslucent.clear();
1202 if (mTranslucentActivityWaiting != null) {
1203 // Call the callback with a timeout indication.
1204 notifyActivityDrawnLocked(null);
1205 mTranslucentActivityWaiting = null;
1206 }
1207 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1208 }
1209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001210 // If the top activity is not fullscreen, then we need to
1211 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001212 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001213 boolean behindFullscreen = !isStackVisible();
1214
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001215 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001216 final TaskRecord task = mTaskHistory.get(taskNdx);
1217 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001218 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1219 final ActivityRecord r = activities.get(activityNdx);
1220 if (r.finishing) {
1221 continue;
1222 }
1223 if (aboveTop && r != top) {
1224 continue;
1225 }
1226 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001227 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1228 // but must be drawn initially for the animation as though they were visible.
1229 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001230 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001231 TAG, "Make visible? " + r + " finishing=" + r.finishing
1232 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001233
Craig Mautnerd44711d2013-02-23 11:24:36 -08001234 // First: if this is not the current activity being started, make
1235 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001236 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001237 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001238 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001239
1240 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001241 // This activity needs to be visible, but isn't even
1242 // running... get it started, but don't resume it
1243 // at this point.
1244 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1245 if (r != starting) {
1246 r.startFreezingScreenLocked(r.app, configChanges);
1247 }
1248 if (!r.visible || r.mLaunchTaskBehind) {
1249 if (DEBUG_VISBILITY) Slog.v(
1250 TAG, "Starting and making visible: " + r);
1251 setVisibile(r, true);
1252 }
1253 if (r != starting) {
1254 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001255 }
1256
1257 } else if (r.visible) {
1258 // If this activity is already visible, then there is nothing
1259 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001260 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001262 try {
George Mount6ba042b2014-07-28 11:12:28 -07001263 if (r.returningOptions != null) {
1264 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1265 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001266 }
1267 } catch(RemoteException e) {
1268 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001269 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001270 // This activity is not currently visible, but is running.
1271 // Tell it to become visible.
1272 r.visible = true;
1273 if (r.state != ActivityState.RESUMED && r != starting) {
1274 // If this activity is paused, tell it
1275 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001276 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001277 TAG, "Making visible and scheduling visibility: " + r);
1278 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001279 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001280 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001281 mUndrawnActivitiesBelowTopTranslucent.add(r);
1282 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001283 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001284 r.sleeping = false;
1285 r.app.pendingUiClean = true;
1286 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1287 r.stopFreezingScreenLocked(false);
1288 } catch (Exception e) {
1289 // Just skip on any failure; we'll make it
1290 // visible when it next restarts.
1291 Slog.w(TAG, "Exception thrown making visibile: "
1292 + r.intent.getComponent(), e);
1293 }
1294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001295 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296
Craig Mautnerd44711d2013-02-23 11:24:36 -08001297 // Aggregate current change flags.
1298 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001299
Craig Mautnerd44711d2013-02-23 11:24:36 -08001300 if (r.fullscreen) {
1301 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001302 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1303 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001304 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001305 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001306 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001307 }
1308 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001309 if (DEBUG_VISBILITY) Slog.v(
1310 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1311 + " state=" + r.state
1312 + " behindFullscreen=" + behindFullscreen);
1313 // Now for any activities that aren't visible to the user, make
1314 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001315 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001316 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001318 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001319 switch (r.state) {
1320 case STOPPING:
1321 case STOPPED:
1322 if (r.app != null && r.app.thread != null) {
1323 if (DEBUG_VISBILITY) Slog.v(
1324 TAG, "Scheduling invisibility: " + r);
1325 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1326 }
1327 break;
1328
1329 case INITIALIZING:
1330 case RESUMED:
1331 case PAUSING:
1332 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001333 // This case created for transitioning activities from
1334 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001335 if (getVisibleBehindActivity() == r) {
1336 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001337 } else {
1338 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1339 mStackSupervisor.mStoppingActivities.add(r);
1340 }
1341 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001342 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001343 break;
1344
1345 default:
1346 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001347 }
1348 } catch (Exception e) {
1349 // Just skip on any failure; we'll make it
1350 // visible when it next restarts.
1351 Slog.w(TAG, "Exception thrown making hidden: "
1352 + r.intent.getComponent(), e);
1353 }
1354 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001355 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001356 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 }
1358 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001359 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001360
1361 if (mTranslucentActivityWaiting != null &&
1362 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1363 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1364 notifyActivityDrawnLocked(null);
1365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 }
Craig Mautner58547802013-03-05 08:23:53 -08001367
George Mount6ba042b2014-07-28 11:12:28 -07001368 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001369 mTranslucentActivityWaiting = r;
1370 mUndrawnActivitiesBelowTopTranslucent.clear();
1371 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1372 }
1373
1374 /**
1375 * Called as activities below the top translucent activity are redrawn. When the last one is
1376 * redrawn notify the top activity by calling
1377 * {@link Activity#onTranslucentConversionComplete}.
1378 *
1379 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1380 * occurred and the activity will be notified immediately.
1381 */
1382 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001383 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001384 if ((r == null)
1385 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1386 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1387 // The last undrawn activity below the top has just been drawn. If there is an
1388 // opaque activity at the top, notify it that it can become translucent safely now.
1389 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1390 mTranslucentActivityWaiting = null;
1391 mUndrawnActivitiesBelowTopTranslucent.clear();
1392 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1393
Craig Mautner71dd1b62014-02-18 15:48:52 -08001394 if (waitingActivity != null) {
1395 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1396 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1397 try {
1398 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1399 waitingActivity.appToken, r != null);
1400 } catch (RemoteException e) {
1401 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001402 }
1403 }
1404 }
1405 }
1406
Craig Mautnera61bc652013-10-28 15:43:18 -07001407 /** If any activities below the top running one are in the INITIALIZING state and they have a
1408 * starting window displayed then remove that starting window. It is possible that the activity
1409 * in this state will never resumed in which case that starting window will be orphaned. */
1410 void cancelInitializingActivities() {
1411 final ActivityRecord topActivity = topRunningActivityLocked(null);
1412 boolean aboveTop = true;
1413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1414 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1415 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1416 final ActivityRecord r = activities.get(activityNdx);
1417 if (aboveTop) {
1418 if (r == topActivity) {
1419 aboveTop = false;
1420 }
1421 continue;
1422 }
1423
1424 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1425 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1426 r.mStartingWindowShown = false;
1427 mWindowManager.removeAppStartingWindow(r.appToken);
1428 }
1429 }
1430 }
1431 }
1432
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 /**
1434 * Ensure that the top activity in the stack is resumed.
1435 *
1436 * @param prev The previously resumed activity, for when in the process
1437 * of pausing; can be null to call from elsewhere.
1438 *
1439 * @return Returns true if something is being resumed, or false if
1440 * nothing happened.
1441 */
1442 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001443 return resumeTopActivityLocked(prev, null);
1444 }
1445
1446 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001447 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1448
Craig Mautnerdf88d732014-01-27 09:21:32 -08001449 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001450 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001451 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001452 // Do not resume this stack if its parent is not resumed.
1453 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1454 return false;
1455 }
1456
Craig Mautnera61bc652013-10-28 15:43:18 -07001457 cancelInitializingActivities();
1458
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 // Find the first activity that is not finishing.
1460 ActivityRecord next = topRunningActivityLocked(null);
1461
1462 // Remember how we'll process this pause/resume situation, and ensure
1463 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001464 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1465 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466
Craig Mautner84984fa2014-06-19 11:19:20 -07001467 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 if (next == null) {
1469 // There are no more activities! Let's just start up the
1470 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001471 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001472 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001473 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001474 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001475 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1476 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1477 return isOnHomeDisplay() &&
1478 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 }
1480
1481 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001482
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001484 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1485 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 // Make sure we have executed any pending transitions, since there
1487 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001488 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001490 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001491 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001492 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 return false;
1494 }
1495
Craig Mautner525f3d92013-05-07 14:01:50 -07001496 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001497 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001498 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001499 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001500 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001501 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001502 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001503 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001504 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001505 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001506 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001507 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001508 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1509 "resumeTopActivityLocked: Launching home next");
1510 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001511 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1512 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1513 return isOnHomeDisplay() &&
1514 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001515 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001516 }
1517
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001518 // If we are sleeping, and there is no resumed activity, and the top
1519 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001520 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001521 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001522 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 // Make sure we have executed any pending transitions, since there
1524 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001525 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001527 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001528 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001529 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 return false;
1531 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001532
1533 // Make sure that the user who owns this activity is started. If not,
1534 // we will just leave it as is because someone should be bringing
1535 // another user's activities to the top of the stack.
1536 if (mService.mStartedUsers.get(next.userId) == null) {
1537 Slog.w(TAG, "Skipping resume of top activity " + next
1538 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001539 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001540 return false;
1541 }
1542
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 // The activity may be waiting for stop, but that is no longer
1544 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001545 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001546 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001547 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001548 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549
1550 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1551
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001552 // If we are currently pausing an activity, then don't do anything
1553 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001554 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001555 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1556 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001557 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001558 return false;
1559 }
1560
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001561 // Okay we are now going to start a switch, to 'next'. We may first
1562 // have to pause the current activity, but this is an important point
1563 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001564 // XXX "App Redirected" dialog is getting too many false positives
1565 // at this point, so turn off for now.
1566 if (false) {
1567 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1568 long now = SystemClock.uptimeMillis();
1569 final boolean inTime = mLastStartedActivity.startTime != 0
1570 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1571 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1572 final int nextUid = next.info.applicationInfo.uid;
1573 if (inTime && lastUid != nextUid
1574 && lastUid != next.launchedFromUid
1575 && mService.checkPermission(
1576 android.Manifest.permission.STOP_APP_SWITCHES,
1577 -1, next.launchedFromUid)
1578 != PackageManager.PERMISSION_GRANTED) {
1579 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1580 } else {
1581 next.startTime = now;
1582 mLastStartedActivity = next;
1583 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001584 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001585 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001586 mLastStartedActivity = next;
1587 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001588 }
Craig Mautner58547802013-03-05 08:23:53 -08001589
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001590 // We need to start pausing the current activity so the top one
1591 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001592 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001593 if (mResumedActivity != null) {
1594 pausing = true;
1595 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001596 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001597 }
1598 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001599 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1600 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001601 // At this point we want to put the upcoming activity's process
1602 // at the top of the LRU list, since we know we will be needing it
1603 // very soon and it would be a waste to let it get killed if it
1604 // happens to be sitting towards the end.
1605 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001606 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001607 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001608 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001609 return true;
1610 }
1611
Christopher Tated3f175c2012-06-14 14:16:54 -07001612 // If the most recent activity was noHistory but was only stopped rather
1613 // than stopped+finished because the device went to sleep, we need to make
1614 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001615 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001616 !mLastNoHistoryActivity.finishing) {
1617 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1618 " on new resume");
1619 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1620 null, "no-history", false);
1621 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001622 }
1623
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 if (prev != null && prev != next) {
1625 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1626 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001627 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001628 if (DEBUG_SWITCH) Slog.v(
1629 TAG, "Resuming top, waiting visible to hide: " + prev);
1630 } else {
1631 // The next activity is already visible, so hide the previous
1632 // activity's windows right now so we can show the new one ASAP.
1633 // We only do this if the previous is finishing, which should mean
1634 // it is on top of the one being resumed so hiding it quickly
1635 // is good. Otherwise, we want to do the normal route of allowing
1636 // the resumed activity to be shown so we can decide if the
1637 // previous should actually be hidden depending on whether the
1638 // new one is found to be full-screen or not.
1639 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001640 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1642 + prev + ", waitingVisible="
1643 + (prev != null ? prev.waitingVisible : null)
1644 + ", nowVisible=" + next.nowVisible);
1645 } else {
1646 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1647 + prev + ", waitingVisible="
1648 + (prev != null ? prev.waitingVisible : null)
1649 + ", nowVisible=" + next.nowVisible);
1650 }
1651 }
1652 }
1653
Dianne Hackborne7f97212011-02-24 14:40:20 -08001654 // Launching this app's activity, make sure the app is no longer
1655 // considered stopped.
1656 try {
1657 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001658 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001659 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001660 } catch (IllegalArgumentException e) {
1661 Slog.w(TAG, "Failed trying to unstop package "
1662 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001663 }
1664
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 // We are starting up the next activity, so tell the window manager
1666 // that the previous one will be hidden soon. This way it can know
1667 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001668 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001669 if (prev != null) {
1670 if (prev.finishing) {
1671 if (DEBUG_TRANSITION) Slog.v(TAG,
1672 "Prepare close transition: prev=" + prev);
1673 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001674 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001675 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001676 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001677 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001678 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1679 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001681 mWindowManager.setAppWillBeHidden(prev.appToken);
1682 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001683 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001684 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001686 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001687 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001689 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001690 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001691 : next.mLaunchTaskBehind
1692 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1693 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 }
1695 }
1696 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001697 mWindowManager.setAppWillBeHidden(prev.appToken);
1698 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 }
Craig Mautner967212c2013-04-13 21:10:58 -07001700 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001701 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001703 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001704 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001706 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 }
1708 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001709
1710 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001711 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001712 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1713 if (opts != null) {
1714 resumeAnimOptions = opts.toBundle();
1715 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001716 next.applyOptionsLocked();
1717 } else {
1718 next.clearOptionsLocked();
1719 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001720
Craig Mautnercf910b02013-04-23 11:23:27 -07001721 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 if (next.app != null && next.app.thread != null) {
1723 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1724
1725 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001726 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001728 // schedule launch ticks to collect information about slow apps.
1729 next.startLaunchTickingLocked();
1730
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001731 ActivityRecord lastResumedActivity =
1732 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 ActivityState lastState = next.state;
1734
1735 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001736
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001737 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 next.state = ActivityState.RESUMED;
1739 mResumedActivity = next;
1740 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001741 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001742 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001744 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745
1746 // Have the window manager re-evaluate the orientation of
1747 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001748 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001749 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001750 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001751 mService.mConfiguration,
1752 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1753 if (config != null) {
1754 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001756 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001758
Craig Mautner525f3d92013-05-07 14:01:50 -07001759 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001760 // The configuration update wasn't able to keep the existing
1761 // instance of the activity, and instead started a new one.
1762 // We should be all done, but let's just make sure our activity
1763 // is still at the top and schedule another run if something
1764 // weird happened.
1765 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001766 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 "Activity config changed during resume: " + next
1768 + ", new next: " + nextNext);
1769 if (nextNext != next) {
1770 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001771 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001773 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001774 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001775 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001776 return true;
1777 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001778 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001779 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 }
Craig Mautner58547802013-03-05 08:23:53 -08001781
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 try {
1783 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001784 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 if (a != null) {
1786 final int N = a.size();
1787 if (!next.finishing && N > 0) {
1788 if (DEBUG_RESULTS) Slog.v(
1789 TAG, "Delivering results to " + next
1790 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001791 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792 }
1793 }
1794
1795 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001796 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
1798
1799 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001800 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001802
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001803 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001804 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001805 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001806 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001807 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001808 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001809 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001810
Craig Mautner0eea92c2013-05-16 13:35:39 -07001811 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812
Craig Mautnerac6f8432013-07-17 13:24:59 -07001813 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 } catch (Exception e) {
1815 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001816 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1817 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001819 if (lastStack != null) {
1820 lastStack.mResumedActivity = lastResumedActivity;
1821 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 Slog.i(TAG, "Restarting because process died: " + next);
1823 if (!next.hasBeenLaunched) {
1824 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001825 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1826 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001827 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001828 next.appToken, next.packageName, next.theme,
1829 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001830 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1831 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001832 }
George Mount2c92c972014-03-20 09:38:23 -07001833 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001834 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 return true;
1836 }
1837
1838 // From this point on, if something goes wrong there is no way
1839 // to recover the activity.
1840 try {
1841 next.visible = true;
1842 completeResumeLocked(next);
1843 } catch (Exception e) {
1844 // If any exception gets thrown, toss away this
1845 // activity and try the next one.
1846 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001847 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001848 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001849 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001850 return true;
1851 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001852 next.stopped = false;
1853
1854 } else {
1855 // Whoops, need to restart this activity!
1856 if (!next.hasBeenLaunched) {
1857 next.hasBeenLaunched = true;
1858 } else {
1859 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001860 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001861 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001862 mService.compatibilityInfoForPackageLocked(
1863 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001864 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001865 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001866 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001867 }
1868 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1869 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001870 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001871 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 }
1873
Craig Mautnercf910b02013-04-23 11:23:27 -07001874 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001875 return true;
1876 }
1877
Craig Mautnerac6f8432013-07-17 13:24:59 -07001878 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001879 // If this is being moved to the top by another activity or being launched from the home
1880 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001881 if (isOnHomeDisplay()) {
1882 ActivityStack lastStack = mStackSupervisor.getLastStack();
1883 final boolean fromHome = lastStack.isHomeStack();
1884 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001885 task.setTaskToReturnTo(fromHome
1886 ? lastStack.topTask() == null
1887 ? HOME_ACTIVITY_TYPE
1888 : lastStack.topTask().taskType
1889 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001890 }
1891 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001892 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001893 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001894
Craig Mautnerac6f8432013-07-17 13:24:59 -07001895 mTaskHistory.remove(task);
1896 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001897 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001898 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001899 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001900 while (--taskNdx >= 0) {
1901 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001902 break;
1903 }
1904 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001905 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001906 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001907 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001908 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001909 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001910
Craig Mautner8849a5e2013-04-02 16:41:03 -07001911 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001912 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001913 TaskRecord rTask = r.task;
1914 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001915 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1916 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001917 // Last activity in task had been removed or ActivityManagerService is reusing task.
1918 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001919 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001920 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001921 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001922 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001923 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001924 if (!newTask) {
1925 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001926 boolean startIt = true;
1927 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1928 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001929 if (task.getTopActivity() == null) {
1930 // All activities in task are finishing.
1931 continue;
1932 }
Craig Mautner70a86932013-02-28 22:37:44 -08001933 if (task == r.task) {
1934 // Here it is! Now, if this is not yet visible to the
1935 // user, then just add it without starting; it will
1936 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001937 if (!startIt) {
1938 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1939 + task, new RuntimeException("here").fillInStackTrace());
1940 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001941 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001942 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1943 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001944 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001945 r.userId, r.info.configChanges, task.voiceSession != null,
1946 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001948 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001950 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951 return;
1952 }
1953 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001954 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 startIt = false;
1956 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 }
1958 }
1959
1960 // Place a new activity at top of stack, so it is next to interact
1961 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001962
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001963 // If we are not placing the new activity frontmost, we do not want
1964 // to deliver the onUserLeaving callback to the actual frontmost
1965 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001966 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001967 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001968 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1969 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 }
Craig Mautner70a86932013-02-28 22:37:44 -08001971
1972 task = r.task;
1973
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001975 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001976 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001977 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001978 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001979
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001980 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001981 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001982 // We want to show the starting preview window if we are
1983 // switching to a new task, or the next activity's process is
1984 // not currently running.
1985 boolean showStartingIcon = newTask;
1986 ProcessRecord proc = r.app;
1987 if (proc == null) {
1988 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1989 }
1990 if (proc == null || proc.thread == null) {
1991 showStartingIcon = true;
1992 }
1993 if (DEBUG_TRANSITION) Slog.v(TAG,
1994 "Prepare open transition: starting " + r);
1995 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001996 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001999 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002000 ? r.mLaunchTaskBehind
2001 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2002 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002003 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002004 mNoAnimActivities.remove(r);
2005 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002006 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002007 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002008 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002009 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002010 boolean doShow = true;
2011 if (newTask) {
2012 // Even though this activity is starting fresh, we still need
2013 // to reset it to make sure we apply affinities to move any
2014 // existing activities from other tasks in to it.
2015 // If the caller has requested that the target task be
2016 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002017 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018 resetTaskIfNeededLocked(r, r);
2019 doShow = topRunningNonDelayedActivityLocked(null) == r;
2020 }
George Mount70778d72014-07-01 16:33:45 -07002021 } else if (options != null && new ActivityOptions(options).getAnimationType()
2022 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2023 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002025 if (r.mLaunchTaskBehind) {
2026 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2027 // tell WindowManager that r is visible even though it is at the back of the stack.
2028 mWindowManager.setAppVisibility(r.appToken, true);
2029 ensureActivitiesVisibleLocked(null, 0);
2030 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002031 // Figure out if we are transitioning from another activity that is
2032 // "has the same starting icon" as the next one. This allows the
2033 // window manager to keep the previous window it had previously
2034 // created, if it still had one.
2035 ActivityRecord prev = mResumedActivity;
2036 if (prev != null) {
2037 // We don't want to reuse the previous starting preview if:
2038 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002039 if (prev.task != r.task) {
2040 prev = null;
2041 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002042 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002043 else if (prev.nowVisible) {
2044 prev = null;
2045 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002047 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002048 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002049 mService.compatibilityInfoForPackageLocked(
2050 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002051 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002052 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002053 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 }
2055 } else {
2056 // If this is the first activity, don't do any fancy animations,
2057 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002058 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002059 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002060 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002061 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002062 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002063 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 }
2065 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002066 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 }
2068
2069 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002070 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 }
2072 }
2073
Dianne Hackbornbe707852011-11-11 14:32:10 -08002074 final void validateAppTokensLocked() {
2075 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002076 mValidateAppTokens.ensureCapacity(numActivities());
2077 final int numTasks = mTaskHistory.size();
2078 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2079 TaskRecord task = mTaskHistory.get(taskNdx);
2080 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002081 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002082 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002083 }
Craig Mautner000f0022013-02-26 15:04:29 -08002084 TaskGroup group = new TaskGroup();
2085 group.taskId = task.taskId;
2086 mValidateAppTokens.add(group);
2087 final int numActivities = activities.size();
2088 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2089 final ActivityRecord r = activities.get(activityNdx);
2090 group.tokens.add(r.appToken);
2091 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002092 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002093 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002094 }
2095
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 /**
2097 * Perform a reset of the given task, if needed as part of launching it.
2098 * Returns the new HistoryRecord at the top of the task.
2099 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002100 /**
2101 * Helper method for #resetTaskIfNeededLocked.
2102 * We are inside of the task being reset... we'll either finish this activity, push it out
2103 * for another task, or leave it as-is.
2104 * @param task The task containing the Activity (taskTop) that might be reset.
2105 * @param forceReset
2106 * @return An ActivityOptions that needs to be processed.
2107 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002108 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002109 ActivityOptions topOptions = null;
2110
2111 int replyChainEnd = -1;
2112 boolean canMoveOptions = true;
2113
2114 // We only do this for activities that are not the root of the task (since if we finish
2115 // the root, we may no longer have the task!).
2116 final ArrayList<ActivityRecord> activities = task.mActivities;
2117 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002118 final int rootActivityNdx = task.findEffectiveRootIndex();
2119 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002120 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002121 if (target.frontOfTask)
2122 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002123
2124 final int flags = target.info.flags;
2125 final boolean finishOnTaskLaunch =
2126 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2127 final boolean allowTaskReparenting =
2128 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2129 final boolean clearWhenTaskReset =
2130 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2131
2132 if (!finishOnTaskLaunch
2133 && !clearWhenTaskReset
2134 && target.resultTo != null) {
2135 // If this activity is sending a reply to a previous
2136 // activity, we can't do anything with it now until
2137 // we reach the start of the reply chain.
2138 // XXX note that we are assuming the result is always
2139 // to the previous activity, which is almost always
2140 // the case but we really shouldn't count on.
2141 if (replyChainEnd < 0) {
2142 replyChainEnd = i;
2143 }
2144 } else if (!finishOnTaskLaunch
2145 && !clearWhenTaskReset
2146 && allowTaskReparenting
2147 && target.taskAffinity != null
2148 && !target.taskAffinity.equals(task.affinity)) {
2149 // If this activity has an affinity for another
2150 // task, then we need to move it out of here. We will
2151 // move it as far out of the way as possible, to the
2152 // bottom of the activity stack. This also keeps it
2153 // correctly ordered with any activities we previously
2154 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002155 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002156 final ActivityRecord bottom =
2157 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002158 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002159 if (bottom != null && target.taskAffinity != null
2160 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002161 // If the activity currently at the bottom has the
2162 // same task affinity as the one we are moving,
2163 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002164 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002165 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002166 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002167 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002168 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002169 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002170 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002171 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2172 + " out to new task " + target.task);
2173 }
2174
Craig Mautnere3a74d52013-02-22 14:14:58 -08002175 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002176 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002177
Craig Mautner525f3d92013-05-07 14:01:50 -07002178 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002179 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2180 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002181 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002182 if (p.finishing) {
2183 continue;
2184 }
2185
Craig Mautnere3a74d52013-02-22 14:14:58 -08002186 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002187 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002188 topOptions = p.takeOptionsLocked();
2189 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002190 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002191 }
2192 }
2193 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002194 + task + " adding to task=" + targetTask
2195 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002196 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2197 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002198 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002199 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002200
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002201 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002202 }
2203
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002204 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002205 if (VALIDATE_TOKENS) {
2206 validateAppTokensLocked();
2207 }
2208
2209 replyChainEnd = -1;
2210 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2211 // If the activity should just be removed -- either
2212 // because it asks for it, or the task should be
2213 // cleared -- then finish it and anything that is
2214 // part of its reply chain.
2215 int end;
2216 if (clearWhenTaskReset) {
2217 // In this case, we want to finish this activity
2218 // and everything above it, so be sneaky and pretend
2219 // like these are all in the reply chain.
2220 end = numActivities - 1;
2221 } else if (replyChainEnd < 0) {
2222 end = i;
2223 } else {
2224 end = replyChainEnd;
2225 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002226 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002227 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002228 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002229 if (p.finishing) {
2230 continue;
2231 }
2232 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002233 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002234 topOptions = p.takeOptionsLocked();
2235 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002236 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002237 }
2238 }
Craig Mautner58547802013-03-05 08:23:53 -08002239 if (DEBUG_TASKS) Slog.w(TAG,
2240 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002241 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002242 end--;
2243 srcPos--;
2244 }
2245 }
2246 replyChainEnd = -1;
2247 } else {
2248 // If we were in the middle of a chain, well the
2249 // activity that started it all doesn't want anything
2250 // special, so leave it all as-is.
2251 replyChainEnd = -1;
2252 }
2253 }
2254
2255 return topOptions;
2256 }
2257
2258 /**
2259 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2260 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2261 * @param affinityTask The task we are looking for an affinity to.
2262 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2263 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2264 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2265 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002266 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002267 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002268 int replyChainEnd = -1;
2269 final int taskId = task.taskId;
2270 final String taskAffinity = task.affinity;
2271
2272 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2273 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002274 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2275
2276 // Do not operate on or below the effective root Activity.
2277 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002278 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002279 if (target.frontOfTask)
2280 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002281
2282 final int flags = target.info.flags;
2283 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2284 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2285
2286 if (target.resultTo != null) {
2287 // If this activity is sending a reply to a previous
2288 // activity, we can't do anything with it now until
2289 // we reach the start of the reply chain.
2290 // XXX note that we are assuming the result is always
2291 // to the previous activity, which is almost always
2292 // the case but we really shouldn't count on.
2293 if (replyChainEnd < 0) {
2294 replyChainEnd = i;
2295 }
2296 } else if (topTaskIsHigher
2297 && allowTaskReparenting
2298 && taskAffinity != null
2299 && taskAffinity.equals(target.taskAffinity)) {
2300 // This activity has an affinity for our task. Either remove it if we are
2301 // clearing or move it over to our task. Note that
2302 // we currently punt on the case where we are resetting a
2303 // task that is not at the top but who has activities above
2304 // with an affinity to it... this is really not a normal
2305 // case, and we will need to later pull that task to the front
2306 // and usually at that point we will do the reset and pick
2307 // up those remaining activities. (This only happens if
2308 // someone starts an activity in a new task from an activity
2309 // in a task that is not currently on top.)
2310 if (forceReset || finishOnTaskLaunch) {
2311 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2312 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2313 for (int srcPos = start; srcPos >= i; --srcPos) {
2314 final ActivityRecord p = activities.get(srcPos);
2315 if (p.finishing) {
2316 continue;
2317 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002318 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002319 }
2320 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002321 if (taskInsertionPoint < 0) {
2322 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002323
Craig Mautner77878772013-03-04 19:46:24 -08002324 }
Craig Mautner77878772013-03-04 19:46:24 -08002325
2326 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2327 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2328 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2329 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002330 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002331 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002332 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002333
Craig Mautnere3a74d52013-02-22 14:14:58 -08002334 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2335 + " to stack at " + task,
2336 new RuntimeException("here").fillInStackTrace());
2337 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2338 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002339 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002340 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002341 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002342 if (VALIDATE_TOKENS) {
2343 validateAppTokensLocked();
2344 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002345
2346 // Now we've moved it in to place... but what if this is
2347 // a singleTop activity and we have put it on top of another
2348 // instance of the same activity? Then we drop the instance
2349 // below so it remains singleTop.
2350 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2351 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002352 int targetNdx = taskActivities.indexOf(target);
2353 if (targetNdx > 0) {
2354 ActivityRecord p = taskActivities.get(targetNdx - 1);
2355 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002356 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2357 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002358 }
2359 }
2360 }
2361 }
2362
2363 replyChainEnd = -1;
2364 }
2365 }
Craig Mautner77878772013-03-04 19:46:24 -08002366 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002367 }
2368
Craig Mautner8849a5e2013-04-02 16:41:03 -07002369 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002370 ActivityRecord newActivity) {
2371 boolean forceReset =
2372 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2373 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2374 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2375 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2376 forceReset = true;
2377 }
2378 }
2379
2380 final TaskRecord task = taskTop.task;
2381
2382 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2383 * for remaining tasks. Used for later tasks to reparent to task. */
2384 boolean taskFound = false;
2385
2386 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2387 ActivityOptions topOptions = null;
2388
Craig Mautner77878772013-03-04 19:46:24 -08002389 // Preserve the location for reparenting in the new task.
2390 int reparentInsertionPoint = -1;
2391
Craig Mautnere3a74d52013-02-22 14:14:58 -08002392 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2393 final TaskRecord targetTask = mTaskHistory.get(i);
2394
2395 if (targetTask == task) {
2396 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2397 taskFound = true;
2398 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002399 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2400 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002401 }
2402 }
2403
Craig Mautner70a86932013-02-28 22:37:44 -08002404 int taskNdx = mTaskHistory.indexOf(task);
2405 do {
2406 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2407 } while (taskTop == null && taskNdx >= 0);
2408
Craig Mautnere3a74d52013-02-22 14:14:58 -08002409 if (topOptions != null) {
2410 // If we got some ActivityOptions from an activity on top that
2411 // was removed from the task, propagate them to the new real top.
2412 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002413 taskTop.updateOptionsLocked(topOptions);
2414 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002415 topOptions.abort();
2416 }
2417 }
2418
2419 return taskTop;
2420 }
2421
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2423 String resultWho, int requestCode, int resultCode, Intent data) {
2424
2425 if (callingUid > 0) {
2426 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002427 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002428 }
2429
2430 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2431 + " : who=" + resultWho + " req=" + requestCode
2432 + " res=" + resultCode + " data=" + data);
2433 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2434 try {
2435 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2436 list.add(new ResultInfo(resultWho, requestCode,
2437 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002438 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 return;
2440 } catch (Exception e) {
2441 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2442 }
2443 }
2444
2445 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2446 }
2447
Craig Mautner04f0b702013-10-22 12:31:01 -07002448 private void adjustFocusedActivityLocked(ActivityRecord r) {
2449 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2450 ActivityRecord next = topRunningActivityLocked(null);
2451 if (next != r) {
2452 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002453 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2454 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002455 }
2456 }
Winson Chung648c83b2014-04-28 15:11:56 -07002457 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2458 if (top != null) {
2459 mService.setFocusedActivityLocked(top);
2460 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002461 }
2462 }
2463
Craig Mautnerf3333272013-04-22 10:55:53 -07002464 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2466 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2467 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2468 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002469 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002470 if (DEBUG_STATES) {
2471 Slog.d(TAG, "no-history finish of " + r);
2472 }
2473 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002474 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002475 } else {
2476 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2477 + " on stop because we're just sleeping");
2478 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002479 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002480 }
2481
2482 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002483 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002484 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002485 try {
2486 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002487 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2488 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 r.state = ActivityState.STOPPING;
2490 if (DEBUG_VISBILITY) Slog.v(
2491 TAG, "Stopping visible=" + r.visible + " for " + r);
2492 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002493 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002494 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002495 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002496 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002497 r.setSleeping(true);
2498 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002499 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002500 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002501 } catch (Exception e) {
2502 // Maybe just ignore exceptions here... if the process
2503 // has crashed, our death notification will clean things
2504 // up.
2505 Slog.w(TAG, "Exception thrown during pause", e);
2506 // Just in case, assume it to be stopped.
2507 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002508 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002509 r.state = ActivityState.STOPPED;
2510 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002511 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512 }
2513 }
2514 }
2515 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002516
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002517 /**
2518 * @return Returns true if the activity is being finished, false if for
2519 * some reason it is being left as-is.
2520 */
2521 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002522 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002523 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002524 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002525 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002526 + ", result=" + resultCode + ", data=" + resultData
2527 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002528 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002529 return false;
2530 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531
Craig Mautnerd44711d2013-02-23 11:24:36 -08002532 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 return true;
2534 }
2535
Craig Mautnerd2328952013-03-05 12:46:26 -08002536 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002537 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2538 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2539 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2540 ActivityRecord r = activities.get(activityNdx);
2541 if (r.resultTo == self && r.requestCode == requestCode) {
2542 if ((r.resultWho == null && resultWho == null) ||
2543 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2544 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2545 false);
2546 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002547 }
2548 }
2549 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002550 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002551 }
2552
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002553 final void finishTopRunningActivityLocked(ProcessRecord app) {
2554 ActivityRecord r = topRunningActivityLocked(null);
2555 if (r != null && r.app == app) {
2556 // If the top running activity is from this crashing
2557 // process, then terminate it to avoid getting in a loop.
2558 Slog.w(TAG, " Force finishing activity "
2559 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002560 int taskNdx = mTaskHistory.indexOf(r.task);
2561 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002562 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002563 // Also terminate any activities below it that aren't yet
2564 // stopped, to avoid a situation where one will get
2565 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002566 --activityNdx;
2567 if (activityNdx < 0) {
2568 do {
2569 --taskNdx;
2570 if (taskNdx < 0) {
2571 break;
2572 }
2573 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2574 } while (activityNdx < 0);
2575 }
2576 if (activityNdx >= 0) {
2577 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002578 if (r.state == ActivityState.RESUMED
2579 || r.state == ActivityState.PAUSING
2580 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002581 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002582 Slog.w(TAG, " Force finishing activity "
2583 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002584 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002585 }
2586 }
2587 }
2588 }
2589 }
2590
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002591 final void finishVoiceTask(IVoiceInteractionSession session) {
2592 IBinder sessionBinder = session.asBinder();
2593 boolean didOne = false;
2594 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2595 TaskRecord tr = mTaskHistory.get(taskNdx);
2596 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2597 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2598 ActivityRecord r = tr.mActivities.get(activityNdx);
2599 if (!r.finishing) {
2600 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2601 false);
2602 didOne = true;
2603 }
2604 }
2605 }
2606 }
2607 if (didOne) {
2608 mService.updateOomAdjLocked();
2609 }
2610 }
2611
Craig Mautnerd2328952013-03-05 12:46:26 -08002612 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002613 ArrayList<ActivityRecord> activities = r.task.mActivities;
2614 for (int index = activities.indexOf(r); index >= 0; --index) {
2615 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002616 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002617 break;
2618 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002619 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002620 }
2621 return true;
2622 }
2623
Dianne Hackborn5c607432012-02-28 14:44:19 -08002624 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2625 // send the result
2626 ActivityRecord resultTo = r.resultTo;
2627 if (resultTo != null) {
2628 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2629 + " who=" + r.resultWho + " req=" + r.requestCode
2630 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002631 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002632 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002633 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002634 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002635 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002636 if (r.info.applicationInfo.uid > 0) {
2637 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2638 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002639 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002640 }
2641 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2642 resultData);
2643 r.resultTo = null;
2644 }
2645 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2646
2647 // Make sure this HistoryRecord is not holding on to other resources,
2648 // because clients have remote IPC references to this object so we
2649 // can't assume that will go away and want to avoid circular IPC refs.
2650 r.results = null;
2651 r.pendingResults = null;
2652 r.newIntents = null;
2653 r.icicle = null;
2654 }
2655
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 /**
2657 * @return Returns true if this activity has been removed from the history
2658 * list, or false if it is still in the list and will be removed later.
2659 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002660 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2661 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 if (r.finishing) {
2663 Slog.w(TAG, "Duplicate finish request for " + r);
2664 return false;
2665 }
2666
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002667 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002668 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002669 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002670 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002671 task.taskId, r.shortComponentName, reason);
2672 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002673 final int index = activities.indexOf(r);
2674 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002675 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002676 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002677 // If the caller asked that this activity (and all above it)
2678 // be cleared when the task is reset, don't lose that information,
2679 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002680 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002681 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 }
2683 }
2684
2685 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002686
2687 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688
Dianne Hackborn5c607432012-02-28 14:44:19 -08002689 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002690
Craig Mautnerde4ef022013-04-07 19:01:33 -07002691 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002692 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002693 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002695 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002696 ? AppTransition.TRANSIT_TASK_CLOSE
2697 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002698
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002700 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002701
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002702 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002703 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2704 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2705 startPausingLocked(false, false);
2706 }
2707
Craig Mautneraea74a52014-03-08 14:23:10 -08002708 if (endTask) {
2709 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2710 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 } else if (r.state != ActivityState.PAUSING) {
2712 // If the activity is PAUSING, we will complete the finish once
2713 // it is done pausing; else we can just directly finish it here.
2714 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002715 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 } else {
2717 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2718 }
2719
2720 return false;
2721 }
2722
Craig Mautnerf3333272013-04-22 10:55:53 -07002723 static final int FINISH_IMMEDIATELY = 0;
2724 static final int FINISH_AFTER_PAUSE = 1;
2725 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002726
Craig Mautnerf3333272013-04-22 10:55:53 -07002727 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 // First things first: if this activity is currently visible,
2729 // and the resumed activity is not yet visible, then hold off on
2730 // finishing until the resumed one becomes visible.
2731 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002732 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2733 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002734 if (mStackSupervisor.mStoppingActivities.size() > 3
2735 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 // If we already have a few activities waiting to stop,
2737 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002738 // them out. Or if r is the last of activity of the last task the stack
2739 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002740 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002741 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002742 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 }
2744 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002745 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2746 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002747 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002748 if (oomAdj) {
2749 mService.updateOomAdjLocked();
2750 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002751 return r;
2752 }
2753
2754 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002755 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002756 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002757 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002758 if (mResumedActivity == r) {
2759 mResumedActivity = null;
2760 }
2761 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002762 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 r.state = ActivityState.FINISHING;
2764
2765 if (mode == FINISH_IMMEDIATELY
2766 || prevState == ActivityState.STOPPED
2767 || prevState == ActivityState.INITIALIZING) {
2768 // If this activity is already stopped, we can just finish
2769 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002770 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002771 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002772 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002773 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002774 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002775 if (DEBUG_CONTAINERS) Slog.d(TAG,
2776 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2777 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002778 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002780
2781 // Need to go through the full pause cycle to get this
2782 // activity into the stopped state and then finish it.
2783 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002784 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002785 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002786 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 return r;
2788 }
2789
Craig Mautneree36c772014-07-16 14:56:05 -07002790 void finishAllActivitiesLocked(boolean immediately) {
2791 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002792 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2793 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2794 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2795 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002796 noActivitiesInStack = false;
2797 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002798 continue;
2799 }
Craig Mautneree36c772014-07-16 14:56:05 -07002800 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002801 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2802 }
2803 }
Craig Mautneree36c772014-07-16 14:56:05 -07002804 if (noActivitiesInStack) {
2805 mActivityContainer.onTaskListEmptyLocked();
2806 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002807 }
2808
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002809 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2810 // Basic case: for simple app-centric recents, we need to recreate
2811 // the task if the affinity has changed.
2812 if (srec == null || srec.task.affinity == null ||
2813 !srec.task.affinity.equals(destAffinity)) {
2814 return true;
2815 }
2816 // Document-centric case: an app may be split in to multiple documents;
2817 // they need to re-create their task if this current activity is the root
2818 // of a document, unless simply finishing it will return them to the the
2819 // correct app behind.
2820 if (srec.frontOfTask && srec.task != null) {
2821 // Okay, this activity is at the root of its task. What to do, what to do...
2822 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2823 // Finishing won't return to an application, so we need to recreate.
2824 return true;
2825 }
2826 // We now need to get the task below it to determine what to do.
2827 int taskIdx = mTaskHistory.indexOf(srec.task);
2828 if (taskIdx <= 0) {
2829 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2830 return false;
2831 }
2832 if (taskIdx == 0) {
2833 // At the bottom of the stack, nothing to go back to.
2834 return true;
2835 }
2836 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2837 if (!srec.task.affinity.equals(prevTask.affinity)) {
2838 // These are different apps, so need to recreate.
2839 return true;
2840 }
2841 }
2842 return false;
2843 }
2844
Craig Mautnerd2328952013-03-05 12:46:26 -08002845 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002846 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002847 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002848 final TaskRecord task = srec.task;
2849 final ArrayList<ActivityRecord> activities = task.mActivities;
2850 final int start = activities.indexOf(srec);
2851 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002852 return false;
2853 }
2854 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002855 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002856 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002857 final ComponentName dest = destIntent.getComponent();
2858 if (start > 0 && dest != null) {
2859 for (int i = finishTo; i >= 0; i--) {
2860 ActivityRecord r = activities.get(i);
2861 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002862 r.info.name.equals(dest.getClassName())) {
2863 finishTo = i;
2864 parent = r;
2865 foundParentInTask = true;
2866 break;
2867 }
2868 }
2869 }
2870
2871 IActivityController controller = mService.mController;
2872 if (controller != null) {
2873 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2874 if (next != null) {
2875 // ask watcher if this is allowed
2876 boolean resumeOK = true;
2877 try {
2878 resumeOK = controller.activityResuming(next.packageName);
2879 } catch (RemoteException e) {
2880 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002881 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002882 }
2883
2884 if (!resumeOK) {
2885 return false;
2886 }
2887 }
2888 }
2889 final long origId = Binder.clearCallingIdentity();
2890 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002891 ActivityRecord r = activities.get(i);
2892 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002893 // Only return the supplied result for the first activity finished
2894 resultCode = Activity.RESULT_CANCELED;
2895 resultData = null;
2896 }
2897
2898 if (parent != null && foundParentInTask) {
2899 final int parentLaunchMode = parent.info.launchMode;
2900 final int destIntentFlags = destIntent.getFlags();
2901 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2902 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2903 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2904 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2905 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2906 } else {
2907 try {
2908 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2909 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002910 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002911 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002912 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002913 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002914 foundParentInTask = res == ActivityManager.START_SUCCESS;
2915 } catch (RemoteException e) {
2916 foundParentInTask = false;
2917 }
2918 requestFinishActivityLocked(parent.appToken, resultCode,
2919 resultData, "navigate-up", true);
2920 }
2921 }
2922 Binder.restoreCallingIdentity(origId);
2923 return foundParentInTask;
2924 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 /**
2926 * Perform the common clean-up of an activity record. This is called both
2927 * as part of destroyActivityLocked() (when destroying the client-side
2928 * representation) and cleaning things up as a result of its hosting
2929 * processing going away, in which case there is no remaining client-side
2930 * state to destroy so only the cleanup here is needed.
2931 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002932 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2933 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 if (mResumedActivity == r) {
2935 mResumedActivity = null;
2936 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002937 if (mPausingActivity == r) {
2938 mPausingActivity = null;
2939 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002940 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941
2942 r.configDestroy = false;
2943 r.frozenBeforeDestroy = false;
2944
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002945 if (setState) {
2946 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2947 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002948 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002949 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002950 }
2951
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 // Make sure this record is no longer in the pending finishes list.
2953 // This could happen, for example, if we are trimming activities
2954 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002955 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002956 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002957
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002958 // Remove any pending results.
2959 if (r.finishing && r.pendingResults != null) {
2960 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2961 PendingIntentRecord rec = apr.get();
2962 if (rec != null) {
2963 mService.cancelIntentSenderLocked(rec, false);
2964 }
2965 }
2966 r.pendingResults = null;
2967 }
2968
2969 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002970 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971 }
2972
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002973 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002974 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07002975 if (getVisibleBehindActivity() == r) {
2976 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002977 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002978 }
2979
2980 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002981 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002983 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002984 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002985 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002986 }
2987
Craig Mautner04a0ea62014-01-13 12:51:26 -08002988 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002989 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002990 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2991 r.makeFinishing();
2992 if (DEBUG_ADD_REMOVE) {
2993 RuntimeException here = new RuntimeException("here");
2994 here.fillInStackTrace();
2995 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002997 r.takeFromHistory();
2998 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002999 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003000 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003001 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003002 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003003 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003004 if (VALIDATE_TOKENS) {
3005 validateAppTokensLocked();
3006 }
Craig Mautner312ba862014-02-10 17:55:01 -08003007 final TaskRecord task = r.task;
3008 if (task != null && task.removeActivity(r)) {
3009 if (DEBUG_STACK) Slog.i(TAG,
3010 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003011 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3012 task.isOverHomeStack()) {
3013 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08003014 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003015 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08003016 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003017 cleanUpActivityServicesLocked(r);
3018 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003019 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 /**
3022 * Perform clean-up of service connections in an activity record.
3023 */
3024 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3025 // Throw away any services that have been bound by this activity.
3026 if (r.connections != null) {
3027 Iterator<ConnectionRecord> it = r.connections.iterator();
3028 while (it.hasNext()) {
3029 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003030 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 }
3032 r.connections = null;
3033 }
3034 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003035
Craig Mautneree2e45a2014-06-27 12:10:03 -07003036 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003037 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003038 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003039 mHandler.sendMessage(msg);
3040 }
3041
Craig Mautneree2e45a2014-06-27 12:10:03 -07003042 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003043 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003044 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003045 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3046 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3047 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3048 final ActivityRecord r = activities.get(activityNdx);
3049 if (r.finishing) {
3050 continue;
3051 }
3052 if (r.fullscreen) {
3053 lastIsOpaque = true;
3054 }
3055 if (owner != null && r.app != owner) {
3056 continue;
3057 }
3058 if (!lastIsOpaque) {
3059 continue;
3060 }
3061 // We can destroy this one if we have its icicle saved and
3062 // it is not in the process of pausing/stopping/finishing.
3063 if (r.app != null && r != mResumedActivity && r != mPausingActivity
3064 && r.haveState && !r.visible && r.stopped
3065 && r.state != ActivityState.DESTROYING
3066 && r.state != ActivityState.DESTROYED) {
3067 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3068 + " resumed=" + mResumedActivity
3069 + " pausing=" + mPausingActivity);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003070 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003071 activityRemoved = true;
3072 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003073 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003074 }
3075 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003076 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003077 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003078 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003079 }
3080
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081 /**
3082 * Destroy the current CLIENT SIDE instance of an activity. This may be
3083 * called both when actually finishing an activity, or when performing
3084 * a configuration switch where we destroy the current client-side object
3085 * but then create a new client-side object for this same HistoryRecord.
3086 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003087 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003088 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003089 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003090 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3091 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003092 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003093 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094
3095 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003096
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003097 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003098
3099 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003101 if (hadApp) {
3102 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003103 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3105 mService.mHeavyWeightProcess = null;
3106 mService.mHandler.sendEmptyMessage(
3107 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3108 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003109 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003110 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003111 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003112 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003113 }
3114 }
3115
3116 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 try {
3119 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003120 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121 r.configChangeFlags);
3122 } catch (Exception e) {
3123 // We can just ignore exceptions here... if the process
3124 // has crashed, our death notification will clean things
3125 // up.
3126 //Slog.w(TAG, "Exception thrown during finish", e);
3127 if (r.finishing) {
3128 removeActivityFromHistoryLocked(r);
3129 removedFromHistory = true;
3130 skipDestroy = true;
3131 }
3132 }
3133
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003134 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003135
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003136 // If the activity is finishing, we need to wait on removing it
3137 // from the list to give it a chance to do its cleanup. During
3138 // that time it may make calls back with its token so we need to
3139 // be able to find it on the list and so we don't want to remove
3140 // it from the list yet. Otherwise, we can just immediately put
3141 // it in the destroyed state since we are not removing it from the
3142 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003144 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3145 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003147 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003148 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3149 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003150 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003152 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003153 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 }
3155 } else {
3156 // remove this record from the history.
3157 if (r.finishing) {
3158 removeActivityFromHistoryLocked(r);
3159 removedFromHistory = true;
3160 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003161 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003163 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003164 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 }
3166 }
3167
3168 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003170 if (!mLRUActivities.remove(r) && hadApp) {
3171 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3172 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 return removedFromHistory;
3175 }
3176
Craig Mautnerd2328952013-03-05 12:46:26 -08003177 final void activityDestroyedLocked(IBinder token) {
3178 final long origId = Binder.clearCallingIdentity();
3179 try {
3180 ActivityRecord r = ActivityRecord.forToken(token);
3181 if (r != null) {
3182 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003184 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003185
3186 if (isInStackLocked(token) != null) {
3187 if (r.state == ActivityState.DESTROYING) {
3188 cleanUpActivityLocked(r, true, false);
3189 removeActivityFromHistoryLocked(r);
3190 }
3191 }
Craig Mautner05d29032013-05-03 13:40:13 -07003192 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003193 } finally {
3194 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 }
3196 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003197
Jose Lima4b6c6692014-08-12 17:41:12 -07003198 void releaseBackgroundResources() {
3199 if (hasVisibleBehindActivity() &&
3200 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3201 final ActivityRecord r = getVisibleBehindActivity();
3202 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3203 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003204 " thread=" + r.app.thread);
3205 if (r != null && r.app != null && r.app.thread != null) {
3206 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003207 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003208 } catch (RemoteException e) {
3209 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003210 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003211 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003212 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
3213 backgroundResourcesReleased(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003214 }
3215 }
3216 }
3217
Jose Lima4b6c6692014-08-12 17:41:12 -07003218 final void backgroundResourcesReleased(IBinder token) {
3219 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3220 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003221 if (r != null) {
3222 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003223 setVisibleBehindActivity(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003224 }
3225 mStackSupervisor.resumeTopActivitiesLocked();
3226 }
3227
Jose Lima4b6c6692014-08-12 17:41:12 -07003228 boolean hasVisibleBehindActivity() {
3229 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003230 }
3231
Jose Lima4b6c6692014-08-12 17:41:12 -07003232 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003233 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003234 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003235 }
3236 }
3237
Jose Lima4b6c6692014-08-12 17:41:12 -07003238 ActivityRecord getVisibleBehindActivity() {
3239 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003240 }
3241
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003242 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3243 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003245 if (DEBUG_CLEANUP) Slog.v(
3246 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 + " with " + i + " entries");
3248 while (i > 0) {
3249 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003250 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003251 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003252 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003253 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003254 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003255 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 }
3257 }
3258 }
3259
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003260 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3261 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003262 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3263 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003264 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3265 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003266 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003267 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003268 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3269 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003270
3271 boolean hasVisibleActivities = false;
3272
3273 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003274 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003275 if (DEBUG_CLEANUP) Slog.v(
3276 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003277 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3278 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3279 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3280 final ActivityRecord r = activities.get(activityNdx);
3281 --i;
3282 if (DEBUG_CLEANUP) Slog.v(
3283 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3284 if (r.app == app) {
3285 boolean remove;
3286 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3287 // Don't currently have state for the activity, or
3288 // it is finishing -- always remove it.
3289 remove = true;
3290 } else if (r.launchCount > 2 &&
3291 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3292 // We have launched this activity too many times since it was
3293 // able to run, so give up and remove it.
3294 remove = true;
3295 } else {
3296 // The process may be gone, but the activity lives on!
3297 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003298 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003299 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003300 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003301 RuntimeException here = new RuntimeException("here");
3302 here.fillInStackTrace();
3303 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3304 + ": haveState=" + r.haveState
3305 + " stateNotNeeded=" + r.stateNotNeeded
3306 + " finishing=" + r.finishing
3307 + " state=" + r.state, here);
3308 }
3309 if (!r.finishing) {
3310 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3311 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3312 r.userId, System.identityHashCode(r),
3313 r.task.taskId, r.shortComponentName,
3314 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003315 if (r.state == ActivityState.RESUMED) {
3316 mService.updateUsageStats(r, false);
3317 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003318 }
3319 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003320
Craig Mautner0247fc82013-02-28 14:32:06 -08003321 } else {
3322 // We have the current state for this activity, so
3323 // it can be restarted later when needed.
3324 if (localLOGV) Slog.v(
3325 TAG, "Keeping entry, setting app to null");
3326 if (r.visible) {
3327 hasVisibleActivities = true;
3328 }
3329 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3330 + r);
3331 r.app = null;
3332 r.nowVisible = false;
3333 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003334 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003335 "App died, clearing saved state of " + r);
3336 r.icicle = null;
3337 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003338 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003339
Craig Mautnerd2328952013-03-05 12:46:26 -08003340 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003341 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003342 }
3343 }
3344
3345 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003347
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003348 final void updateTransitLocked(int transit, Bundle options) {
3349 if (options != null) {
3350 ActivityRecord r = topRunningActivityLocked(null);
3351 if (r != null && r.state != ActivityState.RESUMED) {
3352 r.updateOptionsLocked(options);
3353 } else {
3354 ActivityOptions.abort(options);
3355 }
3356 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003357 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003358 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003359
Craig Mautner21d24a22014-04-23 11:45:37 -07003360 void updateTaskMovement(TaskRecord task, boolean toFront) {
3361 if (task.isPersistable) {
3362 task.mLastTimeMoved = System.currentTimeMillis();
3363 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3364 // recently will be most negative, tasks sent to the bottom before that will be less
3365 // negative. Similarly for recent tasks moved to the top which will be most positive.
3366 if (!toFront) {
3367 task.mLastTimeMoved *= -1;
3368 }
3369 }
3370 }
3371
Craig Mautner84984fa2014-06-19 11:19:20 -07003372 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003373 final int top = mTaskHistory.size() - 1;
3374 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3375 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003376 if (task.taskType == homeStackTaskType) {
3377 if (DEBUG_TASKS || DEBUG_STACK)
3378 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003379 mTaskHistory.remove(taskNdx);
3380 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003381 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003382 mWindowManager.moveTaskToTop(task.taskId);
3383 return;
3384 }
3385 }
3386 }
3387
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003388 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003389 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003390
Craig Mautner11bf9a52013-02-19 14:08:51 -08003391 final int numTasks = mTaskHistory.size();
3392 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003393 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003394 // nothing to do!
3395 if (reason != null &&
3396 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3397 ActivityOptions.abort(options);
3398 } else {
3399 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3400 }
3401 return;
3402 }
3403
Craig Mautnere0a38842013-12-16 16:14:02 -08003404 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003405
Craig Mautner11bf9a52013-02-19 14:08:51 -08003406 // Shift all activities with this task up to the top
3407 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003408 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003409
3410 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003411 if (reason != null &&
3412 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003413 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003414 ActivityRecord r = topRunningActivityLocked(null);
3415 if (r != null) {
3416 mNoAnimActivities.add(r);
3417 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003418 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003419 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003420 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003421 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003422
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003423 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003424
Craig Mautner05d29032013-05-03 13:40:13 -07003425 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003426 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003427
3428 if (VALIDATE_TOKENS) {
3429 validateAppTokensLocked();
3430 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003431 }
3432
3433 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003434 * Worker method for rearranging history stack. Implements the function of moving all
3435 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003436 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003437 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003438 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3439 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003440 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003441 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003442 * @return Returns true if the move completed, false if not.
3443 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003444 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003445 final TaskRecord tr = taskForIdLocked(taskId);
3446 if (tr == null) {
3447 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3448 return false;
3449 }
3450
3451 Slog.i(TAG, "moveTaskToBack: " + tr);
3452
3453 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003454
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003455 // If we have a watcher, preflight the move before committing to it. First check
3456 // for *other* available tasks, but if none are available, then try again allowing the
3457 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003458 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003459 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003460 if (next == null) {
3461 next = topRunningActivityLocked(null, 0);
3462 }
3463 if (next != null) {
3464 // ask watcher if this is allowed
3465 boolean moveOK = true;
3466 try {
3467 moveOK = mService.mController.activityResuming(next.packageName);
3468 } catch (RemoteException e) {
3469 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003470 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 }
3472 if (!moveOK) {
3473 return false;
3474 }
3475 }
3476 }
3477
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003478 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003479 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480
Craig Mautner11bf9a52013-02-19 14:08:51 -08003481 mTaskHistory.remove(tr);
3482 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003483 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003484
Craig Mautnerc8143c62013-09-03 12:15:57 -07003485 // There is an assumption that moving a task to the back moves it behind the home activity.
3486 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003487 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003488 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3489 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003490 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003491 break;
3492 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003493 if (taskNdx == 1) {
3494 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003495 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003496 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003497 }
3498
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003499 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003500 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3501 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502 ActivityRecord r = topRunningActivityLocked(null);
3503 if (r != null) {
3504 mNoAnimActivities.add(r);
3505 }
3506 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003507 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003509 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003510
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003511 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003512 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003513 }
3514
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003515 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003516 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
3517 final int taskToReturnTo = tr.getTaskToReturnTo();
3518 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3519 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003520 }
3521
Craig Mautner05d29032013-05-03 13:40:13 -07003522 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 return true;
3524 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003525
Craig Mautner8849a5e2013-04-02 16:41:03 -07003526 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003527 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003528 final Uri data = r.intent.getData();
3529 final String strData = data != null ? data.toSafeString() : null;
3530
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003532 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003534 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003535 }
3536
3537 /**
3538 * Make sure the given activity matches the current configuration. Returns
3539 * false if the activity had to be destroyed. Returns true if the
3540 * configuration is the same, or the activity will remain running as-is
3541 * for whatever reason. Ensures the HistoryRecord is updated with the
3542 * correct configuration and all other bookkeeping is handled.
3543 */
3544 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3545 int globalChanges) {
3546 if (mConfigWillChange) {
3547 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3548 "Skipping config check (will change): " + r);
3549 return true;
3550 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003551
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003552 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3553 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003554
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003555 // Short circuit: if the two configurations are the exact same
3556 // object (the common case), then there is nothing to do.
3557 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003558 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3560 "Configuration unchanged in " + r);
3561 return true;
3562 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003564 // We don't worry about activities that are finishing.
3565 if (r.finishing) {
3566 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3567 "Configuration doesn't matter in finishing " + r);
3568 r.stopFreezingScreenLocked(false);
3569 return true;
3570 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003571
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003572 // Okay we now are going to make this activity have the new config.
3573 // But then we need to figure out how it needs to deal with that.
3574 Configuration oldConfig = r.configuration;
3575 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003576
3577 // Determine what has changed. May be nothing, if this is a config
3578 // that has come back from the app after going idle. In that case
3579 // we just want to leave the official config object now in the
3580 // activity and do nothing else.
3581 final int changes = oldConfig.diff(newConfig);
3582 if (changes == 0 && !r.forceNewConfig) {
3583 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3584 "Configuration no differences in " + r);
3585 return true;
3586 }
3587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 // If the activity isn't currently running, just leave the new
3589 // configuration and it will pick that up next time it starts.
3590 if (r.app == null || r.app.thread == null) {
3591 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3592 "Configuration doesn't matter not running " + r);
3593 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003594 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003595 return true;
3596 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003597
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003598 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003599 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3600 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3601 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003602 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003603 + ", newConfig=" + newConfig);
3604 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003605 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003606 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3607 r.configChangeFlags |= changes;
3608 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003609 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003610 if (r.app == null || r.app.thread == null) {
3611 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003612 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003613 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003614 } else if (r.state == ActivityState.PAUSING) {
3615 // A little annoying: we are waiting for this activity to
3616 // finish pausing. Let's not do anything now, but just
3617 // flag that it needs to be restarted when done pausing.
3618 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003619 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003620 r.configDestroy = true;
3621 return true;
3622 } else if (r.state == ActivityState.RESUMED) {
3623 // Try to optimize this case: the configuration is changing
3624 // and we need to restart the top, resumed activity.
3625 // Instead of doing the normal handshaking, just say
3626 // "restart!".
3627 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003628 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003629 relaunchActivityLocked(r, r.configChangeFlags, true);
3630 r.configChangeFlags = 0;
3631 } else {
3632 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003633 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003634 relaunchActivityLocked(r, r.configChangeFlags, false);
3635 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003637
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003638 // All done... tell the caller we weren't able to keep this
3639 // activity around.
3640 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003642
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003643 // Default case: the activity can handle this new configuration, so
3644 // hand it over. Note that we don't need to give it the new
3645 // configuration, since we always send configuration changes to all
3646 // process when they happen so it can just use whatever configuration
3647 // it last got.
3648 if (r.app != null && r.app.thread != null) {
3649 try {
3650 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003651 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003652 } catch (RemoteException e) {
3653 // If process died, whatever.
3654 }
3655 }
3656 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003657
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658 return true;
3659 }
3660
Craig Mautnerc8143c62013-09-03 12:15:57 -07003661 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003662 int changes, boolean andResume) {
3663 List<ResultInfo> results = null;
3664 List<Intent> newIntents = null;
3665 if (andResume) {
3666 results = r.results;
3667 newIntents = r.newIntents;
3668 }
3669 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3670 + " with results=" + results + " newIntents=" + newIntents
3671 + " andResume=" + andResume);
3672 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003673 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003674 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003675
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003677
Craig Mautner34b73df2014-01-12 21:11:08 -08003678 mStackSupervisor.removeChildActivityContainers(r);
3679
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003680 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003681 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3682 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3683 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003684 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003685 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003686 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003687 // Note: don't need to call pauseIfSleepingLocked() here, because
3688 // the caller will only pass in 'andResume' if this activity is
3689 // currently resumed, which implies we aren't sleeping.
3690 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003691 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 }
3693
3694 if (andResume) {
3695 r.results = null;
3696 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003697 r.state = ActivityState.RESUMED;
3698 } else {
3699 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3700 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003701 }
3702
3703 return true;
3704 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003705
3706 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003707 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3708 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3709 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3710 final ActivityRecord r = activities.get(activityNdx);
3711 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003712 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003713 }
3714 if (r.fullscreen && !r.finishing) {
3715 return false;
3716 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003717 }
3718 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003719 final ActivityRecord r = ActivityRecord.forToken(token);
3720 if (r == null) {
3721 return false;
3722 }
3723 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3724 + " would have returned true for r=" + r);
3725 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003726 }
3727
3728 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003729 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3730 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3731 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3732 final ActivityRecord r = activities.get(activityNdx);
3733 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003734 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003735 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003736 }
3737 }
3738 }
3739
3740 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3741 boolean didSomething = false;
3742 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003743 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003744 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3745 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3746 int numActivities = activities.size();
3747 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3748 ActivityRecord r = activities.get(activityNdx);
3749 final boolean samePackage = r.packageName.equals(name)
3750 || (name == null && r.userId == userId);
3751 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3752 && (samePackage || r.task == lastTask)
3753 && (r.app == null || evenPersistent || !r.app.persistent)) {
3754 if (!doit) {
3755 if (r.finishing) {
3756 // If this activity is just finishing, then it is not
3757 // interesting as far as something to stop.
3758 continue;
3759 }
3760 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003761 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003762 if (r.isHomeActivity()) {
3763 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3764 Slog.i(TAG, "Skip force-stop again " + r);
3765 continue;
3766 } else {
3767 homeActivity = r.realActivity;
3768 }
3769 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003770 didSomething = true;
3771 Slog.i(TAG, " Force finishing activity " + r);
3772 if (samePackage) {
3773 if (r.app != null) {
3774 r.app.removed = true;
3775 }
3776 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003777 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003778 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003779 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3780 true)) {
3781 // r has been deleted from mActivities, accommodate.
3782 --numActivities;
3783 --activityNdx;
3784 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003785 }
3786 }
3787 }
3788 return didSomething;
3789 }
3790
Dianne Hackborn09233282014-04-30 11:33:59 -07003791 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003792 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003793 final TaskRecord task = mTaskHistory.get(taskNdx);
3794 ActivityRecord r = null;
3795 ActivityRecord top = null;
3796 int numActivities = 0;
3797 int numRunning = 0;
3798 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003799 if (activities.isEmpty()) {
3800 continue;
3801 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003802 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3803 continue;
3804 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003805 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3806 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003807
Craig Mautneraab647e2013-02-28 16:31:36 -08003808 // Initialize state for next task if needed.
3809 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3810 top = r;
3811 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003812 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003813
3814 // Add 'r' into the current task.
3815 numActivities++;
3816 if (r.app != null && r.app.thread != null) {
3817 numRunning++;
3818 }
3819
3820 if (localLOGV) Slog.v(
3821 TAG, r.intent.getComponent().flattenToShortString()
3822 + ": task=" + r.task);
3823 }
3824
3825 RunningTaskInfo ci = new RunningTaskInfo();
3826 ci.id = task.taskId;
3827 ci.baseActivity = r.intent.getComponent();
3828 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003829 ci.lastActiveTime = task.lastActiveTime;
3830
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003831 if (top.task != null) {
3832 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003833 }
3834 ci.numActivities = numActivities;
3835 ci.numRunning = numRunning;
3836 //System.out.println(
3837 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003838 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003839 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003840 }
3841
3842 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003843 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003844 if (DEBUG_SWITCH) Slog.d(
3845 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003846 if (top >= 0) {
3847 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3848 int activityTop = activities.size() - 1;
3849 if (activityTop > 0) {
3850 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3851 "unhandled-back", true);
3852 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003853 }
3854 }
3855
Craig Mautner6b74cb52013-09-27 17:02:21 -07003856 /**
3857 * Reset local parameters because an app's activity died.
3858 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003859 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003860 */
3861 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003862 if (mPausingActivity != null && mPausingActivity.app == app) {
3863 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3864 "App died while pausing: " + mPausingActivity);
3865 mPausingActivity = null;
3866 }
3867 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3868 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003869 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003870 }
3871
Craig Mautner19091252013-10-05 00:03:53 -07003872 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003873 }
3874
Craig Mautnercae015f2013-02-08 14:31:27 -08003875 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003876 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3877 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3878 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3879 final ActivityRecord r = activities.get(activityNdx);
3880 if (r.app == app) {
3881 Slog.w(TAG, " Force finishing activity "
3882 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07003883 // Force the destroy to skip right to removal.
3884 r.app = null;
3885 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003886 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003887 }
3888 }
3889 }
3890
Dianne Hackborn390517b2013-05-30 15:03:32 -07003891 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003892 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003893 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003894 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3895 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003896 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3897 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003898 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003899 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003900 if (printed) {
3901 header = null;
3902 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003903 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003904 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003905 }
3906
3907 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3908 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3909
3910 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003911 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3912 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003913 }
3914 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003915 final int top = mTaskHistory.size() - 1;
3916 if (top >= 0) {
3917 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3918 int listTop = list.size() - 1;
3919 if (listTop >= 0) {
3920 activities.add(list.get(listTop));
3921 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003922 }
3923 } else {
3924 ItemMatcher matcher = new ItemMatcher();
3925 matcher.build(name);
3926
Craig Mautneraab647e2013-02-28 16:31:36 -08003927 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3928 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3929 if (matcher.match(r1, r1.intent.getComponent())) {
3930 activities.add(r1);
3931 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003932 }
3933 }
3934 }
3935
3936 return activities;
3937 }
3938
3939 ActivityRecord restartPackage(String packageName) {
3940 ActivityRecord starting = topRunningActivityLocked(null);
3941
3942 // All activities that came from the package must be
3943 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003944 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3945 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3946 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3947 final ActivityRecord a = activities.get(activityNdx);
3948 if (a.info.packageName.equals(packageName)) {
3949 a.forceNewConfig = true;
3950 if (starting != null && a == starting && a.visible) {
3951 a.startFreezingScreenLocked(starting.app,
3952 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3953 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003954 }
3955 }
3956 }
3957
3958 return starting;
3959 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003960
Craig Mautner41db4a72014-05-07 17:20:56 -07003961 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003962 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003963 mWindowManager.removeTask(task.taskId);
3964 final ActivityRecord r = mResumedActivity;
3965 if (r != null && r.task == task) {
3966 mResumedActivity = null;
3967 }
3968
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003969 final int taskNdx = mTaskHistory.indexOf(task);
3970 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07003971 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
3972 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
3973 if (!nextTask.isOverHomeStack()) {
3974 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
3975 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003976 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003977 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003978 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07003979
3980 if (task.mActivities.isEmpty()) {
3981 final boolean isVoiceSession = task.voiceSession != null;
3982 if (isVoiceSession) {
3983 try {
3984 task.voiceSession.taskFinished(task.intent, task.taskId);
3985 } catch (RemoteException e) {
3986 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003987 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003988 if (task.autoRemoveFromRecents() || isVoiceSession) {
3989 // Task creator asked to remove this when done, or this task was a voice
3990 // interaction, so it should not remain on the recent tasks list.
3991 mService.mRecentTasks.remove(task);
Craig Mautnera228ae92014-07-09 05:44:55 -07003992 task.closeRecentsChain();
Craig Mautner41db4a72014-05-07 17:20:56 -07003993 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003994 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003995
3996 if (mTaskHistory.isEmpty()) {
3997 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3998 if (isOnHomeDisplay()) {
3999 mStackSupervisor.moveHomeStack(!isHomeStack());
4000 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004001 if (mStacks != null) {
4002 mStacks.remove(this);
4003 mStacks.add(0, this);
4004 }
Craig Mautnerd163e752014-06-13 17:18:47 -07004005 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004006 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004007 }
4008
Dianne Hackborn91097de2014-04-04 18:02:06 -07004009 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4010 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4011 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004012 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4013 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004014 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004015 return task;
4016 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004017
4018 ArrayList<TaskRecord> getAllTasks() {
4019 return new ArrayList<TaskRecord>(mTaskHistory);
4020 }
4021
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004022 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004023 task.stack = this;
4024 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004025 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004026 } else {
4027 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004028 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004029 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004030 if (!moving && task.voiceSession != null) {
4031 try {
4032 task.voiceSession.taskStarted(task.intent, task.taskId);
4033 } catch (RemoteException e) {
4034 }
4035 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004036 }
4037
4038 public int getStackId() {
4039 return mStackId;
4040 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004041
4042 @Override
4043 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004044 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4045 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004046 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004047}