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