blob: fd1474f49da239e87a50015d6250ff0957a19dbe [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
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800232 int mThumbnailWidth = -1;
233 int mThumbnailHeight = -1;
234
Craig Mautner858d8a62013-04-23 17:08:34 -0700235 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700236
Craig Mautnerc00204b2013-03-05 15:02:14 -0800237 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800238 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800239 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
240 ArrayList<ActivityStack> mStacks;
241 /** The attached Display's unique identifier, or -1 if detached */
242 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800243
Craig Mautner27084302013-03-25 08:05:25 -0700244 /** Run all ActivityStacks through this */
245 final ActivityStackSupervisor mStackSupervisor;
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 }
Craig Mautnera0026042014-04-23 11:45:37 -0700287 activityPausedLocked(r.appToken, true, r.persistentState);
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;
Winson Chung376543b2014-05-21 17:43:28 -0700358 // Get the activity screenshot thumbnail dimensions
359 Resources res = mService.mContext.getResources();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700360 mThumbnailWidth = mService.mThumbnailWidth;
361 mThumbnailHeight = mService.mThumbnailHeight;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362 }
Craig Mautner5962b122012-10-05 14:45:52 -0700363
Amith Yamasani734983f2014-03-04 16:48:05 -0800364 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100365 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800366 */
Kenny Guy2a764942014-04-02 13:29:20 +0100367 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100368 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100369 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
370 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800371 }
372 return false;
373 }
374
375 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100376 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700377 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
378 }
379
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700382 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
383 if (r != null) {
384 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800385 }
386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 return null;
388 }
389
390 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800391 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
392 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800393 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800394 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
395 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800396 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800397 return r;
398 }
399 }
400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 return null;
402 }
403
404 /**
405 * This is a simplified version of topRunningActivityLocked that provides a number of
406 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800407 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700408 * @param token If non-null, any history records matching this token will be skipped.
409 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800410 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700411 * @return Returns the HistoryRecord of the next activity on the stack.
412 */
413 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800414 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
415 TaskRecord task = mTaskHistory.get(taskNdx);
416 if (task.taskId == taskId) {
417 continue;
418 }
419 ArrayList<ActivityRecord> activities = task.mActivities;
420 for (int i = activities.size() - 1; i >= 0; --i) {
421 final ActivityRecord r = activities.get(i);
422 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800423 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 return r;
425 }
426 }
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 return null;
429 }
430
Craig Mautner8849a5e2013-04-02 16:41:03 -0700431 final ActivityRecord topActivity() {
432 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700433 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700434 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
435 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700436 final int topActivityNdx = activities.size() - 1;
437 if (topActivityNdx >= 0) {
438 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700439 }
440 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700441 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700442 }
443
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700444 final TaskRecord topTask() {
445 final int size = mTaskHistory.size();
446 if (size > 0) {
447 return mTaskHistory.get(size - 1);
448 }
449 return null;
450 }
451
Craig Mautnerd2328952013-03-05 12:46:26 -0800452 TaskRecord taskForIdLocked(int id) {
453 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
454 final TaskRecord task = mTaskHistory.get(taskNdx);
455 if (task.taskId == id) {
456 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800457 }
458 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700459 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700460 }
461
Craig Mautnerd2328952013-03-05 12:46:26 -0800462 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800463 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800464 if (r != null) {
465 final TaskRecord task = r.task;
466 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
467 if (task.stack != this) Slog.w(TAG,
468 "Illegal state! task does not point to stack it is in.");
469 return r;
470 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800471 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800472 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800473 }
474
Craig Mautner2420ead2013-04-01 17:13:20 -0700475 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700476 final boolean hadit = mLRUActivities.remove(r);
477 mLRUActivities.add(r);
478 return hadit;
479 }
480
Craig Mautnerde4ef022013-04-07 19:01:33 -0700481 final boolean isHomeStack() {
482 return mStackId == HOME_STACK_ID;
483 }
484
Craig Mautnere0a38842013-12-16 16:14:02 -0800485 final boolean isOnHomeDisplay() {
486 return isAttached() &&
487 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
488 }
489
490 final void moveToFront() {
491 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800492 if (isOnHomeDisplay()) {
493 mStackSupervisor.moveHomeStack(isHomeStack());
494 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800495 mStacks.remove(this);
496 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800497 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800498 }
499
500 final boolean isAttached() {
501 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800502 }
503
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 /**
505 * Returns the top activity in any existing task matching the given
506 * Intent. Returns null if no such task is found.
507 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700508 ActivityRecord findTaskLocked(ActivityRecord target) {
509 Intent intent = target.intent;
510 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700511 ComponentName cls = intent.getComponent();
512 if (info.targetActivity != null) {
513 cls = new ComponentName(info.packageName, info.targetActivity);
514 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700515 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700516 boolean isDocument = intent != null & intent.isDocument();
517 // If documentData is non-null then it must match the existing task data.
518 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800519
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700520 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800521 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
522 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700523 if (task.voiceSession != null) {
524 // We never match voice sessions; those always run independently.
525 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
526 continue;
527 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700528 if (task.userId != userId) {
529 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700530 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700531 continue;
532 }
Craig Mautner000f0022013-02-26 15:04:29 -0800533 final ActivityRecord r = task.getTopActivity();
534 if (r == null || r.finishing || r.userId != userId ||
535 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700536 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800537 continue;
538 }
539
Craig Mautnerd00f4742014-03-12 14:17:26 -0700540 final Intent taskIntent = task.intent;
541 final Intent affinityIntent = task.affinityIntent;
542 final boolean taskIsDocument;
543 final Uri taskDocumentData;
544 if (taskIntent != null && taskIntent.isDocument()) {
545 taskIsDocument = true;
546 taskDocumentData = taskIntent.getData();
547 } else if (affinityIntent != null && affinityIntent.isDocument()) {
548 taskIsDocument = true;
549 taskDocumentData = affinityIntent.getData();
550 } else {
551 taskIsDocument = false;
552 taskDocumentData = null;
553 }
554
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700555 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700556 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700557 + "/aff=" + r.task.affinity + " to new cls="
558 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700559 if (!isDocument && !taskIsDocument && task.affinity != null) {
560 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700561 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700562 return r;
563 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700564 } else if (taskIntent != null && taskIntent.getComponent() != null &&
565 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700566 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700567 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800568 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700569 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
570 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800571 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700572 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
573 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700574 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700575 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800576 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700577 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
578 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800579 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700580 } else if (DEBUG_TASKS) {
581 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700582 }
583 }
584
585 return null;
586 }
587
588 /**
589 * Returns the first activity (starting from the top of the stack) that
590 * is the same as the given activity. Returns null if no such activity
591 * is found.
592 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700593 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700594 ComponentName cls = intent.getComponent();
595 if (info.targetActivity != null) {
596 cls = new ComponentName(info.packageName, info.targetActivity);
597 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700598 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700599
Craig Mautner000f0022013-02-26 15:04:29 -0800600 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700601 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100602 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700603 return null;
604 }
605 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800606 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
607 ActivityRecord r = activities.get(activityNdx);
608 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700609 //Slog.i(TAG, "Found matching class!");
610 //dump();
611 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
612 return r;
613 }
614 }
615 }
616
617 return null;
618 }
619
Amith Yamasani742a6712011-05-04 14:49:28 -0700620 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700621 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700622 */
Craig Mautner93529a42013-10-04 15:03:13 -0700623 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800624 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700625 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800626 }
627 mCurrentUser = userId;
628
629 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800630 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800631 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700632 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100633 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700634 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
635 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800636 mTaskHistory.remove(i);
637 mTaskHistory.add(task);
638 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800639 // Use same value for i.
640 } else {
641 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800642 }
643 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700644 if (VALIDATE_TOKENS) {
645 validateAppTokensLocked();
646 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700647 }
648
Craig Mautner2420ead2013-04-01 17:13:20 -0700649 void minimalResumeActivityLocked(ActivityRecord r) {
650 r.state = ActivityState.RESUMED;
651 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
652 + " (starting new instance)");
653 r.stopped = false;
654 mResumedActivity = r;
655 r.task.touchActiveTime();
656 mService.addRecentTaskLocked(r.task);
657 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700658 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700659 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700660 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700661 }
662
Dianne Hackborncee04b52013-07-03 17:01:28 -0700663 private void startLaunchTraces() {
664 if (mFullyDrawnStartTime != 0) {
665 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
666 }
667 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
668 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
669 }
670
671 private void stopFullyDrawnTraceIfNeeded() {
672 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
673 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
674 mFullyDrawnStartTime = 0;
675 }
676 }
677
Craig Mautnere79d42682013-04-01 19:01:53 -0700678 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700679 if (r.displayStartTime == 0) {
680 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
681 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700682 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700683 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700684 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700685 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700686 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700687 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700688 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700689 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800690
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700691 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700692 // Make sure that there is no activity waiting for this to launch.
693 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
694 r.displayStartTime = r.fullyDrawnStartTime = 0;
695 } else {
696 mStackSupervisor.removeTimeoutsForActivityLocked(r);
697 mStackSupervisor.scheduleIdleTimeoutLocked(r);
698 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700699 }
700
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800701 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800702 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800703 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
704 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
705 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
706 activities.get(activityNdx).setSleeping(false);
707 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800708 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800709 }
710
Craig Mautner0eea92c2013-05-16 13:35:39 -0700711 /**
712 * @return true if something must be done before going to sleep.
713 */
714 boolean checkReadyForSleepLocked() {
715 if (mResumedActivity != null) {
716 // Still have something resumed; can't sleep until it is paused.
717 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
718 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
719 startPausingLocked(false, true);
720 return true;
721 }
722 if (mPausingActivity != null) {
723 // Still waiting for something to pause; can't sleep yet.
724 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
725 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800726 }
727
Craig Mautner0eea92c2013-05-16 13:35:39 -0700728 return false;
729 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800730
Craig Mautner0eea92c2013-05-16 13:35:39 -0700731 void goToSleep() {
732 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800733
Craig Mautner0eea92c2013-05-16 13:35:39 -0700734 // Make sure any stopped but visible activities are now sleeping.
735 // This ensures that the activity's onStop() is called.
736 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
737 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
738 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
739 final ActivityRecord r = activities.get(activityNdx);
740 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
741 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800742 }
743 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800744 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700745 }
Craig Mautner59c00972012-07-30 12:10:24 -0700746
Winson Chung376543b2014-05-21 17:43:28 -0700747 /**
748 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
749 * again when requested.
750 */
751 private void invalidateLastScreenshot() {
752 mLastScreenshotActivity = null;
753 if (mLastScreenshotBitmap != null) {
754 mLastScreenshotBitmap.recycle();
755 }
756 mLastScreenshotBitmap = null;
757 }
758
Dianne Hackbornd2835932010-12-13 16:28:46 -0800759 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700760 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800761 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700762 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800763 return null;
764 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800765
Winson Chung083baf92014-07-11 10:32:42 -0700766 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700767 // This is an optimization -- since we never show Home or Recents within Recents itself,
768 // we can just go ahead and skip taking the screenshot if this is the home stack. In
769 // the case where the most recent task is not the task that was supplied, then the stack
770 // has changed, so invalidate the last screenshot().
771 invalidateLastScreenshot();
772 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700773 return null;
774 }
775
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800776 int w = mThumbnailWidth;
777 int h = mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800778 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800779 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700780 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800781 || mLastScreenshotBitmap.getWidth() != w
782 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700783 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800784 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700785 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700786 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800787 }
788 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700789 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700790 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800791 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800792 }
Winson Chung376543b2014-05-21 17:43:28 -0700793 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800794 return null;
795 }
796
Craig Mautnercf910b02013-04-23 11:23:27 -0700797 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800798 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800799 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700800 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800801 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700802 ActivityRecord prev = mResumedActivity;
803 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700804 Slog.e(TAG, "Trying to pause when nothing is resumed",
805 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700806 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 return;
808 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800809
810 if (mActivityContainer.mParentActivity == null) {
811 // Top level stack, not a child. Look for child stacks.
812 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
813 }
814
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700815 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
816 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800818 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700820 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
821 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 prev.state = ActivityState.PAUSING;
823 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700824 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700825 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Alan Viverettea2ff6112014-07-14 17:46:27 -0700826 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task)) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700827 prev.updateThumbnail(screenshotActivities(prev), null);
828 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700829 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700830
831 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800832
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700833 if (prev.app != null && prev.app.thread != null) {
834 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
835 try {
836 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700837 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700839 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800840 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
841 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842 } catch (Exception e) {
843 // Ignore exception, if process died other code will cleanup.
844 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800845 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700847 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848 }
849 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800850 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700852 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 }
854
855 // If we are not going to sleep, we want to ensure the device is
856 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700857 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700858 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700859 }
860
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800861 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 // Have the window manager pause its key dispatching until the new
863 // activity has started. If we're pausing the activity just because
864 // the screen is being turned off and the UI is sleeping, don't interrupt
865 // key dispatch; the same activity will pick it up again on wakeup.
866 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800867 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 } else {
869 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
870 }
871
872 // Schedule a pause timeout in case the app doesn't respond.
873 // We don't give it much time because this directly impacts the
874 // responsiveness seen by the user.
875 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
876 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700877 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700878 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
879 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
880 } else {
881 // This activity failed to schedule the
882 // pause, so just treat it as being paused now.
883 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700884 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885 }
886 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700887
Craig Mautnera0026042014-04-23 11:45:37 -0700888 final void activityPausedLocked(IBinder token, boolean timeout,
889 PersistableBundle persistentState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700890 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800891 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892
Craig Mautnerd2328952013-03-05 12:46:26 -0800893 final ActivityRecord r = isInStackLocked(token);
894 if (r != null) {
895 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Craig Mautner21d24a22014-04-23 11:45:37 -0700896 if (persistentState != null) {
897 r.persistentState = persistentState;
898 mService.notifyTaskPersisterLocked(r.task, false);
899 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800900 if (mPausingActivity == r) {
901 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
902 + (timeout ? " (due to timeout)" : " (pause complete)"));
903 r.state = ActivityState.PAUSED;
904 completePauseLocked();
905 } else {
906 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
907 r.userId, System.identityHashCode(r), r.shortComponentName,
908 mPausingActivity != null
909 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700910 }
911 }
912 }
913
Craig Mautnera0026042014-04-23 11:45:37 -0700914 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
915 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700916 if (r.state != ActivityState.STOPPING) {
917 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
918 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
919 return;
920 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700921 if (persistentState != null) {
922 r.persistentState = persistentState;
923 mService.notifyTaskPersisterLocked(r.task, false);
924 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700925 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700926 if (icicle != null) {
927 // If icicle is null, this is happening due to a timeout, so we
928 // haven't really saved the state.
929 r.icicle = icicle;
930 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800931 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700932 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700933 }
934 if (!r.stopped) {
935 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
936 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
937 r.stopped = true;
938 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700939 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
940 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700941 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700942 if (r.finishing) {
943 r.clearOptionsLocked();
944 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700945 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700946 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700947 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700948 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700949 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800950 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700951 }
952 }
953 }
954
Craig Mautnerc8143c62013-09-03 12:15:57 -0700955 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800956 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800958
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 if (prev != null) {
960 if (prev.finishing) {
961 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700962 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800963 } else if (prev.app != null) {
964 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
965 if (prev.waitingVisible) {
966 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700967 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800968 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
969 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800970 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800971 if (prev.configDestroy) {
972 // The previous is being paused because the configuration
973 // is changing, which means it is actually stopping...
974 // To juggle the fact that we are also starting a new
975 // instance right now, we need to first completely stop
976 // the current instance before starting the new one.
977 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700978 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700979 } else if (!hasVisibleBehindActivity()) {
980 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700981 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700982 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700983 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
984 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800985 // If we already have a few activities waiting to stop,
986 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700987 // them out. Or if r is the last of activity of the last task the stack
988 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700990 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800991 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700992 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 }
994 }
995 } else {
996 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
997 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800998 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800999 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001000 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001001
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001002 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001003 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -07001004 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001005 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001006 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001007 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001008 if (top == null || (prev != null && top != prev)) {
1009 // If there are no more activities available to run,
1010 // do resume anyway to start something. Also if the top
1011 // activity on the stack is not the just paused activity,
1012 // we need to go ahead and resume it to ensure we complete
1013 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -07001014 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001015 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001016 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001017
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001018 if (prev != null) {
1019 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001020
Craig Mautner525f3d92013-05-07 14:01:50 -07001021 if (prev.app != null && prev.cpuTimeAtResume > 0
1022 && mService.mBatteryStatsService.isOnBattery()) {
1023 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -07001024 synchronized (mService.mProcessCpuThread) {
1025 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -07001026 - prev.cpuTimeAtResume;
1027 }
1028 if (diff > 0) {
1029 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1030 synchronized (bsi) {
1031 BatteryStatsImpl.Uid.Proc ps =
1032 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001033 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001034 if (ps != null) {
1035 ps.addForegroundTimeLocked(diff);
1036 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 }
1038 }
1039 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001040 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042 }
1043
1044 /**
1045 * Once we know that we have asked an application to put an activity in
1046 * the resumed state (either by launching it or explicitly telling it),
1047 * this function updates the rest of our state to match that fact.
1048 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001049 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050 next.idle = false;
1051 next.results = null;
1052 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001053
1054 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1055 ProcessRecord app = next.task.mActivities.get(0).app;
1056 if (app != null && app != mService.mHomeProcess) {
1057 mService.mHomeProcess = app;
1058 }
1059 }
1060
Craig Mautner07566322013-09-26 16:42:55 -07001061 if (next.nowVisible) {
1062 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001063 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001064 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065
1066 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001067 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001069 mStackSupervisor.reportResumedActivityLocked(next);
1070
1071 next.resumeKeyDispatchingLocked();
1072 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001073
1074 // Mark the point when the activity is resuming
1075 // TODO: To be more accurate, the mark should be before the onCreate,
1076 // not after the onResume. But for subsequent starts, onResume is fine.
1077 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001078 synchronized (mService.mProcessCpuThread) {
1079 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001080 }
1081 } else {
1082 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1083 }
Winson Chung376543b2014-05-21 17:43:28 -07001084
1085 // If we are resuming the activity that we had last screenshotted, then we know it will be
1086 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1087 if (next == mLastScreenshotActivity) {
1088 invalidateLastScreenshot();
1089 }
George Mount6ba042b2014-07-28 11:12:28 -07001090 next.returningOptions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001091 }
1092
Craig Mautnere3a00d72014-04-16 08:31:19 -07001093 private void setVisibile(ActivityRecord r, boolean visible) {
1094 r.visible = visible;
1095 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001096 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001097 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001098 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001099 container.setVisible(visible);
1100 }
1101 }
1102
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001103 // Find the first visible activity above the passed activity and if it is translucent return it
1104 // otherwise return null;
1105 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1106 TaskRecord task = r.task;
1107 if (task == null) {
1108 return null;
1109 }
1110
1111 ActivityStack stack = task.stack;
1112 if (stack == null) {
1113 return null;
1114 }
1115
1116 int stackNdx = mStacks.indexOf(stack);
1117
1118 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1119 int taskNdx = tasks.indexOf(task);
1120
1121 ArrayList<ActivityRecord> activities = task.mActivities;
1122 int activityNdx = activities.indexOf(r) + 1;
1123
1124 final int numStacks = mStacks.size();
1125 while (stackNdx < numStacks) {
1126 tasks = mStacks.get(stackNdx).mTaskHistory;
1127 final int numTasks = tasks.size();
1128 while (taskNdx < numTasks) {
1129 activities = tasks.get(taskNdx).mActivities;
1130 final int numActivities = activities.size();
1131 while (activityNdx < numActivities) {
1132 final ActivityRecord activity = activities.get(activityNdx);
1133 if (!activity.finishing) {
1134 return activity.fullscreen ? null : activity;
1135 }
1136 ++activityNdx;
1137 }
1138 activityNdx = 0;
1139 ++taskNdx;
1140 }
1141 taskNdx = 0;
1142 ++stackNdx;
1143 }
1144
1145 return null;
1146 }
1147
Jose Lima7ba71252014-04-30 12:59:27 -07001148 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1149 // If so, this stack is hidden, otherwise it is visible.
1150 private boolean isStackVisible() {
1151 if (!isAttached()) {
1152 return false;
1153 }
1154
1155 if (mStackSupervisor.isFrontStack(this)) {
1156 return true;
1157 }
1158
Jose Lima729cb232014-05-05 15:59:40 -07001159 /**
1160 * Start at the task above this one and go up, looking for a visible
1161 * fullscreen activity, or a translucent activity that requested the
1162 * wallpaper to be shown behind it.
1163 */
Jose Lima7ba71252014-04-30 12:59:27 -07001164 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1165 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1166 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001167 final TaskRecord task = tasks.get(taskNdx);
1168 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001169 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1170 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001171
1172 // Conditions for an activity to obscure the stack we're
1173 // examining:
1174 // 1. Not Finishing AND Visible AND:
1175 // 2. Either:
1176 // - Full Screen Activity OR
1177 // - On top of Home and our stack is NOT home
1178 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001179 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001180 return false;
1181 }
1182 }
1183 }
1184 }
1185
1186 return true;
1187 }
1188
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001189 /**
1190 * Make sure that all activities that need to be visible (that is, they
1191 * currently can be seen by the user) actually are.
1192 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001193 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1194 ActivityRecord top = topRunningActivityLocked(null);
1195 if (top == null) {
1196 return;
1197 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001198 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199 TAG, "ensureActivitiesVisible behind " + top
1200 + " configChanges=0x" + Integer.toHexString(configChanges));
1201
Craig Mautner5eda9b32013-07-02 11:58:16 -07001202 if (mTranslucentActivityWaiting != top) {
1203 mUndrawnActivitiesBelowTopTranslucent.clear();
1204 if (mTranslucentActivityWaiting != null) {
1205 // Call the callback with a timeout indication.
1206 notifyActivityDrawnLocked(null);
1207 mTranslucentActivityWaiting = null;
1208 }
1209 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1210 }
1211
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 // If the top activity is not fullscreen, then we need to
1213 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001214 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001215 boolean behindFullscreen = !isStackVisible();
1216
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001217 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001218 final TaskRecord task = mTaskHistory.get(taskNdx);
1219 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001220 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1221 final ActivityRecord r = activities.get(activityNdx);
1222 if (r.finishing) {
1223 continue;
1224 }
1225 if (aboveTop && r != top) {
1226 continue;
1227 }
1228 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001229 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1230 // but must be drawn initially for the animation as though they were visible.
1231 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001232 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001233 TAG, "Make visible? " + r + " finishing=" + r.finishing
1234 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001235
Craig Mautnerd44711d2013-02-23 11:24:36 -08001236 // First: if this is not the current activity being started, make
1237 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001238 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001239 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001240 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001241
1242 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001243 // This activity needs to be visible, but isn't even
1244 // running... get it started, but don't resume it
1245 // at this point.
1246 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1247 if (r != starting) {
1248 r.startFreezingScreenLocked(r.app, configChanges);
1249 }
1250 if (!r.visible || r.mLaunchTaskBehind) {
1251 if (DEBUG_VISBILITY) Slog.v(
1252 TAG, "Starting and making visible: " + r);
1253 setVisibile(r, true);
1254 }
1255 if (r != starting) {
1256 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001257 }
1258
1259 } else if (r.visible) {
1260 // If this activity is already visible, then there is nothing
1261 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001262 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001263 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001264 try {
George Mount6ba042b2014-07-28 11:12:28 -07001265 if (r.returningOptions != null) {
1266 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1267 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001268 }
1269 } catch(RemoteException e) {
1270 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001271 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001272 // This activity is not currently visible, but is running.
1273 // Tell it to become visible.
1274 r.visible = true;
1275 if (r.state != ActivityState.RESUMED && r != starting) {
1276 // If this activity is paused, tell it
1277 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001278 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001279 TAG, "Making visible and scheduling visibility: " + r);
1280 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001281 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001282 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001283 mUndrawnActivitiesBelowTopTranslucent.add(r);
1284 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001285 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001286 r.sleeping = false;
1287 r.app.pendingUiClean = true;
1288 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1289 r.stopFreezingScreenLocked(false);
1290 } catch (Exception e) {
1291 // Just skip on any failure; we'll make it
1292 // visible when it next restarts.
1293 Slog.w(TAG, "Exception thrown making visibile: "
1294 + r.intent.getComponent(), e);
1295 }
1296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001297 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001298
Craig Mautnerd44711d2013-02-23 11:24:36 -08001299 // Aggregate current change flags.
1300 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001301
Craig Mautnerd44711d2013-02-23 11:24:36 -08001302 if (r.fullscreen) {
1303 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001304 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1305 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001306 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001307 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001308 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001309 }
1310 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001311 if (DEBUG_VISBILITY) Slog.v(
1312 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1313 + " state=" + r.state
1314 + " behindFullscreen=" + behindFullscreen);
1315 // Now for any activities that aren't visible to the user, make
1316 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001318 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001320 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001321 switch (r.state) {
1322 case STOPPING:
1323 case STOPPED:
1324 if (r.app != null && r.app.thread != null) {
1325 if (DEBUG_VISBILITY) Slog.v(
1326 TAG, "Scheduling invisibility: " + r);
1327 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1328 }
1329 break;
1330
1331 case INITIALIZING:
1332 case RESUMED:
1333 case PAUSING:
1334 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001335 // This case created for transitioning activities from
1336 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001337 if (getVisibleBehindActivity() == r) {
1338 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001339 } else {
1340 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1341 mStackSupervisor.mStoppingActivities.add(r);
1342 }
1343 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001344 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001345 break;
1346
1347 default:
1348 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001349 }
1350 } catch (Exception e) {
1351 // Just skip on any failure; we'll make it
1352 // visible when it next restarts.
1353 Slog.w(TAG, "Exception thrown making hidden: "
1354 + r.intent.getComponent(), e);
1355 }
1356 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001357 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001358 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001359 }
1360 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001362
1363 if (mTranslucentActivityWaiting != null &&
1364 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1365 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1366 notifyActivityDrawnLocked(null);
1367 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001368 }
Craig Mautner58547802013-03-05 08:23:53 -08001369
George Mount6ba042b2014-07-28 11:12:28 -07001370 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001371 mTranslucentActivityWaiting = r;
1372 mUndrawnActivitiesBelowTopTranslucent.clear();
1373 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1374 }
1375
1376 /**
1377 * Called as activities below the top translucent activity are redrawn. When the last one is
1378 * redrawn notify the top activity by calling
1379 * {@link Activity#onTranslucentConversionComplete}.
1380 *
1381 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1382 * occurred and the activity will be notified immediately.
1383 */
1384 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001385 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001386 if ((r == null)
1387 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1388 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1389 // The last undrawn activity below the top has just been drawn. If there is an
1390 // opaque activity at the top, notify it that it can become translucent safely now.
1391 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1392 mTranslucentActivityWaiting = null;
1393 mUndrawnActivitiesBelowTopTranslucent.clear();
1394 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1395
Craig Mautner71dd1b62014-02-18 15:48:52 -08001396 if (waitingActivity != null) {
1397 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1398 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1399 try {
1400 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1401 waitingActivity.appToken, r != null);
1402 } catch (RemoteException e) {
1403 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001404 }
1405 }
1406 }
1407 }
1408
Craig Mautnera61bc652013-10-28 15:43:18 -07001409 /** If any activities below the top running one are in the INITIALIZING state and they have a
1410 * starting window displayed then remove that starting window. It is possible that the activity
1411 * in this state will never resumed in which case that starting window will be orphaned. */
1412 void cancelInitializingActivities() {
1413 final ActivityRecord topActivity = topRunningActivityLocked(null);
1414 boolean aboveTop = true;
1415 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1416 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1417 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1418 final ActivityRecord r = activities.get(activityNdx);
1419 if (aboveTop) {
1420 if (r == topActivity) {
1421 aboveTop = false;
1422 }
1423 continue;
1424 }
1425
1426 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1427 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1428 r.mStartingWindowShown = false;
1429 mWindowManager.removeAppStartingWindow(r.appToken);
1430 }
1431 }
1432 }
1433 }
1434
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 /**
1436 * Ensure that the top activity in the stack is resumed.
1437 *
1438 * @param prev The previously resumed activity, for when in the process
1439 * of pausing; can be null to call from elsewhere.
1440 *
1441 * @return Returns true if something is being resumed, or false if
1442 * nothing happened.
1443 */
1444 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001445 return resumeTopActivityLocked(prev, null);
1446 }
1447
1448 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001449 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1450
Craig Mautnerdf88d732014-01-27 09:21:32 -08001451 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001452 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001453 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001454 // Do not resume this stack if its parent is not resumed.
1455 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1456 return false;
1457 }
1458
Craig Mautnera61bc652013-10-28 15:43:18 -07001459 cancelInitializingActivities();
1460
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 // Find the first activity that is not finishing.
1462 ActivityRecord next = topRunningActivityLocked(null);
1463
1464 // Remember how we'll process this pause/resume situation, and ensure
1465 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001466 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1467 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468
Craig Mautner84984fa2014-06-19 11:19:20 -07001469 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 if (next == null) {
1471 // There are no more activities! Let's just start up the
1472 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001473 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001474 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001475 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001476 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001477 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1478 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1479 return isOnHomeDisplay() &&
1480 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 }
1482
1483 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001484
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001485 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001486 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1487 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 // Make sure we have executed any pending transitions, since there
1489 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001490 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001491 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001492 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001493 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001494 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 return false;
1496 }
1497
Craig Mautner525f3d92013-05-07 14:01:50 -07001498 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001499 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001500 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001501 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001502 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001503 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001504 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001505 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001506 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001507 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001508 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001509 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001510 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1511 "resumeTopActivityLocked: Launching home next");
1512 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001513 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1514 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1515 return isOnHomeDisplay() &&
1516 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001517 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001518 }
1519
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 // If we are sleeping, and there is no resumed activity, and the top
1521 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001522 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001523 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001524 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001525 // Make sure we have executed any pending transitions, since there
1526 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001527 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001529 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001530 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001531 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001532 return false;
1533 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001534
1535 // Make sure that the user who owns this activity is started. If not,
1536 // we will just leave it as is because someone should be bringing
1537 // another user's activities to the top of the stack.
1538 if (mService.mStartedUsers.get(next.userId) == null) {
1539 Slog.w(TAG, "Skipping resume of top activity " + next
1540 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001541 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001542 return false;
1543 }
1544
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 // The activity may be waiting for stop, but that is no longer
1546 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001547 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001548 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001549 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001550 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551
1552 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1553
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001554 // If we are currently pausing an activity, then don't do anything
1555 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001556 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001557 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1558 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001559 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001560 return false;
1561 }
1562
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001563 // Okay we are now going to start a switch, to 'next'. We may first
1564 // have to pause the current activity, but this is an important point
1565 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001566 // XXX "App Redirected" dialog is getting too many false positives
1567 // at this point, so turn off for now.
1568 if (false) {
1569 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1570 long now = SystemClock.uptimeMillis();
1571 final boolean inTime = mLastStartedActivity.startTime != 0
1572 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1573 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1574 final int nextUid = next.info.applicationInfo.uid;
1575 if (inTime && lastUid != nextUid
1576 && lastUid != next.launchedFromUid
1577 && mService.checkPermission(
1578 android.Manifest.permission.STOP_APP_SWITCHES,
1579 -1, next.launchedFromUid)
1580 != PackageManager.PERMISSION_GRANTED) {
1581 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1582 } else {
1583 next.startTime = now;
1584 mLastStartedActivity = next;
1585 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001586 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001587 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001588 mLastStartedActivity = next;
1589 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001590 }
Craig Mautner58547802013-03-05 08:23:53 -08001591
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001592 // We need to start pausing the current activity so the top one
1593 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001594 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001595 if (mResumedActivity != null) {
1596 pausing = true;
1597 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001598 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001599 }
1600 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001601 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1602 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001603 // At this point we want to put the upcoming activity's process
1604 // at the top of the LRU list, since we know we will be needing it
1605 // very soon and it would be a waste to let it get killed if it
1606 // happens to be sitting towards the end.
1607 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001608 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001609 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001610 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 return true;
1612 }
1613
Christopher Tated3f175c2012-06-14 14:16:54 -07001614 // If the most recent activity was noHistory but was only stopped rather
1615 // than stopped+finished because the device went to sleep, we need to make
1616 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001617 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001618 !mLastNoHistoryActivity.finishing) {
1619 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1620 " on new resume");
1621 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1622 null, "no-history", false);
1623 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001624 }
1625
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 if (prev != null && prev != next) {
1627 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1628 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001629 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 if (DEBUG_SWITCH) Slog.v(
1631 TAG, "Resuming top, waiting visible to hide: " + prev);
1632 } else {
1633 // The next activity is already visible, so hide the previous
1634 // activity's windows right now so we can show the new one ASAP.
1635 // We only do this if the previous is finishing, which should mean
1636 // it is on top of the one being resumed so hiding it quickly
1637 // is good. Otherwise, we want to do the normal route of allowing
1638 // the resumed activity to be shown so we can decide if the
1639 // previous should actually be hidden depending on whether the
1640 // new one is found to be full-screen or not.
1641 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001642 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001643 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1644 + prev + ", waitingVisible="
1645 + (prev != null ? prev.waitingVisible : null)
1646 + ", nowVisible=" + next.nowVisible);
1647 } else {
1648 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1649 + prev + ", waitingVisible="
1650 + (prev != null ? prev.waitingVisible : null)
1651 + ", nowVisible=" + next.nowVisible);
1652 }
1653 }
1654 }
1655
Dianne Hackborne7f97212011-02-24 14:40:20 -08001656 // Launching this app's activity, make sure the app is no longer
1657 // considered stopped.
1658 try {
1659 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001660 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001661 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001662 } catch (IllegalArgumentException e) {
1663 Slog.w(TAG, "Failed trying to unstop package "
1664 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001665 }
1666
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 // We are starting up the next activity, so tell the window manager
1668 // that the previous one will be hidden soon. This way it can know
1669 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001670 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 if (prev != null) {
1672 if (prev.finishing) {
1673 if (DEBUG_TRANSITION) Slog.v(TAG,
1674 "Prepare close transition: prev=" + prev);
1675 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001676 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001677 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001678 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001679 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001680 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1681 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001682 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001683 mWindowManager.setAppWillBeHidden(prev.appToken);
1684 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001686 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001688 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001689 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001691 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001692 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001693 : next.mLaunchTaskBehind
1694 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1695 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 }
1697 }
1698 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001699 mWindowManager.setAppWillBeHidden(prev.appToken);
1700 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001701 }
Craig Mautner967212c2013-04-13 21:10:58 -07001702 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001703 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001705 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001706 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001708 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001709 }
1710 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001711
1712 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001713 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001714 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1715 if (opts != null) {
1716 resumeAnimOptions = opts.toBundle();
1717 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001718 next.applyOptionsLocked();
1719 } else {
1720 next.clearOptionsLocked();
1721 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722
Craig Mautnercf910b02013-04-23 11:23:27 -07001723 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 if (next.app != null && next.app.thread != null) {
1725 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1726
1727 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001728 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001729
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001730 // schedule launch ticks to collect information about slow apps.
1731 next.startLaunchTickingLocked();
1732
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001733 ActivityRecord lastResumedActivity =
1734 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 ActivityState lastState = next.state;
1736
1737 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001738
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001739 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 next.state = ActivityState.RESUMED;
1741 mResumedActivity = next;
1742 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001743 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001744 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001746 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747
1748 // Have the window manager re-evaluate the orientation of
1749 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001750 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001751 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001752 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001753 mService.mConfiguration,
1754 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1755 if (config != null) {
1756 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001758 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001759 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001760
Craig Mautner525f3d92013-05-07 14:01:50 -07001761 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762 // The configuration update wasn't able to keep the existing
1763 // instance of the activity, and instead started a new one.
1764 // We should be all done, but let's just make sure our activity
1765 // is still at the top and schedule another run if something
1766 // weird happened.
1767 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001768 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 "Activity config changed during resume: " + next
1770 + ", new next: " + nextNext);
1771 if (nextNext != next) {
1772 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001773 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001775 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001776 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001777 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001778 return true;
1779 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001780 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001781 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 }
Craig Mautner58547802013-03-05 08:23:53 -08001783
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 try {
1785 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001786 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 if (a != null) {
1788 final int N = a.size();
1789 if (!next.finishing && N > 0) {
1790 if (DEBUG_RESULTS) Slog.v(
1791 TAG, "Delivering results to " + next
1792 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001793 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 }
1795 }
1796
1797 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001798 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 }
1800
1801 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001802 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001804
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001805 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001806 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001807 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001808 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001809 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001810 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001811 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001812
Craig Mautner0eea92c2013-05-16 13:35:39 -07001813 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814
Craig Mautnerac6f8432013-07-17 13:24:59 -07001815 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001816 } catch (Exception e) {
1817 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001818 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1819 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001821 if (lastStack != null) {
1822 lastStack.mResumedActivity = lastResumedActivity;
1823 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 Slog.i(TAG, "Restarting because process died: " + next);
1825 if (!next.hasBeenLaunched) {
1826 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001827 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1828 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001829 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001830 next.appToken, next.packageName, next.theme,
1831 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001832 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1833 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001834 }
George Mount2c92c972014-03-20 09:38:23 -07001835 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001836 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837 return true;
1838 }
1839
1840 // From this point on, if something goes wrong there is no way
1841 // to recover the activity.
1842 try {
1843 next.visible = true;
1844 completeResumeLocked(next);
1845 } catch (Exception e) {
1846 // If any exception gets thrown, toss away this
1847 // activity and try the next one.
1848 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001849 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001850 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001851 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001852 return true;
1853 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 next.stopped = false;
1855
1856 } else {
1857 // Whoops, need to restart this activity!
1858 if (!next.hasBeenLaunched) {
1859 next.hasBeenLaunched = true;
1860 } else {
1861 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001862 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001863 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001864 mService.compatibilityInfoForPackageLocked(
1865 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001866 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001867 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001868 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 }
1870 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1871 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001872 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001873 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 }
1875
Craig Mautnercf910b02013-04-23 11:23:27 -07001876 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 return true;
1878 }
1879
Craig Mautnerac6f8432013-07-17 13:24:59 -07001880 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001881 // If this is being moved to the top by another activity or being launched from the home
1882 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001883 if (isOnHomeDisplay()) {
1884 ActivityStack lastStack = mStackSupervisor.getLastStack();
1885 final boolean fromHome = lastStack.isHomeStack();
1886 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001887 task.setTaskToReturnTo(fromHome
1888 ? lastStack.topTask() == null
1889 ? HOME_ACTIVITY_TYPE
1890 : lastStack.topTask().taskType
1891 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001892 }
1893 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001894 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001895 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001896
Craig Mautnerac6f8432013-07-17 13:24:59 -07001897 mTaskHistory.remove(task);
1898 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001899 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001900 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001901 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001902 while (--taskNdx >= 0) {
1903 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001904 break;
1905 }
1906 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001907 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001908 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001909 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001910 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001911 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001912
Craig Mautner8849a5e2013-04-02 16:41:03 -07001913 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001914 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001915 TaskRecord rTask = r.task;
1916 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001917 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1918 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001919 // Last activity in task had been removed or ActivityManagerService is reusing task.
1920 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001921 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001922 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001923 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001924 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001925 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926 if (!newTask) {
1927 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001928 boolean startIt = true;
1929 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1930 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001931 if (task.getTopActivity() == null) {
1932 // All activities in task are finishing.
1933 continue;
1934 }
Craig Mautner70a86932013-02-28 22:37:44 -08001935 if (task == r.task) {
1936 // Here it is! Now, if this is not yet visible to the
1937 // user, then just add it without starting; it will
1938 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001939 if (!startIt) {
1940 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1941 + task, new RuntimeException("here").fillInStackTrace());
1942 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001943 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001944 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1945 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001946 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001947 r.userId, r.info.configChanges, task.voiceSession != null,
1948 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001950 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001952 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001953 return;
1954 }
1955 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001956 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 startIt = false;
1958 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 }
1960 }
1961
1962 // Place a new activity at top of stack, so it is next to interact
1963 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001964
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001965 // If we are not placing the new activity frontmost, we do not want
1966 // to deliver the onUserLeaving callback to the actual frontmost
1967 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001968 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001969 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001970 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1971 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 }
Craig Mautner70a86932013-02-28 22:37:44 -08001973
1974 task = r.task;
1975
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001976 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001977 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001978 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001979 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001980 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001981
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001982 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001983 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 // We want to show the starting preview window if we are
1985 // switching to a new task, or the next activity's process is
1986 // not currently running.
1987 boolean showStartingIcon = newTask;
1988 ProcessRecord proc = r.app;
1989 if (proc == null) {
1990 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1991 }
1992 if (proc == null || proc.thread == null) {
1993 showStartingIcon = true;
1994 }
1995 if (DEBUG_TRANSITION) Slog.v(TAG,
1996 "Prepare open transition: starting " + r);
1997 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001998 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002001 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002002 ? r.mLaunchTaskBehind
2003 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2004 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002005 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 mNoAnimActivities.remove(r);
2007 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002008 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002009 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002010 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002011 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002012 boolean doShow = true;
2013 if (newTask) {
2014 // Even though this activity is starting fresh, we still need
2015 // to reset it to make sure we apply affinities to move any
2016 // existing activities from other tasks in to it.
2017 // If the caller has requested that the target task be
2018 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002019 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 resetTaskIfNeededLocked(r, r);
2021 doShow = topRunningNonDelayedActivityLocked(null) == r;
2022 }
George Mount70778d72014-07-01 16:33:45 -07002023 } else if (options != null && new ActivityOptions(options).getAnimationType()
2024 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2025 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002026 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002027 if (r.mLaunchTaskBehind) {
2028 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2029 // tell WindowManager that r is visible even though it is at the back of the stack.
2030 mWindowManager.setAppVisibility(r.appToken, true);
2031 ensureActivitiesVisibleLocked(null, 0);
2032 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002033 // Figure out if we are transitioning from another activity that is
2034 // "has the same starting icon" as the next one. This allows the
2035 // window manager to keep the previous window it had previously
2036 // created, if it still had one.
2037 ActivityRecord prev = mResumedActivity;
2038 if (prev != null) {
2039 // We don't want to reuse the previous starting preview if:
2040 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002041 if (prev.task != r.task) {
2042 prev = null;
2043 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002044 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002045 else if (prev.nowVisible) {
2046 prev = null;
2047 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002049 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002050 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002051 mService.compatibilityInfoForPackageLocked(
2052 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002053 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002054 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002055 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 }
2057 } else {
2058 // If this is the first activity, don't do any fancy animations,
2059 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002060 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002061 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002062 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002063 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002064 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002065 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 }
2067 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002068 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002069 }
2070
2071 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002072 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002073 }
2074 }
2075
Dianne Hackbornbe707852011-11-11 14:32:10 -08002076 final void validateAppTokensLocked() {
2077 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002078 mValidateAppTokens.ensureCapacity(numActivities());
2079 final int numTasks = mTaskHistory.size();
2080 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2081 TaskRecord task = mTaskHistory.get(taskNdx);
2082 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002083 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002084 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002085 }
Craig Mautner000f0022013-02-26 15:04:29 -08002086 TaskGroup group = new TaskGroup();
2087 group.taskId = task.taskId;
2088 mValidateAppTokens.add(group);
2089 final int numActivities = activities.size();
2090 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2091 final ActivityRecord r = activities.get(activityNdx);
2092 group.tokens.add(r.appToken);
2093 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002094 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002095 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002096 }
2097
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002098 /**
2099 * Perform a reset of the given task, if needed as part of launching it.
2100 * Returns the new HistoryRecord at the top of the task.
2101 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002102 /**
2103 * Helper method for #resetTaskIfNeededLocked.
2104 * We are inside of the task being reset... we'll either finish this activity, push it out
2105 * for another task, or leave it as-is.
2106 * @param task The task containing the Activity (taskTop) that might be reset.
2107 * @param forceReset
2108 * @return An ActivityOptions that needs to be processed.
2109 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002110 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002111 ActivityOptions topOptions = null;
2112
2113 int replyChainEnd = -1;
2114 boolean canMoveOptions = true;
2115
2116 // We only do this for activities that are not the root of the task (since if we finish
2117 // the root, we may no longer have the task!).
2118 final ArrayList<ActivityRecord> activities = task.mActivities;
2119 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002120 final int rootActivityNdx = task.findEffectiveRootIndex();
2121 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002122 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002123 if (target.frontOfTask)
2124 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002125
2126 final int flags = target.info.flags;
2127 final boolean finishOnTaskLaunch =
2128 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2129 final boolean allowTaskReparenting =
2130 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2131 final boolean clearWhenTaskReset =
2132 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2133
2134 if (!finishOnTaskLaunch
2135 && !clearWhenTaskReset
2136 && target.resultTo != null) {
2137 // If this activity is sending a reply to a previous
2138 // activity, we can't do anything with it now until
2139 // we reach the start of the reply chain.
2140 // XXX note that we are assuming the result is always
2141 // to the previous activity, which is almost always
2142 // the case but we really shouldn't count on.
2143 if (replyChainEnd < 0) {
2144 replyChainEnd = i;
2145 }
2146 } else if (!finishOnTaskLaunch
2147 && !clearWhenTaskReset
2148 && allowTaskReparenting
2149 && target.taskAffinity != null
2150 && !target.taskAffinity.equals(task.affinity)) {
2151 // If this activity has an affinity for another
2152 // task, then we need to move it out of here. We will
2153 // move it as far out of the way as possible, to the
2154 // bottom of the activity stack. This also keeps it
2155 // correctly ordered with any activities we previously
2156 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002157 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002158 final ActivityRecord bottom =
2159 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002160 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002161 if (bottom != null && target.taskAffinity != null
2162 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002163 // If the activity currently at the bottom has the
2164 // same task affinity as the one we are moving,
2165 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002166 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002167 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002168 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002169 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002170 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002171 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002172 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002173 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2174 + " out to new task " + target.task);
2175 }
2176
Craig Mautnere3a74d52013-02-22 14:14:58 -08002177 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002178 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002179
Craig Mautner525f3d92013-05-07 14:01:50 -07002180 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002181 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2182 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002183 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002184 if (p.finishing) {
2185 continue;
2186 }
2187
Craig Mautnere3a74d52013-02-22 14:14:58 -08002188 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002189 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002190 topOptions = p.takeOptionsLocked();
2191 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002192 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002193 }
2194 }
2195 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002196 + task + " adding to task=" + targetTask
2197 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002198 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2199 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002200 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002201 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002202
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002203 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002204 }
2205
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002206 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002207 if (VALIDATE_TOKENS) {
2208 validateAppTokensLocked();
2209 }
2210
2211 replyChainEnd = -1;
2212 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2213 // If the activity should just be removed -- either
2214 // because it asks for it, or the task should be
2215 // cleared -- then finish it and anything that is
2216 // part of its reply chain.
2217 int end;
2218 if (clearWhenTaskReset) {
2219 // In this case, we want to finish this activity
2220 // and everything above it, so be sneaky and pretend
2221 // like these are all in the reply chain.
2222 end = numActivities - 1;
2223 } else if (replyChainEnd < 0) {
2224 end = i;
2225 } else {
2226 end = replyChainEnd;
2227 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002228 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002229 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002230 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002231 if (p.finishing) {
2232 continue;
2233 }
2234 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002235 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002236 topOptions = p.takeOptionsLocked();
2237 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002238 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002239 }
2240 }
Craig Mautner58547802013-03-05 08:23:53 -08002241 if (DEBUG_TASKS) Slog.w(TAG,
2242 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002243 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002244 end--;
2245 srcPos--;
2246 }
2247 }
2248 replyChainEnd = -1;
2249 } else {
2250 // If we were in the middle of a chain, well the
2251 // activity that started it all doesn't want anything
2252 // special, so leave it all as-is.
2253 replyChainEnd = -1;
2254 }
2255 }
2256
2257 return topOptions;
2258 }
2259
2260 /**
2261 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2262 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2263 * @param affinityTask The task we are looking for an affinity to.
2264 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2265 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2266 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2267 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002268 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002269 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002270 int replyChainEnd = -1;
2271 final int taskId = task.taskId;
2272 final String taskAffinity = task.affinity;
2273
2274 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2275 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002276 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2277
2278 // Do not operate on or below the effective root Activity.
2279 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002280 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002281 if (target.frontOfTask)
2282 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002283
2284 final int flags = target.info.flags;
2285 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2286 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2287
2288 if (target.resultTo != null) {
2289 // If this activity is sending a reply to a previous
2290 // activity, we can't do anything with it now until
2291 // we reach the start of the reply chain.
2292 // XXX note that we are assuming the result is always
2293 // to the previous activity, which is almost always
2294 // the case but we really shouldn't count on.
2295 if (replyChainEnd < 0) {
2296 replyChainEnd = i;
2297 }
2298 } else if (topTaskIsHigher
2299 && allowTaskReparenting
2300 && taskAffinity != null
2301 && taskAffinity.equals(target.taskAffinity)) {
2302 // This activity has an affinity for our task. Either remove it if we are
2303 // clearing or move it over to our task. Note that
2304 // we currently punt on the case where we are resetting a
2305 // task that is not at the top but who has activities above
2306 // with an affinity to it... this is really not a normal
2307 // case, and we will need to later pull that task to the front
2308 // and usually at that point we will do the reset and pick
2309 // up those remaining activities. (This only happens if
2310 // someone starts an activity in a new task from an activity
2311 // in a task that is not currently on top.)
2312 if (forceReset || finishOnTaskLaunch) {
2313 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2314 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2315 for (int srcPos = start; srcPos >= i; --srcPos) {
2316 final ActivityRecord p = activities.get(srcPos);
2317 if (p.finishing) {
2318 continue;
2319 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002320 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002321 }
2322 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002323 if (taskInsertionPoint < 0) {
2324 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002325
Craig Mautner77878772013-03-04 19:46:24 -08002326 }
Craig Mautner77878772013-03-04 19:46:24 -08002327
2328 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2329 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2330 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2331 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002332 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002333 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002334 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002335
Craig Mautnere3a74d52013-02-22 14:14:58 -08002336 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2337 + " to stack at " + task,
2338 new RuntimeException("here").fillInStackTrace());
2339 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2340 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002341 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002342 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002343 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002344 if (VALIDATE_TOKENS) {
2345 validateAppTokensLocked();
2346 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002347
2348 // Now we've moved it in to place... but what if this is
2349 // a singleTop activity and we have put it on top of another
2350 // instance of the same activity? Then we drop the instance
2351 // below so it remains singleTop.
2352 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2353 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002354 int targetNdx = taskActivities.indexOf(target);
2355 if (targetNdx > 0) {
2356 ActivityRecord p = taskActivities.get(targetNdx - 1);
2357 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002358 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2359 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002360 }
2361 }
2362 }
2363 }
2364
2365 replyChainEnd = -1;
2366 }
2367 }
Craig Mautner77878772013-03-04 19:46:24 -08002368 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002369 }
2370
Craig Mautner8849a5e2013-04-02 16:41:03 -07002371 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002372 ActivityRecord newActivity) {
2373 boolean forceReset =
2374 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2375 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2376 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2377 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2378 forceReset = true;
2379 }
2380 }
2381
2382 final TaskRecord task = taskTop.task;
2383
2384 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2385 * for remaining tasks. Used for later tasks to reparent to task. */
2386 boolean taskFound = false;
2387
2388 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2389 ActivityOptions topOptions = null;
2390
Craig Mautner77878772013-03-04 19:46:24 -08002391 // Preserve the location for reparenting in the new task.
2392 int reparentInsertionPoint = -1;
2393
Craig Mautnere3a74d52013-02-22 14:14:58 -08002394 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2395 final TaskRecord targetTask = mTaskHistory.get(i);
2396
2397 if (targetTask == task) {
2398 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2399 taskFound = true;
2400 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002401 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2402 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002403 }
2404 }
2405
Craig Mautner70a86932013-02-28 22:37:44 -08002406 int taskNdx = mTaskHistory.indexOf(task);
2407 do {
2408 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2409 } while (taskTop == null && taskNdx >= 0);
2410
Craig Mautnere3a74d52013-02-22 14:14:58 -08002411 if (topOptions != null) {
2412 // If we got some ActivityOptions from an activity on top that
2413 // was removed from the task, propagate them to the new real top.
2414 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002415 taskTop.updateOptionsLocked(topOptions);
2416 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002417 topOptions.abort();
2418 }
2419 }
2420
2421 return taskTop;
2422 }
2423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2425 String resultWho, int requestCode, int resultCode, Intent data) {
2426
2427 if (callingUid > 0) {
2428 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002429 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002430 }
2431
2432 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2433 + " : who=" + resultWho + " req=" + requestCode
2434 + " res=" + resultCode + " data=" + data);
2435 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2436 try {
2437 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2438 list.add(new ResultInfo(resultWho, requestCode,
2439 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002440 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 return;
2442 } catch (Exception e) {
2443 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2444 }
2445 }
2446
2447 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2448 }
2449
Craig Mautner04f0b702013-10-22 12:31:01 -07002450 private void adjustFocusedActivityLocked(ActivityRecord r) {
2451 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2452 ActivityRecord next = topRunningActivityLocked(null);
2453 if (next != r) {
2454 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002455 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2456 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002457 }
2458 }
Winson Chung648c83b2014-04-28 15:11:56 -07002459 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2460 if (top != null) {
2461 mService.setFocusedActivityLocked(top);
2462 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002463 }
2464 }
2465
Craig Mautnerf3333272013-04-22 10:55:53 -07002466 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002467 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2468 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2469 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2470 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002471 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002472 if (DEBUG_STATES) {
2473 Slog.d(TAG, "no-history finish of " + r);
2474 }
2475 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002476 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002477 } else {
2478 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2479 + " on stop because we're just sleeping");
2480 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002482 }
2483
2484 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002485 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002486 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 try {
2488 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002489 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2490 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002491 r.state = ActivityState.STOPPING;
2492 if (DEBUG_VISBILITY) Slog.v(
2493 TAG, "Stopping visible=" + r.visible + " for " + r);
2494 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002495 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002497 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002498 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002499 r.setSleeping(true);
2500 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002501 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002502 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002503 } catch (Exception e) {
2504 // Maybe just ignore exceptions here... if the process
2505 // has crashed, our death notification will clean things
2506 // up.
2507 Slog.w(TAG, "Exception thrown during pause", e);
2508 // Just in case, assume it to be stopped.
2509 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002510 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 r.state = ActivityState.STOPPED;
2512 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002513 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002514 }
2515 }
2516 }
2517 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002518
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002519 /**
2520 * @return Returns true if the activity is being finished, false if for
2521 * some reason it is being left as-is.
2522 */
2523 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002524 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002525 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002526 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002527 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002528 + ", result=" + resultCode + ", data=" + resultData
2529 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002530 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 return false;
2532 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533
Craig Mautnerd44711d2013-02-23 11:24:36 -08002534 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 return true;
2536 }
2537
Craig Mautnerd2328952013-03-05 12:46:26 -08002538 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002539 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2540 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2541 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2542 ActivityRecord r = activities.get(activityNdx);
2543 if (r.resultTo == self && r.requestCode == requestCode) {
2544 if ((r.resultWho == null && resultWho == null) ||
2545 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2546 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2547 false);
2548 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002549 }
2550 }
2551 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002552 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002553 }
2554
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002555 final void finishTopRunningActivityLocked(ProcessRecord app) {
2556 ActivityRecord r = topRunningActivityLocked(null);
2557 if (r != null && r.app == app) {
2558 // If the top running activity is from this crashing
2559 // process, then terminate it to avoid getting in a loop.
2560 Slog.w(TAG, " Force finishing activity "
2561 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002562 int taskNdx = mTaskHistory.indexOf(r.task);
2563 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002564 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002565 // Also terminate any activities below it that aren't yet
2566 // stopped, to avoid a situation where one will get
2567 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002568 --activityNdx;
2569 if (activityNdx < 0) {
2570 do {
2571 --taskNdx;
2572 if (taskNdx < 0) {
2573 break;
2574 }
2575 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2576 } while (activityNdx < 0);
2577 }
2578 if (activityNdx >= 0) {
2579 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002580 if (r.state == ActivityState.RESUMED
2581 || r.state == ActivityState.PAUSING
2582 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002583 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002584 Slog.w(TAG, " Force finishing activity "
2585 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002586 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002587 }
2588 }
2589 }
2590 }
2591 }
2592
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002593 final void finishVoiceTask(IVoiceInteractionSession session) {
2594 IBinder sessionBinder = session.asBinder();
2595 boolean didOne = false;
2596 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2597 TaskRecord tr = mTaskHistory.get(taskNdx);
2598 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2599 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2600 ActivityRecord r = tr.mActivities.get(activityNdx);
2601 if (!r.finishing) {
2602 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2603 false);
2604 didOne = true;
2605 }
2606 }
2607 }
2608 }
2609 if (didOne) {
2610 mService.updateOomAdjLocked();
2611 }
2612 }
2613
Craig Mautnerd2328952013-03-05 12:46:26 -08002614 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002615 ArrayList<ActivityRecord> activities = r.task.mActivities;
2616 for (int index = activities.indexOf(r); index >= 0; --index) {
2617 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002618 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002619 break;
2620 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002621 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002622 }
2623 return true;
2624 }
2625
Dianne Hackborn5c607432012-02-28 14:44:19 -08002626 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2627 // send the result
2628 ActivityRecord resultTo = r.resultTo;
2629 if (resultTo != null) {
2630 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2631 + " who=" + r.resultWho + " req=" + r.requestCode
2632 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002633 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002634 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002635 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002636 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002637 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002638 if (r.info.applicationInfo.uid > 0) {
2639 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2640 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002641 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002642 }
2643 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2644 resultData);
2645 r.resultTo = null;
2646 }
2647 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2648
2649 // Make sure this HistoryRecord is not holding on to other resources,
2650 // because clients have remote IPC references to this object so we
2651 // can't assume that will go away and want to avoid circular IPC refs.
2652 r.results = null;
2653 r.pendingResults = null;
2654 r.newIntents = null;
2655 r.icicle = null;
2656 }
2657
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 /**
2659 * @return Returns true if this activity has been removed from the history
2660 * list, or false if it is still in the list and will be removed later.
2661 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002662 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2663 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002664 if (r.finishing) {
2665 Slog.w(TAG, "Duplicate finish request for " + r);
2666 return false;
2667 }
2668
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002669 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002670 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002672 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002673 task.taskId, r.shortComponentName, reason);
2674 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002675 final int index = activities.indexOf(r);
2676 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002677 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002678 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002679 // If the caller asked that this activity (and all above it)
2680 // be cleared when the task is reset, don't lose that information,
2681 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002682 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002683 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002684 }
2685 }
2686
2687 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002688
2689 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690
Dianne Hackborn5c607432012-02-28 14:44:19 -08002691 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002692
Craig Mautnerde4ef022013-04-07 19:01:33 -07002693 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002694 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002695 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002697 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002698 ? AppTransition.TRANSIT_TASK_CLOSE
2699 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002700
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002702 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002703
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002704 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002705 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2706 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2707 startPausingLocked(false, false);
2708 }
2709
Craig Mautneraea74a52014-03-08 14:23:10 -08002710 if (endTask) {
2711 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2712 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 } else if (r.state != ActivityState.PAUSING) {
2714 // If the activity is PAUSING, we will complete the finish once
2715 // it is done pausing; else we can just directly finish it here.
2716 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002717 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002718 } else {
2719 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2720 }
2721
2722 return false;
2723 }
2724
Craig Mautnerf3333272013-04-22 10:55:53 -07002725 static final int FINISH_IMMEDIATELY = 0;
2726 static final int FINISH_AFTER_PAUSE = 1;
2727 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728
Craig Mautnerf3333272013-04-22 10:55:53 -07002729 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 // First things first: if this activity is currently visible,
2731 // and the resumed activity is not yet visible, then hold off on
2732 // finishing until the resumed one becomes visible.
2733 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002734 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2735 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002736 if (mStackSupervisor.mStoppingActivities.size() > 3
2737 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002738 // If we already have a few activities waiting to stop,
2739 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002740 // them out. Or if r is the last of activity of the last task the stack
2741 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002742 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002743 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002744 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 }
2746 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002747 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2748 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002750 if (oomAdj) {
2751 mService.updateOomAdjLocked();
2752 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002753 return r;
2754 }
2755
2756 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002757 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002758 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002759 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 if (mResumedActivity == r) {
2761 mResumedActivity = null;
2762 }
2763 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002764 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002765 r.state = ActivityState.FINISHING;
2766
2767 if (mode == FINISH_IMMEDIATELY
2768 || prevState == ActivityState.STOPPED
2769 || prevState == ActivityState.INITIALIZING) {
2770 // If this activity is already stopped, we can just finish
2771 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002772 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002773 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002774 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002775 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002776 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002777 if (DEBUG_CONTAINERS) Slog.d(TAG,
2778 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2779 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002780 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002781 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002782
2783 // Need to go through the full pause cycle to get this
2784 // activity into the stopped state and then finish it.
2785 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002786 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002787 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002788 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 return r;
2790 }
2791
Craig Mautneree36c772014-07-16 14:56:05 -07002792 void finishAllActivitiesLocked(boolean immediately) {
2793 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002794 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2795 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2796 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2797 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002798 noActivitiesInStack = false;
2799 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002800 continue;
2801 }
Craig Mautneree36c772014-07-16 14:56:05 -07002802 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002803 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2804 }
2805 }
Craig Mautneree36c772014-07-16 14:56:05 -07002806 if (noActivitiesInStack) {
2807 mActivityContainer.onTaskListEmptyLocked();
2808 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002809 }
2810
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002811 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2812 // Basic case: for simple app-centric recents, we need to recreate
2813 // the task if the affinity has changed.
2814 if (srec == null || srec.task.affinity == null ||
2815 !srec.task.affinity.equals(destAffinity)) {
2816 return true;
2817 }
2818 // Document-centric case: an app may be split in to multiple documents;
2819 // they need to re-create their task if this current activity is the root
2820 // of a document, unless simply finishing it will return them to the the
2821 // correct app behind.
2822 if (srec.frontOfTask && srec.task != null) {
2823 // Okay, this activity is at the root of its task. What to do, what to do...
2824 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2825 // Finishing won't return to an application, so we need to recreate.
2826 return true;
2827 }
2828 // We now need to get the task below it to determine what to do.
2829 int taskIdx = mTaskHistory.indexOf(srec.task);
2830 if (taskIdx <= 0) {
2831 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2832 return false;
2833 }
2834 if (taskIdx == 0) {
2835 // At the bottom of the stack, nothing to go back to.
2836 return true;
2837 }
2838 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2839 if (!srec.task.affinity.equals(prevTask.affinity)) {
2840 // These are different apps, so need to recreate.
2841 return true;
2842 }
2843 }
2844 return false;
2845 }
2846
Craig Mautnerd2328952013-03-05 12:46:26 -08002847 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002848 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002849 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002850 final TaskRecord task = srec.task;
2851 final ArrayList<ActivityRecord> activities = task.mActivities;
2852 final int start = activities.indexOf(srec);
2853 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002854 return false;
2855 }
2856 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002857 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002858 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002859 final ComponentName dest = destIntent.getComponent();
2860 if (start > 0 && dest != null) {
2861 for (int i = finishTo; i >= 0; i--) {
2862 ActivityRecord r = activities.get(i);
2863 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002864 r.info.name.equals(dest.getClassName())) {
2865 finishTo = i;
2866 parent = r;
2867 foundParentInTask = true;
2868 break;
2869 }
2870 }
2871 }
2872
2873 IActivityController controller = mService.mController;
2874 if (controller != null) {
2875 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2876 if (next != null) {
2877 // ask watcher if this is allowed
2878 boolean resumeOK = true;
2879 try {
2880 resumeOK = controller.activityResuming(next.packageName);
2881 } catch (RemoteException e) {
2882 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002883 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002884 }
2885
2886 if (!resumeOK) {
2887 return false;
2888 }
2889 }
2890 }
2891 final long origId = Binder.clearCallingIdentity();
2892 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002893 ActivityRecord r = activities.get(i);
2894 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002895 // Only return the supplied result for the first activity finished
2896 resultCode = Activity.RESULT_CANCELED;
2897 resultData = null;
2898 }
2899
2900 if (parent != null && foundParentInTask) {
2901 final int parentLaunchMode = parent.info.launchMode;
2902 final int destIntentFlags = destIntent.getFlags();
2903 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2904 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2905 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2906 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2907 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2908 } else {
2909 try {
2910 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2911 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002912 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002913 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002914 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002915 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002916 foundParentInTask = res == ActivityManager.START_SUCCESS;
2917 } catch (RemoteException e) {
2918 foundParentInTask = false;
2919 }
2920 requestFinishActivityLocked(parent.appToken, resultCode,
2921 resultData, "navigate-up", true);
2922 }
2923 }
2924 Binder.restoreCallingIdentity(origId);
2925 return foundParentInTask;
2926 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 /**
2928 * Perform the common clean-up of an activity record. This is called both
2929 * as part of destroyActivityLocked() (when destroying the client-side
2930 * representation) and cleaning things up as a result of its hosting
2931 * processing going away, in which case there is no remaining client-side
2932 * state to destroy so only the cleanup here is needed.
2933 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002934 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2935 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 if (mResumedActivity == r) {
2937 mResumedActivity = null;
2938 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002939 if (mPausingActivity == r) {
2940 mPausingActivity = null;
2941 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002942 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002943
2944 r.configDestroy = false;
2945 r.frozenBeforeDestroy = false;
2946
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002947 if (setState) {
2948 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2949 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002950 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002951 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002952 }
2953
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002954 // Make sure this record is no longer in the pending finishes list.
2955 // This could happen, for example, if we are trimming activities
2956 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002957 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002958 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002959
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960 // Remove any pending results.
2961 if (r.finishing && r.pendingResults != null) {
2962 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2963 PendingIntentRecord rec = apr.get();
2964 if (rec != null) {
2965 mService.cancelIntentSenderLocked(rec, false);
2966 }
2967 }
2968 r.pendingResults = null;
2969 }
2970
2971 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002972 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002973 }
2974
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002976 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07002977 if (getVisibleBehindActivity() == r) {
2978 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002979 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002980 }
2981
2982 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002983 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002984 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002985 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002986 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002987 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 }
2989
Craig Mautner04a0ea62014-01-13 12:51:26 -08002990 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002991 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002992 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2993 r.makeFinishing();
2994 if (DEBUG_ADD_REMOVE) {
2995 RuntimeException here = new RuntimeException("here");
2996 here.fillInStackTrace();
2997 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002999 r.takeFromHistory();
3000 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003001 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003002 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003003 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003004 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003005 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003006 if (VALIDATE_TOKENS) {
3007 validateAppTokensLocked();
3008 }
Craig Mautner312ba862014-02-10 17:55:01 -08003009 final TaskRecord task = r.task;
3010 if (task != null && task.removeActivity(r)) {
3011 if (DEBUG_STACK) Slog.i(TAG,
3012 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003013 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3014 task.isOverHomeStack()) {
3015 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08003016 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003017 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08003018 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003019 cleanUpActivityServicesLocked(r);
3020 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003022
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 /**
3024 * Perform clean-up of service connections in an activity record.
3025 */
3026 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3027 // Throw away any services that have been bound by this activity.
3028 if (r.connections != null) {
3029 Iterator<ConnectionRecord> it = r.connections.iterator();
3030 while (it.hasNext()) {
3031 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003032 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 }
3034 r.connections = null;
3035 }
3036 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003037
Craig Mautneree2e45a2014-06-27 12:10:03 -07003038 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003039 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003040 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003041 mHandler.sendMessage(msg);
3042 }
3043
Craig Mautneree2e45a2014-06-27 12:10:03 -07003044 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003045 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003046 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003047 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3048 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3049 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3050 final ActivityRecord r = activities.get(activityNdx);
3051 if (r.finishing) {
3052 continue;
3053 }
3054 if (r.fullscreen) {
3055 lastIsOpaque = true;
3056 }
3057 if (owner != null && r.app != owner) {
3058 continue;
3059 }
3060 if (!lastIsOpaque) {
3061 continue;
3062 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003063 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003064 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3065 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003066 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003067 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003068 activityRemoved = true;
3069 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003070 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003071 }
3072 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003073 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003074 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003075 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003076 }
3077
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003078 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3079 if (r.isDestroyable()) {
3080 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3081 + " resumed=" + mResumedActivity
3082 + " pausing=" + mPausingActivity + " for reason " + reason);
3083 return destroyActivityLocked(r, true, reason);
3084 }
3085 return false;
3086 }
3087
3088 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3089 String reason) {
3090 // Iterate over tasks starting at the back (oldest) first.
3091 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3092 int maxTasks = tasks.size() / 4;
3093 if (maxTasks < 1) {
3094 maxTasks = 1;
3095 }
3096 int numReleased = 0;
3097 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3098 final TaskRecord task = mTaskHistory.get(taskNdx);
3099 if (!tasks.contains(task)) {
3100 continue;
3101 }
3102 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3103 int curNum = 0;
3104 final ArrayList<ActivityRecord> activities = task.mActivities;
3105 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3106 final ActivityRecord activity = activities.get(actNdx);
3107 if (activity.app == app && activity.isDestroyable()) {
3108 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3109 + " in state " + activity.state + " resumed=" + mResumedActivity
3110 + " pausing=" + mPausingActivity + " for reason " + reason);
3111 destroyActivityLocked(activity, true, reason);
3112 if (activities.get(actNdx) != activity) {
3113 // Was removed from list, back up so we don't miss the next one.
3114 actNdx--;
3115 }
3116 curNum++;
3117 }
3118 }
3119 if (curNum > 0) {
3120 numReleased += curNum;
3121 maxTasks--;
3122 if (mTaskHistory.get(taskNdx) != task) {
3123 // The entire task got removed, back up so we don't miss the next one.
3124 taskNdx--;
3125 }
3126 }
3127 }
3128 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3129 return numReleased;
3130 }
3131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 /**
3133 * Destroy the current CLIENT SIDE instance of an activity. This may be
3134 * called both when actually finishing an activity, or when performing
3135 * a configuration switch where we destroy the current client-side object
3136 * but then create a new client-side object for this same HistoryRecord.
3137 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003138 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003139 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003140 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3142 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003143 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003144 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003145
3146 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003147
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003148 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149
3150 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 if (hadApp) {
3153 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003154 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3156 mService.mHeavyWeightProcess = null;
3157 mService.mHandler.sendEmptyMessage(
3158 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3159 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003160 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003161 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003162 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003163 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003164 }
3165 }
3166
3167 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003168
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 try {
3170 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003171 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 r.configChangeFlags);
3173 } catch (Exception e) {
3174 // We can just ignore exceptions here... if the process
3175 // has crashed, our death notification will clean things
3176 // up.
3177 //Slog.w(TAG, "Exception thrown during finish", e);
3178 if (r.finishing) {
3179 removeActivityFromHistoryLocked(r);
3180 removedFromHistory = true;
3181 skipDestroy = true;
3182 }
3183 }
3184
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003185 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003186
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003187 // If the activity is finishing, we need to wait on removing it
3188 // from the list to give it a chance to do its cleanup. During
3189 // that time it may make calls back with its token so we need to
3190 // be able to find it on the list and so we don't want to remove
3191 // it from the list yet. Otherwise, we can just immediately put
3192 // it in the destroyed state since we are not removing it from the
3193 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003195 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3196 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003198 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003199 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3200 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003201 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003202 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003203 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003204 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 }
3206 } else {
3207 // remove this record from the history.
3208 if (r.finishing) {
3209 removeActivityFromHistoryLocked(r);
3210 removedFromHistory = true;
3211 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003212 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003214 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003215 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216 }
3217 }
3218
3219 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003220
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221 if (!mLRUActivities.remove(r) && hadApp) {
3222 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3223 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003224
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 return removedFromHistory;
3226 }
3227
Craig Mautnerd2328952013-03-05 12:46:26 -08003228 final void activityDestroyedLocked(IBinder token) {
3229 final long origId = Binder.clearCallingIdentity();
3230 try {
3231 ActivityRecord r = ActivityRecord.forToken(token);
3232 if (r != null) {
3233 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003235 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003236
3237 if (isInStackLocked(token) != null) {
3238 if (r.state == ActivityState.DESTROYING) {
3239 cleanUpActivityLocked(r, true, false);
3240 removeActivityFromHistoryLocked(r);
3241 }
3242 }
Craig Mautner05d29032013-05-03 13:40:13 -07003243 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003244 } finally {
3245 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003246 }
3247 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003248
Jose Lima4b6c6692014-08-12 17:41:12 -07003249 void releaseBackgroundResources() {
3250 if (hasVisibleBehindActivity() &&
3251 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3252 final ActivityRecord r = getVisibleBehindActivity();
3253 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3254 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003255 " thread=" + r.app.thread);
3256 if (r != null && r.app != null && r.app.thread != null) {
3257 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003258 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003259 } catch (RemoteException e) {
3260 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003261 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003262 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003263 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
3264 backgroundResourcesReleased(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003265 }
3266 }
3267 }
3268
Jose Lima4b6c6692014-08-12 17:41:12 -07003269 final void backgroundResourcesReleased(IBinder token) {
3270 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3271 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003272 if (r != null) {
3273 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003274 setVisibleBehindActivity(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003275 }
3276 mStackSupervisor.resumeTopActivitiesLocked();
3277 }
3278
Jose Lima4b6c6692014-08-12 17:41:12 -07003279 boolean hasVisibleBehindActivity() {
3280 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003281 }
3282
Jose Lima4b6c6692014-08-12 17:41:12 -07003283 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003284 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003285 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003286 }
3287 }
3288
Jose Lima4b6c6692014-08-12 17:41:12 -07003289 ActivityRecord getVisibleBehindActivity() {
3290 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003291 }
3292
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003293 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3294 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003296 if (DEBUG_CLEANUP) Slog.v(
3297 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003298 + " with " + i + " entries");
3299 while (i > 0) {
3300 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003301 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003302 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003304 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003306 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003307 }
3308 }
3309 }
3310
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003311 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3312 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003313 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3314 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003315 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3316 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003317 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003318 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003319 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3320 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003321
3322 boolean hasVisibleActivities = false;
3323
3324 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003325 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003326 if (DEBUG_CLEANUP) Slog.v(
3327 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003328 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3329 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3330 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3331 final ActivityRecord r = activities.get(activityNdx);
3332 --i;
3333 if (DEBUG_CLEANUP) Slog.v(
3334 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3335 if (r.app == app) {
3336 boolean remove;
3337 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3338 // Don't currently have state for the activity, or
3339 // it is finishing -- always remove it.
3340 remove = true;
3341 } else if (r.launchCount > 2 &&
3342 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3343 // We have launched this activity too many times since it was
3344 // able to run, so give up and remove it.
3345 remove = true;
3346 } else {
3347 // The process may be gone, but the activity lives on!
3348 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003349 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003350 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003351 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003352 RuntimeException here = new RuntimeException("here");
3353 here.fillInStackTrace();
3354 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3355 + ": haveState=" + r.haveState
3356 + " stateNotNeeded=" + r.stateNotNeeded
3357 + " finishing=" + r.finishing
3358 + " state=" + r.state, here);
3359 }
3360 if (!r.finishing) {
3361 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3362 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3363 r.userId, System.identityHashCode(r),
3364 r.task.taskId, r.shortComponentName,
3365 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003366 if (r.state == ActivityState.RESUMED) {
3367 mService.updateUsageStats(r, false);
3368 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003369 }
3370 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003371
Craig Mautner0247fc82013-02-28 14:32:06 -08003372 } else {
3373 // We have the current state for this activity, so
3374 // it can be restarted later when needed.
3375 if (localLOGV) Slog.v(
3376 TAG, "Keeping entry, setting app to null");
3377 if (r.visible) {
3378 hasVisibleActivities = true;
3379 }
3380 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3381 + r);
3382 r.app = null;
3383 r.nowVisible = false;
3384 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003385 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003386 "App died, clearing saved state of " + r);
3387 r.icicle = null;
3388 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003389 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003390
Craig Mautnerd2328952013-03-05 12:46:26 -08003391 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003392 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003393 }
3394 }
3395
3396 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003397 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003398
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003399 final void updateTransitLocked(int transit, Bundle options) {
3400 if (options != null) {
3401 ActivityRecord r = topRunningActivityLocked(null);
3402 if (r != null && r.state != ActivityState.RESUMED) {
3403 r.updateOptionsLocked(options);
3404 } else {
3405 ActivityOptions.abort(options);
3406 }
3407 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003408 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003409 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003410
Craig Mautner21d24a22014-04-23 11:45:37 -07003411 void updateTaskMovement(TaskRecord task, boolean toFront) {
3412 if (task.isPersistable) {
3413 task.mLastTimeMoved = System.currentTimeMillis();
3414 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3415 // recently will be most negative, tasks sent to the bottom before that will be less
3416 // negative. Similarly for recent tasks moved to the top which will be most positive.
3417 if (!toFront) {
3418 task.mLastTimeMoved *= -1;
3419 }
3420 }
3421 }
3422
Craig Mautner84984fa2014-06-19 11:19:20 -07003423 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003424 final int top = mTaskHistory.size() - 1;
3425 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3426 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003427 if (task.taskType == homeStackTaskType) {
3428 if (DEBUG_TASKS || DEBUG_STACK)
3429 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003430 mTaskHistory.remove(taskNdx);
3431 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003432 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003433 mWindowManager.moveTaskToTop(task.taskId);
3434 return;
3435 }
3436 }
3437 }
3438
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003439 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003440 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003441
Craig Mautner11bf9a52013-02-19 14:08:51 -08003442 final int numTasks = mTaskHistory.size();
3443 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003444 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003445 // nothing to do!
3446 if (reason != null &&
3447 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3448 ActivityOptions.abort(options);
3449 } else {
3450 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3451 }
3452 return;
3453 }
3454
Craig Mautnere0a38842013-12-16 16:14:02 -08003455 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003456
Craig Mautner11bf9a52013-02-19 14:08:51 -08003457 // Shift all activities with this task up to the top
3458 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003459 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003460
3461 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003462 if (reason != null &&
3463 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003464 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003465 ActivityRecord r = topRunningActivityLocked(null);
3466 if (r != null) {
3467 mNoAnimActivities.add(r);
3468 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003469 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003470 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003471 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003472 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003473
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003474 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003475
Craig Mautner05d29032013-05-03 13:40:13 -07003476 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003477 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003478
3479 if (VALIDATE_TOKENS) {
3480 validateAppTokensLocked();
3481 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003482 }
3483
3484 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003485 * Worker method for rearranging history stack. Implements the function of moving all
3486 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003487 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003488 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003489 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3490 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003491 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003492 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003493 * @return Returns true if the move completed, false if not.
3494 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003495 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003496 final TaskRecord tr = taskForIdLocked(taskId);
3497 if (tr == null) {
3498 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3499 return false;
3500 }
3501
3502 Slog.i(TAG, "moveTaskToBack: " + tr);
3503
3504 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003506 // If we have a watcher, preflight the move before committing to it. First check
3507 // for *other* available tasks, but if none are available, then try again allowing the
3508 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003509 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003510 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003511 if (next == null) {
3512 next = topRunningActivityLocked(null, 0);
3513 }
3514 if (next != null) {
3515 // ask watcher if this is allowed
3516 boolean moveOK = true;
3517 try {
3518 moveOK = mService.mController.activityResuming(next.packageName);
3519 } catch (RemoteException e) {
3520 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003521 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 }
3523 if (!moveOK) {
3524 return false;
3525 }
3526 }
3527 }
3528
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003529 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003530 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531
Craig Mautner11bf9a52013-02-19 14:08:51 -08003532 mTaskHistory.remove(tr);
3533 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003534 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003535
Craig Mautnerc8143c62013-09-03 12:15:57 -07003536 // There is an assumption that moving a task to the back moves it behind the home activity.
3537 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003538 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003539 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3540 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003541 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003542 break;
3543 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003544 if (taskNdx == 1) {
3545 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003546 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003547 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003548 }
3549
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003551 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3552 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003553 ActivityRecord r = topRunningActivityLocked(null);
3554 if (r != null) {
3555 mNoAnimActivities.add(r);
3556 }
3557 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003558 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003560 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003561
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003562 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003563 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003564 }
3565
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003566 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003567 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
3568 final int taskToReturnTo = tr.getTaskToReturnTo();
3569 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3570 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003571 }
3572
Craig Mautner05d29032013-05-03 13:40:13 -07003573 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 return true;
3575 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003576
Craig Mautner8849a5e2013-04-02 16:41:03 -07003577 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003579 final Uri data = r.intent.getData();
3580 final String strData = data != null ? data.toSafeString() : null;
3581
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003582 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003583 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003584 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003585 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003586 }
3587
3588 /**
3589 * Make sure the given activity matches the current configuration. Returns
3590 * false if the activity had to be destroyed. Returns true if the
3591 * configuration is the same, or the activity will remain running as-is
3592 * for whatever reason. Ensures the HistoryRecord is updated with the
3593 * correct configuration and all other bookkeeping is handled.
3594 */
3595 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3596 int globalChanges) {
3597 if (mConfigWillChange) {
3598 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3599 "Skipping config check (will change): " + r);
3600 return true;
3601 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003602
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003603 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3604 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003605
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003606 // Short circuit: if the two configurations are the exact same
3607 // object (the common case), then there is nothing to do.
3608 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003609 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003610 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3611 "Configuration unchanged in " + r);
3612 return true;
3613 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003614
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 // We don't worry about activities that are finishing.
3616 if (r.finishing) {
3617 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3618 "Configuration doesn't matter in finishing " + r);
3619 r.stopFreezingScreenLocked(false);
3620 return true;
3621 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003622
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 // Okay we now are going to make this activity have the new config.
3624 // But then we need to figure out how it needs to deal with that.
3625 Configuration oldConfig = r.configuration;
3626 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003627
3628 // Determine what has changed. May be nothing, if this is a config
3629 // that has come back from the app after going idle. In that case
3630 // we just want to leave the official config object now in the
3631 // activity and do nothing else.
3632 final int changes = oldConfig.diff(newConfig);
3633 if (changes == 0 && !r.forceNewConfig) {
3634 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3635 "Configuration no differences in " + r);
3636 return true;
3637 }
3638
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003639 // If the activity isn't currently running, just leave the new
3640 // configuration and it will pick that up next time it starts.
3641 if (r.app == null || r.app.thread == null) {
3642 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3643 "Configuration doesn't matter not running " + r);
3644 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003645 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003646 return true;
3647 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003648
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003649 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003650 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3651 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3652 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003653 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003654 + ", newConfig=" + newConfig);
3655 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003656 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003657 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3658 r.configChangeFlags |= changes;
3659 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003660 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003661 if (r.app == null || r.app.thread == null) {
3662 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003663 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003664 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003665 } else if (r.state == ActivityState.PAUSING) {
3666 // A little annoying: we are waiting for this activity to
3667 // finish pausing. Let's not do anything now, but just
3668 // flag that it needs to be restarted when done pausing.
3669 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003670 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003671 r.configDestroy = true;
3672 return true;
3673 } else if (r.state == ActivityState.RESUMED) {
3674 // Try to optimize this case: the configuration is changing
3675 // and we need to restart the top, resumed activity.
3676 // Instead of doing the normal handshaking, just say
3677 // "restart!".
3678 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003679 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003680 relaunchActivityLocked(r, r.configChangeFlags, true);
3681 r.configChangeFlags = 0;
3682 } else {
3683 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003684 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003685 relaunchActivityLocked(r, r.configChangeFlags, false);
3686 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003687 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003688
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003689 // All done... tell the caller we weren't able to keep this
3690 // activity around.
3691 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003693
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003694 // Default case: the activity can handle this new configuration, so
3695 // hand it over. Note that we don't need to give it the new
3696 // configuration, since we always send configuration changes to all
3697 // process when they happen so it can just use whatever configuration
3698 // it last got.
3699 if (r.app != null && r.app.thread != null) {
3700 try {
3701 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003702 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703 } catch (RemoteException e) {
3704 // If process died, whatever.
3705 }
3706 }
3707 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003708
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003709 return true;
3710 }
3711
Craig Mautnerc8143c62013-09-03 12:15:57 -07003712 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 int changes, boolean andResume) {
3714 List<ResultInfo> results = null;
3715 List<Intent> newIntents = null;
3716 if (andResume) {
3717 results = r.results;
3718 newIntents = r.newIntents;
3719 }
3720 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3721 + " with results=" + results + " newIntents=" + newIntents
3722 + " andResume=" + andResume);
3723 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003724 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003726
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003727 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003728
Craig Mautner34b73df2014-01-12 21:11:08 -08003729 mStackSupervisor.removeChildActivityContainers(r);
3730
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003732 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3733 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3734 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003735 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003736 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003737 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003738 // Note: don't need to call pauseIfSleepingLocked() here, because
3739 // the caller will only pass in 'andResume' if this activity is
3740 // currently resumed, which implies we aren't sleeping.
3741 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003742 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003743 }
3744
3745 if (andResume) {
3746 r.results = null;
3747 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003748 r.state = ActivityState.RESUMED;
3749 } else {
3750 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3751 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 }
3753
3754 return true;
3755 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003756
3757 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003758 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3759 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3760 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3761 final ActivityRecord r = activities.get(activityNdx);
3762 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003763 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003764 }
3765 if (r.fullscreen && !r.finishing) {
3766 return false;
3767 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003768 }
3769 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003770 final ActivityRecord r = ActivityRecord.forToken(token);
3771 if (r == null) {
3772 return false;
3773 }
3774 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3775 + " would have returned true for r=" + r);
3776 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003777 }
3778
3779 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003780 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3781 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3782 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3783 final ActivityRecord r = activities.get(activityNdx);
3784 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003785 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003786 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003787 }
3788 }
3789 }
3790
3791 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3792 boolean didSomething = false;
3793 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003794 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003795 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3796 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3797 int numActivities = activities.size();
3798 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3799 ActivityRecord r = activities.get(activityNdx);
3800 final boolean samePackage = r.packageName.equals(name)
3801 || (name == null && r.userId == userId);
3802 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3803 && (samePackage || r.task == lastTask)
3804 && (r.app == null || evenPersistent || !r.app.persistent)) {
3805 if (!doit) {
3806 if (r.finishing) {
3807 // If this activity is just finishing, then it is not
3808 // interesting as far as something to stop.
3809 continue;
3810 }
3811 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003812 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003813 if (r.isHomeActivity()) {
3814 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3815 Slog.i(TAG, "Skip force-stop again " + r);
3816 continue;
3817 } else {
3818 homeActivity = r.realActivity;
3819 }
3820 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003821 didSomething = true;
3822 Slog.i(TAG, " Force finishing activity " + r);
3823 if (samePackage) {
3824 if (r.app != null) {
3825 r.app.removed = true;
3826 }
3827 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003828 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003829 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003830 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3831 true)) {
3832 // r has been deleted from mActivities, accommodate.
3833 --numActivities;
3834 --activityNdx;
3835 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003836 }
3837 }
3838 }
3839 return didSomething;
3840 }
3841
Dianne Hackborn09233282014-04-30 11:33:59 -07003842 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003843 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003844 final TaskRecord task = mTaskHistory.get(taskNdx);
3845 ActivityRecord r = null;
3846 ActivityRecord top = null;
3847 int numActivities = 0;
3848 int numRunning = 0;
3849 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003850 if (activities.isEmpty()) {
3851 continue;
3852 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07003853 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07003854 continue;
3855 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003856 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3857 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003858
Craig Mautneraab647e2013-02-28 16:31:36 -08003859 // Initialize state for next task if needed.
3860 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3861 top = r;
3862 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003863 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003864
3865 // Add 'r' into the current task.
3866 numActivities++;
3867 if (r.app != null && r.app.thread != null) {
3868 numRunning++;
3869 }
3870
3871 if (localLOGV) Slog.v(
3872 TAG, r.intent.getComponent().flattenToShortString()
3873 + ": task=" + r.task);
3874 }
3875
3876 RunningTaskInfo ci = new RunningTaskInfo();
3877 ci.id = task.taskId;
3878 ci.baseActivity = r.intent.getComponent();
3879 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003880 ci.lastActiveTime = task.lastActiveTime;
3881
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003882 if (top.task != null) {
3883 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003884 }
3885 ci.numActivities = numActivities;
3886 ci.numRunning = numRunning;
3887 //System.out.println(
3888 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003889 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003890 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003891 }
3892
3893 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003894 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003895 if (DEBUG_SWITCH) Slog.d(
3896 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003897 if (top >= 0) {
3898 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3899 int activityTop = activities.size() - 1;
3900 if (activityTop > 0) {
3901 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3902 "unhandled-back", true);
3903 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003904 }
3905 }
3906
Craig Mautner6b74cb52013-09-27 17:02:21 -07003907 /**
3908 * Reset local parameters because an app's activity died.
3909 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003910 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003911 */
3912 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003913 if (mPausingActivity != null && mPausingActivity.app == app) {
3914 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3915 "App died while pausing: " + mPausingActivity);
3916 mPausingActivity = null;
3917 }
3918 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3919 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003920 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003921 }
3922
Craig Mautner19091252013-10-05 00:03:53 -07003923 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003924 }
3925
Craig Mautnercae015f2013-02-08 14:31:27 -08003926 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003927 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3928 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3929 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3930 final ActivityRecord r = activities.get(activityNdx);
3931 if (r.app == app) {
3932 Slog.w(TAG, " Force finishing activity "
3933 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07003934 // Force the destroy to skip right to removal.
3935 r.app = null;
3936 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003937 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003938 }
3939 }
3940 }
3941
Dianne Hackborn390517b2013-05-30 15:03:32 -07003942 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003943 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003944 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003945 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3946 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003947 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3948 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003949 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003950 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003951 if (printed) {
3952 header = null;
3953 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003954 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003955 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003956 }
3957
3958 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3959 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3960
3961 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003962 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3963 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003964 }
3965 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003966 final int top = mTaskHistory.size() - 1;
3967 if (top >= 0) {
3968 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3969 int listTop = list.size() - 1;
3970 if (listTop >= 0) {
3971 activities.add(list.get(listTop));
3972 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003973 }
3974 } else {
3975 ItemMatcher matcher = new ItemMatcher();
3976 matcher.build(name);
3977
Craig Mautneraab647e2013-02-28 16:31:36 -08003978 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3979 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3980 if (matcher.match(r1, r1.intent.getComponent())) {
3981 activities.add(r1);
3982 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003983 }
3984 }
3985 }
3986
3987 return activities;
3988 }
3989
3990 ActivityRecord restartPackage(String packageName) {
3991 ActivityRecord starting = topRunningActivityLocked(null);
3992
3993 // All activities that came from the package must be
3994 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003995 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3996 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3997 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3998 final ActivityRecord a = activities.get(activityNdx);
3999 if (a.info.packageName.equals(packageName)) {
4000 a.forceNewConfig = true;
4001 if (starting != null && a == starting && a.visible) {
4002 a.startFreezingScreenLocked(starting.app,
4003 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4004 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004005 }
4006 }
4007 }
4008
4009 return starting;
4010 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004011
Craig Mautner41db4a72014-05-07 17:20:56 -07004012 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004013 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08004014 mWindowManager.removeTask(task.taskId);
4015 final ActivityRecord r = mResumedActivity;
4016 if (r != null && r.task == task) {
4017 mResumedActivity = null;
4018 }
4019
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004020 final int taskNdx = mTaskHistory.indexOf(task);
4021 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004022 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4023 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4024 if (!nextTask.isOverHomeStack()) {
4025 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4026 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004027 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004028 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004029 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004030
4031 if (task.mActivities.isEmpty()) {
4032 final boolean isVoiceSession = task.voiceSession != null;
4033 if (isVoiceSession) {
4034 try {
4035 task.voiceSession.taskFinished(task.intent, task.taskId);
4036 } catch (RemoteException e) {
4037 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004038 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004039 if (task.autoRemoveFromRecents() || isVoiceSession) {
4040 // Task creator asked to remove this when done, or this task was a voice
4041 // interaction, so it should not remain on the recent tasks list.
4042 mService.mRecentTasks.remove(task);
Craig Mautnera228ae92014-07-09 05:44:55 -07004043 task.closeRecentsChain();
Craig Mautner41db4a72014-05-07 17:20:56 -07004044 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004045 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004046
4047 if (mTaskHistory.isEmpty()) {
4048 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
4049 if (isOnHomeDisplay()) {
4050 mStackSupervisor.moveHomeStack(!isHomeStack());
4051 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004052 if (mStacks != null) {
4053 mStacks.remove(this);
4054 mStacks.add(0, this);
4055 }
Craig Mautnerd163e752014-06-13 17:18:47 -07004056 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004057 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004058 }
4059
Dianne Hackborn91097de2014-04-04 18:02:06 -07004060 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4061 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4062 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004063 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4064 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004065 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004066 return task;
4067 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004068
4069 ArrayList<TaskRecord> getAllTasks() {
4070 return new ArrayList<TaskRecord>(mTaskHistory);
4071 }
4072
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004073 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004074 task.stack = this;
4075 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004076 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004077 } else {
4078 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004079 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004080 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004081 if (!moving && task.voiceSession != null) {
4082 try {
4083 task.voiceSession.taskStarted(task.intent, task.taskId);
4084 } catch (RemoteException e) {
4085 }
4086 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004087 }
4088
4089 public int getStackId() {
4090 return mStackId;
4091 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004092
4093 @Override
4094 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004095 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4096 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004097 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004098}