blob: aeee3be9d2ab87bd76da695dc4a65eec2ba3bf7b [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 Mautneree2e45a2014-06-27 12:10:03 -070049import com.android.server.am.ActivityStackSupervisor.ActivityDisplay;
Craig Mautner4b71aa12012-12-27 17:20:01 -080050import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070052import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053
54import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070055import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070056import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080058import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080060import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080066import android.content.res.Resources;
67import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080068import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070070import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080071import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.Handler;
73import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090074import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070076import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.os.RemoteException;
78import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080079import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070080import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070081import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070084import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
Craig Mautnercae015f2013-02-08 14:31:27 -080086import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088import java.lang.ref.WeakReference;
89import java.util.ArrayList;
90import java.util.Iterator;
91import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080092import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093
94/**
95 * State and management of a single stack of activities.
96 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070097final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080098
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070099 // Ticks during which we check progress while waiting for an app to launch.
100 static final int LAUNCH_TICK = 500;
101
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700102 // How long we wait until giving up on the last activity to pause. This
103 // is short because it directly impacts the responsiveness of starting the
104 // next activity.
105 static final int PAUSE_TIMEOUT = 500;
106
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700107 // How long we wait for the activity to tell us it has stopped before
108 // giving up. This is a good amount of time because we really need this
109 // from the application in order to get its saved state.
110 static final int STOP_TIMEOUT = 10*1000;
111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112 // How long we wait until giving up on an activity telling us it has
113 // finished destroying itself.
114 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800117 // disabled.
118 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700120 // How long between activity launches that we consider safe to not warn
121 // the user about an unexpected activity being launched on top.
122 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124 // Set to false to disable the preview that is shown while a new activity
125 // is being started.
126 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800127
Craig Mautner5eda9b32013-07-02 11:58:16 -0700128 // How long to wait for all background Activities to redraw following a call to
129 // convertToTranslucent().
130 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
131
Craig Mautnera61bc652013-10-28 15:43:18 -0700132 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700133
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134 enum ActivityState {
135 INITIALIZING,
136 RESUMED,
137 PAUSING,
138 PAUSED,
139 STOPPING,
140 STOPPED,
141 FINISHING,
142 DESTROYING,
143 DESTROYED
144 }
145
146 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700147 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 /**
150 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151 * running) activities. It contains #TaskRecord objects.
152 */
153 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
154
155 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156 * Used for validating app tokens with window manager.
157 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800158 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 /**
161 * List of running activities, sorted by recent usage.
162 * The first entry in the list is the least recently used.
163 * It contains HistoryRecord objects.
164 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700165 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168 * Animations that for the current transition have requested not to
169 * be considered for the transition animation.
170 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172
173 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 * When we are in the process of pausing an activity, before starting the
175 * next one, this variable holds the activity that is currently being paused.
176 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800177 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700178
179 /**
180 * This is the last activity that we put into the paused state. This is
181 * used to determine if we need to do an activity transition while sleeping,
182 * when we normally hold the top activity paused.
183 */
184 ActivityRecord mLastPausedActivity = null;
185
186 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700187 * Activities that specify No History must be removed once the user navigates away from them.
188 * If the device goes to sleep with such an activity in the paused state then we save it here
189 * and finish it later if another activity replaces it on wakeup.
190 */
191 ActivityRecord mLastNoHistoryActivity = null;
192
193 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 * Current activity that is resumed, or null if there is none.
195 */
196 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700199 * This is the last activity that has been started. It is only used to
200 * identify when multiple activities are started at once so that the user
201 * can be warned they may not be in the activity they think they are.
202 */
203 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800204
Craig Mautner5eda9b32013-07-02 11:58:16 -0700205 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
206 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
207 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
208 // Activity in mTranslucentActivityWaiting is notified via
209 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
210 // background activity being drawn then the same call will be made with a true value.
211 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700212 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700213 new ArrayList<ActivityRecord>();
Craig Mautner233ceee2014-05-09 17:05:11 -0700214 // Options passed from the caller of the convertToTranslucent to the activity that will
215 // appear below it.
216 ActivityOptions mReturningActivityOptions = null;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700217
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700218 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700219 * Set when we know we are going to be calling updateConfiguration()
220 * soon, so want to skip intermediate config checks.
221 */
222 boolean mConfigWillChange;
223
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700224 long mLaunchStartTime = 0;
225 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800226
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800227 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800228 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
229 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
230 */
231 private ActivityRecord mLastScreenshotActivity = null;
232 private Bitmap mLastScreenshotBitmap = null;
233
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800234 int mThumbnailWidth = -1;
235 int mThumbnailHeight = -1;
236
Craig Mautner858d8a62013-04-23 17:08:34 -0700237 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700238
Craig Mautnerc00204b2013-03-05 15:02:14 -0800239 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800241 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
242 ArrayList<ActivityStack> mStacks;
243 /** The attached Display's unique identifier, or -1 if detached */
244 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800245
Craig Mautner27084302013-03-25 08:05:25 -0700246 /** Run all ActivityStacks through this */
247 final ActivityStackSupervisor mStackSupervisor;
248
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700249 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700250 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
251 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
252 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
253 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700254 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700255 static final int STOP_MEDIA_PLAYING_TIMEOUT_MSG =
256 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257
258 static class ScheduleDestroyArgs {
259 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700261 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700262 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700263 mReason = reason;
264 }
265 }
266
Zoran Marcetaf958b322012-08-09 20:27:12 +0900267 final Handler mHandler;
268
269 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 //public Handler() {
271 // if (localLOGV) Slog.v(TAG, "Handler started!");
272 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700273 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 super(looper);
275 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276
Zoran Marcetaf958b322012-08-09 20:27:12 +0900277 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 public void handleMessage(Message msg) {
279 switch (msg.what) {
280 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800281 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282 // We don't at this point know if the activity is fullscreen,
283 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800284 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 synchronized (mService) {
286 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700287 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700288 }
Craig Mautnera0026042014-04-23 11:45:37 -0700289 activityPausedLocked(r.appToken, true, r.persistentState);
Craig Mautnerd2328952013-03-05 12:46:26 -0800290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700292 case LAUNCH_TICK_MSG: {
293 ActivityRecord r = (ActivityRecord)msg.obj;
294 synchronized (mService) {
295 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700296 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700297 }
298 }
299 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800301 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700302 // We don't at this point know if the activity is fullscreen,
303 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800304 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800305 synchronized (mService) {
306 activityDestroyedLocked(r != null ? r.appToken : null);
307 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700309 case STOP_TIMEOUT_MSG: {
310 ActivityRecord r = (ActivityRecord)msg.obj;
311 // We don't at this point know if the activity is fullscreen,
312 // so we need to be conservative and assume it isn't.
313 Slog.w(TAG, "Activity stop timeout for " + r);
314 synchronized (mService) {
315 if (r.isInHistory()) {
316 activityStoppedLocked(r, null, null, null);
317 }
318 }
319 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700320 case DESTROY_ACTIVITIES_MSG: {
321 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
322 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700323 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700324 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700325 } break;
326 case TRANSLUCENT_TIMEOUT_MSG: {
327 synchronized (mService) {
328 notifyActivityDrawnLocked(null);
329 }
330 } break;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700331 case STOP_MEDIA_PLAYING_TIMEOUT_MSG: {
332 synchronized (mService) {
333 final ActivityRecord r = getMediaPlayer();
334 Slog.e(TAG, "Timeout waiting for stopMediaPlaying player=" + r);
335 if (r != null) {
336 mService.killAppAtUsersRequest(r.app, null);
337 }
338 }
339 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700340 }
341 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800342 }
343
Craig Mautner34b73df2014-01-12 21:11:08 -0800344 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800345 int count = 0;
346 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
347 count += mTaskHistory.get(taskNdx).mActivities.size();
348 }
349 return count;
350 }
351
Craig Mautner4a1cb222013-12-04 16:14:06 -0800352 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
353 mActivityContainer = activityContainer;
354 mStackSupervisor = activityContainer.getOuter();
355 mService = mStackSupervisor.mService;
356 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
357 mWindowManager = mService.mWindowManager;
358 mStackId = activityContainer.mStackId;
359 mCurrentUser = mService.mCurrentUserId;
Winson Chung376543b2014-05-21 17:43:28 -0700360 // Get the activity screenshot thumbnail dimensions
361 Resources res = mService.mContext.getResources();
Winson Chung2eb4f9c2014-05-22 11:22:41 -0700362 mThumbnailWidth = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
363 mThumbnailHeight = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700364 }
Craig Mautner5962b122012-10-05 14:45:52 -0700365
Amith Yamasani734983f2014-03-04 16:48:05 -0800366 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100367 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800368 */
Kenny Guy2a764942014-04-02 13:29:20 +0100369 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100370 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100371 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
372 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800373 }
374 return false;
375 }
376
377 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100378 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700379 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
380 }
381
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700382 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700384 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
385 if (r != null) {
386 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800387 }
388 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700389 return null;
390 }
391
392 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
394 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800395 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800396 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
397 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800398 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800399 return r;
400 }
401 }
402 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700403 return null;
404 }
405
406 /**
407 * This is a simplified version of topRunningActivityLocked that provides a number of
408 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800409 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700410 * @param token If non-null, any history records matching this token will be skipped.
411 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800412 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700413 * @return Returns the HistoryRecord of the next activity on the stack.
414 */
415 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800416 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
417 TaskRecord task = mTaskHistory.get(taskNdx);
418 if (task.taskId == taskId) {
419 continue;
420 }
421 ArrayList<ActivityRecord> activities = task.mActivities;
422 for (int i = activities.size() - 1; i >= 0; --i) {
423 final ActivityRecord r = activities.get(i);
424 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800425 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800426 return r;
427 }
428 }
429 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700430 return null;
431 }
432
Craig Mautner8849a5e2013-04-02 16:41:03 -0700433 final ActivityRecord topActivity() {
434 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700435 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700436 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
437 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700438 final int topActivityNdx = activities.size() - 1;
439 if (topActivityNdx >= 0) {
440 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700441 }
442 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700443 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700444 }
445
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700446 final TaskRecord topTask() {
447 final int size = mTaskHistory.size();
448 if (size > 0) {
449 return mTaskHistory.get(size - 1);
450 }
451 return null;
452 }
453
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 TaskRecord taskForIdLocked(int id) {
455 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
456 final TaskRecord task = mTaskHistory.get(taskNdx);
457 if (task.taskId == id) {
458 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800459 }
460 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700461 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 }
463
Craig Mautnerd2328952013-03-05 12:46:26 -0800464 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800465 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800466 if (r != null) {
467 final TaskRecord task = r.task;
468 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
469 if (task.stack != this) Slog.w(TAG,
470 "Illegal state! task does not point to stack it is in.");
471 return r;
472 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800473 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800474 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800475 }
476
Craig Mautner2420ead2013-04-01 17:13:20 -0700477 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700478 final boolean hadit = mLRUActivities.remove(r);
479 mLRUActivities.add(r);
480 return hadit;
481 }
482
Craig Mautnerde4ef022013-04-07 19:01:33 -0700483 final boolean isHomeStack() {
484 return mStackId == HOME_STACK_ID;
485 }
486
Craig Mautnere0a38842013-12-16 16:14:02 -0800487 final boolean isOnHomeDisplay() {
488 return isAttached() &&
489 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
490 }
491
492 final void moveToFront() {
493 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800494 if (isOnHomeDisplay()) {
495 mStackSupervisor.moveHomeStack(isHomeStack());
496 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800497 mStacks.remove(this);
498 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800499 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800500 }
501
502 final boolean isAttached() {
503 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 }
505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700506 /**
507 * Returns the top activity in any existing task matching the given
508 * Intent. Returns null if no such task is found.
509 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700510 ActivityRecord findTaskLocked(ActivityRecord target) {
511 Intent intent = target.intent;
512 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700513 ComponentName cls = intent.getComponent();
514 if (info.targetActivity != null) {
515 cls = new ComponentName(info.packageName, info.targetActivity);
516 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700517 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700518 boolean isDocument = intent != null & intent.isDocument();
519 // If documentData is non-null then it must match the existing task data.
520 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800521
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700522 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800523 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
524 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700525 if (task.voiceSession != null) {
526 // We never match voice sessions; those always run independently.
527 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
528 continue;
529 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700530 if (task.userId != userId) {
531 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700532 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700533 continue;
534 }
Craig Mautner000f0022013-02-26 15:04:29 -0800535 final ActivityRecord r = task.getTopActivity();
536 if (r == null || r.finishing || r.userId != userId ||
537 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700538 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800539 continue;
540 }
541
Craig Mautnerd00f4742014-03-12 14:17:26 -0700542 final Intent taskIntent = task.intent;
543 final Intent affinityIntent = task.affinityIntent;
544 final boolean taskIsDocument;
545 final Uri taskDocumentData;
546 if (taskIntent != null && taskIntent.isDocument()) {
547 taskIsDocument = true;
548 taskDocumentData = taskIntent.getData();
549 } else if (affinityIntent != null && affinityIntent.isDocument()) {
550 taskIsDocument = true;
551 taskDocumentData = affinityIntent.getData();
552 } else {
553 taskIsDocument = false;
554 taskDocumentData = null;
555 }
556
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700557 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700558 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700559 + "/aff=" + r.task.affinity + " to new cls="
560 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700561 if (!isDocument && !taskIsDocument && task.affinity != null) {
562 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700563 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700564 return r;
565 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700566 } else if (taskIntent != null && taskIntent.getComponent() != null &&
567 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700568 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700569 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800570 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700571 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
572 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800573 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700574 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
575 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700576 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700577 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800578 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700579 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
580 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800581 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700582 } else if (DEBUG_TASKS) {
583 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700584 }
585 }
586
587 return null;
588 }
589
590 /**
591 * Returns the first activity (starting from the top of the stack) that
592 * is the same as the given activity. Returns null if no such activity
593 * is found.
594 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700595 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700596 ComponentName cls = intent.getComponent();
597 if (info.targetActivity != null) {
598 cls = new ComponentName(info.packageName, info.targetActivity);
599 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700600 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700601
Craig Mautner000f0022013-02-26 15:04:29 -0800602 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700603 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100604 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700605 return null;
606 }
607 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800608 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
609 ActivityRecord r = activities.get(activityNdx);
610 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700611 //Slog.i(TAG, "Found matching class!");
612 //dump();
613 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
614 return r;
615 }
616 }
617 }
618
619 return null;
620 }
621
Amith Yamasani742a6712011-05-04 14:49:28 -0700622 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700623 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700624 */
Craig Mautner93529a42013-10-04 15:03:13 -0700625 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800626 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700627 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 }
629 mCurrentUser = userId;
630
631 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800632 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800633 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700634 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100635 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700636 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
637 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800638 mTaskHistory.remove(i);
639 mTaskHistory.add(task);
640 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800641 // Use same value for i.
642 } else {
643 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800644 }
645 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700646 if (VALIDATE_TOKENS) {
647 validateAppTokensLocked();
648 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700649 }
650
Craig Mautner2420ead2013-04-01 17:13:20 -0700651 void minimalResumeActivityLocked(ActivityRecord r) {
652 r.state = ActivityState.RESUMED;
653 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
654 + " (starting new instance)");
655 r.stopped = false;
656 mResumedActivity = r;
657 r.task.touchActiveTime();
658 mService.addRecentTaskLocked(r.task);
659 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700660 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700661 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700662 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700663 }
664
Dianne Hackborncee04b52013-07-03 17:01:28 -0700665 private void startLaunchTraces() {
666 if (mFullyDrawnStartTime != 0) {
667 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
668 }
669 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
670 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
671 }
672
673 private void stopFullyDrawnTraceIfNeeded() {
674 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
675 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
676 mFullyDrawnStartTime = 0;
677 }
678 }
679
Craig Mautnere79d42682013-04-01 19:01:53 -0700680 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700681 if (r.displayStartTime == 0) {
682 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
683 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700684 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700685 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700686 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700687 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700688 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700689 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700690 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700691 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800692
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700693 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700694 // Make sure that there is no activity waiting for this to launch.
695 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
696 r.displayStartTime = r.fullyDrawnStartTime = 0;
697 } else {
698 mStackSupervisor.removeTimeoutsForActivityLocked(r);
699 mStackSupervisor.scheduleIdleTimeoutLocked(r);
700 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700701 }
702
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800703 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800704 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800705 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
706 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
707 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
708 activities.get(activityNdx).setSleeping(false);
709 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800710 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800711 }
712
Craig Mautner0eea92c2013-05-16 13:35:39 -0700713 /**
714 * @return true if something must be done before going to sleep.
715 */
716 boolean checkReadyForSleepLocked() {
717 if (mResumedActivity != null) {
718 // Still have something resumed; can't sleep until it is paused.
719 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
720 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
721 startPausingLocked(false, true);
722 return true;
723 }
724 if (mPausingActivity != null) {
725 // Still waiting for something to pause; can't sleep yet.
726 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
727 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800728 }
729
Craig Mautner0eea92c2013-05-16 13:35:39 -0700730 return false;
731 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800732
Craig Mautner0eea92c2013-05-16 13:35:39 -0700733 void goToSleep() {
734 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800735
Craig Mautner0eea92c2013-05-16 13:35:39 -0700736 // Make sure any stopped but visible activities are now sleeping.
737 // This ensures that the activity's onStop() is called.
738 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
739 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
740 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
741 final ActivityRecord r = activities.get(activityNdx);
742 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
743 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800744 }
745 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800746 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700747 }
Craig Mautner59c00972012-07-30 12:10:24 -0700748
Winson Chung376543b2014-05-21 17:43:28 -0700749 /**
750 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
751 * again when requested.
752 */
753 private void invalidateLastScreenshot() {
754 mLastScreenshotActivity = null;
755 if (mLastScreenshotBitmap != null) {
756 mLastScreenshotBitmap.recycle();
757 }
758 mLastScreenshotBitmap = null;
759 }
760
Dianne Hackbornd2835932010-12-13 16:28:46 -0800761 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700762 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800763 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700764 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800765 return null;
766 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800767
Winson Chung083baf92014-07-11 10:32:42 -0700768 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700769 // This is an optimization -- since we never show Home or Recents within Recents itself,
770 // we can just go ahead and skip taking the screenshot if this is the home stack. In
771 // the case where the most recent task is not the task that was supplied, then the stack
772 // has changed, so invalidate the last screenshot().
773 invalidateLastScreenshot();
774 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700775 return null;
776 }
777
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800778 int w = mThumbnailWidth;
779 int h = mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800780 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800781 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700782 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800783 || mLastScreenshotBitmap.getWidth() != w
784 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700785 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800786 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700787 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700788 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800789 }
790 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700791 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700792 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800793 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800794 }
Winson Chung376543b2014-05-21 17:43:28 -0700795 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800796 return null;
797 }
798
Craig Mautnercf910b02013-04-23 11:23:27 -0700799 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800800 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800801 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700802 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800803 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700804 ActivityRecord prev = mResumedActivity;
805 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700806 Slog.e(TAG, "Trying to pause when nothing is resumed",
807 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700808 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700809 return;
810 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800811
812 if (mActivityContainer.mParentActivity == null) {
813 // Top level stack, not a child. Look for child stacks.
814 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
815 }
816
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700817 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
818 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800820 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700822 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
823 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700824 prev.state = ActivityState.PAUSING;
825 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700826 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700827 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Alan Viverettea2ff6112014-07-14 17:46:27 -0700828 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task)) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700829 prev.updateThumbnail(screenshotActivities(prev), null);
830 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700831 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832
833 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800834
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 if (prev.app != null && prev.app.thread != null) {
836 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
837 try {
838 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700839 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700841 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800842 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
843 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 } catch (Exception e) {
845 // Ignore exception, if process died other code will cleanup.
846 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800847 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700849 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 }
851 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800852 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700854 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700855 }
856
857 // If we are not going to sleep, we want to ensure the device is
858 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700859 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700860 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700861 }
862
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800863 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 // Have the window manager pause its key dispatching until the new
865 // activity has started. If we're pausing the activity just because
866 // the screen is being turned off and the UI is sleeping, don't interrupt
867 // key dispatch; the same activity will pick it up again on wakeup.
868 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800869 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 } else {
871 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
872 }
873
874 // Schedule a pause timeout in case the app doesn't respond.
875 // We don't give it much time because this directly impacts the
876 // responsiveness seen by the user.
877 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
878 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700879 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
881 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
882 } else {
883 // This activity failed to schedule the
884 // pause, so just treat it as being paused now.
885 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700886 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887 }
888 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700889
Craig Mautnera0026042014-04-23 11:45:37 -0700890 final void activityPausedLocked(IBinder token, boolean timeout,
891 PersistableBundle persistentState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800893 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700894
Craig Mautnerd2328952013-03-05 12:46:26 -0800895 final ActivityRecord r = isInStackLocked(token);
896 if (r != null) {
897 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Craig Mautner21d24a22014-04-23 11:45:37 -0700898 if (persistentState != null) {
899 r.persistentState = persistentState;
900 mService.notifyTaskPersisterLocked(r.task, false);
901 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800902 if (mPausingActivity == r) {
903 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
904 + (timeout ? " (due to timeout)" : " (pause complete)"));
905 r.state = ActivityState.PAUSED;
906 completePauseLocked();
907 } else {
908 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
909 r.userId, System.identityHashCode(r), r.shortComponentName,
910 mPausingActivity != null
911 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 }
913 }
914 }
915
Craig Mautnera0026042014-04-23 11:45:37 -0700916 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
917 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700918 if (r.state != ActivityState.STOPPING) {
919 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
920 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
921 return;
922 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700923 if (persistentState != null) {
924 r.persistentState = persistentState;
925 mService.notifyTaskPersisterLocked(r.task, false);
926 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700927 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700928 if (icicle != null) {
929 // If icicle is null, this is happening due to a timeout, so we
930 // haven't really saved the state.
931 r.icicle = icicle;
932 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800933 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700934 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700935 }
936 if (!r.stopped) {
937 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
938 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
939 r.stopped = true;
940 r.state = ActivityState.STOPPED;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700941 if (mActivityContainer.mActivityDisplay.mMediaPlayingActivity == r) {
942 mStackSupervisor.setMediaPlayingLocked(r, false);
943 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700944 if (r.finishing) {
945 r.clearOptionsLocked();
946 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700947 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700948 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700949 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700950 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700951 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800952 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700953 }
954 }
955 }
956
Craig Mautnerc8143c62013-09-03 12:15:57 -0700957 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800958 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700959 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800960
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800961 if (prev != null) {
962 if (prev.finishing) {
963 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700964 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 } else if (prev.app != null) {
966 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
967 if (prev.waitingVisible) {
968 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800970 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
971 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800972 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800973 if (prev.configDestroy) {
974 // The previous is being paused because the configuration
975 // is changing, which means it is actually stopping...
976 // To juggle the fact that we are also starting a new
977 // instance right now, we need to first completely stop
978 // the current instance before starting the new one.
979 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700980 destroyActivityLocked(prev, true, "pause-config");
981 } else if (!isMediaPlaying()) {
982 // If we were playing then resumeTopActivities will release resources before
983 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700984 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700985 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
986 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800987 // If we already have a few activities waiting to stop,
988 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700989 // them out. Or if r is the last of activity of the last task the stack
990 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800991 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700992 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700994 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800995 }
996 }
997 } else {
998 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
999 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001000 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001003
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001004 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001005 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -07001006 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001008 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001010 if (top == null || (prev != null && top != prev)) {
1011 // If there are no more activities available to run,
1012 // do resume anyway to start something. Also if the top
1013 // activity on the stack is not the just paused activity,
1014 // we need to go ahead and resume it to ensure we complete
1015 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -07001016 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001017 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001019
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001020 if (prev != null) {
1021 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001022
Craig Mautner525f3d92013-05-07 14:01:50 -07001023 if (prev.app != null && prev.cpuTimeAtResume > 0
1024 && mService.mBatteryStatsService.isOnBattery()) {
1025 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -07001026 synchronized (mService.mProcessCpuThread) {
1027 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -07001028 - prev.cpuTimeAtResume;
1029 }
1030 if (diff > 0) {
1031 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1032 synchronized (bsi) {
1033 BatteryStatsImpl.Uid.Proc ps =
1034 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001035 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001036 if (ps != null) {
1037 ps.addForegroundTimeLocked(diff);
1038 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001039 }
1040 }
1041 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001042 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001043 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001044 }
1045
1046 /**
1047 * Once we know that we have asked an application to put an activity in
1048 * the resumed state (either by launching it or explicitly telling it),
1049 * this function updates the rest of our state to match that fact.
1050 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001051 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001052 next.idle = false;
1053 next.results = null;
1054 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -07001055 if (next.nowVisible) {
1056 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1057 mStackSupervisor.dismissKeyguard();
1058 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059
1060 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001061 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001063 mStackSupervisor.reportResumedActivityLocked(next);
1064
1065 next.resumeKeyDispatchingLocked();
1066 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067
1068 // Mark the point when the activity is resuming
1069 // TODO: To be more accurate, the mark should be before the onCreate,
1070 // not after the onResume. But for subsequent starts, onResume is fine.
1071 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001072 synchronized (mService.mProcessCpuThread) {
1073 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001074 }
1075 } else {
1076 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1077 }
Winson Chung376543b2014-05-21 17:43:28 -07001078
1079 // If we are resuming the activity that we had last screenshotted, then we know it will be
1080 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1081 if (next == mLastScreenshotActivity) {
1082 invalidateLastScreenshot();
1083 }
George Mount01e9a972014-07-09 15:39:23 -07001084 mReturningActivityOptions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001085 }
1086
Craig Mautnere3a00d72014-04-16 08:31:19 -07001087 private void setVisibile(ActivityRecord r, boolean visible) {
1088 r.visible = visible;
1089 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001090 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001091 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001092 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001093 container.setVisible(visible);
1094 }
1095 }
1096
Jose Lima7ba71252014-04-30 12:59:27 -07001097 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1098 // If so, this stack is hidden, otherwise it is visible.
1099 private boolean isStackVisible() {
1100 if (!isAttached()) {
1101 return false;
1102 }
1103
1104 if (mStackSupervisor.isFrontStack(this)) {
1105 return true;
1106 }
1107
Jose Lima729cb232014-05-05 15:59:40 -07001108 /**
1109 * Start at the task above this one and go up, looking for a visible
1110 * fullscreen activity, or a translucent activity that requested the
1111 * wallpaper to be shown behind it.
1112 */
Jose Lima7ba71252014-04-30 12:59:27 -07001113 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1114 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1115 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001116 final TaskRecord task = tasks.get(taskNdx);
1117 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001118 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1119 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001120
1121 // Conditions for an activity to obscure the stack we're
1122 // examining:
1123 // 1. Not Finishing AND Visible AND:
1124 // 2. Either:
1125 // - Full Screen Activity OR
1126 // - On top of Home and our stack is NOT home
1127 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001128 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001129 return false;
1130 }
1131 }
1132 }
1133 }
1134
1135 return true;
1136 }
1137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 /**
1139 * Make sure that all activities that need to be visible (that is, they
1140 * currently can be seen by the user) actually are.
1141 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001142 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1143 ActivityRecord top = topRunningActivityLocked(null);
1144 if (top == null) {
1145 return;
1146 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001147 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001148 TAG, "ensureActivitiesVisible behind " + top
1149 + " configChanges=0x" + Integer.toHexString(configChanges));
1150
Craig Mautner5eda9b32013-07-02 11:58:16 -07001151 if (mTranslucentActivityWaiting != top) {
1152 mUndrawnActivitiesBelowTopTranslucent.clear();
1153 if (mTranslucentActivityWaiting != null) {
1154 // Call the callback with a timeout indication.
1155 notifyActivityDrawnLocked(null);
1156 mTranslucentActivityWaiting = null;
1157 }
1158 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1159 }
1160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001161 // If the top activity is not fullscreen, then we need to
1162 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001163 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001164 boolean behindFullscreen = !isStackVisible();
1165
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001166 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001167 final TaskRecord task = mTaskHistory.get(taskNdx);
1168 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001169 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1170 final ActivityRecord r = activities.get(activityNdx);
1171 if (r.finishing) {
1172 continue;
1173 }
1174 if (aboveTop && r != top) {
1175 continue;
1176 }
1177 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001178 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1179 // but must be drawn initially for the animation as though they were visible.
1180 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001181 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001182 TAG, "Make visible? " + r + " finishing=" + r.finishing
1183 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001184
Craig Mautnerd44711d2013-02-23 11:24:36 -08001185 // First: if this is not the current activity being started, make
1186 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001187 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001188 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001189 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001190
1191 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001192 // This activity needs to be visible, but isn't even
1193 // running... get it started, but don't resume it
1194 // at this point.
1195 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1196 if (r != starting) {
1197 r.startFreezingScreenLocked(r.app, configChanges);
1198 }
1199 if (!r.visible || r.mLaunchTaskBehind) {
1200 if (DEBUG_VISBILITY) Slog.v(
1201 TAG, "Starting and making visible: " + r);
1202 setVisibile(r, true);
1203 }
1204 if (r != starting) {
1205 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001206 }
1207
1208 } else if (r.visible) {
1209 // If this activity is already visible, then there is nothing
1210 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001211 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001213 try {
George Mount01e9a972014-07-09 15:39:23 -07001214 if (mReturningActivityOptions != null && r == top && activityNdx > 0) {
1215 ActivityRecord under = activities.get(activityNdx - 1);
1216 under.app.thread.scheduleOnNewActivityOptions(under.appToken,
1217 mReturningActivityOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001218 }
1219 } catch(RemoteException e) {
1220 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001221 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001222 // This activity is not currently visible, but is running.
1223 // Tell it to become visible.
1224 r.visible = true;
1225 if (r.state != ActivityState.RESUMED && r != starting) {
1226 // If this activity is paused, tell it
1227 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001228 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001229 TAG, "Making visible and scheduling visibility: " + r);
1230 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001231 if (mTranslucentActivityWaiting != null) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001232 r.updateOptionsLocked(mReturningActivityOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001233 mUndrawnActivitiesBelowTopTranslucent.add(r);
1234 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001235 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001236 r.sleeping = false;
1237 r.app.pendingUiClean = true;
1238 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1239 r.stopFreezingScreenLocked(false);
1240 } catch (Exception e) {
1241 // Just skip on any failure; we'll make it
1242 // visible when it next restarts.
1243 Slog.w(TAG, "Exception thrown making visibile: "
1244 + r.intent.getComponent(), e);
1245 }
1246 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001247 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248
Craig Mautnerd44711d2013-02-23 11:24:36 -08001249 // Aggregate current change flags.
1250 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001251
Craig Mautnerd44711d2013-02-23 11:24:36 -08001252 if (r.fullscreen) {
1253 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001254 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1255 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001256 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001257 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001258 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001259 }
1260 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001261 if (DEBUG_VISBILITY) Slog.v(
1262 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1263 + " state=" + r.state
1264 + " behindFullscreen=" + behindFullscreen);
1265 // Now for any activities that aren't visible to the user, make
1266 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001267 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001268 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001269 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001270 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001271 switch (r.state) {
1272 case STOPPING:
1273 case STOPPED:
1274 if (r.app != null && r.app.thread != null) {
1275 if (DEBUG_VISBILITY) Slog.v(
1276 TAG, "Scheduling invisibility: " + r);
1277 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1278 }
1279 break;
1280
1281 case INITIALIZING:
1282 case RESUMED:
1283 case PAUSING:
1284 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001285 // This case created for transitioning activities from
1286 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautneree2e45a2014-06-27 12:10:03 -07001287 if (getMediaPlayer() == r) {
1288 releaseMediaResources();
1289 } else {
1290 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1291 mStackSupervisor.mStoppingActivities.add(r);
1292 }
1293 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001294 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001295 break;
1296
1297 default:
1298 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001299 }
1300 } catch (Exception e) {
1301 // Just skip on any failure; we'll make it
1302 // visible when it next restarts.
1303 Slog.w(TAG, "Exception thrown making hidden: "
1304 + r.intent.getComponent(), e);
1305 }
1306 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001307 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001308 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 }
1310 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001312
1313 if (mTranslucentActivityWaiting != null &&
1314 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1315 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1316 notifyActivityDrawnLocked(null);
1317 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001318 }
Craig Mautner58547802013-03-05 08:23:53 -08001319
Craig Mautner233ceee2014-05-09 17:05:11 -07001320 void convertToTranslucent(ActivityRecord r, ActivityOptions options) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001321 mTranslucentActivityWaiting = r;
1322 mUndrawnActivitiesBelowTopTranslucent.clear();
Craig Mautner233ceee2014-05-09 17:05:11 -07001323 mReturningActivityOptions = options;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001324 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1325 }
1326
1327 /**
1328 * Called as activities below the top translucent activity are redrawn. When the last one is
1329 * redrawn notify the top activity by calling
1330 * {@link Activity#onTranslucentConversionComplete}.
1331 *
1332 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1333 * occurred and the activity will be notified immediately.
1334 */
1335 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001336 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001337 if ((r == null)
1338 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1339 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1340 // The last undrawn activity below the top has just been drawn. If there is an
1341 // opaque activity at the top, notify it that it can become translucent safely now.
1342 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1343 mTranslucentActivityWaiting = null;
1344 mUndrawnActivitiesBelowTopTranslucent.clear();
1345 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1346
Craig Mautner71dd1b62014-02-18 15:48:52 -08001347 if (waitingActivity != null) {
1348 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1349 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1350 try {
1351 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1352 waitingActivity.appToken, r != null);
1353 } catch (RemoteException e) {
1354 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001355 }
1356 }
1357 }
1358 }
1359
Craig Mautnera61bc652013-10-28 15:43:18 -07001360 /** If any activities below the top running one are in the INITIALIZING state and they have a
1361 * starting window displayed then remove that starting window. It is possible that the activity
1362 * in this state will never resumed in which case that starting window will be orphaned. */
1363 void cancelInitializingActivities() {
1364 final ActivityRecord topActivity = topRunningActivityLocked(null);
1365 boolean aboveTop = true;
1366 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1367 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1368 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1369 final ActivityRecord r = activities.get(activityNdx);
1370 if (aboveTop) {
1371 if (r == topActivity) {
1372 aboveTop = false;
1373 }
1374 continue;
1375 }
1376
1377 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1378 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1379 r.mStartingWindowShown = false;
1380 mWindowManager.removeAppStartingWindow(r.appToken);
1381 }
1382 }
1383 }
1384 }
1385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386 /**
1387 * Ensure that the top activity in the stack is resumed.
1388 *
1389 * @param prev The previously resumed activity, for when in the process
1390 * of pausing; can be null to call from elsewhere.
1391 *
1392 * @return Returns true if something is being resumed, or false if
1393 * nothing happened.
1394 */
1395 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001396 return resumeTopActivityLocked(prev, null);
1397 }
1398
1399 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001400 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1401
Craig Mautnerdf88d732014-01-27 09:21:32 -08001402 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001403 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001404 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001405 // Do not resume this stack if its parent is not resumed.
1406 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1407 return false;
1408 }
1409
Craig Mautnera61bc652013-10-28 15:43:18 -07001410 cancelInitializingActivities();
1411
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001412 // Find the first activity that is not finishing.
1413 ActivityRecord next = topRunningActivityLocked(null);
1414
1415 // Remember how we'll process this pause/resume situation, and ensure
1416 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001417 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1418 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419
Craig Mautner84984fa2014-06-19 11:19:20 -07001420 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 if (next == null) {
1422 // There are no more activities! Let's just start up the
1423 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001424 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001425 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001426 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001427 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001428 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1429 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1430 return isOnHomeDisplay() &&
1431 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432 }
1433
1434 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001435
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001437 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1438 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 // Make sure we have executed any pending transitions, since there
1440 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001441 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001443 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001444 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001445 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 return false;
1447 }
1448
Craig Mautner525f3d92013-05-07 14:01:50 -07001449 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001450 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001451 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001452 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001453 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001454 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001455 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001456 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001457 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001458 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001459 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001460 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001461 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1462 "resumeTopActivityLocked: Launching home next");
1463 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001464 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1465 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1466 return isOnHomeDisplay() &&
1467 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001468 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001469 }
1470
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 // If we are sleeping, and there is no resumed activity, and the top
1472 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001473 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001474 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001475 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 // Make sure we have executed any pending transitions, since there
1477 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001478 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001480 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001481 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001482 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 return false;
1484 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001485
1486 // Make sure that the user who owns this activity is started. If not,
1487 // we will just leave it as is because someone should be bringing
1488 // another user's activities to the top of the stack.
1489 if (mService.mStartedUsers.get(next.userId) == null) {
1490 Slog.w(TAG, "Skipping resume of top activity " + next
1491 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001492 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001493 return false;
1494 }
1495
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 // The activity may be waiting for stop, but that is no longer
1497 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001498 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001499 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001500 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001501 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502
1503 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1504
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001505 // If we are currently pausing an activity, then don't do anything
1506 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001507 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001508 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1509 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001510 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001511 return false;
1512 }
1513
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001514 // Okay we are now going to start a switch, to 'next'. We may first
1515 // have to pause the current activity, but this is an important point
1516 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001517 // XXX "App Redirected" dialog is getting too many false positives
1518 // at this point, so turn off for now.
1519 if (false) {
1520 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1521 long now = SystemClock.uptimeMillis();
1522 final boolean inTime = mLastStartedActivity.startTime != 0
1523 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1524 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1525 final int nextUid = next.info.applicationInfo.uid;
1526 if (inTime && lastUid != nextUid
1527 && lastUid != next.launchedFromUid
1528 && mService.checkPermission(
1529 android.Manifest.permission.STOP_APP_SWITCHES,
1530 -1, next.launchedFromUid)
1531 != PackageManager.PERMISSION_GRANTED) {
1532 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1533 } else {
1534 next.startTime = now;
1535 mLastStartedActivity = next;
1536 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001537 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001538 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001539 mLastStartedActivity = next;
1540 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001541 }
Craig Mautner58547802013-03-05 08:23:53 -08001542
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 // We need to start pausing the current activity so the top one
1544 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001545 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001546 if (mResumedActivity != null) {
1547 pausing = true;
1548 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001549 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001550 }
1551 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001552 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1553 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001554 // At this point we want to put the upcoming activity's process
1555 // at the top of the LRU list, since we know we will be needing it
1556 // very soon and it would be a waste to let it get killed if it
1557 // happens to be sitting towards the end.
1558 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001559 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001560 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001561 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001562 return true;
1563 }
1564
Christopher Tated3f175c2012-06-14 14:16:54 -07001565 // If the most recent activity was noHistory but was only stopped rather
1566 // than stopped+finished because the device went to sleep, we need to make
1567 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001568 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001569 !mLastNoHistoryActivity.finishing) {
1570 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1571 " on new resume");
1572 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1573 null, "no-history", false);
1574 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001575 }
1576
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001577 if (prev != null && prev != next) {
1578 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1579 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001580 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 if (DEBUG_SWITCH) Slog.v(
1582 TAG, "Resuming top, waiting visible to hide: " + prev);
1583 } else {
1584 // The next activity is already visible, so hide the previous
1585 // activity's windows right now so we can show the new one ASAP.
1586 // We only do this if the previous is finishing, which should mean
1587 // it is on top of the one being resumed so hiding it quickly
1588 // is good. Otherwise, we want to do the normal route of allowing
1589 // the resumed activity to be shown so we can decide if the
1590 // previous should actually be hidden depending on whether the
1591 // new one is found to be full-screen or not.
1592 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001593 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001594 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1595 + prev + ", waitingVisible="
1596 + (prev != null ? prev.waitingVisible : null)
1597 + ", nowVisible=" + next.nowVisible);
1598 } else {
1599 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1600 + prev + ", waitingVisible="
1601 + (prev != null ? prev.waitingVisible : null)
1602 + ", nowVisible=" + next.nowVisible);
1603 }
1604 }
1605 }
1606
Dianne Hackborne7f97212011-02-24 14:40:20 -08001607 // Launching this app's activity, make sure the app is no longer
1608 // considered stopped.
1609 try {
1610 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001611 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001612 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001613 } catch (IllegalArgumentException e) {
1614 Slog.w(TAG, "Failed trying to unstop package "
1615 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001616 }
1617
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 // We are starting up the next activity, so tell the window manager
1619 // that the previous one will be hidden soon. This way it can know
1620 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001621 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001622 if (prev != null) {
1623 if (prev.finishing) {
1624 if (DEBUG_TRANSITION) Slog.v(TAG,
1625 "Prepare close transition: prev=" + prev);
1626 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001627 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001628 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001629 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001630 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001631 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1632 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001634 mWindowManager.setAppWillBeHidden(prev.appToken);
1635 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001637 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001639 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001640 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001642 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001643 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001644 : next.mLaunchTaskBehind
1645 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1646 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001647 }
1648 }
1649 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001650 mWindowManager.setAppWillBeHidden(prev.appToken);
1651 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 }
Craig Mautner967212c2013-04-13 21:10:58 -07001653 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001654 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001656 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001657 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001658 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001659 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 }
1661 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001662
1663 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001664 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001665 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1666 if (opts != null) {
1667 resumeAnimOptions = opts.toBundle();
1668 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001669 next.applyOptionsLocked();
1670 } else {
1671 next.clearOptionsLocked();
1672 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673
Craig Mautnercf910b02013-04-23 11:23:27 -07001674 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 if (next.app != null && next.app.thread != null) {
1676 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1677
1678 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001679 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001681 // schedule launch ticks to collect information about slow apps.
1682 next.startLaunchTickingLocked();
1683
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001684 ActivityRecord lastResumedActivity =
1685 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001686 ActivityState lastState = next.state;
1687
1688 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001689
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001690 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 next.state = ActivityState.RESUMED;
1692 mResumedActivity = next;
1693 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001694 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001695 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001697 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698
1699 // Have the window manager re-evaluate the orientation of
1700 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001701 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001702 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001703 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001704 mService.mConfiguration,
1705 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1706 if (config != null) {
1707 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001708 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001709 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001711
Craig Mautner525f3d92013-05-07 14:01:50 -07001712 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 // The configuration update wasn't able to keep the existing
1714 // instance of the activity, and instead started a new one.
1715 // We should be all done, but let's just make sure our activity
1716 // is still at the top and schedule another run if something
1717 // weird happened.
1718 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001719 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001720 "Activity config changed during resume: " + next
1721 + ", new next: " + nextNext);
1722 if (nextNext != next) {
1723 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001724 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001726 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001727 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001728 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001729 return true;
1730 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001731 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001732 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 }
Craig Mautner58547802013-03-05 08:23:53 -08001734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 try {
1736 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001737 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 if (a != null) {
1739 final int N = a.size();
1740 if (!next.finishing && N > 0) {
1741 if (DEBUG_RESULTS) Slog.v(
1742 TAG, "Delivering results to " + next
1743 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001744 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745 }
1746 }
1747
1748 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001749 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 }
1751
1752 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001753 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001755
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001756 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001757 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001758 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001759 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001760 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001761 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001762 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001763
Craig Mautner0eea92c2013-05-16 13:35:39 -07001764 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765
Craig Mautnerac6f8432013-07-17 13:24:59 -07001766 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 } catch (Exception e) {
1768 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001769 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1770 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001772 if (lastStack != null) {
1773 lastStack.mResumedActivity = lastResumedActivity;
1774 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 Slog.i(TAG, "Restarting because process died: " + next);
1776 if (!next.hasBeenLaunched) {
1777 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001778 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1779 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001780 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001781 next.appToken, next.packageName, next.theme,
1782 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001783 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1784 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 }
George Mount2c92c972014-03-20 09:38:23 -07001786 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001787 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 return true;
1789 }
1790
1791 // From this point on, if something goes wrong there is no way
1792 // to recover the activity.
1793 try {
1794 next.visible = true;
1795 completeResumeLocked(next);
1796 } catch (Exception e) {
1797 // If any exception gets thrown, toss away this
1798 // activity and try the next one.
1799 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001800 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001801 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001802 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 return true;
1804 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001805 next.stopped = false;
1806
1807 } else {
1808 // Whoops, need to restart this activity!
1809 if (!next.hasBeenLaunched) {
1810 next.hasBeenLaunched = true;
1811 } else {
1812 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001813 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001814 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001815 mService.compatibilityInfoForPackageLocked(
1816 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001818 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001819 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 }
1821 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1822 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001823 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001824 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825 }
1826
Craig Mautnercf910b02013-04-23 11:23:27 -07001827 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 return true;
1829 }
1830
Craig Mautnerac6f8432013-07-17 13:24:59 -07001831 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001832 // If this is being moved to the top by another activity or being launched from the home
1833 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001834 if (isOnHomeDisplay()) {
1835 ActivityStack lastStack = mStackSupervisor.getLastStack();
1836 final boolean fromHome = lastStack.isHomeStack();
1837 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001838 task.setTaskToReturnTo(fromHome
1839 ? lastStack.topTask() == null
1840 ? HOME_ACTIVITY_TYPE
1841 : lastStack.topTask().taskType
1842 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001843 }
1844 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001845 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001846 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001847
Craig Mautnerac6f8432013-07-17 13:24:59 -07001848 mTaskHistory.remove(task);
1849 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001850 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001851 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001852 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001853 while (--taskNdx >= 0) {
1854 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001855 break;
1856 }
1857 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001858 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001859 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001860 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001861 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001862 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001863
Craig Mautner8849a5e2013-04-02 16:41:03 -07001864 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001865 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001866 TaskRecord rTask = r.task;
1867 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001868 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1869 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001870 // Last activity in task had been removed or ActivityManagerService is reusing task.
1871 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001872 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001873 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001874 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001875 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001876 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 if (!newTask) {
1878 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001879 boolean startIt = true;
1880 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1881 task = mTaskHistory.get(taskNdx);
1882 if (task == r.task) {
1883 // Here it is! Now, if this is not yet visible to the
1884 // user, then just add it without starting; it will
1885 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001886 if (!startIt) {
1887 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1888 + task, new RuntimeException("here").fillInStackTrace());
1889 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001890 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001891 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1892 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001893 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001894 r.userId, r.info.configChanges, task.voiceSession != null,
1895 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001896 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001897 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001899 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001900 return;
1901 }
1902 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001903 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001904 startIt = false;
1905 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906 }
1907 }
1908
1909 // Place a new activity at top of stack, so it is next to interact
1910 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001911
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 // If we are not placing the new activity frontmost, we do not want
1913 // to deliver the onUserLeaving callback to the actual frontmost
1914 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001915 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001916 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001917 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1918 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001919 }
Craig Mautner70a86932013-02-28 22:37:44 -08001920
1921 task = r.task;
1922
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001924 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001925 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001926 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001927 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001928
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001929 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001930 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001931 // We want to show the starting preview window if we are
1932 // switching to a new task, or the next activity's process is
1933 // not currently running.
1934 boolean showStartingIcon = newTask;
1935 ProcessRecord proc = r.app;
1936 if (proc == null) {
1937 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1938 }
1939 if (proc == null || proc.thread == null) {
1940 showStartingIcon = true;
1941 }
1942 if (DEBUG_TRANSITION) Slog.v(TAG,
1943 "Prepare open transition: starting " + r);
1944 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001945 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001946 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001948 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07001949 ? r.mLaunchTaskBehind
1950 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1951 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08001952 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001953 mNoAnimActivities.remove(r);
1954 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001955 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001956 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001957 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07001958 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 boolean doShow = true;
1960 if (newTask) {
1961 // Even though this activity is starting fresh, we still need
1962 // to reset it to make sure we apply affinities to move any
1963 // existing activities from other tasks in to it.
1964 // If the caller has requested that the target task be
1965 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001966 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 resetTaskIfNeededLocked(r, r);
1968 doShow = topRunningNonDelayedActivityLocked(null) == r;
1969 }
George Mount70778d72014-07-01 16:33:45 -07001970 } else if (options != null && new ActivityOptions(options).getAnimationType()
1971 == ActivityOptions.ANIM_SCENE_TRANSITION) {
1972 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001973 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001974 if (r.mLaunchTaskBehind) {
1975 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
1976 // tell WindowManager that r is visible even though it is at the back of the stack.
1977 mWindowManager.setAppVisibility(r.appToken, true);
1978 ensureActivitiesVisibleLocked(null, 0);
1979 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 // Figure out if we are transitioning from another activity that is
1981 // "has the same starting icon" as the next one. This allows the
1982 // window manager to keep the previous window it had previously
1983 // created, if it still had one.
1984 ActivityRecord prev = mResumedActivity;
1985 if (prev != null) {
1986 // We don't want to reuse the previous starting preview if:
1987 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001988 if (prev.task != r.task) {
1989 prev = null;
1990 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001992 else if (prev.nowVisible) {
1993 prev = null;
1994 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001995 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001996 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001997 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001998 mService.compatibilityInfoForPackageLocked(
1999 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002000 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002001 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002002 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 }
2004 } else {
2005 // If this is the first activity, don't do any fancy animations,
2006 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002007 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002008 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002009 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002010 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002011 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002012 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002013 }
2014 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002015 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 }
2017
2018 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002019 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 }
2021 }
2022
Dianne Hackbornbe707852011-11-11 14:32:10 -08002023 final void validateAppTokensLocked() {
2024 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002025 mValidateAppTokens.ensureCapacity(numActivities());
2026 final int numTasks = mTaskHistory.size();
2027 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2028 TaskRecord task = mTaskHistory.get(taskNdx);
2029 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002030 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002031 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002032 }
Craig Mautner000f0022013-02-26 15:04:29 -08002033 TaskGroup group = new TaskGroup();
2034 group.taskId = task.taskId;
2035 mValidateAppTokens.add(group);
2036 final int numActivities = activities.size();
2037 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2038 final ActivityRecord r = activities.get(activityNdx);
2039 group.tokens.add(r.appToken);
2040 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002041 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002042 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002043 }
2044
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002045 /**
2046 * Perform a reset of the given task, if needed as part of launching it.
2047 * Returns the new HistoryRecord at the top of the task.
2048 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002049 /**
2050 * Helper method for #resetTaskIfNeededLocked.
2051 * We are inside of the task being reset... we'll either finish this activity, push it out
2052 * for another task, or leave it as-is.
2053 * @param task The task containing the Activity (taskTop) that might be reset.
2054 * @param forceReset
2055 * @return An ActivityOptions that needs to be processed.
2056 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002057 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002058 ActivityOptions topOptions = null;
2059
2060 int replyChainEnd = -1;
2061 boolean canMoveOptions = true;
2062
2063 // We only do this for activities that are not the root of the task (since if we finish
2064 // the root, we may no longer have the task!).
2065 final ArrayList<ActivityRecord> activities = task.mActivities;
2066 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002067 final int rootActivityNdx = task.findEffectiveRootIndex();
2068 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002069 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002070 if (target.frontOfTask)
2071 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002072
2073 final int flags = target.info.flags;
2074 final boolean finishOnTaskLaunch =
2075 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2076 final boolean allowTaskReparenting =
2077 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2078 final boolean clearWhenTaskReset =
2079 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2080
2081 if (!finishOnTaskLaunch
2082 && !clearWhenTaskReset
2083 && target.resultTo != null) {
2084 // If this activity is sending a reply to a previous
2085 // activity, we can't do anything with it now until
2086 // we reach the start of the reply chain.
2087 // XXX note that we are assuming the result is always
2088 // to the previous activity, which is almost always
2089 // the case but we really shouldn't count on.
2090 if (replyChainEnd < 0) {
2091 replyChainEnd = i;
2092 }
2093 } else if (!finishOnTaskLaunch
2094 && !clearWhenTaskReset
2095 && allowTaskReparenting
2096 && target.taskAffinity != null
2097 && !target.taskAffinity.equals(task.affinity)) {
2098 // If this activity has an affinity for another
2099 // task, then we need to move it out of here. We will
2100 // move it as far out of the way as possible, to the
2101 // bottom of the activity stack. This also keeps it
2102 // correctly ordered with any activities we previously
2103 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002104 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002105 final ActivityRecord bottom =
2106 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002107 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002108 if (bottom != null && target.taskAffinity != null
2109 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 // If the activity currently at the bottom has the
2111 // same task affinity as the one we are moving,
2112 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002113 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002114 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002115 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002116 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002117 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002118 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002119 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002120 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2121 + " out to new task " + target.task);
2122 }
2123
Craig Mautnere3a74d52013-02-22 14:14:58 -08002124 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002125 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002126
Craig Mautner525f3d92013-05-07 14:01:50 -07002127 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002128 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2129 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002130 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002131 if (p.finishing) {
2132 continue;
2133 }
2134
Craig Mautnere3a74d52013-02-22 14:14:58 -08002135 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002136 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002137 topOptions = p.takeOptionsLocked();
2138 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002139 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140 }
2141 }
2142 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002143 + task + " adding to task=" + targetTask
2144 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002145 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2146 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002147 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002148 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002149
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002150 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002151 }
2152
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002153 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002154 if (VALIDATE_TOKENS) {
2155 validateAppTokensLocked();
2156 }
2157
2158 replyChainEnd = -1;
2159 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2160 // If the activity should just be removed -- either
2161 // because it asks for it, or the task should be
2162 // cleared -- then finish it and anything that is
2163 // part of its reply chain.
2164 int end;
2165 if (clearWhenTaskReset) {
2166 // In this case, we want to finish this activity
2167 // and everything above it, so be sneaky and pretend
2168 // like these are all in the reply chain.
2169 end = numActivities - 1;
2170 } else if (replyChainEnd < 0) {
2171 end = i;
2172 } else {
2173 end = replyChainEnd;
2174 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002175 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002176 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002177 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002178 if (p.finishing) {
2179 continue;
2180 }
2181 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002182 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002183 topOptions = p.takeOptionsLocked();
2184 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002185 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002186 }
2187 }
Craig Mautner58547802013-03-05 08:23:53 -08002188 if (DEBUG_TASKS) Slog.w(TAG,
2189 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002190 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002191 end--;
2192 srcPos--;
2193 }
2194 }
2195 replyChainEnd = -1;
2196 } else {
2197 // If we were in the middle of a chain, well the
2198 // activity that started it all doesn't want anything
2199 // special, so leave it all as-is.
2200 replyChainEnd = -1;
2201 }
2202 }
2203
2204 return topOptions;
2205 }
2206
2207 /**
2208 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2209 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2210 * @param affinityTask The task we are looking for an affinity to.
2211 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2212 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2213 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2214 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002215 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002216 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002217 int replyChainEnd = -1;
2218 final int taskId = task.taskId;
2219 final String taskAffinity = task.affinity;
2220
2221 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2222 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002223 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2224
2225 // Do not operate on or below the effective root Activity.
2226 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002227 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002228 if (target.frontOfTask)
2229 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230
2231 final int flags = target.info.flags;
2232 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2233 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2234
2235 if (target.resultTo != null) {
2236 // If this activity is sending a reply to a previous
2237 // activity, we can't do anything with it now until
2238 // we reach the start of the reply chain.
2239 // XXX note that we are assuming the result is always
2240 // to the previous activity, which is almost always
2241 // the case but we really shouldn't count on.
2242 if (replyChainEnd < 0) {
2243 replyChainEnd = i;
2244 }
2245 } else if (topTaskIsHigher
2246 && allowTaskReparenting
2247 && taskAffinity != null
2248 && taskAffinity.equals(target.taskAffinity)) {
2249 // This activity has an affinity for our task. Either remove it if we are
2250 // clearing or move it over to our task. Note that
2251 // we currently punt on the case where we are resetting a
2252 // task that is not at the top but who has activities above
2253 // with an affinity to it... this is really not a normal
2254 // case, and we will need to later pull that task to the front
2255 // and usually at that point we will do the reset and pick
2256 // up those remaining activities. (This only happens if
2257 // someone starts an activity in a new task from an activity
2258 // in a task that is not currently on top.)
2259 if (forceReset || finishOnTaskLaunch) {
2260 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2261 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2262 for (int srcPos = start; srcPos >= i; --srcPos) {
2263 final ActivityRecord p = activities.get(srcPos);
2264 if (p.finishing) {
2265 continue;
2266 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002267 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002268 }
2269 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002270 if (taskInsertionPoint < 0) {
2271 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002272
Craig Mautner77878772013-03-04 19:46:24 -08002273 }
Craig Mautner77878772013-03-04 19:46:24 -08002274
2275 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2276 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2277 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2278 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002279 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002280 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002281 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282
Craig Mautnere3a74d52013-02-22 14:14:58 -08002283 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2284 + " to stack at " + task,
2285 new RuntimeException("here").fillInStackTrace());
2286 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2287 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002288 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002289 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002290 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002291 if (VALIDATE_TOKENS) {
2292 validateAppTokensLocked();
2293 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002294
2295 // Now we've moved it in to place... but what if this is
2296 // a singleTop activity and we have put it on top of another
2297 // instance of the same activity? Then we drop the instance
2298 // below so it remains singleTop.
2299 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2300 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002301 int targetNdx = taskActivities.indexOf(target);
2302 if (targetNdx > 0) {
2303 ActivityRecord p = taskActivities.get(targetNdx - 1);
2304 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002305 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2306 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002307 }
2308 }
2309 }
2310 }
2311
2312 replyChainEnd = -1;
2313 }
2314 }
Craig Mautner77878772013-03-04 19:46:24 -08002315 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 }
2317
Craig Mautner8849a5e2013-04-02 16:41:03 -07002318 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002319 ActivityRecord newActivity) {
2320 boolean forceReset =
2321 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2322 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2323 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2324 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2325 forceReset = true;
2326 }
2327 }
2328
2329 final TaskRecord task = taskTop.task;
2330
2331 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2332 * for remaining tasks. Used for later tasks to reparent to task. */
2333 boolean taskFound = false;
2334
2335 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2336 ActivityOptions topOptions = null;
2337
Craig Mautner77878772013-03-04 19:46:24 -08002338 // Preserve the location for reparenting in the new task.
2339 int reparentInsertionPoint = -1;
2340
Craig Mautnere3a74d52013-02-22 14:14:58 -08002341 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2342 final TaskRecord targetTask = mTaskHistory.get(i);
2343
2344 if (targetTask == task) {
2345 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2346 taskFound = true;
2347 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002348 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2349 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002350 }
2351 }
2352
Craig Mautner70a86932013-02-28 22:37:44 -08002353 int taskNdx = mTaskHistory.indexOf(task);
2354 do {
2355 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2356 } while (taskTop == null && taskNdx >= 0);
2357
Craig Mautnere3a74d52013-02-22 14:14:58 -08002358 if (topOptions != null) {
2359 // If we got some ActivityOptions from an activity on top that
2360 // was removed from the task, propagate them to the new real top.
2361 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002362 taskTop.updateOptionsLocked(topOptions);
2363 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002364 topOptions.abort();
2365 }
2366 }
2367
2368 return taskTop;
2369 }
2370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2372 String resultWho, int requestCode, int resultCode, Intent data) {
2373
2374 if (callingUid > 0) {
2375 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002376 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002377 }
2378
2379 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2380 + " : who=" + resultWho + " req=" + requestCode
2381 + " res=" + resultCode + " data=" + data);
2382 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2383 try {
2384 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2385 list.add(new ResultInfo(resultWho, requestCode,
2386 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002387 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002388 return;
2389 } catch (Exception e) {
2390 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2391 }
2392 }
2393
2394 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2395 }
2396
Craig Mautner04f0b702013-10-22 12:31:01 -07002397 private void adjustFocusedActivityLocked(ActivityRecord r) {
2398 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2399 ActivityRecord next = topRunningActivityLocked(null);
2400 if (next != r) {
2401 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002402 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2403 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002404 }
2405 }
Winson Chung648c83b2014-04-28 15:11:56 -07002406 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2407 if (top != null) {
2408 mService.setFocusedActivityLocked(top);
2409 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002410 }
2411 }
2412
Craig Mautnerf3333272013-04-22 10:55:53 -07002413 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002414 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2415 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2416 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2417 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002418 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002419 if (DEBUG_STATES) {
2420 Slog.d(TAG, "no-history finish of " + r);
2421 }
2422 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002423 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002424 } else {
2425 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2426 + " on stop because we're just sleeping");
2427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002428 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002429 }
2430
2431 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002432 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002433 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 try {
2435 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002436 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2437 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 r.state = ActivityState.STOPPING;
2439 if (DEBUG_VISBILITY) Slog.v(
2440 TAG, "Stopping visible=" + r.visible + " for " + r);
2441 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002442 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002443 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002444 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002445 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002446 r.setSleeping(true);
2447 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002448 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002449 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002450 } catch (Exception e) {
2451 // Maybe just ignore exceptions here... if the process
2452 // has crashed, our death notification will clean things
2453 // up.
2454 Slog.w(TAG, "Exception thrown during pause", e);
2455 // Just in case, assume it to be stopped.
2456 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002457 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458 r.state = ActivityState.STOPPED;
2459 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002460 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002461 }
2462 }
2463 }
2464 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002465
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 /**
2467 * @return Returns true if the activity is being finished, false if for
2468 * some reason it is being left as-is.
2469 */
2470 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002471 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002472 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002473 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002474 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002475 + ", result=" + resultCode + ", data=" + resultData
2476 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002477 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002478 return false;
2479 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002480
Craig Mautnerd44711d2013-02-23 11:24:36 -08002481 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002482 return true;
2483 }
2484
Craig Mautnerd2328952013-03-05 12:46:26 -08002485 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002486 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2487 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2488 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2489 ActivityRecord r = activities.get(activityNdx);
2490 if (r.resultTo == self && r.requestCode == requestCode) {
2491 if ((r.resultWho == null && resultWho == null) ||
2492 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2493 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2494 false);
2495 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002496 }
2497 }
2498 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002499 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002500 }
2501
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002502 final void finishTopRunningActivityLocked(ProcessRecord app) {
2503 ActivityRecord r = topRunningActivityLocked(null);
2504 if (r != null && r.app == app) {
2505 // If the top running activity is from this crashing
2506 // process, then terminate it to avoid getting in a loop.
2507 Slog.w(TAG, " Force finishing activity "
2508 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002509 int taskNdx = mTaskHistory.indexOf(r.task);
2510 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002511 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002512 // Also terminate any activities below it that aren't yet
2513 // stopped, to avoid a situation where one will get
2514 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002515 --activityNdx;
2516 if (activityNdx < 0) {
2517 do {
2518 --taskNdx;
2519 if (taskNdx < 0) {
2520 break;
2521 }
2522 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2523 } while (activityNdx < 0);
2524 }
2525 if (activityNdx >= 0) {
2526 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002527 if (r.state == ActivityState.RESUMED
2528 || r.state == ActivityState.PAUSING
2529 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002530 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002531 Slog.w(TAG, " Force finishing activity "
2532 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002533 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002534 }
2535 }
2536 }
2537 }
2538 }
2539
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002540 final void finishVoiceTask(IVoiceInteractionSession session) {
2541 IBinder sessionBinder = session.asBinder();
2542 boolean didOne = false;
2543 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2544 TaskRecord tr = mTaskHistory.get(taskNdx);
2545 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2546 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2547 ActivityRecord r = tr.mActivities.get(activityNdx);
2548 if (!r.finishing) {
2549 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2550 false);
2551 didOne = true;
2552 }
2553 }
2554 }
2555 }
2556 if (didOne) {
2557 mService.updateOomAdjLocked();
2558 }
2559 }
2560
Craig Mautnerd2328952013-03-05 12:46:26 -08002561 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002562 ArrayList<ActivityRecord> activities = r.task.mActivities;
2563 for (int index = activities.indexOf(r); index >= 0; --index) {
2564 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002565 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002566 break;
2567 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002568 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002569 }
2570 return true;
2571 }
2572
Dianne Hackborn5c607432012-02-28 14:44:19 -08002573 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2574 // send the result
2575 ActivityRecord resultTo = r.resultTo;
2576 if (resultTo != null) {
2577 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2578 + " who=" + r.resultWho + " req=" + r.requestCode
2579 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002580 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002581 if (resultData != null) {
2582 resultData.prepareToLeaveUser(r.userId);
2583 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002584 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002585 if (r.info.applicationInfo.uid > 0) {
2586 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2587 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002588 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002589 }
2590 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2591 resultData);
2592 r.resultTo = null;
2593 }
2594 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2595
2596 // Make sure this HistoryRecord is not holding on to other resources,
2597 // because clients have remote IPC references to this object so we
2598 // can't assume that will go away and want to avoid circular IPC refs.
2599 r.results = null;
2600 r.pendingResults = null;
2601 r.newIntents = null;
2602 r.icicle = null;
2603 }
2604
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 /**
2606 * @return Returns true if this activity has been removed from the history
2607 * list, or false if it is still in the list and will be removed later.
2608 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002609 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2610 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611 if (r.finishing) {
2612 Slog.w(TAG, "Duplicate finish request for " + r);
2613 return false;
2614 }
2615
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002616 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002617 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002618 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002619 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002620 task.taskId, r.shortComponentName, reason);
2621 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002622 final int index = activities.indexOf(r);
2623 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002624 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002625 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002626 // If the caller asked that this activity (and all above it)
2627 // be cleared when the task is reset, don't lose that information,
2628 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002629 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002630 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002631 }
2632 }
2633
2634 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002635
2636 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637
Dianne Hackborn5c607432012-02-28 14:44:19 -08002638 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002639
Craig Mautnerde4ef022013-04-07 19:01:33 -07002640 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002641 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002642 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002644 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002645 ? AppTransition.TRANSIT_TASK_CLOSE
2646 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002647
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002648 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002649 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002650
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002651 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2653 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2654 startPausingLocked(false, false);
2655 }
2656
Craig Mautneraea74a52014-03-08 14:23:10 -08002657 if (endTask) {
2658 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2659 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002660 } else if (r.state != ActivityState.PAUSING) {
2661 // If the activity is PAUSING, we will complete the finish once
2662 // it is done pausing; else we can just directly finish it here.
2663 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002664 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002665 } else {
2666 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2667 }
2668
2669 return false;
2670 }
2671
Craig Mautnerf3333272013-04-22 10:55:53 -07002672 static final int FINISH_IMMEDIATELY = 0;
2673 static final int FINISH_AFTER_PAUSE = 1;
2674 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675
Craig Mautnerf3333272013-04-22 10:55:53 -07002676 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677 // First things first: if this activity is currently visible,
2678 // and the resumed activity is not yet visible, then hold off on
2679 // finishing until the resumed one becomes visible.
2680 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002681 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2682 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002683 if (mStackSupervisor.mStoppingActivities.size() > 3
2684 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002685 // If we already have a few activities waiting to stop,
2686 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002687 // them out. Or if r is the last of activity of the last task the stack
2688 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002689 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002690 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002691 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002692 }
2693 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002694 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2695 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002697 if (oomAdj) {
2698 mService.updateOomAdjLocked();
2699 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002700 return r;
2701 }
2702
2703 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002704 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002705 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002706 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 if (mResumedActivity == r) {
2708 mResumedActivity = null;
2709 }
2710 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002711 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 r.state = ActivityState.FINISHING;
2713
2714 if (mode == FINISH_IMMEDIATELY
2715 || prevState == ActivityState.STOPPED
2716 || prevState == ActivityState.INITIALIZING) {
2717 // If this activity is already stopped, we can just finish
2718 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002719 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002720 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002721 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002722 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002723 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002724 if (DEBUG_CONTAINERS) Slog.d(TAG,
2725 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2726 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002727 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002729
2730 // Need to go through the full pause cycle to get this
2731 // activity into the stopped state and then finish it.
2732 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002733 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002734 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002735 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 return r;
2737 }
2738
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002739 void finishAllActivitiesLocked() {
2740 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2741 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2742 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2743 final ActivityRecord r = activities.get(activityNdx);
2744 if (r.finishing) {
2745 continue;
2746 }
2747 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
2748 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2749 }
2750 }
2751 }
2752
Craig Mautnerd2328952013-03-05 12:46:26 -08002753 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002754 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002755 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002756 final TaskRecord task = srec.task;
2757 final ArrayList<ActivityRecord> activities = task.mActivities;
2758 final int start = activities.indexOf(srec);
2759 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002760 return false;
2761 }
2762 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002763 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002764 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002765 final ComponentName dest = destIntent.getComponent();
2766 if (start > 0 && dest != null) {
2767 for (int i = finishTo; i >= 0; i--) {
2768 ActivityRecord r = activities.get(i);
2769 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002770 r.info.name.equals(dest.getClassName())) {
2771 finishTo = i;
2772 parent = r;
2773 foundParentInTask = true;
2774 break;
2775 }
2776 }
2777 }
2778
2779 IActivityController controller = mService.mController;
2780 if (controller != null) {
2781 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2782 if (next != null) {
2783 // ask watcher if this is allowed
2784 boolean resumeOK = true;
2785 try {
2786 resumeOK = controller.activityResuming(next.packageName);
2787 } catch (RemoteException e) {
2788 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002789 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002790 }
2791
2792 if (!resumeOK) {
2793 return false;
2794 }
2795 }
2796 }
2797 final long origId = Binder.clearCallingIdentity();
2798 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002799 ActivityRecord r = activities.get(i);
2800 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002801 // Only return the supplied result for the first activity finished
2802 resultCode = Activity.RESULT_CANCELED;
2803 resultData = null;
2804 }
2805
2806 if (parent != null && foundParentInTask) {
2807 final int parentLaunchMode = parent.info.launchMode;
2808 final int destIntentFlags = destIntent.getFlags();
2809 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2810 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2811 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2812 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2813 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2814 } else {
2815 try {
2816 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2817 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002818 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002819 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002820 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002821 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002822 foundParentInTask = res == ActivityManager.START_SUCCESS;
2823 } catch (RemoteException e) {
2824 foundParentInTask = false;
2825 }
2826 requestFinishActivityLocked(parent.appToken, resultCode,
2827 resultData, "navigate-up", true);
2828 }
2829 }
2830 Binder.restoreCallingIdentity(origId);
2831 return foundParentInTask;
2832 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002833 /**
2834 * Perform the common clean-up of an activity record. This is called both
2835 * as part of destroyActivityLocked() (when destroying the client-side
2836 * representation) and cleaning things up as a result of its hosting
2837 * processing going away, in which case there is no remaining client-side
2838 * state to destroy so only the cleanup here is needed.
2839 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002840 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2841 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 if (mResumedActivity == r) {
2843 mResumedActivity = null;
2844 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002845 if (mPausingActivity == r) {
2846 mPausingActivity = null;
2847 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002848 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849
2850 r.configDestroy = false;
2851 r.frozenBeforeDestroy = false;
2852
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002853 if (setState) {
2854 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2855 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002856 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002857 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002858 }
2859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 // Make sure this record is no longer in the pending finishes list.
2861 // This could happen, for example, if we are trimming activities
2862 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002863 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002864 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002865
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002866 // Remove any pending results.
2867 if (r.finishing && r.pendingResults != null) {
2868 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2869 PendingIntentRecord rec = apr.get();
2870 if (rec != null) {
2871 mService.cancelIntentSenderLocked(rec, false);
2872 }
2873 }
2874 r.pendingResults = null;
2875 }
2876
2877 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002878 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002879 }
2880
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002882 removeTimeoutsForActivityLocked(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002883 if (getMediaPlayer() == r) {
2884 mStackSupervisor.setMediaPlayingLocked(r, false);
2885 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002886 }
2887
2888 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002889 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002891 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002892 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002893 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 }
2895
Craig Mautner04a0ea62014-01-13 12:51:26 -08002896 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002897 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002898 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2899 r.makeFinishing();
2900 if (DEBUG_ADD_REMOVE) {
2901 RuntimeException here = new RuntimeException("here");
2902 here.fillInStackTrace();
2903 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002905 r.takeFromHistory();
2906 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002907 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002908 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002909 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002910 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002911 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002912 if (VALIDATE_TOKENS) {
2913 validateAppTokensLocked();
2914 }
Craig Mautner312ba862014-02-10 17:55:01 -08002915 final TaskRecord task = r.task;
2916 if (task != null && task.removeActivity(r)) {
2917 if (DEBUG_STACK) Slog.i(TAG,
2918 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07002919 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
2920 task.isOverHomeStack()) {
2921 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08002922 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002923 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08002924 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002925 cleanUpActivityServicesLocked(r);
2926 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002928
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 /**
2930 * Perform clean-up of service connections in an activity record.
2931 */
2932 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2933 // Throw away any services that have been bound by this activity.
2934 if (r.connections != null) {
2935 Iterator<ConnectionRecord> it = r.connections.iterator();
2936 while (it.hasNext()) {
2937 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002938 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 }
2940 r.connections = null;
2941 }
2942 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002943
Craig Mautneree2e45a2014-06-27 12:10:03 -07002944 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002945 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002946 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002947 mHandler.sendMessage(msg);
2948 }
2949
Craig Mautneree2e45a2014-06-27 12:10:03 -07002950 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002951 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002952 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002953 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2954 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2955 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2956 final ActivityRecord r = activities.get(activityNdx);
2957 if (r.finishing) {
2958 continue;
2959 }
2960 if (r.fullscreen) {
2961 lastIsOpaque = true;
2962 }
2963 if (owner != null && r.app != owner) {
2964 continue;
2965 }
2966 if (!lastIsOpaque) {
2967 continue;
2968 }
2969 // We can destroy this one if we have its icicle saved and
2970 // it is not in the process of pausing/stopping/finishing.
2971 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2972 && r.haveState && !r.visible && r.stopped
2973 && r.state != ActivityState.DESTROYING
2974 && r.state != ActivityState.DESTROYED) {
2975 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2976 + " resumed=" + mResumedActivity
2977 + " pausing=" + mPausingActivity);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002978 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002979 activityRemoved = true;
2980 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002981 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002982 }
2983 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002984 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002985 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002986 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002987 }
2988
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 /**
2990 * Destroy the current CLIENT SIDE instance of an activity. This may be
2991 * called both when actually finishing an activity, or when performing
2992 * a configuration switch where we destroy the current client-side object
2993 * but then create a new client-side object for this same HistoryRecord.
2994 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07002995 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002996 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002997 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2999 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003000 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003001 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002
3003 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003004
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003005 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003006
3007 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003008
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 if (hadApp) {
3010 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003011 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3013 mService.mHeavyWeightProcess = null;
3014 mService.mHandler.sendEmptyMessage(
3015 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3016 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003017 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003018 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003019 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003020 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
3022 }
3023
3024 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003025
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003026 try {
3027 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003028 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 r.configChangeFlags);
3030 } catch (Exception e) {
3031 // We can just ignore exceptions here... if the process
3032 // has crashed, our death notification will clean things
3033 // up.
3034 //Slog.w(TAG, "Exception thrown during finish", e);
3035 if (r.finishing) {
3036 removeActivityFromHistoryLocked(r);
3037 removedFromHistory = true;
3038 skipDestroy = true;
3039 }
3040 }
3041
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003043
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003044 // If the activity is finishing, we need to wait on removing it
3045 // from the list to give it a chance to do its cleanup. During
3046 // that time it may make calls back with its token so we need to
3047 // be able to find it on the list and so we don't want to remove
3048 // it from the list yet. Otherwise, we can just immediately put
3049 // it in the destroyed state since we are not removing it from the
3050 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003051 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003052 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3053 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003055 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003056 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3057 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003058 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003059 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003060 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003061 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 }
3063 } else {
3064 // remove this record from the history.
3065 if (r.finishing) {
3066 removeActivityFromHistoryLocked(r);
3067 removedFromHistory = true;
3068 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003069 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003071 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003072 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003073 }
3074 }
3075
3076 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003077
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 if (!mLRUActivities.remove(r) && hadApp) {
3079 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3080 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 return removedFromHistory;
3083 }
3084
Craig Mautnerd2328952013-03-05 12:46:26 -08003085 final void activityDestroyedLocked(IBinder token) {
3086 final long origId = Binder.clearCallingIdentity();
3087 try {
3088 ActivityRecord r = ActivityRecord.forToken(token);
3089 if (r != null) {
3090 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003091 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003092 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003093
3094 if (isInStackLocked(token) != null) {
3095 if (r.state == ActivityState.DESTROYING) {
3096 cleanUpActivityLocked(r, true, false);
3097 removeActivityFromHistoryLocked(r);
3098 }
3099 }
Craig Mautner05d29032013-05-03 13:40:13 -07003100 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003101 } finally {
3102 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103 }
3104 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003105
Craig Mautneree2e45a2014-06-27 12:10:03 -07003106 void releaseMediaResources() {
3107 if (isMediaPlaying() && !mHandler.hasMessages(STOP_MEDIA_PLAYING_TIMEOUT_MSG)) {
3108 final ActivityRecord r = getMediaPlayer();
3109 if (DEBUG_STATES) Slog.d(TAG, "releaseMediaResources activtyDisplay=" +
3110 mActivityContainer.mActivityDisplay + " mediaPlayer=" + r + " app=" + r.app +
3111 " thread=" + r.app.thread);
3112 if (r != null && r.app != null && r.app.thread != null) {
3113 try {
3114 r.app.thread.scheduleStopMediaPlaying(r.appToken);
3115 } catch (RemoteException e) {
3116 }
3117 mHandler.sendEmptyMessageDelayed(STOP_MEDIA_PLAYING_TIMEOUT_MSG, 500);
3118 } else {
3119 Slog.e(TAG, "releaseMediaResources: activity " + r + " no longer running");
3120 mediaResourcesReleased(r.appToken);
3121 }
3122 }
3123 }
3124
3125 final void mediaResourcesReleased(IBinder token) {
3126 mHandler.removeMessages(STOP_MEDIA_PLAYING_TIMEOUT_MSG);
3127 final ActivityRecord r = getMediaPlayer();
3128 if (r != null) {
3129 mStackSupervisor.mStoppingActivities.add(r);
3130 setMediaPlayer(null);
3131 }
3132 mStackSupervisor.resumeTopActivitiesLocked();
3133 }
3134
3135 boolean isMediaPlaying() {
3136 return isAttached() && mActivityContainer.mActivityDisplay.isMediaPlaying();
3137 }
3138
3139 void setMediaPlayer(ActivityRecord r) {
3140 if (isAttached()) {
3141 mActivityContainer.mActivityDisplay.setMediaPlaying(r);
3142 }
3143 }
3144
3145 ActivityRecord getMediaPlayer() {
3146 return isAttached() ? mActivityContainer.mActivityDisplay.mMediaPlayingActivity : null;
3147 }
3148
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003149 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3150 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003152 if (DEBUG_CLEANUP) Slog.v(
3153 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 + " with " + i + " entries");
3155 while (i > 0) {
3156 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003157 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003158 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003159 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003160 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003161 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003162 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 }
3164 }
3165 }
3166
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003167 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3168 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003169 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3170 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003171 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3172 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003173 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003174 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003175 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3176 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003177
3178 boolean hasVisibleActivities = false;
3179
3180 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003181 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003182 if (DEBUG_CLEANUP) Slog.v(
3183 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003184 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3185 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3186 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3187 final ActivityRecord r = activities.get(activityNdx);
3188 --i;
3189 if (DEBUG_CLEANUP) Slog.v(
3190 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3191 if (r.app == app) {
3192 boolean remove;
3193 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3194 // Don't currently have state for the activity, or
3195 // it is finishing -- always remove it.
3196 remove = true;
3197 } else if (r.launchCount > 2 &&
3198 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3199 // We have launched this activity too many times since it was
3200 // able to run, so give up and remove it.
3201 remove = true;
3202 } else {
3203 // The process may be gone, but the activity lives on!
3204 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003205 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003206 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003207 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003208 RuntimeException here = new RuntimeException("here");
3209 here.fillInStackTrace();
3210 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3211 + ": haveState=" + r.haveState
3212 + " stateNotNeeded=" + r.stateNotNeeded
3213 + " finishing=" + r.finishing
3214 + " state=" + r.state, here);
3215 }
3216 if (!r.finishing) {
3217 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3218 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3219 r.userId, System.identityHashCode(r),
3220 r.task.taskId, r.shortComponentName,
3221 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003222 if (r.state == ActivityState.RESUMED) {
3223 mService.updateUsageStats(r, false);
3224 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003225 }
3226 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003227
Craig Mautner0247fc82013-02-28 14:32:06 -08003228 } else {
3229 // We have the current state for this activity, so
3230 // it can be restarted later when needed.
3231 if (localLOGV) Slog.v(
3232 TAG, "Keeping entry, setting app to null");
3233 if (r.visible) {
3234 hasVisibleActivities = true;
3235 }
3236 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3237 + r);
3238 r.app = null;
3239 r.nowVisible = false;
3240 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003241 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003242 "App died, clearing saved state of " + r);
3243 r.icicle = null;
3244 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003245 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003246
Craig Mautnerd2328952013-03-05 12:46:26 -08003247 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003248 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003249 }
3250 }
3251
3252 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003254
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003255 final void updateTransitLocked(int transit, Bundle options) {
3256 if (options != null) {
3257 ActivityRecord r = topRunningActivityLocked(null);
3258 if (r != null && r.state != ActivityState.RESUMED) {
3259 r.updateOptionsLocked(options);
3260 } else {
3261 ActivityOptions.abort(options);
3262 }
3263 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003264 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003265 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003266
Craig Mautner21d24a22014-04-23 11:45:37 -07003267 void updateTaskMovement(TaskRecord task, boolean toFront) {
3268 if (task.isPersistable) {
3269 task.mLastTimeMoved = System.currentTimeMillis();
3270 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3271 // recently will be most negative, tasks sent to the bottom before that will be less
3272 // negative. Similarly for recent tasks moved to the top which will be most positive.
3273 if (!toFront) {
3274 task.mLastTimeMoved *= -1;
3275 }
3276 }
3277 }
3278
Craig Mautner84984fa2014-06-19 11:19:20 -07003279 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003280 final int top = mTaskHistory.size() - 1;
3281 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3282 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003283 if (task.taskType == homeStackTaskType) {
3284 if (DEBUG_TASKS || DEBUG_STACK)
3285 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003286 mTaskHistory.remove(taskNdx);
3287 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003288 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003289 mWindowManager.moveTaskToTop(task.taskId);
3290 return;
3291 }
3292 }
3293 }
3294
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003295 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003296 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297
Craig Mautner11bf9a52013-02-19 14:08:51 -08003298 final int numTasks = mTaskHistory.size();
3299 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003300 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003301 // nothing to do!
3302 if (reason != null &&
3303 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3304 ActivityOptions.abort(options);
3305 } else {
3306 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3307 }
3308 return;
3309 }
3310
Craig Mautnere0a38842013-12-16 16:14:02 -08003311 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003312
Craig Mautner11bf9a52013-02-19 14:08:51 -08003313 // Shift all activities with this task up to the top
3314 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003315 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003316
3317 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 if (reason != null &&
3319 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003320 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 ActivityRecord r = topRunningActivityLocked(null);
3322 if (r != null) {
3323 mNoAnimActivities.add(r);
3324 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003325 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003326 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003327 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003329
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003330 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331
Craig Mautner05d29032013-05-03 13:40:13 -07003332 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003333 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003334
3335 if (VALIDATE_TOKENS) {
3336 validateAppTokensLocked();
3337 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 }
3339
3340 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003341 * Worker method for rearranging history stack. Implements the function of moving all
3342 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003344 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3346 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003347 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003348 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 * @return Returns true if the move completed, false if not.
3350 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003351 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003352 final TaskRecord tr = taskForIdLocked(taskId);
3353 if (tr == null) {
3354 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3355 return false;
3356 }
3357
3358 Slog.i(TAG, "moveTaskToBack: " + tr);
3359
3360 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003361
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003362 // If we have a watcher, preflight the move before committing to it. First check
3363 // for *other* available tasks, but if none are available, then try again allowing the
3364 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003365 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003366 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 if (next == null) {
3368 next = topRunningActivityLocked(null, 0);
3369 }
3370 if (next != null) {
3371 // ask watcher if this is allowed
3372 boolean moveOK = true;
3373 try {
3374 moveOK = mService.mController.activityResuming(next.packageName);
3375 } catch (RemoteException e) {
3376 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003377 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003378 }
3379 if (!moveOK) {
3380 return false;
3381 }
3382 }
3383 }
3384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003385 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003386 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003387
Craig Mautner11bf9a52013-02-19 14:08:51 -08003388 mTaskHistory.remove(tr);
3389 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003390 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003391
Craig Mautnerc8143c62013-09-03 12:15:57 -07003392 // There is an assumption that moving a task to the back moves it behind the home activity.
3393 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003394 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003395 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3396 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003397 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003398 break;
3399 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003400 if (taskNdx == 1) {
3401 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003402 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003403 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003404 }
3405
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003406 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003407 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3408 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003409 ActivityRecord r = topRunningActivityLocked(null);
3410 if (r != null) {
3411 mNoAnimActivities.add(r);
3412 }
3413 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003414 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003415 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003416 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003417
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003418 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003419 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003420 }
3421
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003422 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003423 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
3424 final int taskToReturnTo = tr.getTaskToReturnTo();
3425 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3426 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003427 }
3428
Craig Mautner05d29032013-05-03 13:40:13 -07003429 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003430 return true;
3431 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003432
Craig Mautner8849a5e2013-04-02 16:41:03 -07003433 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003434 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003435 final Uri data = r.intent.getData();
3436 final String strData = data != null ? data.toSafeString() : null;
3437
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003438 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003439 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003440 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003441 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003442 }
3443
3444 /**
3445 * Make sure the given activity matches the current configuration. Returns
3446 * false if the activity had to be destroyed. Returns true if the
3447 * configuration is the same, or the activity will remain running as-is
3448 * for whatever reason. Ensures the HistoryRecord is updated with the
3449 * correct configuration and all other bookkeeping is handled.
3450 */
3451 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3452 int globalChanges) {
3453 if (mConfigWillChange) {
3454 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3455 "Skipping config check (will change): " + r);
3456 return true;
3457 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003458
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3460 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003461
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003462 // Short circuit: if the two configurations are the exact same
3463 // object (the common case), then there is nothing to do.
3464 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003465 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003466 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3467 "Configuration unchanged in " + r);
3468 return true;
3469 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003470
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 // We don't worry about activities that are finishing.
3472 if (r.finishing) {
3473 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3474 "Configuration doesn't matter in finishing " + r);
3475 r.stopFreezingScreenLocked(false);
3476 return true;
3477 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003478
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479 // Okay we now are going to make this activity have the new config.
3480 // But then we need to figure out how it needs to deal with that.
3481 Configuration oldConfig = r.configuration;
3482 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003483
3484 // Determine what has changed. May be nothing, if this is a config
3485 // that has come back from the app after going idle. In that case
3486 // we just want to leave the official config object now in the
3487 // activity and do nothing else.
3488 final int changes = oldConfig.diff(newConfig);
3489 if (changes == 0 && !r.forceNewConfig) {
3490 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3491 "Configuration no differences in " + r);
3492 return true;
3493 }
3494
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 // If the activity isn't currently running, just leave the new
3496 // configuration and it will pick that up next time it starts.
3497 if (r.app == null || r.app.thread == null) {
3498 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3499 "Configuration doesn't matter not running " + r);
3500 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003501 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502 return true;
3503 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003504
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003505 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003506 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3507 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3508 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003509 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003510 + ", newConfig=" + newConfig);
3511 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003512 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003513 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3514 r.configChangeFlags |= changes;
3515 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003516 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003517 if (r.app == null || r.app.thread == null) {
3518 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003519 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003520 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003521 } else if (r.state == ActivityState.PAUSING) {
3522 // A little annoying: we are waiting for this activity to
3523 // finish pausing. Let's not do anything now, but just
3524 // flag that it needs to be restarted when done pausing.
3525 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003526 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003527 r.configDestroy = true;
3528 return true;
3529 } else if (r.state == ActivityState.RESUMED) {
3530 // Try to optimize this case: the configuration is changing
3531 // and we need to restart the top, resumed activity.
3532 // Instead of doing the normal handshaking, just say
3533 // "restart!".
3534 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003535 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003536 relaunchActivityLocked(r, r.configChangeFlags, true);
3537 r.configChangeFlags = 0;
3538 } else {
3539 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003540 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003541 relaunchActivityLocked(r, r.configChangeFlags, false);
3542 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003544
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003545 // All done... tell the caller we weren't able to keep this
3546 // activity around.
3547 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003548 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003549
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 // Default case: the activity can handle this new configuration, so
3551 // hand it over. Note that we don't need to give it the new
3552 // configuration, since we always send configuration changes to all
3553 // process when they happen so it can just use whatever configuration
3554 // it last got.
3555 if (r.app != null && r.app.thread != null) {
3556 try {
3557 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003558 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 } catch (RemoteException e) {
3560 // If process died, whatever.
3561 }
3562 }
3563 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003564
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003565 return true;
3566 }
3567
Craig Mautnerc8143c62013-09-03 12:15:57 -07003568 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003569 int changes, boolean andResume) {
3570 List<ResultInfo> results = null;
3571 List<Intent> newIntents = null;
3572 if (andResume) {
3573 results = r.results;
3574 newIntents = r.newIntents;
3575 }
3576 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3577 + " with results=" + results + " newIntents=" + newIntents
3578 + " andResume=" + andResume);
3579 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003580 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003581 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003582
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003583 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003584
Craig Mautner34b73df2014-01-12 21:11:08 -08003585 mStackSupervisor.removeChildActivityContainers(r);
3586
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003587 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003588 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3589 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3590 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003591 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003592 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003593 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 // Note: don't need to call pauseIfSleepingLocked() here, because
3595 // the caller will only pass in 'andResume' if this activity is
3596 // currently resumed, which implies we aren't sleeping.
3597 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003598 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003599 }
3600
3601 if (andResume) {
3602 r.results = null;
3603 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003604 r.state = ActivityState.RESUMED;
3605 } else {
3606 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3607 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 }
3609
3610 return true;
3611 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003612
3613 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003614 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3615 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3616 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3617 final ActivityRecord r = activities.get(activityNdx);
3618 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003619 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003620 }
3621 if (r.fullscreen && !r.finishing) {
3622 return false;
3623 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003624 }
3625 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003626 final ActivityRecord r = ActivityRecord.forToken(token);
3627 if (r == null) {
3628 return false;
3629 }
3630 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3631 + " would have returned true for r=" + r);
3632 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003633 }
3634
3635 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003636 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3637 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3638 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3639 final ActivityRecord r = activities.get(activityNdx);
3640 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003641 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003642 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003643 }
3644 }
3645 }
3646
3647 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3648 boolean didSomething = false;
3649 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003650 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003651 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3652 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3653 int numActivities = activities.size();
3654 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3655 ActivityRecord r = activities.get(activityNdx);
3656 final boolean samePackage = r.packageName.equals(name)
3657 || (name == null && r.userId == userId);
3658 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3659 && (samePackage || r.task == lastTask)
3660 && (r.app == null || evenPersistent || !r.app.persistent)) {
3661 if (!doit) {
3662 if (r.finishing) {
3663 // If this activity is just finishing, then it is not
3664 // interesting as far as something to stop.
3665 continue;
3666 }
3667 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003668 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003669 if (r.isHomeActivity()) {
3670 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3671 Slog.i(TAG, "Skip force-stop again " + r);
3672 continue;
3673 } else {
3674 homeActivity = r.realActivity;
3675 }
3676 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003677 didSomething = true;
3678 Slog.i(TAG, " Force finishing activity " + r);
3679 if (samePackage) {
3680 if (r.app != null) {
3681 r.app.removed = true;
3682 }
3683 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003684 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003685 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003686 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3687 true)) {
3688 // r has been deleted from mActivities, accommodate.
3689 --numActivities;
3690 --activityNdx;
3691 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003692 }
3693 }
3694 }
3695 return didSomething;
3696 }
3697
Dianne Hackborn09233282014-04-30 11:33:59 -07003698 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003699 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003700 final TaskRecord task = mTaskHistory.get(taskNdx);
3701 ActivityRecord r = null;
3702 ActivityRecord top = null;
3703 int numActivities = 0;
3704 int numRunning = 0;
3705 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003706 if (activities.isEmpty()) {
3707 continue;
3708 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003709 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3710 continue;
3711 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003712 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3713 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003714
Craig Mautneraab647e2013-02-28 16:31:36 -08003715 // Initialize state for next task if needed.
3716 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3717 top = r;
3718 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003719 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003720
3721 // Add 'r' into the current task.
3722 numActivities++;
3723 if (r.app != null && r.app.thread != null) {
3724 numRunning++;
3725 }
3726
3727 if (localLOGV) Slog.v(
3728 TAG, r.intent.getComponent().flattenToShortString()
3729 + ": task=" + r.task);
3730 }
3731
3732 RunningTaskInfo ci = new RunningTaskInfo();
3733 ci.id = task.taskId;
3734 ci.baseActivity = r.intent.getComponent();
3735 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003736 ci.lastActiveTime = task.lastActiveTime;
3737
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003738 if (top.task != null) {
3739 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003740 }
3741 ci.numActivities = numActivities;
3742 ci.numRunning = numRunning;
3743 //System.out.println(
3744 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003745 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003746 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003747 }
3748
3749 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003750 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003751 if (DEBUG_SWITCH) Slog.d(
3752 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003753 if (top >= 0) {
3754 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3755 int activityTop = activities.size() - 1;
3756 if (activityTop > 0) {
3757 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3758 "unhandled-back", true);
3759 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003760 }
3761 }
3762
Craig Mautner6b74cb52013-09-27 17:02:21 -07003763 /**
3764 * Reset local parameters because an app's activity died.
3765 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003766 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003767 */
3768 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003769 if (mPausingActivity != null && mPausingActivity.app == app) {
3770 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3771 "App died while pausing: " + mPausingActivity);
3772 mPausingActivity = null;
3773 }
3774 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3775 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003776 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003777 }
3778
Craig Mautner19091252013-10-05 00:03:53 -07003779 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003780 }
3781
Craig Mautnercae015f2013-02-08 14:31:27 -08003782 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003783 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3784 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3785 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3786 final ActivityRecord r = activities.get(activityNdx);
3787 if (r.app == app) {
3788 Slog.w(TAG, " Force finishing activity "
3789 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003790 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003791 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003792 }
3793 }
3794 }
3795
Dianne Hackborn390517b2013-05-30 15:03:32 -07003796 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003797 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003798 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003799 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3800 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003801 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3802 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003803 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003804 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003805 if (printed) {
3806 header = null;
3807 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003808 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003809 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003810 }
3811
3812 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3813 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3814
3815 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003816 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3817 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003818 }
3819 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003820 final int top = mTaskHistory.size() - 1;
3821 if (top >= 0) {
3822 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3823 int listTop = list.size() - 1;
3824 if (listTop >= 0) {
3825 activities.add(list.get(listTop));
3826 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003827 }
3828 } else {
3829 ItemMatcher matcher = new ItemMatcher();
3830 matcher.build(name);
3831
Craig Mautneraab647e2013-02-28 16:31:36 -08003832 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3833 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3834 if (matcher.match(r1, r1.intent.getComponent())) {
3835 activities.add(r1);
3836 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003837 }
3838 }
3839 }
3840
3841 return activities;
3842 }
3843
3844 ActivityRecord restartPackage(String packageName) {
3845 ActivityRecord starting = topRunningActivityLocked(null);
3846
3847 // All activities that came from the package must be
3848 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003849 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3850 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3851 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3852 final ActivityRecord a = activities.get(activityNdx);
3853 if (a.info.packageName.equals(packageName)) {
3854 a.forceNewConfig = true;
3855 if (starting != null && a == starting && a.visible) {
3856 a.startFreezingScreenLocked(starting.app,
3857 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3858 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003859 }
3860 }
3861 }
3862
3863 return starting;
3864 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003865
Craig Mautner41db4a72014-05-07 17:20:56 -07003866 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003867 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003868 mWindowManager.removeTask(task.taskId);
3869 final ActivityRecord r = mResumedActivity;
3870 if (r != null && r.task == task) {
3871 mResumedActivity = null;
3872 }
3873
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003874 final int taskNdx = mTaskHistory.indexOf(task);
3875 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07003876 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
3877 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
3878 if (!nextTask.isOverHomeStack()) {
3879 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
3880 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003881 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003882 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003883 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07003884
3885 if (task.mActivities.isEmpty()) {
3886 final boolean isVoiceSession = task.voiceSession != null;
3887 if (isVoiceSession) {
3888 try {
3889 task.voiceSession.taskFinished(task.intent, task.taskId);
3890 } catch (RemoteException e) {
3891 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003892 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003893 if (task.autoRemoveFromRecents() || isVoiceSession) {
3894 // Task creator asked to remove this when done, or this task was a voice
3895 // interaction, so it should not remain on the recent tasks list.
3896 mService.mRecentTasks.remove(task);
Craig Mautnera228ae92014-07-09 05:44:55 -07003897 task.closeRecentsChain();
Craig Mautner41db4a72014-05-07 17:20:56 -07003898 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003899 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003900
3901 if (mTaskHistory.isEmpty()) {
3902 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3903 if (isOnHomeDisplay()) {
3904 mStackSupervisor.moveHomeStack(!isHomeStack());
3905 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003906 if (mStacks != null) {
3907 mStacks.remove(this);
3908 mStacks.add(0, this);
3909 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003910 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08003911 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003912 }
3913
Dianne Hackborn91097de2014-04-04 18:02:06 -07003914 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
3915 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
3916 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07003917 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
3918 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003919 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003920 return task;
3921 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003922
3923 ArrayList<TaskRecord> getAllTasks() {
3924 return new ArrayList<TaskRecord>(mTaskHistory);
3925 }
3926
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003927 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003928 task.stack = this;
3929 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003930 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003931 } else {
3932 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003933 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003934 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003935 if (!moving && task.voiceSession != null) {
3936 try {
3937 task.voiceSession.taskStarted(task.intent, task.taskId);
3938 } catch (RemoteException e) {
3939 }
3940 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003941 }
3942
3943 public int getStackId() {
3944 return mStackId;
3945 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003946
3947 @Override
3948 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003949 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3950 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003951 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952}