blob: 152f8d7fbd6f851e76c18eeaf812b6c8e7a8e582 [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;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070041import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070042import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070043import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
44
Dianne Hackborn89ad4562014-08-24 16:45:38 -070045import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070046import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070048import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080049import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080050import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080051import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080052import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070053import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054
55import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070056import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070057import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080059import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080061import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080067import android.content.res.Resources;
68import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080069import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070071import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.Handler;
74import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090075import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070077import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.os.RemoteException;
79import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080080import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070081import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070082import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070085import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080088import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
91import java.util.Iterator;
92import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080093import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094
95/**
96 * State and management of a single stack of activities.
97 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070098final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080099
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700100 // Ticks during which we check progress while waiting for an app to launch.
101 static final int LAUNCH_TICK = 500;
102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700103 // How long we wait until giving up on the last activity to pause. This
104 // is short because it directly impacts the responsiveness of starting the
105 // next activity.
106 static final int PAUSE_TIMEOUT = 500;
107
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700108 // How long we wait for the activity to tell us it has stopped before
109 // giving up. This is a good amount of time because we really need this
110 // from the application in order to get its saved state.
111 static final int STOP_TIMEOUT = 10*1000;
112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700113 // How long we wait until giving up on an activity telling us it has
114 // finished destroying itself.
115 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800118 // disabled.
119 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700121 // How long between activity launches that we consider safe to not warn
122 // the user about an unexpected activity being launched on top.
123 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 // Set to false to disable the preview that is shown while a new activity
126 // is being started.
127 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800128
Craig Mautner5eda9b32013-07-02 11:58:16 -0700129 // How long to wait for all background Activities to redraw following a call to
130 // convertToTranslucent().
131 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
132
Craig Mautnera61bc652013-10-28 15:43:18 -0700133 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135 enum ActivityState {
136 INITIALIZING,
137 RESUMED,
138 PAUSING,
139 PAUSED,
140 STOPPING,
141 STOPPED,
142 FINISHING,
143 DESTROYING,
144 DESTROYED
145 }
146
147 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700148 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700150 /**
151 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152 * running) activities. It contains #TaskRecord objects.
153 */
154 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
155
156 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800157 * Used for validating app tokens with window manager.
158 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800159 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 /**
162 * List of running activities, sorted by recent usage.
163 * The first entry in the list is the least recently used.
164 * It contains HistoryRecord objects.
165 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700166 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * Animations that for the current transition have requested not to
170 * be considered for the transition animation.
171 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 * When we are in the process of pausing an activity, before starting the
176 * next one, this variable holds the activity that is currently being paused.
177 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800178 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179
180 /**
181 * This is the last activity that we put into the paused state. This is
182 * used to determine if we need to do an activity transition while sleeping,
183 * when we normally hold the top activity paused.
184 */
185 ActivityRecord mLastPausedActivity = null;
186
187 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700188 * Activities that specify No History must be removed once the user navigates away from them.
189 * If the device goes to sleep with such an activity in the paused state then we save it here
190 * and finish it later if another activity replaces it on wakeup.
191 */
192 ActivityRecord mLastNoHistoryActivity = null;
193
194 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195 * Current activity that is resumed, or null if there is none.
196 */
197 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700200 * This is the last activity that has been started. It is only used to
201 * identify when multiple activities are started at once so that the user
202 * can be warned they may not be in the activity they think they are.
203 */
204 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800205
Craig Mautner5eda9b32013-07-02 11:58:16 -0700206 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
207 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
208 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
209 // Activity in mTranslucentActivityWaiting is notified via
210 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
211 // background activity being drawn then the same call will be made with a true value.
212 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700213 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700214 new ArrayList<ActivityRecord>();
215
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700216 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700217 * Set when we know we are going to be calling updateConfiguration()
218 * soon, so want to skip intermediate config checks.
219 */
220 boolean mConfigWillChange;
221
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700222 long mLaunchStartTime = 0;
223 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800224
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800225 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800226 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
227 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
228 */
229 private ActivityRecord mLastScreenshotActivity = null;
230 private Bitmap mLastScreenshotBitmap = null;
231
Craig Mautner858d8a62013-04-23 17:08:34 -0700232 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700233
Craig Mautnerc00204b2013-03-05 15:02:14 -0800234 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800236 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
237 ArrayList<ActivityStack> mStacks;
238 /** The attached Display's unique identifier, or -1 if detached */
239 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240
Craig Mautner27084302013-03-25 08:05:25 -0700241 /** Run all ActivityStacks through this */
242 final ActivityStackSupervisor mStackSupervisor;
243
Craig Mautner544efa72014-09-04 16:41:20 -0700244 /** Used to keep resumeTopActivityLocked() from being entered recursively */
245 private boolean inResumeTopActivity;
246
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700247 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700248 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
249 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
250 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
251 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700252 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700253 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700254 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700255
256 static class ScheduleDestroyArgs {
257 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700258 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700259 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700261 mReason = reason;
262 }
263 }
264
Zoran Marcetaf958b322012-08-09 20:27:12 +0900265 final Handler mHandler;
266
267 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268 //public Handler() {
269 // if (localLOGV) Slog.v(TAG, "Handler started!");
270 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700271 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900272 super(looper);
273 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274
Zoran Marcetaf958b322012-08-09 20:27:12 +0900275 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276 public void handleMessage(Message msg) {
277 switch (msg.what) {
278 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280 // We don't at this point know if the activity is fullscreen,
281 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800282 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 synchronized (mService) {
284 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700285 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700286 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700287 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800288 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700290 case LAUNCH_TICK_MSG: {
291 ActivityRecord r = (ActivityRecord)msg.obj;
292 synchronized (mService) {
293 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700294 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700295 }
296 }
297 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800299 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 // We don't at this point know if the activity is fullscreen,
301 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800302 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800303 synchronized (mService) {
304 activityDestroyedLocked(r != null ? r.appToken : null);
305 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700307 case STOP_TIMEOUT_MSG: {
308 ActivityRecord r = (ActivityRecord)msg.obj;
309 // We don't at this point know if the activity is fullscreen,
310 // so we need to be conservative and assume it isn't.
311 Slog.w(TAG, "Activity stop timeout for " + r);
312 synchronized (mService) {
313 if (r.isInHistory()) {
314 activityStoppedLocked(r, null, null, null);
315 }
316 }
317 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700318 case DESTROY_ACTIVITIES_MSG: {
319 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
320 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700321 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700322 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700323 } break;
324 case TRANSLUCENT_TIMEOUT_MSG: {
325 synchronized (mService) {
326 notifyActivityDrawnLocked(null);
327 }
328 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700329 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700330 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700331 final ActivityRecord r = getVisibleBehindActivity();
332 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700333 if (r != null) {
334 mService.killAppAtUsersRequest(r.app, null);
335 }
336 }
337 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700338 }
339 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800340 }
341
Craig Mautner34b73df2014-01-12 21:11:08 -0800342 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800343 int count = 0;
344 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
345 count += mTaskHistory.get(taskNdx).mActivities.size();
346 }
347 return count;
348 }
349
Craig Mautner4a1cb222013-12-04 16:14:06 -0800350 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
351 mActivityContainer = activityContainer;
352 mStackSupervisor = activityContainer.getOuter();
353 mService = mStackSupervisor.mService;
354 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
355 mWindowManager = mService.mWindowManager;
356 mStackId = activityContainer.mStackId;
357 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700358 }
Craig Mautner5962b122012-10-05 14:45:52 -0700359
Amith Yamasani734983f2014-03-04 16:48:05 -0800360 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100361 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800362 */
Kenny Guy2a764942014-04-02 13:29:20 +0100363 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100364 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100365 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
366 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800367 }
368 return false;
369 }
370
371 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100372 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700373 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
374 }
375
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800377 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700378 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
379 if (r != null) {
380 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800381 }
382 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700383 return null;
384 }
385
386 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800387 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
388 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800389 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800390 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
391 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800392 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800393 return r;
394 }
395 }
396 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 return null;
398 }
399
400 /**
401 * This is a simplified version of topRunningActivityLocked that provides a number of
402 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800403 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700404 * @param token If non-null, any history records matching this token will be skipped.
405 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800406 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700407 * @return Returns the HistoryRecord of the next activity on the stack.
408 */
409 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800410 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
411 TaskRecord task = mTaskHistory.get(taskNdx);
412 if (task.taskId == taskId) {
413 continue;
414 }
415 ArrayList<ActivityRecord> activities = task.mActivities;
416 for (int i = activities.size() - 1; i >= 0; --i) {
417 final ActivityRecord r = activities.get(i);
418 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800419 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800420 return r;
421 }
422 }
423 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700424 return null;
425 }
426
Craig Mautner8849a5e2013-04-02 16:41:03 -0700427 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700428 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
429 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700430 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
431 final ActivityRecord r = activities.get(activityNdx);
432 if (!r.finishing) {
433 return r;
434 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700435 }
436 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700437 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700438 }
439
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700440 final TaskRecord topTask() {
441 final int size = mTaskHistory.size();
442 if (size > 0) {
443 return mTaskHistory.get(size - 1);
444 }
445 return null;
446 }
447
Craig Mautnerd2328952013-03-05 12:46:26 -0800448 TaskRecord taskForIdLocked(int id) {
449 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
450 final TaskRecord task = mTaskHistory.get(taskNdx);
451 if (task.taskId == id) {
452 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800453 }
454 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700455 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 }
457
Craig Mautnerd2328952013-03-05 12:46:26 -0800458 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800459 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800460 if (r != null) {
461 final TaskRecord task = r.task;
Dianne Hackborn925e2b32014-09-03 16:40:49 -0700462 if (task != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800463 if (task.stack != this) Slog.w(TAG,
464 "Illegal state! task does not point to stack it is in.");
465 return r;
466 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800467 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800468 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800469 }
470
Craig Mautner2420ead2013-04-01 17:13:20 -0700471 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700472 final boolean hadit = mLRUActivities.remove(r);
473 mLRUActivities.add(r);
474 return hadit;
475 }
476
Craig Mautnerde4ef022013-04-07 19:01:33 -0700477 final boolean isHomeStack() {
478 return mStackId == HOME_STACK_ID;
479 }
480
Craig Mautnere0a38842013-12-16 16:14:02 -0800481 final boolean isOnHomeDisplay() {
482 return isAttached() &&
483 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
484 }
485
486 final void moveToFront() {
487 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800488 if (isOnHomeDisplay()) {
489 mStackSupervisor.moveHomeStack(isHomeStack());
490 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800491 mStacks.remove(this);
492 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800493 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800494 }
495
496 final boolean isAttached() {
497 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800498 }
499
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700500 /**
501 * Returns the top activity in any existing task matching the given
502 * Intent. Returns null if no such task is found.
503 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700504 ActivityRecord findTaskLocked(ActivityRecord target) {
505 Intent intent = target.intent;
506 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700507 ComponentName cls = intent.getComponent();
508 if (info.targetActivity != null) {
509 cls = new ComponentName(info.packageName, info.targetActivity);
510 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700511 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700512 boolean isDocument = intent != null & intent.isDocument();
513 // If documentData is non-null then it must match the existing task data.
514 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800515
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700516 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800517 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
518 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700519 if (task.voiceSession != null) {
520 // We never match voice sessions; those always run independently.
521 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
522 continue;
523 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700524 if (task.userId != userId) {
525 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700526 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700527 continue;
528 }
Craig Mautner000f0022013-02-26 15:04:29 -0800529 final ActivityRecord r = task.getTopActivity();
530 if (r == null || r.finishing || r.userId != userId ||
531 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700532 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800533 continue;
534 }
535
Craig Mautnerd00f4742014-03-12 14:17:26 -0700536 final Intent taskIntent = task.intent;
537 final Intent affinityIntent = task.affinityIntent;
538 final boolean taskIsDocument;
539 final Uri taskDocumentData;
540 if (taskIntent != null && taskIntent.isDocument()) {
541 taskIsDocument = true;
542 taskDocumentData = taskIntent.getData();
543 } else if (affinityIntent != null && affinityIntent.isDocument()) {
544 taskIsDocument = true;
545 taskDocumentData = affinityIntent.getData();
546 } else {
547 taskIsDocument = false;
548 taskDocumentData = null;
549 }
550
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700551 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700552 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700553 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700554 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700555 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
556 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700557 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700558 return r;
559 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700560 } else if (taskIntent != null && taskIntent.getComponent() != null &&
561 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700562 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700563 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800564 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700565 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
566 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800567 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700568 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
569 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700570 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700571 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800572 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700573 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
574 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800575 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700576 } else if (DEBUG_TASKS) {
577 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700578 }
579 }
580
581 return null;
582 }
583
584 /**
585 * Returns the first activity (starting from the top of the stack) that
586 * is the same as the given activity. Returns null if no such activity
587 * is found.
588 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700589 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700590 ComponentName cls = intent.getComponent();
591 if (info.targetActivity != null) {
592 cls = new ComponentName(info.packageName, info.targetActivity);
593 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700594 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700595
Craig Mautner000f0022013-02-26 15:04:29 -0800596 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700597 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100598 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700599 return null;
600 }
601 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800602 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
603 ActivityRecord r = activities.get(activityNdx);
604 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 //Slog.i(TAG, "Found matching class!");
606 //dump();
607 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
608 return r;
609 }
610 }
611 }
612
613 return null;
614 }
615
Amith Yamasani742a6712011-05-04 14:49:28 -0700616 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700617 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700618 */
Craig Mautner93529a42013-10-04 15:03:13 -0700619 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800620 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700621 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800622 }
623 mCurrentUser = userId;
624
625 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800626 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800627 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700628 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100629 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700630 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
631 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800632 mTaskHistory.remove(i);
633 mTaskHistory.add(task);
634 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800635 // Use same value for i.
636 } else {
637 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800638 }
639 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700640 if (VALIDATE_TOKENS) {
641 validateAppTokensLocked();
642 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700643 }
644
Craig Mautner2420ead2013-04-01 17:13:20 -0700645 void minimalResumeActivityLocked(ActivityRecord r) {
646 r.state = ActivityState.RESUMED;
647 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
648 + " (starting new instance)");
649 r.stopped = false;
650 mResumedActivity = r;
651 r.task.touchActiveTime();
652 mService.addRecentTaskLocked(r.task);
653 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700654 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700655 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700656 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700657 }
658
Dianne Hackborncee04b52013-07-03 17:01:28 -0700659 private void startLaunchTraces() {
660 if (mFullyDrawnStartTime != 0) {
661 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
662 }
663 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
664 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
665 }
666
667 private void stopFullyDrawnTraceIfNeeded() {
668 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
669 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
670 mFullyDrawnStartTime = 0;
671 }
672 }
673
Craig Mautnere79d42682013-04-01 19:01:53 -0700674 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700675 if (r.displayStartTime == 0) {
676 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
677 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700678 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700679 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700680 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700681 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700682 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700683 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700684 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700685 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800686
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700687 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700688 // Make sure that there is no activity waiting for this to launch.
689 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
690 r.displayStartTime = r.fullyDrawnStartTime = 0;
691 } else {
692 mStackSupervisor.removeTimeoutsForActivityLocked(r);
693 mStackSupervisor.scheduleIdleTimeoutLocked(r);
694 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700695 }
696
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800697 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800698 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800699 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
700 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
701 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
702 activities.get(activityNdx).setSleeping(false);
703 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800704 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800705 }
706
Craig Mautner0eea92c2013-05-16 13:35:39 -0700707 /**
708 * @return true if something must be done before going to sleep.
709 */
710 boolean checkReadyForSleepLocked() {
711 if (mResumedActivity != null) {
712 // Still have something resumed; can't sleep until it is paused.
713 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
714 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700715 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700716 return true;
717 }
718 if (mPausingActivity != null) {
719 // Still waiting for something to pause; can't sleep yet.
720 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
721 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800722 }
723
Craig Mautner0eea92c2013-05-16 13:35:39 -0700724 return false;
725 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800726
Craig Mautner0eea92c2013-05-16 13:35:39 -0700727 void goToSleep() {
728 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800729
Craig Mautner0eea92c2013-05-16 13:35:39 -0700730 // Make sure any stopped but visible activities are now sleeping.
731 // This ensures that the activity's onStop() is called.
732 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
733 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
734 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
735 final ActivityRecord r = activities.get(activityNdx);
736 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
737 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800738 }
739 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800740 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700741 }
Craig Mautner59c00972012-07-30 12:10:24 -0700742
Winson Chung376543b2014-05-21 17:43:28 -0700743 /**
744 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
745 * again when requested.
746 */
747 private void invalidateLastScreenshot() {
748 mLastScreenshotActivity = null;
749 if (mLastScreenshotBitmap != null) {
750 mLastScreenshotBitmap.recycle();
751 }
752 mLastScreenshotBitmap = null;
753 }
754
Dianne Hackbornd2835932010-12-13 16:28:46 -0800755 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700756 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800757 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700758 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800759 return null;
760 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800761
Winson Chung083baf92014-07-11 10:32:42 -0700762 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700763 // This is an optimization -- since we never show Home or Recents within Recents itself,
764 // we can just go ahead and skip taking the screenshot if this is the home stack. In
765 // the case where the most recent task is not the task that was supplied, then the stack
766 // has changed, so invalidate the last screenshot().
767 invalidateLastScreenshot();
768 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700769 return null;
770 }
771
Winson Chung48a10a52014-08-27 14:36:51 -0700772 int w = mService.mThumbnailWidth;
773 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800774 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800775 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700776 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800777 || mLastScreenshotBitmap.getWidth() != w
778 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700779 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800780 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700781 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700782 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800783 }
784 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700785 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700786 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800787 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800788 }
Winson Chung376543b2014-05-21 17:43:28 -0700789 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800790 return null;
791 }
792
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700793 /**
794 * Start pausing the currently resumed activity. It is an error to call this if there
795 * is already an activity being paused or there is no resumed activity.
796 *
797 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
798 * @param uiSleeping True if this is happening with the user interface going to sleep (the
799 * screen turning off).
800 * @param resuming True if this is being called as part of resuming the top activity, so
801 * we shouldn't try to instigate a resume here.
802 * @param dontWait True if the caller does not want to wait for the pause to complete. If
803 * set to true, we will immediately complete the pause here before returning.
804 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
805 * it to tell us when it is done.
806 */
807 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
808 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800809 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700810 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
811 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800812 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700813 ActivityRecord prev = mResumedActivity;
814 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700815 if (!resuming) {
816 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
817 mStackSupervisor.resumeTopActivitiesLocked();
818 }
819 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700820 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800821
822 if (mActivityContainer.mParentActivity == null) {
823 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700824 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800825 }
826
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700827 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
828 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800830 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700832 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
833 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 prev.state = ActivityState.PAUSING;
835 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700836 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700837 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Alan Viverettea2ff6112014-07-14 17:46:27 -0700838 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task)) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700839 prev.updateThumbnail(screenshotActivities(prev), null);
840 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700841 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842
843 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800844
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 if (prev.app != null && prev.app.thread != null) {
846 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
847 try {
848 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700849 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700851 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800852 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700853 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 } catch (Exception e) {
855 // Ignore exception, if process died other code will cleanup.
856 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800857 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700859 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 }
861 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800862 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700863 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700864 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700865 }
866
867 // If we are not going to sleep, we want to ensure the device is
868 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700869 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700870 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700871 }
872
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800873 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700874 // Have the window manager pause its key dispatching until the new
875 // activity has started. If we're pausing the activity just because
876 // the screen is being turned off and the UI is sleeping, don't interrupt
877 // key dispatch; the same activity will pick it up again on wakeup.
878 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 } else {
881 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
882 }
883
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700884 if (dontWait) {
885 // If the caller said they don't want to wait for the pause, then complete
886 // the pause now.
887 completePauseLocked(false);
888 return false;
889
890 } else {
891 // Schedule a pause timeout in case the app doesn't respond.
892 // We don't give it much time because this directly impacts the
893 // responsiveness seen by the user.
894 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
895 msg.obj = prev;
896 prev.pauseTime = SystemClock.uptimeMillis();
897 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
898 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
899 return true;
900 }
901
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902 } else {
903 // This activity failed to schedule the
904 // pause, so just treat it as being paused now.
905 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700906 if (!resuming) {
907 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
908 }
909 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700910 }
911 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700912
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700913 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800915 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916
Craig Mautnerd2328952013-03-05 12:46:26 -0800917 final ActivityRecord r = isInStackLocked(token);
918 if (r != null) {
919 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
920 if (mPausingActivity == r) {
921 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
922 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700923 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800924 } else {
925 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
926 r.userId, System.identityHashCode(r), r.shortComponentName,
927 mPausingActivity != null
928 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700929 }
930 }
931 }
932
Craig Mautnera0026042014-04-23 11:45:37 -0700933 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
934 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700935 if (r.state != ActivityState.STOPPING) {
936 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
937 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
938 return;
939 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700940 if (persistentState != null) {
941 r.persistentState = persistentState;
942 mService.notifyTaskPersisterLocked(r.task, false);
943 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700944 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700945 if (icicle != null) {
946 // If icicle is null, this is happening due to a timeout, so we
947 // haven't really saved the state.
948 r.icicle = icicle;
949 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800950 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700951 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700952 }
953 if (!r.stopped) {
954 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
955 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
956 r.stopped = true;
957 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700958 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
959 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700960 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700961 if (r.finishing) {
962 r.clearOptionsLocked();
963 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700964 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700965 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700966 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700967 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700968 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800969 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700970 }
971 }
972 }
973
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700974 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800975 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700976 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800977
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700979 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800980 if (prev.finishing) {
981 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700982 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800983 } else if (prev.app != null) {
984 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
985 if (prev.waitingVisible) {
986 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
989 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800990 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800991 if (prev.configDestroy) {
992 // The previous is being paused because the configuration
993 // is changing, which means it is actually stopping...
994 // To juggle the fact that we are also starting a new
995 // instance right now, we need to first completely stop
996 // the current instance before starting the new one.
997 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700998 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700999 } else if (!hasVisibleBehindActivity()) {
1000 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001001 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001002 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001003 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1004 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001005 // If we already have a few activities waiting to stop,
1006 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001007 // them out. Or if r is the last of activity of the last task the stack
1008 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001009 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001010 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001011 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001012 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 }
1014 }
1015 } else {
1016 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1017 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001018 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001019 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001020 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001021
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001022 if (resumeNext) {
1023 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1024 if (!mService.isSleepingOrShuttingDown()) {
1025 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1026 } else {
1027 mStackSupervisor.checkReadyForSleepLocked();
1028 ActivityRecord top = topStack.topRunningActivityLocked(null);
1029 if (top == null || (prev != null && top != prev)) {
1030 // If there are no more activities available to run,
1031 // do resume anyway to start something. Also if the top
1032 // activity on the stack is not the just paused activity,
1033 // we need to go ahead and resume it to ensure we complete
1034 // an in-flight app switch.
1035 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1036 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001037 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001038 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001039
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001040 if (prev != null) {
1041 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042
Craig Mautner525f3d92013-05-07 14:01:50 -07001043 if (prev.app != null && prev.cpuTimeAtResume > 0
1044 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001045 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1046 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001047 if (diff > 0) {
1048 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1049 synchronized (bsi) {
1050 BatteryStatsImpl.Uid.Proc ps =
1051 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001052 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001053 if (ps != null) {
1054 ps.addForegroundTimeLocked(diff);
1055 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001056 }
1057 }
1058 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001059 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001060 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061 }
1062
1063 /**
1064 * Once we know that we have asked an application to put an activity in
1065 * the resumed state (either by launching it or explicitly telling it),
1066 * this function updates the rest of our state to match that fact.
1067 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001068 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001069 next.idle = false;
1070 next.results = null;
1071 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001072
1073 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1074 ProcessRecord app = next.task.mActivities.get(0).app;
1075 if (app != null && app != mService.mHomeProcess) {
1076 mService.mHomeProcess = app;
1077 }
1078 }
1079
Craig Mautner07566322013-09-26 16:42:55 -07001080 if (next.nowVisible) {
1081 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001082 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001083 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084
1085 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001086 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001088 mStackSupervisor.reportResumedActivityLocked(next);
1089
1090 next.resumeKeyDispatchingLocked();
1091 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092
1093 // Mark the point when the activity is resuming
1094 // TODO: To be more accurate, the mark should be before the onCreate,
1095 // not after the onResume. But for subsequent starts, onResume is fine.
1096 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001097 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001098 } else {
1099 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1100 }
Winson Chung376543b2014-05-21 17:43:28 -07001101
1102 // If we are resuming the activity that we had last screenshotted, then we know it will be
1103 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1104 if (next == mLastScreenshotActivity) {
1105 invalidateLastScreenshot();
1106 }
George Mount6ba042b2014-07-28 11:12:28 -07001107 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001108
1109 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1110 // When resuming an activity, require it to call requestVisibleBehind() again.
1111 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1112 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001113 }
1114
Craig Mautnere3a00d72014-04-16 08:31:19 -07001115 private void setVisibile(ActivityRecord r, boolean visible) {
1116 r.visible = visible;
1117 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001118 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001119 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001120 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001121 container.setVisible(visible);
1122 }
1123 }
1124
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001125 // Find the first visible activity above the passed activity and if it is translucent return it
1126 // otherwise return null;
1127 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1128 TaskRecord task = r.task;
1129 if (task == null) {
1130 return null;
1131 }
1132
1133 ActivityStack stack = task.stack;
1134 if (stack == null) {
1135 return null;
1136 }
1137
1138 int stackNdx = mStacks.indexOf(stack);
1139
1140 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1141 int taskNdx = tasks.indexOf(task);
1142
1143 ArrayList<ActivityRecord> activities = task.mActivities;
1144 int activityNdx = activities.indexOf(r) + 1;
1145
1146 final int numStacks = mStacks.size();
1147 while (stackNdx < numStacks) {
1148 tasks = mStacks.get(stackNdx).mTaskHistory;
1149 final int numTasks = tasks.size();
1150 while (taskNdx < numTasks) {
1151 activities = tasks.get(taskNdx).mActivities;
1152 final int numActivities = activities.size();
1153 while (activityNdx < numActivities) {
1154 final ActivityRecord activity = activities.get(activityNdx);
1155 if (!activity.finishing) {
1156 return activity.fullscreen ? null : activity;
1157 }
1158 ++activityNdx;
1159 }
1160 activityNdx = 0;
1161 ++taskNdx;
1162 }
1163 taskNdx = 0;
1164 ++stackNdx;
1165 }
1166
1167 return null;
1168 }
1169
Jose Lima7ba71252014-04-30 12:59:27 -07001170 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1171 // If so, this stack is hidden, otherwise it is visible.
1172 private boolean isStackVisible() {
1173 if (!isAttached()) {
1174 return false;
1175 }
1176
1177 if (mStackSupervisor.isFrontStack(this)) {
1178 return true;
1179 }
1180
Jose Lima729cb232014-05-05 15:59:40 -07001181 /**
1182 * Start at the task above this one and go up, looking for a visible
1183 * fullscreen activity, or a translucent activity that requested the
1184 * wallpaper to be shown behind it.
1185 */
Jose Lima7ba71252014-04-30 12:59:27 -07001186 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1187 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1188 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001189 final TaskRecord task = tasks.get(taskNdx);
1190 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001191 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1192 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001193
1194 // Conditions for an activity to obscure the stack we're
1195 // examining:
1196 // 1. Not Finishing AND Visible AND:
1197 // 2. Either:
1198 // - Full Screen Activity OR
1199 // - On top of Home and our stack is NOT home
1200 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001201 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001202 return false;
1203 }
1204 }
1205 }
1206 }
1207
1208 return true;
1209 }
1210
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 /**
1212 * Make sure that all activities that need to be visible (that is, they
1213 * currently can be seen by the user) actually are.
1214 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001215 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1216 ActivityRecord top = topRunningActivityLocked(null);
1217 if (top == null) {
1218 return;
1219 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001220 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001221 TAG, "ensureActivitiesVisible behind " + top
1222 + " configChanges=0x" + Integer.toHexString(configChanges));
1223
Craig Mautner5eda9b32013-07-02 11:58:16 -07001224 if (mTranslucentActivityWaiting != top) {
1225 mUndrawnActivitiesBelowTopTranslucent.clear();
1226 if (mTranslucentActivityWaiting != null) {
1227 // Call the callback with a timeout indication.
1228 notifyActivityDrawnLocked(null);
1229 mTranslucentActivityWaiting = null;
1230 }
1231 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1232 }
1233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 // If the top activity is not fullscreen, then we need to
1235 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001236 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001237 boolean behindFullscreen = !isStackVisible();
1238
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001239 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001240 final TaskRecord task = mTaskHistory.get(taskNdx);
1241 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001242 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1243 final ActivityRecord r = activities.get(activityNdx);
1244 if (r.finishing) {
1245 continue;
1246 }
1247 if (aboveTop && r != top) {
1248 continue;
1249 }
1250 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001251 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1252 // but must be drawn initially for the animation as though they were visible.
1253 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001254 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001255 TAG, "Make visible? " + r + " finishing=" + r.finishing
1256 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001257
Craig Mautnerd44711d2013-02-23 11:24:36 -08001258 // First: if this is not the current activity being started, make
1259 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001260 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001261 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001262 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001263
1264 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001265 // This activity needs to be visible, but isn't even
1266 // running... get it started, but don't resume it
1267 // at this point.
1268 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1269 if (r != starting) {
1270 r.startFreezingScreenLocked(r.app, configChanges);
1271 }
1272 if (!r.visible || r.mLaunchTaskBehind) {
1273 if (DEBUG_VISBILITY) Slog.v(
1274 TAG, "Starting and making visible: " + r);
1275 setVisibile(r, true);
1276 }
1277 if (r != starting) {
1278 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001279 }
1280
1281 } else if (r.visible) {
1282 // If this activity is already visible, then there is nothing
1283 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001284 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001286 try {
George Mount6ba042b2014-07-28 11:12:28 -07001287 if (r.returningOptions != null) {
1288 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1289 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001290 }
1291 } catch(RemoteException e) {
1292 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001293 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001294 // This activity is not currently visible, but is running.
1295 // Tell it to become visible.
1296 r.visible = true;
1297 if (r.state != ActivityState.RESUMED && r != starting) {
1298 // If this activity is paused, tell it
1299 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001300 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001301 TAG, "Making visible and scheduling visibility: " + r);
1302 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001303 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001304 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001305 mUndrawnActivitiesBelowTopTranslucent.add(r);
1306 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001307 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001308 r.sleeping = false;
1309 r.app.pendingUiClean = true;
1310 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1311 r.stopFreezingScreenLocked(false);
1312 } catch (Exception e) {
1313 // Just skip on any failure; we'll make it
1314 // visible when it next restarts.
1315 Slog.w(TAG, "Exception thrown making visibile: "
1316 + r.intent.getComponent(), e);
1317 }
1318 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320
Craig Mautnerd44711d2013-02-23 11:24:36 -08001321 // Aggregate current change flags.
1322 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001323
Craig Mautnerd44711d2013-02-23 11:24:36 -08001324 if (r.fullscreen) {
1325 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001326 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1327 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001328 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001329 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001330 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001331 }
1332 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001333 if (DEBUG_VISBILITY) Slog.v(
1334 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1335 + " state=" + r.state
1336 + " behindFullscreen=" + behindFullscreen);
1337 // Now for any activities that aren't visible to the user, make
1338 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001339 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001340 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001341 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001342 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001343 switch (r.state) {
1344 case STOPPING:
1345 case STOPPED:
1346 if (r.app != null && r.app.thread != null) {
1347 if (DEBUG_VISBILITY) Slog.v(
1348 TAG, "Scheduling invisibility: " + r);
1349 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1350 }
1351 break;
1352
1353 case INITIALIZING:
1354 case RESUMED:
1355 case PAUSING:
1356 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001357 // This case created for transitioning activities from
1358 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001359 if (getVisibleBehindActivity() == r) {
1360 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001361 } else {
1362 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1363 mStackSupervisor.mStoppingActivities.add(r);
1364 }
1365 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001366 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001367 break;
1368
1369 default:
1370 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001371 }
1372 } catch (Exception e) {
1373 // Just skip on any failure; we'll make it
1374 // visible when it next restarts.
1375 Slog.w(TAG, "Exception thrown making hidden: "
1376 + r.intent.getComponent(), e);
1377 }
1378 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001379 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381 }
1382 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001383 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001384
1385 if (mTranslucentActivityWaiting != null &&
1386 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1387 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1388 notifyActivityDrawnLocked(null);
1389 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 }
Craig Mautner58547802013-03-05 08:23:53 -08001391
George Mount6ba042b2014-07-28 11:12:28 -07001392 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001393 mTranslucentActivityWaiting = r;
1394 mUndrawnActivitiesBelowTopTranslucent.clear();
1395 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1396 }
1397
1398 /**
1399 * Called as activities below the top translucent activity are redrawn. When the last one is
1400 * redrawn notify the top activity by calling
1401 * {@link Activity#onTranslucentConversionComplete}.
1402 *
1403 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1404 * occurred and the activity will be notified immediately.
1405 */
1406 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001407 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001408 if ((r == null)
1409 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1410 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1411 // The last undrawn activity below the top has just been drawn. If there is an
1412 // opaque activity at the top, notify it that it can become translucent safely now.
1413 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1414 mTranslucentActivityWaiting = null;
1415 mUndrawnActivitiesBelowTopTranslucent.clear();
1416 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1417
Craig Mautner71dd1b62014-02-18 15:48:52 -08001418 if (waitingActivity != null) {
1419 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1420 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1421 try {
1422 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1423 waitingActivity.appToken, r != null);
1424 } catch (RemoteException e) {
1425 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001426 }
1427 }
1428 }
1429 }
1430
Craig Mautnera61bc652013-10-28 15:43:18 -07001431 /** If any activities below the top running one are in the INITIALIZING state and they have a
1432 * starting window displayed then remove that starting window. It is possible that the activity
1433 * in this state will never resumed in which case that starting window will be orphaned. */
1434 void cancelInitializingActivities() {
1435 final ActivityRecord topActivity = topRunningActivityLocked(null);
1436 boolean aboveTop = true;
1437 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1438 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1439 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1440 final ActivityRecord r = activities.get(activityNdx);
1441 if (aboveTop) {
1442 if (r == topActivity) {
1443 aboveTop = false;
1444 }
1445 continue;
1446 }
1447
1448 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1449 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1450 r.mStartingWindowShown = false;
1451 mWindowManager.removeAppStartingWindow(r.appToken);
1452 }
1453 }
1454 }
1455 }
1456
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001457 /**
1458 * Ensure that the top activity in the stack is resumed.
1459 *
1460 * @param prev The previously resumed activity, for when in the process
1461 * of pausing; can be null to call from elsewhere.
1462 *
1463 * @return Returns true if something is being resumed, or false if
1464 * nothing happened.
1465 */
1466 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001467 return resumeTopActivityLocked(prev, null);
1468 }
1469
1470 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner544efa72014-09-04 16:41:20 -07001471 if (inResumeTopActivity) {
1472 // Don't even start recursing.
1473 return false;
1474 }
1475
1476 boolean result = false;
1477 try {
1478 // Protect against recursion.
1479 inResumeTopActivity = true;
1480 result = resumeTopActivityInnerLocked(prev, options);
1481 } finally {
1482 inResumeTopActivity = false;
1483 }
1484 return result;
1485 }
1486
1487 final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001488 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1489
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001490 if (!mService.mBooting && !mService.mBooted) {
1491 // Not ready yet!
1492 return false;
1493 }
1494
Craig Mautnerdf88d732014-01-27 09:21:32 -08001495 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001496 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001497 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001498 // Do not resume this stack if its parent is not resumed.
1499 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1500 return false;
1501 }
1502
Craig Mautnera61bc652013-10-28 15:43:18 -07001503 cancelInitializingActivities();
1504
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505 // Find the first activity that is not finishing.
1506 ActivityRecord next = topRunningActivityLocked(null);
1507
1508 // Remember how we'll process this pause/resume situation, and ensure
1509 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001510 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1511 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512
Craig Mautner84984fa2014-06-19 11:19:20 -07001513 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514 if (next == null) {
1515 // There are no more activities! Let's just start up the
1516 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001517 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001518 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001519 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001520 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001521 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1522 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1523 return isOnHomeDisplay() &&
1524 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001525 }
1526
1527 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001528
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001530 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1531 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001532 // Make sure we have executed any pending transitions, since there
1533 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001534 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001536 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001537 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001538 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Jorim Jaggi4c499be2014-08-27 15:31:31 +02001539
1540 // Make sure to notify Keyguard as well if it is waiting for an activity to be drawn.
1541 mStackSupervisor.notifyActivityDrawnForKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 return false;
1543 }
1544
Craig Mautner525f3d92013-05-07 14:01:50 -07001545 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001546 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001547 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001548 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001549 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001550 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001551 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001552 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001553 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001554 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001555 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001556 } else if (!isOnHomeDisplay()) {
1557 return false;
1558 } else if (!isHomeStack()){
1559 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001560 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001561 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1562 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
louis_chang2d094e92015-01-21 19:01:52 +08001563 return mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001564 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001565 }
1566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 // If we are sleeping, and there is no resumed activity, and the top
1568 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001569 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001570 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001571 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 // Make sure we have executed any pending transitions, since there
1573 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001574 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001576 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001577 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001578 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 return false;
1580 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001581
1582 // Make sure that the user who owns this activity is started. If not,
1583 // we will just leave it as is because someone should be bringing
1584 // another user's activities to the top of the stack.
1585 if (mService.mStartedUsers.get(next.userId) == null) {
1586 Slog.w(TAG, "Skipping resume of top activity " + next
1587 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001588 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001589 return false;
1590 }
1591
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001592 // The activity may be waiting for stop, but that is no longer
1593 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001594 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001595 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001596 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001597 mStackSupervisor.mWaitingVisibleActivities.remove(next);
louis_chang1110f552014-12-19 17:30:45 +08001598 next.waitingVisible = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599
1600 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1601
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001602 // If we are currently pausing an activity, then don't do anything
1603 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001604 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001605 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1606 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001607 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001608 return false;
1609 }
1610
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001611 // Okay we are now going to start a switch, to 'next'. We may first
1612 // have to pause the current activity, but this is an important point
1613 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001614 // XXX "App Redirected" dialog is getting too many false positives
1615 // at this point, so turn off for now.
1616 if (false) {
1617 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1618 long now = SystemClock.uptimeMillis();
1619 final boolean inTime = mLastStartedActivity.startTime != 0
1620 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1621 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1622 final int nextUid = next.info.applicationInfo.uid;
1623 if (inTime && lastUid != nextUid
1624 && lastUid != next.launchedFromUid
1625 && mService.checkPermission(
1626 android.Manifest.permission.STOP_APP_SWITCHES,
1627 -1, next.launchedFromUid)
1628 != PackageManager.PERMISSION_GRANTED) {
1629 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1630 } else {
1631 next.startTime = now;
1632 mLastStartedActivity = next;
1633 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001634 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001635 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001636 mLastStartedActivity = next;
1637 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001638 }
Craig Mautner58547802013-03-05 08:23:53 -08001639
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 // We need to start pausing the current activity so the top one
1641 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001642 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1643 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001644 if (mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001645 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001646 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001647 }
1648 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001649 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1650 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001651 // At this point we want to put the upcoming activity's process
1652 // at the top of the LRU list, since we know we will be needing it
1653 // very soon and it would be a waste to let it get killed if it
1654 // happens to be sitting towards the end.
1655 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001656 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001657 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001658 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 return true;
1660 }
1661
Christopher Tated3f175c2012-06-14 14:16:54 -07001662 // If the most recent activity was noHistory but was only stopped rather
1663 // than stopped+finished because the device went to sleep, we need to make
1664 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001665 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001666 !mLastNoHistoryActivity.finishing) {
1667 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1668 " on new resume");
1669 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1670 null, "no-history", false);
1671 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001672 }
1673
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001674 if (prev != null && prev != next) {
1675 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1676 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001677 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001678 if (DEBUG_SWITCH) Slog.v(
1679 TAG, "Resuming top, waiting visible to hide: " + prev);
1680 } else {
1681 // The next activity is already visible, so hide the previous
1682 // activity's windows right now so we can show the new one ASAP.
1683 // We only do this if the previous is finishing, which should mean
1684 // it is on top of the one being resumed so hiding it quickly
1685 // is good. Otherwise, we want to do the normal route of allowing
1686 // the resumed activity to be shown so we can decide if the
1687 // previous should actually be hidden depending on whether the
1688 // new one is found to be full-screen or not.
1689 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001690 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1692 + prev + ", waitingVisible="
1693 + (prev != null ? prev.waitingVisible : null)
1694 + ", nowVisible=" + next.nowVisible);
1695 } else {
1696 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1697 + prev + ", waitingVisible="
1698 + (prev != null ? prev.waitingVisible : null)
1699 + ", nowVisible=" + next.nowVisible);
1700 }
1701 }
1702 }
1703
Dianne Hackborne7f97212011-02-24 14:40:20 -08001704 // Launching this app's activity, make sure the app is no longer
1705 // considered stopped.
1706 try {
1707 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001708 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001709 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001710 } catch (IllegalArgumentException e) {
1711 Slog.w(TAG, "Failed trying to unstop package "
1712 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001713 }
1714
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 // We are starting up the next activity, so tell the window manager
1716 // that the previous one will be hidden soon. This way it can know
1717 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001718 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 if (prev != null) {
1720 if (prev.finishing) {
1721 if (DEBUG_TRANSITION) Slog.v(TAG,
1722 "Prepare close transition: prev=" + prev);
1723 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001724 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001725 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001727 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001728 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1729 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001731 mWindowManager.setAppWillBeHidden(prev.appToken);
1732 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001734 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001736 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001737 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001739 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001740 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001741 : next.mLaunchTaskBehind
1742 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1743 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 }
1745 }
1746 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001747 mWindowManager.setAppWillBeHidden(prev.appToken);
1748 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 }
Craig Mautner967212c2013-04-13 21:10:58 -07001750 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001751 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001753 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001754 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001756 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 }
1758 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001759
1760 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001761 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001762 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1763 if (opts != null) {
1764 resumeAnimOptions = opts.toBundle();
1765 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001766 next.applyOptionsLocked();
1767 } else {
1768 next.clearOptionsLocked();
1769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770
Craig Mautnercf910b02013-04-23 11:23:27 -07001771 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 if (next.app != null && next.app.thread != null) {
1773 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1774
1775 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001776 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001778 // schedule launch ticks to collect information about slow apps.
1779 next.startLaunchTickingLocked();
1780
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001781 ActivityRecord lastResumedActivity =
1782 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 ActivityState lastState = next.state;
1784
1785 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001786
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001787 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 next.state = ActivityState.RESUMED;
1789 mResumedActivity = next;
1790 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001791 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001792 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001794 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795
1796 // Have the window manager re-evaluate the orientation of
1797 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001798 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001799 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001800 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001801 mService.mConfiguration,
1802 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1803 if (config != null) {
1804 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001805 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001806 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001808
Craig Mautner525f3d92013-05-07 14:01:50 -07001809 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 // The configuration update wasn't able to keep the existing
1811 // instance of the activity, and instead started a new one.
1812 // We should be all done, but let's just make sure our activity
1813 // is still at the top and schedule another run if something
1814 // weird happened.
1815 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001816 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 "Activity config changed during resume: " + next
1818 + ", new next: " + nextNext);
1819 if (nextNext != next) {
1820 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001821 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 }
louis_chang3d9c4312014-12-02 12:38:05 +08001823 if (next == mLastScreenshotActivity) {
1824 invalidateLastScreenshot();
1825 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001826 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001827 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001828 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001829 return true;
1830 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001831 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001832 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833 }
Craig Mautner58547802013-03-05 08:23:53 -08001834
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 try {
1836 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001837 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001838 if (a != null) {
1839 final int N = a.size();
1840 if (!next.finishing && N > 0) {
1841 if (DEBUG_RESULTS) Slog.v(
1842 TAG, "Delivering results to " + next
1843 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001844 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845 }
1846 }
1847
1848 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001849 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001850 }
1851
1852 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001853 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001855
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001856 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001857 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001858 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001859 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001860 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001861 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001862 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001863
Craig Mautner0eea92c2013-05-16 13:35:39 -07001864 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001865
Craig Mautnerac6f8432013-07-17 13:24:59 -07001866 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001867 } catch (Exception e) {
1868 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001869 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1870 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001872 if (lastStack != null) {
1873 lastStack.mResumedActivity = lastResumedActivity;
1874 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001875 Slog.i(TAG, "Restarting because process died: " + next);
1876 if (!next.hasBeenLaunched) {
1877 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001878 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1879 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001880 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001881 next.appToken, next.packageName, next.theme,
1882 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001883 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1884 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 }
George Mount2c92c972014-03-20 09:38:23 -07001886 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001887 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001888 return true;
1889 }
1890
1891 // From this point on, if something goes wrong there is no way
1892 // to recover the activity.
1893 try {
1894 next.visible = true;
1895 completeResumeLocked(next);
1896 } catch (Exception e) {
1897 // If any exception gets thrown, toss away this
1898 // activity and try the next one.
1899 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001900 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001901 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001902 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903 return true;
1904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 next.stopped = false;
1906
1907 } else {
1908 // Whoops, need to restart this activity!
1909 if (!next.hasBeenLaunched) {
1910 next.hasBeenLaunched = true;
1911 } else {
1912 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001913 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001914 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001915 mService.compatibilityInfoForPackageLocked(
1916 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001918 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001919 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001920 }
1921 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1922 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001923 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001924 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001925 }
1926
Craig Mautnercf910b02013-04-23 11:23:27 -07001927 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001928 return true;
1929 }
1930
Craig Mautnerac6f8432013-07-17 13:24:59 -07001931 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001932 // If this is being moved to the top by another activity or being launched from the home
1933 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001934 if (isOnHomeDisplay()) {
1935 ActivityStack lastStack = mStackSupervisor.getLastStack();
1936 final boolean fromHome = lastStack.isHomeStack();
1937 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001938 task.setTaskToReturnTo(fromHome
1939 ? lastStack.topTask() == null
1940 ? HOME_ACTIVITY_TYPE
1941 : lastStack.topTask().taskType
1942 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001943 }
1944 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001945 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001946 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001947
Craig Mautnerac6f8432013-07-17 13:24:59 -07001948 mTaskHistory.remove(task);
1949 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001950 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001951 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001952 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001953 while (--taskNdx >= 0) {
1954 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001955 break;
1956 }
1957 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001958 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001959 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001960 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001961 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001962 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001963
Craig Mautner8849a5e2013-04-02 16:41:03 -07001964 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001965 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001966 TaskRecord rTask = r.task;
1967 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001968 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1969 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001970 // Last activity in task had been removed or ActivityManagerService is reusing task.
1971 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001972 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001973 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001974 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001975 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001976 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001977 if (!newTask) {
1978 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001979 boolean startIt = true;
1980 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1981 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001982 if (task.getTopActivity() == null) {
1983 // All activities in task are finishing.
1984 continue;
1985 }
Craig Mautner70a86932013-02-28 22:37:44 -08001986 if (task == r.task) {
1987 // Here it is! Now, if this is not yet visible to the
1988 // user, then just add it without starting; it will
1989 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001990 if (!startIt) {
1991 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1992 + task, new RuntimeException("here").fillInStackTrace());
1993 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001994 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001995 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1996 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001997 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001998 r.userId, r.info.configChanges, task.voiceSession != null,
1999 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002001 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002002 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002003 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002004 return;
2005 }
2006 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002007 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008 startIt = false;
2009 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002010 }
2011 }
2012
2013 // Place a new activity at top of stack, so it is next to interact
2014 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002015
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 // If we are not placing the new activity frontmost, we do not want
2017 // to deliver the onUserLeaving callback to the actual frontmost
2018 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002019 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002020 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002021 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2022 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 }
Craig Mautner70a86932013-02-28 22:37:44 -08002024
2025 task = r.task;
2026
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002028 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002029 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002030 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002031 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002032
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002033 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002034 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 // We want to show the starting preview window if we are
2036 // switching to a new task, or the next activity's process is
2037 // not currently running.
2038 boolean showStartingIcon = newTask;
2039 ProcessRecord proc = r.app;
2040 if (proc == null) {
2041 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2042 }
2043 if (proc == null || proc.thread == null) {
2044 showStartingIcon = true;
2045 }
2046 if (DEBUG_TRANSITION) Slog.v(TAG,
2047 "Prepare open transition: starting " + r);
2048 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002049 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002050 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002052 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002053 ? r.mLaunchTaskBehind
2054 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2055 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002056 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002057 mNoAnimActivities.remove(r);
2058 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002059 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002060 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002061 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002062 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002063 boolean doShow = true;
2064 if (newTask) {
2065 // Even though this activity is starting fresh, we still need
2066 // to reset it to make sure we apply affinities to move any
2067 // existing activities from other tasks in to it.
2068 // If the caller has requested that the target task be
2069 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002070 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 resetTaskIfNeededLocked(r, r);
2072 doShow = topRunningNonDelayedActivityLocked(null) == r;
2073 }
George Mount70778d72014-07-01 16:33:45 -07002074 } else if (options != null && new ActivityOptions(options).getAnimationType()
2075 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2076 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002078 if (r.mLaunchTaskBehind) {
2079 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2080 // tell WindowManager that r is visible even though it is at the back of the stack.
2081 mWindowManager.setAppVisibility(r.appToken, true);
2082 ensureActivitiesVisibleLocked(null, 0);
2083 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 // Figure out if we are transitioning from another activity that is
2085 // "has the same starting icon" as the next one. This allows the
2086 // window manager to keep the previous window it had previously
2087 // created, if it still had one.
2088 ActivityRecord prev = mResumedActivity;
2089 if (prev != null) {
2090 // We don't want to reuse the previous starting preview if:
2091 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002092 if (prev.task != r.task) {
2093 prev = null;
2094 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002095 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002096 else if (prev.nowVisible) {
2097 prev = null;
2098 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002100 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002101 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002102 mService.compatibilityInfoForPackageLocked(
2103 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002104 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002105 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002106 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002107 }
2108 } else {
2109 // If this is the first activity, don't do any fancy animations,
2110 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002111 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002112 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002113 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002114 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002115 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002116 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 }
2118 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002119 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 }
2121
2122 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002123 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 }
2125 }
2126
Dianne Hackbornbe707852011-11-11 14:32:10 -08002127 final void validateAppTokensLocked() {
2128 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002129 mValidateAppTokens.ensureCapacity(numActivities());
2130 final int numTasks = mTaskHistory.size();
2131 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2132 TaskRecord task = mTaskHistory.get(taskNdx);
2133 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002134 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002135 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002136 }
Craig Mautner000f0022013-02-26 15:04:29 -08002137 TaskGroup group = new TaskGroup();
2138 group.taskId = task.taskId;
2139 mValidateAppTokens.add(group);
2140 final int numActivities = activities.size();
2141 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2142 final ActivityRecord r = activities.get(activityNdx);
2143 group.tokens.add(r.appToken);
2144 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002145 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002146 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002147 }
2148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002149 /**
2150 * Perform a reset of the given task, if needed as part of launching it.
2151 * Returns the new HistoryRecord at the top of the task.
2152 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002153 /**
2154 * Helper method for #resetTaskIfNeededLocked.
2155 * We are inside of the task being reset... we'll either finish this activity, push it out
2156 * for another task, or leave it as-is.
2157 * @param task The task containing the Activity (taskTop) that might be reset.
2158 * @param forceReset
2159 * @return An ActivityOptions that needs to be processed.
2160 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002161 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002162 ActivityOptions topOptions = null;
2163
2164 int replyChainEnd = -1;
2165 boolean canMoveOptions = true;
2166
2167 // We only do this for activities that are not the root of the task (since if we finish
2168 // the root, we may no longer have the task!).
2169 final ArrayList<ActivityRecord> activities = task.mActivities;
2170 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002171 final int rootActivityNdx = task.findEffectiveRootIndex();
2172 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002173 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002174 if (target.frontOfTask)
2175 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002176
2177 final int flags = target.info.flags;
2178 final boolean finishOnTaskLaunch =
2179 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2180 final boolean allowTaskReparenting =
2181 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2182 final boolean clearWhenTaskReset =
2183 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2184
2185 if (!finishOnTaskLaunch
2186 && !clearWhenTaskReset
2187 && target.resultTo != null) {
2188 // If this activity is sending a reply to a previous
2189 // activity, we can't do anything with it now until
2190 // we reach the start of the reply chain.
2191 // XXX note that we are assuming the result is always
2192 // to the previous activity, which is almost always
2193 // the case but we really shouldn't count on.
2194 if (replyChainEnd < 0) {
2195 replyChainEnd = i;
2196 }
2197 } else if (!finishOnTaskLaunch
2198 && !clearWhenTaskReset
2199 && allowTaskReparenting
2200 && target.taskAffinity != null
2201 && !target.taskAffinity.equals(task.affinity)) {
2202 // If this activity has an affinity for another
2203 // task, then we need to move it out of here. We will
2204 // move it as far out of the way as possible, to the
2205 // bottom of the activity stack. This also keeps it
2206 // correctly ordered with any activities we previously
2207 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002208 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002209 final ActivityRecord bottom =
2210 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002211 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002212 if (bottom != null && target.taskAffinity != null
2213 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002214 // If the activity currently at the bottom has the
2215 // same task affinity as the one we are moving,
2216 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002217 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002218 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002219 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002220 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002221 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002222 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002223 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002224 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2225 + " out to new task " + target.task);
2226 }
2227
Craig Mautnere3a74d52013-02-22 14:14:58 -08002228 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002229 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230
Craig Mautner525f3d92013-05-07 14:01:50 -07002231 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002232 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2233 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002234 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002235 if (p.finishing) {
2236 continue;
2237 }
2238
Craig Mautnere3a74d52013-02-22 14:14:58 -08002239 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002240 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002241 topOptions = p.takeOptionsLocked();
2242 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002243 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002244 }
2245 }
2246 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002247 + task + " adding to task=" + targetTask
2248 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002249 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2250 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002251 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002252 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002253
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002254 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002255 }
2256
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002257 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002258 if (VALIDATE_TOKENS) {
2259 validateAppTokensLocked();
2260 }
2261
2262 replyChainEnd = -1;
2263 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2264 // If the activity should just be removed -- either
2265 // because it asks for it, or the task should be
2266 // cleared -- then finish it and anything that is
2267 // part of its reply chain.
2268 int end;
2269 if (clearWhenTaskReset) {
2270 // In this case, we want to finish this activity
2271 // and everything above it, so be sneaky and pretend
2272 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002273 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002274 } else if (replyChainEnd < 0) {
2275 end = i;
2276 } else {
2277 end = replyChainEnd;
2278 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002279 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002280 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002281 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282 if (p.finishing) {
2283 continue;
2284 }
2285 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002286 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002287 topOptions = p.takeOptionsLocked();
2288 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002289 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002290 }
2291 }
Craig Mautner58547802013-03-05 08:23:53 -08002292 if (DEBUG_TASKS) Slog.w(TAG,
2293 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002294 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002295 end--;
2296 srcPos--;
2297 }
2298 }
2299 replyChainEnd = -1;
2300 } else {
2301 // If we were in the middle of a chain, well the
2302 // activity that started it all doesn't want anything
2303 // special, so leave it all as-is.
2304 replyChainEnd = -1;
2305 }
2306 }
2307
2308 return topOptions;
2309 }
2310
2311 /**
2312 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2313 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2314 * @param affinityTask The task we are looking for an affinity to.
2315 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2316 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2317 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2318 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002319 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002320 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002321 int replyChainEnd = -1;
2322 final int taskId = task.taskId;
2323 final String taskAffinity = task.affinity;
2324
2325 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2326 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002327 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2328
2329 // Do not operate on or below the effective root Activity.
2330 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002331 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002332 if (target.frontOfTask)
2333 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002334
2335 final int flags = target.info.flags;
2336 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2337 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2338
2339 if (target.resultTo != null) {
2340 // If this activity is sending a reply to a previous
2341 // activity, we can't do anything with it now until
2342 // we reach the start of the reply chain.
2343 // XXX note that we are assuming the result is always
2344 // to the previous activity, which is almost always
2345 // the case but we really shouldn't count on.
2346 if (replyChainEnd < 0) {
2347 replyChainEnd = i;
2348 }
2349 } else if (topTaskIsHigher
2350 && allowTaskReparenting
2351 && taskAffinity != null
2352 && taskAffinity.equals(target.taskAffinity)) {
2353 // This activity has an affinity for our task. Either remove it if we are
2354 // clearing or move it over to our task. Note that
2355 // we currently punt on the case where we are resetting a
2356 // task that is not at the top but who has activities above
2357 // with an affinity to it... this is really not a normal
2358 // case, and we will need to later pull that task to the front
2359 // and usually at that point we will do the reset and pick
2360 // up those remaining activities. (This only happens if
2361 // someone starts an activity in a new task from an activity
2362 // in a task that is not currently on top.)
2363 if (forceReset || finishOnTaskLaunch) {
2364 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2365 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2366 for (int srcPos = start; srcPos >= i; --srcPos) {
2367 final ActivityRecord p = activities.get(srcPos);
2368 if (p.finishing) {
2369 continue;
2370 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002371 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002372 }
2373 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002374 if (taskInsertionPoint < 0) {
2375 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002376
Craig Mautner77878772013-03-04 19:46:24 -08002377 }
Craig Mautner77878772013-03-04 19:46:24 -08002378
2379 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2380 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2381 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2382 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002383 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002384 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002385 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002386
Craig Mautnere3a74d52013-02-22 14:14:58 -08002387 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2388 + " to stack at " + task,
2389 new RuntimeException("here").fillInStackTrace());
2390 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2391 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002392 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002393 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002394 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002395 if (VALIDATE_TOKENS) {
2396 validateAppTokensLocked();
2397 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002398
2399 // Now we've moved it in to place... but what if this is
2400 // a singleTop activity and we have put it on top of another
2401 // instance of the same activity? Then we drop the instance
2402 // below so it remains singleTop.
2403 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2404 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002405 int targetNdx = taskActivities.indexOf(target);
2406 if (targetNdx > 0) {
2407 ActivityRecord p = taskActivities.get(targetNdx - 1);
2408 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002409 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2410 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002411 }
2412 }
2413 }
2414 }
2415
2416 replyChainEnd = -1;
2417 }
2418 }
Craig Mautner77878772013-03-04 19:46:24 -08002419 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002420 }
2421
Craig Mautner8849a5e2013-04-02 16:41:03 -07002422 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002423 ActivityRecord newActivity) {
2424 boolean forceReset =
2425 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2426 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2427 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2428 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2429 forceReset = true;
2430 }
2431 }
2432
2433 final TaskRecord task = taskTop.task;
2434
2435 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2436 * for remaining tasks. Used for later tasks to reparent to task. */
2437 boolean taskFound = false;
2438
2439 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2440 ActivityOptions topOptions = null;
2441
Craig Mautner77878772013-03-04 19:46:24 -08002442 // Preserve the location for reparenting in the new task.
2443 int reparentInsertionPoint = -1;
2444
Craig Mautnere3a74d52013-02-22 14:14:58 -08002445 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2446 final TaskRecord targetTask = mTaskHistory.get(i);
2447
2448 if (targetTask == task) {
2449 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2450 taskFound = true;
2451 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002452 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2453 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002454 }
2455 }
2456
Craig Mautner70a86932013-02-28 22:37:44 -08002457 int taskNdx = mTaskHistory.indexOf(task);
2458 do {
2459 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2460 } while (taskTop == null && taskNdx >= 0);
2461
Craig Mautnere3a74d52013-02-22 14:14:58 -08002462 if (topOptions != null) {
2463 // If we got some ActivityOptions from an activity on top that
2464 // was removed from the task, propagate them to the new real top.
2465 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002466 taskTop.updateOptionsLocked(topOptions);
2467 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002468 topOptions.abort();
2469 }
2470 }
2471
2472 return taskTop;
2473 }
2474
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002475 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2476 String resultWho, int requestCode, int resultCode, Intent data) {
2477
2478 if (callingUid > 0) {
2479 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002480 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 }
2482
2483 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2484 + " : who=" + resultWho + " req=" + requestCode
2485 + " res=" + resultCode + " data=" + data);
2486 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2487 try {
2488 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2489 list.add(new ResultInfo(resultWho, requestCode,
2490 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002491 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002492 return;
2493 } catch (Exception e) {
2494 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2495 }
2496 }
2497
2498 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2499 }
2500
Craig Mautner04f0b702013-10-22 12:31:01 -07002501 private void adjustFocusedActivityLocked(ActivityRecord r) {
2502 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2503 ActivityRecord next = topRunningActivityLocked(null);
2504 if (next != r) {
2505 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002506 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2507 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002508 }
2509 }
Winson Chung648c83b2014-04-28 15:11:56 -07002510 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2511 if (top != null) {
2512 mService.setFocusedActivityLocked(top);
2513 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002514 }
2515 }
2516
Craig Mautnerf3333272013-04-22 10:55:53 -07002517 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002518 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2519 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2520 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2521 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002522 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002523 if (DEBUG_STATES) {
2524 Slog.d(TAG, "no-history finish of " + r);
2525 }
2526 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002527 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002528 } else {
2529 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2530 + " on stop because we're just sleeping");
2531 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002532 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002533 }
2534
2535 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002536 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002537 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 try {
2539 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002540 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2541 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 r.state = ActivityState.STOPPING;
2543 if (DEBUG_VISBILITY) Slog.v(
2544 TAG, "Stopping visible=" + r.visible + " for " + r);
2545 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002546 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002548 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002549 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002550 r.setSleeping(true);
2551 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002552 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002553 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002554 } catch (Exception e) {
2555 // Maybe just ignore exceptions here... if the process
2556 // has crashed, our death notification will clean things
2557 // up.
2558 Slog.w(TAG, "Exception thrown during pause", e);
2559 // Just in case, assume it to be stopped.
2560 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002561 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002562 r.state = ActivityState.STOPPED;
2563 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002564 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565 }
2566 }
2567 }
2568 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002569
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 /**
2571 * @return Returns true if the activity is being finished, false if for
2572 * some reason it is being left as-is.
2573 */
2574 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002575 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002576 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002577 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002578 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002579 + ", result=" + resultCode + ", data=" + resultData
2580 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002581 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002582 return false;
2583 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002584
Craig Mautnerd44711d2013-02-23 11:24:36 -08002585 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 return true;
2587 }
2588
Craig Mautnerd2328952013-03-05 12:46:26 -08002589 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002590 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2591 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2592 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2593 ActivityRecord r = activities.get(activityNdx);
2594 if (r.resultTo == self && r.requestCode == requestCode) {
2595 if ((r.resultWho == null && resultWho == null) ||
2596 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2597 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2598 false);
2599 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002600 }
2601 }
2602 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002603 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002604 }
2605
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002606 final void finishTopRunningActivityLocked(ProcessRecord app) {
2607 ActivityRecord r = topRunningActivityLocked(null);
2608 if (r != null && r.app == app) {
2609 // If the top running activity is from this crashing
2610 // process, then terminate it to avoid getting in a loop.
2611 Slog.w(TAG, " Force finishing activity "
2612 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002613 int taskNdx = mTaskHistory.indexOf(r.task);
2614 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002615 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002616 // Also terminate any activities below it that aren't yet
2617 // stopped, to avoid a situation where one will get
2618 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002619 --activityNdx;
2620 if (activityNdx < 0) {
2621 do {
2622 --taskNdx;
2623 if (taskNdx < 0) {
2624 break;
2625 }
2626 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2627 } while (activityNdx < 0);
2628 }
2629 if (activityNdx >= 0) {
2630 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002631 if (r.state == ActivityState.RESUMED
2632 || r.state == ActivityState.PAUSING
2633 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002634 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002635 Slog.w(TAG, " Force finishing activity "
2636 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002637 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002638 }
2639 }
2640 }
2641 }
2642 }
2643
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002644 final void finishVoiceTask(IVoiceInteractionSession session) {
2645 IBinder sessionBinder = session.asBinder();
2646 boolean didOne = false;
2647 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2648 TaskRecord tr = mTaskHistory.get(taskNdx);
2649 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2650 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2651 ActivityRecord r = tr.mActivities.get(activityNdx);
2652 if (!r.finishing) {
2653 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2654 false);
2655 didOne = true;
2656 }
2657 }
2658 }
2659 }
2660 if (didOne) {
2661 mService.updateOomAdjLocked();
2662 }
2663 }
2664
Craig Mautnerd2328952013-03-05 12:46:26 -08002665 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002666 ArrayList<ActivityRecord> activities = r.task.mActivities;
2667 for (int index = activities.indexOf(r); index >= 0; --index) {
2668 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002669 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002670 break;
2671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002672 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002673 }
2674 return true;
2675 }
2676
Dianne Hackborn5c607432012-02-28 14:44:19 -08002677 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2678 // send the result
2679 ActivityRecord resultTo = r.resultTo;
2680 if (resultTo != null) {
2681 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2682 + " who=" + r.resultWho + " req=" + r.requestCode
2683 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002684 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002685 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002686 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002687 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002688 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002689 if (r.info.applicationInfo.uid > 0) {
2690 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2691 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002692 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002693 }
2694 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2695 resultData);
2696 r.resultTo = null;
2697 }
2698 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2699
2700 // Make sure this HistoryRecord is not holding on to other resources,
2701 // because clients have remote IPC references to this object so we
2702 // can't assume that will go away and want to avoid circular IPC refs.
2703 r.results = null;
2704 r.pendingResults = null;
2705 r.newIntents = null;
2706 r.icicle = null;
2707 }
2708
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 /**
2710 * @return Returns true if this activity has been removed from the history
2711 * list, or false if it is still in the list and will be removed later.
2712 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002713 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2714 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002715 if (r.finishing) {
2716 Slog.w(TAG, "Duplicate finish request for " + r);
2717 return false;
2718 }
2719
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002720 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002721 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002723 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002724 task.taskId, r.shortComponentName, reason);
2725 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002726 final int index = activities.indexOf(r);
2727 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002728 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002729 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002730 // If the caller asked that this activity (and all above it)
2731 // be cleared when the task is reset, don't lose that information,
2732 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002733 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002734 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735 }
2736 }
2737
2738 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002739
2740 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002741
Dianne Hackborn5c607432012-02-28 14:44:19 -08002742 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002743
Craig Mautnerde4ef022013-04-07 19:01:33 -07002744 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002745 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002746 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002747 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002748 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002749 ? AppTransition.TRANSIT_TASK_CLOSE
2750 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002751
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002753 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002754
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002755 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002756 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2757 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002758 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002759 }
2760
Craig Mautneraea74a52014-03-08 14:23:10 -08002761 if (endTask) {
2762 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2763 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 } else if (r.state != ActivityState.PAUSING) {
2765 // If the activity is PAUSING, we will complete the finish once
2766 // it is done pausing; else we can just directly finish it here.
2767 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002768 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 } else {
2770 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2771 }
2772
2773 return false;
2774 }
2775
Craig Mautnerf3333272013-04-22 10:55:53 -07002776 static final int FINISH_IMMEDIATELY = 0;
2777 static final int FINISH_AFTER_PAUSE = 1;
2778 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779
Craig Mautnerf3333272013-04-22 10:55:53 -07002780 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002781 // First things first: if this activity is currently visible,
2782 // and the resumed activity is not yet visible, then hold off on
2783 // finishing until the resumed one becomes visible.
2784 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002785 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2786 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002787 if (mStackSupervisor.mStoppingActivities.size() > 3
2788 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 // If we already have a few activities waiting to stop,
2790 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002791 // them out. Or if r is the last of activity of the last task the stack
2792 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002793 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002794 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002795 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 }
2797 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002798 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2799 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002801 if (oomAdj) {
2802 mService.updateOomAdjLocked();
2803 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002804 return r;
2805 }
2806
2807 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002808 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002809 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002810 mStackSupervisor.mWaitingVisibleActivities.remove(r);
louis_chang1110f552014-12-19 17:30:45 +08002811 r.waitingVisible = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002812 if (mResumedActivity == r) {
2813 mResumedActivity = null;
2814 }
2815 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002816 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002817 r.state = ActivityState.FINISHING;
2818
2819 if (mode == FINISH_IMMEDIATELY
2820 || prevState == ActivityState.STOPPED
2821 || prevState == ActivityState.INITIALIZING) {
2822 // If this activity is already stopped, we can just finish
2823 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002824 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002825 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002826 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002827 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002828 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002829 if (DEBUG_CONTAINERS) Slog.d(TAG,
2830 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2831 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002832 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002833 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002834
2835 // Need to go through the full pause cycle to get this
2836 // activity into the stopped state and then finish it.
2837 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002838 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002839 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002840 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002841 return r;
2842 }
2843
Craig Mautneree36c772014-07-16 14:56:05 -07002844 void finishAllActivitiesLocked(boolean immediately) {
2845 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002846 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2847 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2848 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2849 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002850 noActivitiesInStack = false;
2851 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002852 continue;
2853 }
Craig Mautneree36c772014-07-16 14:56:05 -07002854 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002855 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2856 }
2857 }
Craig Mautneree36c772014-07-16 14:56:05 -07002858 if (noActivitiesInStack) {
2859 mActivityContainer.onTaskListEmptyLocked();
2860 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002861 }
2862
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002863 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2864 // Basic case: for simple app-centric recents, we need to recreate
2865 // the task if the affinity has changed.
2866 if (srec == null || srec.task.affinity == null ||
2867 !srec.task.affinity.equals(destAffinity)) {
2868 return true;
2869 }
2870 // Document-centric case: an app may be split in to multiple documents;
2871 // they need to re-create their task if this current activity is the root
2872 // of a document, unless simply finishing it will return them to the the
2873 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002874 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2875 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002876 // Okay, this activity is at the root of its task. What to do, what to do...
2877 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2878 // Finishing won't return to an application, so we need to recreate.
2879 return true;
2880 }
2881 // We now need to get the task below it to determine what to do.
2882 int taskIdx = mTaskHistory.indexOf(srec.task);
2883 if (taskIdx <= 0) {
2884 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2885 return false;
2886 }
2887 if (taskIdx == 0) {
2888 // At the bottom of the stack, nothing to go back to.
2889 return true;
2890 }
2891 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2892 if (!srec.task.affinity.equals(prevTask.affinity)) {
2893 // These are different apps, so need to recreate.
2894 return true;
2895 }
2896 }
2897 return false;
2898 }
2899
Craig Mautnerd2328952013-03-05 12:46:26 -08002900 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002901 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002902 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002903 final TaskRecord task = srec.task;
2904 final ArrayList<ActivityRecord> activities = task.mActivities;
2905 final int start = activities.indexOf(srec);
2906 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002907 return false;
2908 }
2909 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002910 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002911 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002912 final ComponentName dest = destIntent.getComponent();
2913 if (start > 0 && dest != null) {
2914 for (int i = finishTo; i >= 0; i--) {
2915 ActivityRecord r = activities.get(i);
2916 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002917 r.info.name.equals(dest.getClassName())) {
2918 finishTo = i;
2919 parent = r;
2920 foundParentInTask = true;
2921 break;
2922 }
2923 }
2924 }
2925
2926 IActivityController controller = mService.mController;
2927 if (controller != null) {
2928 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2929 if (next != null) {
2930 // ask watcher if this is allowed
2931 boolean resumeOK = true;
2932 try {
2933 resumeOK = controller.activityResuming(next.packageName);
2934 } catch (RemoteException e) {
2935 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002936 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002937 }
2938
2939 if (!resumeOK) {
2940 return false;
2941 }
2942 }
2943 }
2944 final long origId = Binder.clearCallingIdentity();
2945 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002946 ActivityRecord r = activities.get(i);
2947 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002948 // Only return the supplied result for the first activity finished
2949 resultCode = Activity.RESULT_CANCELED;
2950 resultData = null;
2951 }
2952
2953 if (parent != null && foundParentInTask) {
2954 final int parentLaunchMode = parent.info.launchMode;
2955 final int destIntentFlags = destIntent.getFlags();
2956 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2957 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2958 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2959 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2960 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2961 } else {
2962 try {
2963 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2964 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002965 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002966 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002967 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07002968 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002969 foundParentInTask = res == ActivityManager.START_SUCCESS;
2970 } catch (RemoteException e) {
2971 foundParentInTask = false;
2972 }
2973 requestFinishActivityLocked(parent.appToken, resultCode,
2974 resultData, "navigate-up", true);
2975 }
2976 }
2977 Binder.restoreCallingIdentity(origId);
2978 return foundParentInTask;
2979 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980 /**
2981 * Perform the common clean-up of an activity record. This is called both
2982 * as part of destroyActivityLocked() (when destroying the client-side
2983 * representation) and cleaning things up as a result of its hosting
2984 * processing going away, in which case there is no remaining client-side
2985 * state to destroy so only the cleanup here is needed.
2986 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002987 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2988 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 if (mResumedActivity == r) {
2990 mResumedActivity = null;
2991 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002992 if (mPausingActivity == r) {
2993 mPausingActivity = null;
2994 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002995 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996
2997 r.configDestroy = false;
2998 r.frozenBeforeDestroy = false;
2999
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003000 if (setState) {
3001 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3002 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003003 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003004 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003005 }
3006
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 // Make sure this record is no longer in the pending finishes list.
3008 // This could happen, for example, if we are trimming activities
3009 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003010 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003011 mStackSupervisor.mWaitingVisibleActivities.remove(r);
louis_chang1110f552014-12-19 17:30:45 +08003012 r.waitingVisible = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07003013
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003014 // Remove any pending results.
3015 if (r.finishing && r.pendingResults != null) {
3016 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3017 PendingIntentRecord rec = apr.get();
3018 if (rec != null) {
3019 mService.cancelIntentSenderLocked(rec, false);
3020 }
3021 }
3022 r.pendingResults = null;
3023 }
3024
3025 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003026 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 }
3028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003030 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003031 if (getVisibleBehindActivity() == r) {
3032 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003033 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003034 }
3035
3036 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003037 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003039 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003040 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003041 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 }
3043
Craig Mautner04a0ea62014-01-13 12:51:26 -08003044 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003045 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003046 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3047 r.makeFinishing();
3048 if (DEBUG_ADD_REMOVE) {
3049 RuntimeException here = new RuntimeException("here");
3050 here.fillInStackTrace();
3051 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003053 r.takeFromHistory();
3054 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003055 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003056 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003057 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003058 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003059 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003060 if (VALIDATE_TOKENS) {
3061 validateAppTokensLocked();
3062 }
Craig Mautner312ba862014-02-10 17:55:01 -08003063 final TaskRecord task = r.task;
3064 if (task != null && task.removeActivity(r)) {
3065 if (DEBUG_STACK) Slog.i(TAG,
3066 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003067 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3068 task.isOverHomeStack()) {
3069 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08003070 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003071 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08003072 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003073 cleanUpActivityServicesLocked(r);
3074 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003076
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077 /**
3078 * Perform clean-up of service connections in an activity record.
3079 */
3080 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3081 // Throw away any services that have been bound by this activity.
3082 if (r.connections != null) {
3083 Iterator<ConnectionRecord> it = r.connections.iterator();
3084 while (it.hasNext()) {
3085 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003086 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 }
3088 r.connections = null;
3089 }
3090 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003091
Craig Mautneree2e45a2014-06-27 12:10:03 -07003092 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003093 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003094 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003095 mHandler.sendMessage(msg);
3096 }
3097
Craig Mautneree2e45a2014-06-27 12:10:03 -07003098 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003099 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003100 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003101 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3102 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3103 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3104 final ActivityRecord r = activities.get(activityNdx);
3105 if (r.finishing) {
3106 continue;
3107 }
3108 if (r.fullscreen) {
3109 lastIsOpaque = true;
3110 }
3111 if (owner != null && r.app != owner) {
3112 continue;
3113 }
3114 if (!lastIsOpaque) {
3115 continue;
3116 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003117 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003118 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3119 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003120 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003121 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003122 activityRemoved = true;
3123 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003124 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003125 }
3126 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003127 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003128 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003129 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003130 }
3131
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003132 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3133 if (r.isDestroyable()) {
3134 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3135 + " resumed=" + mResumedActivity
3136 + " pausing=" + mPausingActivity + " for reason " + reason);
3137 return destroyActivityLocked(r, true, reason);
3138 }
3139 return false;
3140 }
3141
3142 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3143 String reason) {
3144 // Iterate over tasks starting at the back (oldest) first.
3145 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3146 int maxTasks = tasks.size() / 4;
3147 if (maxTasks < 1) {
3148 maxTasks = 1;
3149 }
3150 int numReleased = 0;
3151 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3152 final TaskRecord task = mTaskHistory.get(taskNdx);
3153 if (!tasks.contains(task)) {
3154 continue;
3155 }
3156 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3157 int curNum = 0;
3158 final ArrayList<ActivityRecord> activities = task.mActivities;
3159 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3160 final ActivityRecord activity = activities.get(actNdx);
3161 if (activity.app == app && activity.isDestroyable()) {
3162 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3163 + " in state " + activity.state + " resumed=" + mResumedActivity
3164 + " pausing=" + mPausingActivity + " for reason " + reason);
3165 destroyActivityLocked(activity, true, reason);
3166 if (activities.get(actNdx) != activity) {
3167 // Was removed from list, back up so we don't miss the next one.
3168 actNdx--;
3169 }
3170 curNum++;
3171 }
3172 }
3173 if (curNum > 0) {
3174 numReleased += curNum;
3175 maxTasks--;
3176 if (mTaskHistory.get(taskNdx) != task) {
3177 // The entire task got removed, back up so we don't miss the next one.
3178 taskNdx--;
3179 }
3180 }
3181 }
3182 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3183 return numReleased;
3184 }
3185
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003186 /**
3187 * Destroy the current CLIENT SIDE instance of an activity. This may be
3188 * called both when actually finishing an activity, or when performing
3189 * a configuration switch where we destroy the current client-side object
3190 * but then create a new client-side object for this same HistoryRecord.
3191 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003192 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003193 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003194 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3196 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003197 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003198 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003199
3200 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003201
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003202 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003203
3204 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003206 if (hadApp) {
3207 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003208 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003209 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3210 mService.mHeavyWeightProcess = null;
3211 mService.mHandler.sendEmptyMessage(
3212 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3213 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003214 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003215 // Update any services we are bound to that might care about whether
3216 // their client may have activities.
3217 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003218 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003219 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003220 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221 }
3222 }
3223
3224 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003225
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 try {
3227 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003228 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 r.configChangeFlags);
3230 } catch (Exception e) {
3231 // We can just ignore exceptions here... if the process
3232 // has crashed, our death notification will clean things
3233 // up.
3234 //Slog.w(TAG, "Exception thrown during finish", e);
3235 if (r.finishing) {
3236 removeActivityFromHistoryLocked(r);
3237 removedFromHistory = true;
3238 skipDestroy = true;
3239 }
3240 }
3241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003243
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003244 // If the activity is finishing, we need to wait on removing it
3245 // from the list to give it a chance to do its cleanup. During
3246 // that time it may make calls back with its token so we need to
3247 // be able to find it on the list and so we don't want to remove
3248 // it from the list yet. Otherwise, we can just immediately put
3249 // it in the destroyed state since we are not removing it from the
3250 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003252 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3253 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003254 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003255 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3257 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003258 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003259 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003260 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003261 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003262 }
3263 } else {
3264 // remove this record from the history.
3265 if (r.finishing) {
3266 removeActivityFromHistoryLocked(r);
3267 removedFromHistory = true;
3268 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003269 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003271 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003272 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003273 }
3274 }
3275
3276 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278 if (!mLRUActivities.remove(r) && hadApp) {
3279 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3280 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003281
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 return removedFromHistory;
3283 }
3284
Craig Mautnerd2328952013-03-05 12:46:26 -08003285 final void activityDestroyedLocked(IBinder token) {
3286 final long origId = Binder.clearCallingIdentity();
3287 try {
3288 ActivityRecord r = ActivityRecord.forToken(token);
3289 if (r != null) {
3290 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003291 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003292 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003293
3294 if (isInStackLocked(token) != null) {
3295 if (r.state == ActivityState.DESTROYING) {
3296 cleanUpActivityLocked(r, true, false);
3297 removeActivityFromHistoryLocked(r);
3298 }
3299 }
Craig Mautner05d29032013-05-03 13:40:13 -07003300 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003301 } finally {
3302 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 }
3304 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003305
Jose Lima4b6c6692014-08-12 17:41:12 -07003306 void releaseBackgroundResources() {
3307 if (hasVisibleBehindActivity() &&
3308 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3309 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautner64ccb702014-10-01 09:38:40 -07003310 if (r == topRunningActivityLocked(null)) {
3311 // Don't release the top activity if it has requested to run behind the next
3312 // activity.
3313 return;
3314 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003315 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3316 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003317 " thread=" + r.app.thread);
3318 if (r != null && r.app != null && r.app.thread != null) {
3319 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003320 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003321 } catch (RemoteException e) {
3322 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003323 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003324 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003325 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
3326 backgroundResourcesReleased(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003327 }
3328 }
3329 }
3330
Jose Lima4b6c6692014-08-12 17:41:12 -07003331 final void backgroundResourcesReleased(IBinder token) {
3332 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3333 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003334 if (r != null) {
3335 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003336 setVisibleBehindActivity(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003337 }
3338 mStackSupervisor.resumeTopActivitiesLocked();
3339 }
3340
Jose Lima4b6c6692014-08-12 17:41:12 -07003341 boolean hasVisibleBehindActivity() {
3342 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003343 }
3344
Jose Lima4b6c6692014-08-12 17:41:12 -07003345 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003346 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003347 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003348 }
3349 }
3350
Jose Lima4b6c6692014-08-12 17:41:12 -07003351 ActivityRecord getVisibleBehindActivity() {
3352 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003353 }
3354
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003355 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3356 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003358 if (DEBUG_CLEANUP) Slog.v(
3359 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 + " with " + i + " entries");
3361 while (i > 0) {
3362 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003363 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003364 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003366 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003368 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 }
3370 }
3371 }
3372
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003373 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3374 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003375 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3376 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003377 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3378 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003379 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003380 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003381 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3382 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003383
3384 boolean hasVisibleActivities = false;
3385
3386 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003387 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003388 if (DEBUG_CLEANUP) Slog.v(
3389 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003390 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3391 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3392 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3393 final ActivityRecord r = activities.get(activityNdx);
3394 --i;
3395 if (DEBUG_CLEANUP) Slog.v(
3396 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3397 if (r.app == app) {
3398 boolean remove;
3399 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3400 // Don't currently have state for the activity, or
3401 // it is finishing -- always remove it.
3402 remove = true;
3403 } else if (r.launchCount > 2 &&
3404 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3405 // We have launched this activity too many times since it was
3406 // able to run, so give up and remove it.
3407 remove = true;
3408 } else {
3409 // The process may be gone, but the activity lives on!
3410 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003411 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003412 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003413 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003414 RuntimeException here = new RuntimeException("here");
3415 here.fillInStackTrace();
3416 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3417 + ": haveState=" + r.haveState
3418 + " stateNotNeeded=" + r.stateNotNeeded
3419 + " finishing=" + r.finishing
3420 + " state=" + r.state, here);
3421 }
3422 if (!r.finishing) {
3423 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3424 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3425 r.userId, System.identityHashCode(r),
3426 r.task.taskId, r.shortComponentName,
3427 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003428 if (r.state == ActivityState.RESUMED) {
3429 mService.updateUsageStats(r, false);
3430 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003431 }
3432 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003433
Craig Mautner0247fc82013-02-28 14:32:06 -08003434 } else {
3435 // We have the current state for this activity, so
3436 // it can be restarted later when needed.
3437 if (localLOGV) Slog.v(
3438 TAG, "Keeping entry, setting app to null");
3439 if (r.visible) {
3440 hasVisibleActivities = true;
3441 }
3442 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3443 + r);
3444 r.app = null;
3445 r.nowVisible = false;
3446 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003447 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003448 "App died, clearing saved state of " + r);
3449 r.icicle = null;
3450 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003451 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003452
Craig Mautnerd2328952013-03-05 12:46:26 -08003453 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003454 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003455 }
3456 }
3457
3458 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003460
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003461 final void updateTransitLocked(int transit, Bundle options) {
3462 if (options != null) {
3463 ActivityRecord r = topRunningActivityLocked(null);
3464 if (r != null && r.state != ActivityState.RESUMED) {
3465 r.updateOptionsLocked(options);
3466 } else {
3467 ActivityOptions.abort(options);
3468 }
3469 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003470 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003471 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003472
Craig Mautner21d24a22014-04-23 11:45:37 -07003473 void updateTaskMovement(TaskRecord task, boolean toFront) {
3474 if (task.isPersistable) {
3475 task.mLastTimeMoved = System.currentTimeMillis();
3476 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3477 // recently will be most negative, tasks sent to the bottom before that will be less
3478 // negative. Similarly for recent tasks moved to the top which will be most positive.
3479 if (!toFront) {
3480 task.mLastTimeMoved *= -1;
3481 }
3482 }
3483 }
3484
Craig Mautner84984fa2014-06-19 11:19:20 -07003485 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003486 final int top = mTaskHistory.size() - 1;
3487 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3488 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003489 if (task.taskType == homeStackTaskType) {
3490 if (DEBUG_TASKS || DEBUG_STACK)
3491 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003492 mTaskHistory.remove(taskNdx);
3493 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003494 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003495 mWindowManager.moveTaskToTop(task.taskId);
3496 return;
3497 }
3498 }
3499 }
3500
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003501 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003502 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003503
Craig Mautner11bf9a52013-02-19 14:08:51 -08003504 final int numTasks = mTaskHistory.size();
3505 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003506 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003507 // nothing to do!
3508 if (reason != null &&
3509 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3510 ActivityOptions.abort(options);
3511 } else {
3512 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3513 }
3514 return;
3515 }
3516
Craig Mautnere0a38842013-12-16 16:14:02 -08003517 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003518
Craig Mautner11bf9a52013-02-19 14:08:51 -08003519 // Shift all activities with this task up to the top
3520 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003521 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003522
3523 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003524 if (reason != null &&
3525 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003526 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003527 ActivityRecord r = topRunningActivityLocked(null);
3528 if (r != null) {
3529 mNoAnimActivities.add(r);
3530 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003531 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003532 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003533 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003534 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003535
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003536 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003537
Craig Mautner05d29032013-05-03 13:40:13 -07003538 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003539 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003540
3541 if (VALIDATE_TOKENS) {
3542 validateAppTokensLocked();
3543 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003544 }
3545
3546 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003547 * Worker method for rearranging history stack. Implements the function of moving all
3548 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003549 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003550 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003551 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3552 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003553 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003554 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003555 * @return Returns true if the move completed, false if not.
3556 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003557 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003558 final TaskRecord tr = taskForIdLocked(taskId);
3559 if (tr == null) {
3560 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3561 return false;
3562 }
3563
3564 Slog.i(TAG, "moveTaskToBack: " + tr);
3565
3566 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 // If we have a watcher, preflight the move before committing to it. First check
3569 // for *other* available tasks, but if none are available, then try again allowing the
3570 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003571 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003572 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 if (next == null) {
3574 next = topRunningActivityLocked(null, 0);
3575 }
3576 if (next != null) {
3577 // ask watcher if this is allowed
3578 boolean moveOK = true;
3579 try {
3580 moveOK = mService.mController.activityResuming(next.packageName);
3581 } catch (RemoteException e) {
3582 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003583 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003584 }
3585 if (!moveOK) {
3586 return false;
3587 }
3588 }
3589 }
3590
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003591 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003592 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003593
Craig Mautner11bf9a52013-02-19 14:08:51 -08003594 mTaskHistory.remove(tr);
3595 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003596 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003597
Craig Mautnerc8143c62013-09-03 12:15:57 -07003598 // There is an assumption that moving a task to the back moves it behind the home activity.
3599 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003600 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003601 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3602 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003603 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003604 break;
3605 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003606 if (taskNdx == 1) {
3607 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003608 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003609 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003610 }
3611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003612 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003613 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3614 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 ActivityRecord r = topRunningActivityLocked(null);
3616 if (r != null) {
3617 mNoAnimActivities.add(r);
3618 }
3619 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003620 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003621 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003622 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003623
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003624 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003625 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003626 }
3627
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003628 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003629 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003630 if (!mService.mBooting && !mService.mBooted) {
3631 // Not ready yet!
3632 return false;
3633 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003634 final int taskToReturnTo = tr.getTaskToReturnTo();
3635 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3636 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003637 }
3638
Craig Mautner05d29032013-05-03 13:40:13 -07003639 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640 return true;
3641 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003642
Craig Mautner8849a5e2013-04-02 16:41:03 -07003643 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003644 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003645 final Uri data = r.intent.getData();
3646 final String strData = data != null ? data.toSafeString() : null;
3647
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003648 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003649 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003650 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003651 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003652 }
3653
3654 /**
3655 * Make sure the given activity matches the current configuration. Returns
3656 * false if the activity had to be destroyed. Returns true if the
3657 * configuration is the same, or the activity will remain running as-is
3658 * for whatever reason. Ensures the HistoryRecord is updated with the
3659 * correct configuration and all other bookkeeping is handled.
3660 */
3661 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3662 int globalChanges) {
3663 if (mConfigWillChange) {
3664 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3665 "Skipping config check (will change): " + r);
3666 return true;
3667 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003668
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003669 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3670 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003671
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003672 // Short circuit: if the two configurations are the exact same
3673 // object (the common case), then there is nothing to do.
3674 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003675 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3677 "Configuration unchanged in " + r);
3678 return true;
3679 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003680
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003681 // We don't worry about activities that are finishing.
3682 if (r.finishing) {
3683 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3684 "Configuration doesn't matter in finishing " + r);
3685 r.stopFreezingScreenLocked(false);
3686 return true;
3687 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003688
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 // Okay we now are going to make this activity have the new config.
3690 // But then we need to figure out how it needs to deal with that.
3691 Configuration oldConfig = r.configuration;
3692 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003693
3694 // Determine what has changed. May be nothing, if this is a config
3695 // that has come back from the app after going idle. In that case
3696 // we just want to leave the official config object now in the
3697 // activity and do nothing else.
3698 final int changes = oldConfig.diff(newConfig);
3699 if (changes == 0 && !r.forceNewConfig) {
3700 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3701 "Configuration no differences in " + r);
3702 return true;
3703 }
3704
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003705 // If the activity isn't currently running, just leave the new
3706 // configuration and it will pick that up next time it starts.
3707 if (r.app == null || r.app.thread == null) {
3708 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3709 "Configuration doesn't matter not running " + r);
3710 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003711 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003712 return true;
3713 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003714
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003715 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003716 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3717 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3718 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003719 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003720 + ", newConfig=" + newConfig);
3721 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003722 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003723 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3724 r.configChangeFlags |= changes;
3725 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003726 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003727 if (r.app == null || r.app.thread == null) {
3728 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003729 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003730 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003731 } else if (r.state == ActivityState.PAUSING) {
3732 // A little annoying: we are waiting for this activity to
3733 // finish pausing. Let's not do anything now, but just
3734 // flag that it needs to be restarted when done pausing.
3735 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003736 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003737 r.configDestroy = true;
3738 return true;
3739 } else if (r.state == ActivityState.RESUMED) {
3740 // Try to optimize this case: the configuration is changing
3741 // and we need to restart the top, resumed activity.
3742 // Instead of doing the normal handshaking, just say
3743 // "restart!".
3744 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003745 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003746 relaunchActivityLocked(r, r.configChangeFlags, true);
3747 r.configChangeFlags = 0;
3748 } else {
3749 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003750 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003751 relaunchActivityLocked(r, r.configChangeFlags, false);
3752 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003753 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003754
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003755 // All done... tell the caller we weren't able to keep this
3756 // activity around.
3757 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003758 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003759
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003760 // Default case: the activity can handle this new configuration, so
3761 // hand it over. Note that we don't need to give it the new
3762 // configuration, since we always send configuration changes to all
3763 // process when they happen so it can just use whatever configuration
3764 // it last got.
3765 if (r.app != null && r.app.thread != null) {
3766 try {
3767 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003768 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003769 } catch (RemoteException e) {
3770 // If process died, whatever.
3771 }
3772 }
3773 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003774
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003775 return true;
3776 }
3777
Craig Mautnerc8143c62013-09-03 12:15:57 -07003778 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003779 int changes, boolean andResume) {
3780 List<ResultInfo> results = null;
3781 List<Intent> newIntents = null;
3782 if (andResume) {
3783 results = r.results;
3784 newIntents = r.newIntents;
3785 }
3786 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3787 + " with results=" + results + " newIntents=" + newIntents
3788 + " andResume=" + andResume);
3789 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003790 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003791 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003792
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003793 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003794
Craig Mautner34b73df2014-01-12 21:11:08 -08003795 mStackSupervisor.removeChildActivityContainers(r);
3796
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003797 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003798 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3799 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3800 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003801 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003802 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003803 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003804 // Note: don't need to call pauseIfSleepingLocked() here, because
3805 // the caller will only pass in 'andResume' if this activity is
3806 // currently resumed, which implies we aren't sleeping.
3807 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003808 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003809 }
3810
3811 if (andResume) {
3812 r.results = null;
3813 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003814 r.state = ActivityState.RESUMED;
3815 } else {
3816 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3817 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003818 }
3819
3820 return true;
3821 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003822
3823 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003824 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3825 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3826 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3827 final ActivityRecord r = activities.get(activityNdx);
3828 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003829 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003830 }
3831 if (r.fullscreen && !r.finishing) {
3832 return false;
3833 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003834 }
3835 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003836 final ActivityRecord r = ActivityRecord.forToken(token);
3837 if (r == null) {
3838 return false;
3839 }
3840 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3841 + " would have returned true for r=" + r);
3842 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003843 }
3844
3845 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003846 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3847 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3848 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3849 final ActivityRecord r = activities.get(activityNdx);
3850 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003851 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003852 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003853 }
3854 }
3855 }
3856
3857 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3858 boolean didSomething = false;
3859 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003860 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003861 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3862 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3863 int numActivities = activities.size();
3864 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3865 ActivityRecord r = activities.get(activityNdx);
3866 final boolean samePackage = r.packageName.equals(name)
3867 || (name == null && r.userId == userId);
3868 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3869 && (samePackage || r.task == lastTask)
3870 && (r.app == null || evenPersistent || !r.app.persistent)) {
3871 if (!doit) {
3872 if (r.finishing) {
3873 // If this activity is just finishing, then it is not
3874 // interesting as far as something to stop.
3875 continue;
3876 }
3877 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003878 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003879 if (r.isHomeActivity()) {
3880 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3881 Slog.i(TAG, "Skip force-stop again " + r);
3882 continue;
3883 } else {
3884 homeActivity = r.realActivity;
3885 }
3886 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003887 didSomething = true;
3888 Slog.i(TAG, " Force finishing activity " + r);
3889 if (samePackage) {
3890 if (r.app != null) {
3891 r.app.removed = true;
3892 }
3893 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003894 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003895 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003896 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3897 true)) {
3898 // r has been deleted from mActivities, accommodate.
3899 --numActivities;
3900 --activityNdx;
3901 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003902 }
3903 }
3904 }
3905 return didSomething;
3906 }
3907
Dianne Hackborn09233282014-04-30 11:33:59 -07003908 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003909 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003910 final TaskRecord task = mTaskHistory.get(taskNdx);
3911 ActivityRecord r = null;
3912 ActivityRecord top = null;
3913 int numActivities = 0;
3914 int numRunning = 0;
3915 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003916 if (activities.isEmpty()) {
3917 continue;
3918 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07003919 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07003920 continue;
3921 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003922 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3923 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003924
Craig Mautneraab647e2013-02-28 16:31:36 -08003925 // Initialize state for next task if needed.
3926 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3927 top = r;
3928 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003929 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003930
3931 // Add 'r' into the current task.
3932 numActivities++;
3933 if (r.app != null && r.app.thread != null) {
3934 numRunning++;
3935 }
3936
3937 if (localLOGV) Slog.v(
3938 TAG, r.intent.getComponent().flattenToShortString()
3939 + ": task=" + r.task);
3940 }
3941
3942 RunningTaskInfo ci = new RunningTaskInfo();
3943 ci.id = task.taskId;
3944 ci.baseActivity = r.intent.getComponent();
3945 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003946 ci.lastActiveTime = task.lastActiveTime;
3947
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003948 if (top.task != null) {
3949 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003950 }
3951 ci.numActivities = numActivities;
3952 ci.numRunning = numRunning;
3953 //System.out.println(
3954 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003955 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003956 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003957 }
3958
3959 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003960 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003961 if (DEBUG_SWITCH) Slog.d(
3962 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003963 if (top >= 0) {
3964 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3965 int activityTop = activities.size() - 1;
3966 if (activityTop > 0) {
3967 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3968 "unhandled-back", true);
3969 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003970 }
3971 }
3972
Craig Mautner6b74cb52013-09-27 17:02:21 -07003973 /**
3974 * Reset local parameters because an app's activity died.
3975 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003976 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003977 */
3978 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003979 if (mPausingActivity != null && mPausingActivity.app == app) {
3980 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3981 "App died while pausing: " + mPausingActivity);
3982 mPausingActivity = null;
3983 }
3984 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3985 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003986 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003987 }
3988
Craig Mautner19091252013-10-05 00:03:53 -07003989 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003990 }
3991
Craig Mautnercae015f2013-02-08 14:31:27 -08003992 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003993 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3994 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3995 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3996 final ActivityRecord r = activities.get(activityNdx);
3997 if (r.app == app) {
3998 Slog.w(TAG, " Force finishing activity "
3999 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004000 // Force the destroy to skip right to removal.
4001 r.app = null;
4002 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004003 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004004 }
4005 }
4006 }
4007
Dianne Hackborn390517b2013-05-30 15:03:32 -07004008 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004009 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004010 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004011 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4012 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004013 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4014 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004015 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004016 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004017 if (printed) {
4018 header = null;
4019 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004020 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004021 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004022 }
4023
4024 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4025 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4026
4027 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004028 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4029 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004030 }
4031 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004032 final int top = mTaskHistory.size() - 1;
4033 if (top >= 0) {
4034 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4035 int listTop = list.size() - 1;
4036 if (listTop >= 0) {
4037 activities.add(list.get(listTop));
4038 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004039 }
4040 } else {
4041 ItemMatcher matcher = new ItemMatcher();
4042 matcher.build(name);
4043
Craig Mautneraab647e2013-02-28 16:31:36 -08004044 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4045 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4046 if (matcher.match(r1, r1.intent.getComponent())) {
4047 activities.add(r1);
4048 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004049 }
4050 }
4051 }
4052
4053 return activities;
4054 }
4055
4056 ActivityRecord restartPackage(String packageName) {
4057 ActivityRecord starting = topRunningActivityLocked(null);
4058
4059 // All activities that came from the package must be
4060 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004061 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4062 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4063 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4064 final ActivityRecord a = activities.get(activityNdx);
4065 if (a.info.packageName.equals(packageName)) {
4066 a.forceNewConfig = true;
4067 if (starting != null && a == starting && a.visible) {
4068 a.startFreezingScreenLocked(starting.app,
4069 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4070 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004071 }
4072 }
4073 }
4074
4075 return starting;
4076 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004077
Craig Mautner41db4a72014-05-07 17:20:56 -07004078 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004079 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08004080 mWindowManager.removeTask(task.taskId);
4081 final ActivityRecord r = mResumedActivity;
4082 if (r != null && r.task == task) {
4083 mResumedActivity = null;
4084 }
4085
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004086 final int taskNdx = mTaskHistory.indexOf(task);
4087 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004088 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4089 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4090 if (!nextTask.isOverHomeStack()) {
4091 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4092 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004093 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004094 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004095 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004096
4097 if (task.mActivities.isEmpty()) {
4098 final boolean isVoiceSession = task.voiceSession != null;
4099 if (isVoiceSession) {
4100 try {
4101 task.voiceSession.taskFinished(task.intent, task.taskId);
4102 } catch (RemoteException e) {
4103 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004104 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004105 if (task.autoRemoveFromRecents() || isVoiceSession) {
4106 // Task creator asked to remove this when done, or this task was a voice
4107 // interaction, so it should not remain on the recent tasks list.
4108 mService.mRecentTasks.remove(task);
Dianne Hackborn852975d2014-08-22 17:42:43 -07004109 task.removedFromRecents(mService.mTaskPersister);
Craig Mautner41db4a72014-05-07 17:20:56 -07004110 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004111 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004112
4113 if (mTaskHistory.isEmpty()) {
4114 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
4115 if (isOnHomeDisplay()) {
4116 mStackSupervisor.moveHomeStack(!isHomeStack());
4117 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004118 if (mStacks != null) {
4119 mStacks.remove(this);
4120 mStacks.add(0, this);
4121 }
Craig Mautnerd163e752014-06-13 17:18:47 -07004122 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004123 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004124 }
4125
Dianne Hackborn91097de2014-04-04 18:02:06 -07004126 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4127 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4128 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004129 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4130 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004131 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004132 return task;
4133 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004134
4135 ArrayList<TaskRecord> getAllTasks() {
4136 return new ArrayList<TaskRecord>(mTaskHistory);
4137 }
4138
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004139 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004140 task.stack = this;
4141 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004142 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004143 } else {
4144 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004145 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004146 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004147 if (!moving && task.voiceSession != null) {
4148 try {
4149 task.voiceSession.taskStarted(task.intent, task.taskId);
4150 } catch (RemoteException e) {
4151 }
4152 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004153 }
4154
4155 public int getStackId() {
4156 return mStackId;
4157 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004158
4159 @Override
4160 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004161 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4162 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004163 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004164}