blob: 03ce5300e863d9c931f645491761dcd398cf0e0b [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
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
34import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070035import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070037import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070039import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
40
Dianne Hackborn91097de2014-04-04 18:02:06 -070041import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070043import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080044import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080045import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080046import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080047import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049
50import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070051import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070052import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080054import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080056import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.content.res.Resources;
63import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080064import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070066import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080067import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Handler;
69import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090070import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070072import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.RemoteException;
74import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080075import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070076import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070077import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070080import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081
Craig Mautnercae015f2013-02-08 14:31:27 -080082import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080083import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import java.lang.ref.WeakReference;
85import java.util.ArrayList;
86import java.util.Iterator;
87import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080088import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089
90/**
91 * State and management of a single stack of activities.
92 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070093final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080094
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070095 // Ticks during which we check progress while waiting for an app to launch.
96 static final int LAUNCH_TICK = 500;
97
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070098 // How long we wait until giving up on the last activity to pause. This
99 // is short because it directly impacts the responsiveness of starting the
100 // next activity.
101 static final int PAUSE_TIMEOUT = 500;
102
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700103 // How long we wait for the activity to tell us it has stopped before
104 // giving up. This is a good amount of time because we really need this
105 // from the application in order to get its saved state.
106 static final int STOP_TIMEOUT = 10*1000;
107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long we wait until giving up on an activity telling us it has
109 // finished destroying itself.
110 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800113 // disabled.
114 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700116 // How long between activity launches that we consider safe to not warn
117 // the user about an unexpected activity being launched on top.
118 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120 // Set to false to disable the preview that is shown while a new activity
121 // is being started.
122 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800123
Craig Mautner5eda9b32013-07-02 11:58:16 -0700124 // How long to wait for all background Activities to redraw following a call to
125 // convertToTranslucent().
126 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
127
Craig Mautnera61bc652013-10-28 15:43:18 -0700128 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700129
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700130 enum ActivityState {
131 INITIALIZING,
132 RESUMED,
133 PAUSING,
134 PAUSED,
135 STOPPING,
136 STOPPED,
137 FINISHING,
138 DESTROYING,
139 DESTROYED
140 }
141
142 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700143 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800144
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700145 /**
146 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800147 * running) activities. It contains #TaskRecord objects.
148 */
149 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
150
151 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152 * Used for validating app tokens with window manager.
153 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800154 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700156 /**
157 * List of running activities, sorted by recent usage.
158 * The first entry in the list is the least recently used.
159 * It contains HistoryRecord objects.
160 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700161 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162
163 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 * Animations that for the current transition have requested not to
165 * be considered for the transition animation.
166 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700167 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168
169 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 * When we are in the process of pausing an activity, before starting the
171 * next one, this variable holds the activity that is currently being paused.
172 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800173 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174
175 /**
176 * This is the last activity that we put into the paused state. This is
177 * used to determine if we need to do an activity transition while sleeping,
178 * when we normally hold the top activity paused.
179 */
180 ActivityRecord mLastPausedActivity = null;
181
182 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700183 * Activities that specify No History must be removed once the user navigates away from them.
184 * If the device goes to sleep with such an activity in the paused state then we save it here
185 * and finish it later if another activity replaces it on wakeup.
186 */
187 ActivityRecord mLastNoHistoryActivity = null;
188
189 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700190 * Current activity that is resumed, or null if there is none.
191 */
192 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700195 * This is the last activity that has been started. It is only used to
196 * identify when multiple activities are started at once so that the user
197 * can be warned they may not be in the activity they think they are.
198 */
199 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800200
Craig Mautner5eda9b32013-07-02 11:58:16 -0700201 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
202 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
203 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
204 // Activity in mTranslucentActivityWaiting is notified via
205 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
206 // background activity being drawn then the same call will be made with a true value.
207 ActivityRecord mTranslucentActivityWaiting = null;
208 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
209 new ArrayList<ActivityRecord>();
Craig Mautner233ceee2014-05-09 17:05:11 -0700210 // Options passed from the caller of the convertToTranslucent to the activity that will
211 // appear below it.
212 ActivityOptions mReturningActivityOptions = null;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700213
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;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700251
252 static class ScheduleDestroyArgs {
253 final ProcessRecord mOwner;
254 final boolean mOomAdj;
255 final String mReason;
256 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
257 mOwner = owner;
258 mOomAdj = oomAdj;
259 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) {
319 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
320 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700321 } break;
322 case TRANSLUCENT_TIMEOUT_MSG: {
323 synchronized (mService) {
324 notifyActivityDrawnLocked(null);
325 }
326 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700327 }
328 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800329 }
330
Craig Mautner34b73df2014-01-12 21:11:08 -0800331 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800332 int count = 0;
333 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
334 count += mTaskHistory.get(taskNdx).mActivities.size();
335 }
336 return count;
337 }
338
Craig Mautner4a1cb222013-12-04 16:14:06 -0800339 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
340 mActivityContainer = activityContainer;
341 mStackSupervisor = activityContainer.getOuter();
342 mService = mStackSupervisor.mService;
343 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
344 mWindowManager = mService.mWindowManager;
345 mStackId = activityContainer.mStackId;
346 mCurrentUser = mService.mCurrentUserId;
Winson Chung376543b2014-05-21 17:43:28 -0700347 // Get the activity screenshot thumbnail dimensions
348 Resources res = mService.mContext.getResources();
Winson Chung2eb4f9c2014-05-22 11:22:41 -0700349 mThumbnailWidth = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
350 mThumbnailHeight = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700351 }
Craig Mautner5962b122012-10-05 14:45:52 -0700352
Amith Yamasani734983f2014-03-04 16:48:05 -0800353 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100354 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800355 */
Kenny Guy2a764942014-04-02 13:29:20 +0100356 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100357 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100358 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
359 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800360 }
361 return false;
362 }
363
364 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100365 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700366 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
367 }
368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700369 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800370 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700371 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
372 if (r != null) {
373 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800374 }
375 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 return null;
377 }
378
379 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800380 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
381 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800382 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800383 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
384 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800385 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800386 return r;
387 }
388 }
389 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700390 return null;
391 }
392
393 /**
394 * This is a simplified version of topRunningActivityLocked that provides a number of
395 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800396 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 * @param token If non-null, any history records matching this token will be skipped.
398 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800399 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 * @return Returns the HistoryRecord of the next activity on the stack.
401 */
402 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
404 TaskRecord task = mTaskHistory.get(taskNdx);
405 if (task.taskId == taskId) {
406 continue;
407 }
408 ArrayList<ActivityRecord> activities = task.mActivities;
409 for (int i = activities.size() - 1; i >= 0; --i) {
410 final ActivityRecord r = activities.get(i);
411 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800412 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800413 return r;
414 }
415 }
416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700417 return null;
418 }
419
Craig Mautner8849a5e2013-04-02 16:41:03 -0700420 final ActivityRecord topActivity() {
421 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700422 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700423 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
424 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700425 final int topActivityNdx = activities.size() - 1;
426 if (topActivityNdx >= 0) {
427 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700428 }
429 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700430 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700431 }
432
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700433 final TaskRecord topTask() {
434 final int size = mTaskHistory.size();
435 if (size > 0) {
436 return mTaskHistory.get(size - 1);
437 }
438 return null;
439 }
440
Craig Mautnerd2328952013-03-05 12:46:26 -0800441 TaskRecord taskForIdLocked(int id) {
442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
443 final TaskRecord task = mTaskHistory.get(taskNdx);
444 if (task.taskId == id) {
445 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800446 }
447 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700448 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700449 }
450
Craig Mautnerd2328952013-03-05 12:46:26 -0800451 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800452 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800453 if (r != null) {
454 final TaskRecord task = r.task;
455 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
456 if (task.stack != this) Slog.w(TAG,
457 "Illegal state! task does not point to stack it is in.");
458 return r;
459 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800460 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800461 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800462 }
463
Craig Mautner2420ead2013-04-01 17:13:20 -0700464 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700465 final boolean hadit = mLRUActivities.remove(r);
466 mLRUActivities.add(r);
467 return hadit;
468 }
469
Craig Mautnerde4ef022013-04-07 19:01:33 -0700470 final boolean isHomeStack() {
471 return mStackId == HOME_STACK_ID;
472 }
473
Craig Mautnere0a38842013-12-16 16:14:02 -0800474 final boolean isOnHomeDisplay() {
475 return isAttached() &&
476 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
477 }
478
479 final void moveToFront() {
480 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800481 if (isOnHomeDisplay()) {
482 mStackSupervisor.moveHomeStack(isHomeStack());
483 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800484 mStacks.remove(this);
485 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800487 }
488
489 final boolean isAttached() {
490 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800491 }
492
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700493 /**
494 * Returns the top activity in any existing task matching the given
495 * Intent. Returns null if no such task is found.
496 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700497 ActivityRecord findTaskLocked(ActivityRecord target) {
498 Intent intent = target.intent;
499 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700500 ComponentName cls = intent.getComponent();
501 if (info.targetActivity != null) {
502 cls = new ComponentName(info.packageName, info.targetActivity);
503 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700504 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700505 boolean isDocument = intent != null & intent.isDocument();
506 // If documentData is non-null then it must match the existing task data.
507 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800508
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700509 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800510 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
511 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700512 if (task.voiceSession != null) {
513 // We never match voice sessions; those always run independently.
514 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
515 continue;
516 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700517 if (task.userId != userId) {
518 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700519 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700520 continue;
521 }
Craig Mautner000f0022013-02-26 15:04:29 -0800522 final ActivityRecord r = task.getTopActivity();
523 if (r == null || r.finishing || r.userId != userId ||
524 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700525 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800526 continue;
527 }
528
Craig Mautnerd00f4742014-03-12 14:17:26 -0700529 final Intent taskIntent = task.intent;
530 final Intent affinityIntent = task.affinityIntent;
531 final boolean taskIsDocument;
532 final Uri taskDocumentData;
533 if (taskIntent != null && taskIntent.isDocument()) {
534 taskIsDocument = true;
535 taskDocumentData = taskIntent.getData();
536 } else if (affinityIntent != null && affinityIntent.isDocument()) {
537 taskIsDocument = true;
538 taskDocumentData = affinityIntent.getData();
539 } else {
540 taskIsDocument = false;
541 taskDocumentData = null;
542 }
543
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700544 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700545 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700546 + "/aff=" + r.task.affinity + " to new cls="
547 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700548 if (!isDocument && !taskIsDocument && task.affinity != null) {
549 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700550 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700551 return r;
552 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700553 } else if (taskIntent != null && taskIntent.getComponent() != null &&
554 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700555 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700556 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800557 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700558 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
559 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800560 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700561 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
562 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700563 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700564 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800565 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700566 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
567 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800568 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700569 } else if (DEBUG_TASKS) {
570 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700571 }
572 }
573
574 return null;
575 }
576
577 /**
578 * Returns the first activity (starting from the top of the stack) that
579 * is the same as the given activity. Returns null if no such activity
580 * is found.
581 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700582 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700583 ComponentName cls = intent.getComponent();
584 if (info.targetActivity != null) {
585 cls = new ComponentName(info.packageName, info.targetActivity);
586 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700587 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700588
Craig Mautner000f0022013-02-26 15:04:29 -0800589 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700590 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100591 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700592 return null;
593 }
594 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800595 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
596 ActivityRecord r = activities.get(activityNdx);
597 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700598 //Slog.i(TAG, "Found matching class!");
599 //dump();
600 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
601 return r;
602 }
603 }
604 }
605
606 return null;
607 }
608
Amith Yamasani742a6712011-05-04 14:49:28 -0700609 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700610 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700611 */
Craig Mautner93529a42013-10-04 15:03:13 -0700612 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800613 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700614 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800615 }
616 mCurrentUser = userId;
617
618 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800619 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800620 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700621 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100622 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700623 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
624 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800625 mTaskHistory.remove(i);
626 mTaskHistory.add(task);
627 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800628 // Use same value for i.
629 } else {
630 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800631 }
632 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700633 if (VALIDATE_TOKENS) {
634 validateAppTokensLocked();
635 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700636 }
637
Craig Mautner2420ead2013-04-01 17:13:20 -0700638 void minimalResumeActivityLocked(ActivityRecord r) {
639 r.state = ActivityState.RESUMED;
640 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
641 + " (starting new instance)");
642 r.stopped = false;
643 mResumedActivity = r;
644 r.task.touchActiveTime();
645 mService.addRecentTaskLocked(r.task);
646 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700647 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700648 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700649 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700650 }
651
Dianne Hackborncee04b52013-07-03 17:01:28 -0700652 private void startLaunchTraces() {
653 if (mFullyDrawnStartTime != 0) {
654 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
655 }
656 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
657 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
658 }
659
660 private void stopFullyDrawnTraceIfNeeded() {
661 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
662 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
663 mFullyDrawnStartTime = 0;
664 }
665 }
666
Craig Mautnere79d42682013-04-01 19:01:53 -0700667 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700668 if (r.displayStartTime == 0) {
669 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
670 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700671 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700672 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700674 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700675 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700676 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700678 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800679
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700680 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700681 // Make sure that there is no activity waiting for this to launch.
682 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
683 r.displayStartTime = r.fullyDrawnStartTime = 0;
684 } else {
685 mStackSupervisor.removeTimeoutsForActivityLocked(r);
686 mStackSupervisor.scheduleIdleTimeoutLocked(r);
687 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700688 }
689
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800690 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800691 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800692 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
693 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
694 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
695 activities.get(activityNdx).setSleeping(false);
696 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800697 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800698 }
699
Craig Mautner0eea92c2013-05-16 13:35:39 -0700700 /**
701 * @return true if something must be done before going to sleep.
702 */
703 boolean checkReadyForSleepLocked() {
704 if (mResumedActivity != null) {
705 // Still have something resumed; can't sleep until it is paused.
706 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
707 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
708 startPausingLocked(false, true);
709 return true;
710 }
711 if (mPausingActivity != null) {
712 // Still waiting for something to pause; can't sleep yet.
713 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
714 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800715 }
716
Craig Mautner0eea92c2013-05-16 13:35:39 -0700717 return false;
718 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800719
Craig Mautner0eea92c2013-05-16 13:35:39 -0700720 void goToSleep() {
721 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800722
Craig Mautner0eea92c2013-05-16 13:35:39 -0700723 // Make sure any stopped but visible activities are now sleeping.
724 // This ensures that the activity's onStop() is called.
725 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
726 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
727 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
728 final ActivityRecord r = activities.get(activityNdx);
729 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
730 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800731 }
732 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800733 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734 }
Craig Mautner59c00972012-07-30 12:10:24 -0700735
Winson Chung376543b2014-05-21 17:43:28 -0700736 /**
737 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
738 * again when requested.
739 */
740 private void invalidateLastScreenshot() {
741 mLastScreenshotActivity = null;
742 if (mLastScreenshotBitmap != null) {
743 mLastScreenshotBitmap.recycle();
744 }
745 mLastScreenshotBitmap = null;
746 }
747
Dianne Hackbornd2835932010-12-13 16:28:46 -0800748 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700749 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800750 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700751 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800752 return null;
753 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800754
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700755 TaskRecord tr = who.task;
Winson Chung376543b2014-05-21 17:43:28 -0700756 if (mService.getMostRecentTask() != tr || isHomeStack()) {
757 // This is an optimization -- since we never show Home or Recents within Recents itself,
758 // we can just go ahead and skip taking the screenshot if this is the home stack. In
759 // the case where the most recent task is not the task that was supplied, then the stack
760 // has changed, so invalidate the last screenshot().
761 invalidateLastScreenshot();
762 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700763 return null;
764 }
765
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800766 int w = mThumbnailWidth;
767 int h = mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800768 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800769 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700770 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800771 || mLastScreenshotBitmap.getWidth() != w
772 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700773 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800774 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700776 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800777 }
778 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700779 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700780 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800781 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800782 }
Winson Chung376543b2014-05-21 17:43:28 -0700783 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800784 return null;
785 }
786
Craig Mautnercf910b02013-04-23 11:23:27 -0700787 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800788 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800789 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700790 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800791 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700792 ActivityRecord prev = mResumedActivity;
793 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700794 Slog.e(TAG, "Trying to pause when nothing is resumed",
795 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700796 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700797 return;
798 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800799
800 if (mActivityContainer.mParentActivity == null) {
801 // Top level stack, not a child. Look for child stacks.
802 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
803 }
804
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700805 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
806 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800808 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700809 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700810 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
811 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700812 prev.state = ActivityState.PAUSING;
813 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700814 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700815 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Craig Mautnerf6ead5d62014-04-24 10:19:20 -0700816 if (next == null || next.noDisplay || next.task != prev.task) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700817 prev.updateThumbnail(screenshotActivities(prev), null);
818 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700819 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700820
821 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800822
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 if (prev.app != null && prev.app.thread != null) {
824 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
825 try {
826 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700827 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700829 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800830 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
831 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 } catch (Exception e) {
833 // Ignore exception, if process died other code will cleanup.
834 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800835 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700837 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 }
839 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800840 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700841 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700842 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700843 }
844
845 // If we are not going to sleep, we want to ensure the device is
846 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700847 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700848 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 }
850
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800851 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700852 // Have the window manager pause its key dispatching until the new
853 // activity has started. If we're pausing the activity just because
854 // the screen is being turned off and the UI is sleeping, don't interrupt
855 // key dispatch; the same activity will pick it up again on wakeup.
856 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800857 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 } else {
859 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
860 }
861
862 // Schedule a pause timeout in case the app doesn't respond.
863 // We don't give it much time because this directly impacts the
864 // responsiveness seen by the user.
865 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
866 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700867 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
869 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
870 } else {
871 // This activity failed to schedule the
872 // pause, so just treat it as being paused now.
873 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700874 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 }
876 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700877
Craig Mautnera0026042014-04-23 11:45:37 -0700878 final void activityPausedLocked(IBinder token, boolean timeout,
879 PersistableBundle persistentState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800881 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700882
Craig Mautnerd2328952013-03-05 12:46:26 -0800883 final ActivityRecord r = isInStackLocked(token);
884 if (r != null) {
885 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Craig Mautner21d24a22014-04-23 11:45:37 -0700886 if (persistentState != null) {
887 r.persistentState = persistentState;
888 mService.notifyTaskPersisterLocked(r.task, false);
889 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800890 if (mPausingActivity == r) {
891 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
892 + (timeout ? " (due to timeout)" : " (pause complete)"));
893 r.state = ActivityState.PAUSED;
894 completePauseLocked();
895 } else {
896 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
897 r.userId, System.identityHashCode(r), r.shortComponentName,
898 mPausingActivity != null
899 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 }
901 }
902 }
903
Craig Mautnera0026042014-04-23 11:45:37 -0700904 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
905 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700906 if (r.state != ActivityState.STOPPING) {
907 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
908 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
909 return;
910 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700911 if (persistentState != null) {
912 r.persistentState = persistentState;
913 mService.notifyTaskPersisterLocked(r.task, false);
914 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700915 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700916 if (icicle != null) {
917 // If icicle is null, this is happening due to a timeout, so we
918 // haven't really saved the state.
919 r.icicle = icicle;
920 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800921 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700922 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700923 }
924 if (!r.stopped) {
925 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
926 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
927 r.stopped = true;
928 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700929 if (r.finishing) {
930 r.clearOptionsLocked();
931 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700932 if (r.configDestroy) {
933 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700934 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700935 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700936 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800937 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700938 }
939 }
940 }
941
Craig Mautnerc8143c62013-09-03 12:15:57 -0700942 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800943 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800945
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800946 if (prev != null) {
947 if (prev.finishing) {
948 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700949 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800950 } else if (prev.app != null) {
951 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
952 if (prev.waitingVisible) {
953 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700954 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800955 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
956 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800957 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800958 if (prev.configDestroy) {
959 // The previous is being paused because the configuration
960 // is changing, which means it is actually stopping...
961 // To juggle the fact that we are also starting a new
962 // instance right now, we need to first completely stop
963 // the current instance before starting the new one.
964 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
965 destroyActivityLocked(prev, true, false, "pause-config");
966 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700967 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700968 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
969 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800970 // If we already have a few activities waiting to stop,
971 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700972 // them out. Or if r is the last of activity of the last task the stack
973 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800974 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700975 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800976 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700977 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 }
979 }
980 } else {
981 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
982 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800983 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800986
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700987 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700988 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700989 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800990 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700991 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700992 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700993 if (top == null || (prev != null && top != prev)) {
994 // If there are no more activities available to run,
995 // do resume anyway to start something. Also if the top
996 // activity on the stack is not the just paused activity,
997 // we need to go ahead and resume it to ensure we complete
998 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700999 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001000 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001001 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001002
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001003 if (prev != null) {
1004 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001005
Craig Mautner525f3d92013-05-07 14:01:50 -07001006 if (prev.app != null && prev.cpuTimeAtResume > 0
1007 && mService.mBatteryStatsService.isOnBattery()) {
1008 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -07001009 synchronized (mService.mProcessCpuThread) {
1010 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -07001011 - prev.cpuTimeAtResume;
1012 }
1013 if (diff > 0) {
1014 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1015 synchronized (bsi) {
1016 BatteryStatsImpl.Uid.Proc ps =
1017 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001018 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001019 if (ps != null) {
1020 ps.addForegroundTimeLocked(diff);
1021 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001022 }
1023 }
1024 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001025 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001027 }
1028
1029 /**
1030 * Once we know that we have asked an application to put an activity in
1031 * the resumed state (either by launching it or explicitly telling it),
1032 * this function updates the rest of our state to match that fact.
1033 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001034 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035 next.idle = false;
1036 next.results = null;
1037 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -07001038 if (next.nowVisible) {
1039 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1040 mStackSupervisor.dismissKeyguard();
1041 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042
1043 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001044 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001046 mStackSupervisor.reportResumedActivityLocked(next);
1047
1048 next.resumeKeyDispatchingLocked();
1049 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050
1051 // Mark the point when the activity is resuming
1052 // TODO: To be more accurate, the mark should be before the onCreate,
1053 // not after the onResume. But for subsequent starts, onResume is fine.
1054 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001055 synchronized (mService.mProcessCpuThread) {
1056 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057 }
1058 } else {
1059 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1060 }
Winson Chung376543b2014-05-21 17:43:28 -07001061
1062 // If we are resuming the activity that we had last screenshotted, then we know it will be
1063 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1064 if (next == mLastScreenshotActivity) {
1065 invalidateLastScreenshot();
1066 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067 }
1068
Craig Mautner719e6212014-05-29 16:49:41 +00001069 /**
1070 * Determine if home should be visible below the passed record.
1071 * @param record activity we are querying for.
1072 * @return true if home is visible below the passed activity, false otherwise.
1073 */
1074 boolean isActivityOverHome(ActivityRecord record) {
1075 // Start at record and go down, look for either home or a visible fullscreen activity.
1076 final TaskRecord recordTask = record.task;
1077 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
1078 TaskRecord task = mTaskHistory.get(taskNdx);
1079 final ArrayList<ActivityRecord> activities = task.mActivities;
1080 final int startNdx =
1081 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
1082 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
1083 final ActivityRecord r = activities.get(activityNdx);
1084 if (r.isHomeActivity()) {
1085 return true;
1086 }
1087 if (!r.finishing && r.fullscreen) {
1088 // Passed activity is over a fullscreen activity.
1089 return false;
1090 }
1091 }
1092 if (task.mOnTopOfHome) {
1093 // Got to the bottom of a task on top of home without finding a visible fullscreen
1094 // activity. Home is visible.
1095 return true;
1096 }
1097 }
1098 // Got to the bottom of this stack and still don't know. If this is over the home stack
1099 // then record is over home. May not work if we ever get more than two layers.
1100 return mStackSupervisor.isFrontStack(this);
1101 }
1102
Craig Mautnere3a00d72014-04-16 08:31:19 -07001103 private void setVisibile(ActivityRecord r, boolean visible) {
1104 r.visible = visible;
1105 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001106 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001107 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001108 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001109 container.setVisible(visible);
1110 }
1111 }
1112
Jose Lima7ba71252014-04-30 12:59:27 -07001113 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1114 // If so, this stack is hidden, otherwise it is visible.
1115 private boolean isStackVisible() {
1116 if (!isAttached()) {
1117 return false;
1118 }
1119
1120 if (mStackSupervisor.isFrontStack(this)) {
1121 return true;
1122 }
1123
Jose Lima729cb232014-05-05 15:59:40 -07001124 /**
1125 * Start at the task above this one and go up, looking for a visible
1126 * fullscreen activity, or a translucent activity that requested the
1127 * wallpaper to be shown behind it.
1128 */
Jose Lima7ba71252014-04-30 12:59:27 -07001129 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1130 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1131 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner719e6212014-05-29 16:49:41 +00001132 final ArrayList<ActivityRecord> activities = tasks.get(taskNdx).mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001133 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1134 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001135
1136 // Conditions for an activity to obscure the stack we're
1137 // examining:
1138 // 1. Not Finishing AND Visible AND:
1139 // 2. Either:
1140 // - Full Screen Activity OR
1141 // - On top of Home and our stack is NOT home
1142 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner719e6212014-05-29 16:49:41 +00001143 (!isHomeStack() && r.frontOfTask && tasks.get(taskNdx).mOnTopOfHome))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001144 return false;
1145 }
1146 }
1147 }
1148 }
1149
1150 return true;
1151 }
1152
Jose Lima729cb232014-05-05 15:59:40 -07001153 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1154 ActivityRecord r = topRunningActivityLocked(null);
1155 if (r != null) {
1156 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1157 }
1158 }
1159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 /**
1161 * Make sure that all activities that need to be visible (that is, they
1162 * currently can be seen by the user) actually are.
1163 */
Jose Lima729cb232014-05-05 15:59:40 -07001164 final void ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1165 String onlyThisProcess, int configChanges) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001166 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001167 TAG, "ensureActivitiesVisible behind " + top
1168 + " configChanges=0x" + Integer.toHexString(configChanges));
1169
Craig Mautner5eda9b32013-07-02 11:58:16 -07001170 if (mTranslucentActivityWaiting != top) {
1171 mUndrawnActivitiesBelowTopTranslucent.clear();
1172 if (mTranslucentActivityWaiting != null) {
1173 // Call the callback with a timeout indication.
1174 notifyActivityDrawnLocked(null);
1175 mTranslucentActivityWaiting = null;
1176 }
1177 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1178 }
1179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180 // If the top activity is not fullscreen, then we need to
1181 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001182 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001183 boolean behindFullscreen = !isStackVisible();
1184
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001185 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001186 final TaskRecord task = mTaskHistory.get(taskNdx);
1187 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001188 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1189 final ActivityRecord r = activities.get(activityNdx);
1190 if (r.finishing) {
1191 continue;
1192 }
1193 if (aboveTop && r != top) {
1194 continue;
1195 }
1196 aboveTop = false;
1197 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001199 TAG, "Make visible? " + r + " finishing=" + r.finishing
1200 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001201
Craig Mautnerd44711d2013-02-23 11:24:36 -08001202 final boolean doThisProcess = onlyThisProcess == null
1203 || onlyThisProcess.equals(r.processName);
1204
1205 // First: if this is not the current activity being started, make
1206 // sure it matches the current configuration.
1207 if (r != starting && doThisProcess) {
1208 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001209 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001210
1211 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001212 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001213 // This activity needs to be visible, but isn't even
1214 // running... get it started, but don't resume it
1215 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001216 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001217 if (r != starting) {
1218 r.startFreezingScreenLocked(r.app, configChanges);
1219 }
1220 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001221 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001222 TAG, "Starting and making visible: " + r);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001223 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001224 }
1225 if (r != starting) {
George Mount2c92c972014-03-20 09:38:23 -07001226 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001227 }
1228 }
1229
1230 } else if (r.visible) {
1231 // If this activity is already visible, then there is nothing
1232 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001233 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001235
1236 } else if (onlyThisProcess == null) {
1237 // This activity is not currently visible, but is running.
1238 // Tell it to become visible.
1239 r.visible = true;
1240 if (r.state != ActivityState.RESUMED && r != starting) {
1241 // If this activity is paused, tell it
1242 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001243 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001244 TAG, "Making visible and scheduling visibility: " + r);
1245 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001246 if (mTranslucentActivityWaiting != null) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001247 r.updateOptionsLocked(mReturningActivityOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001248 mUndrawnActivitiesBelowTopTranslucent.add(r);
1249 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001250 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001251 r.sleeping = false;
1252 r.app.pendingUiClean = true;
1253 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1254 r.stopFreezingScreenLocked(false);
1255 } catch (Exception e) {
1256 // Just skip on any failure; we'll make it
1257 // visible when it next restarts.
1258 Slog.w(TAG, "Exception thrown making visibile: "
1259 + r.intent.getComponent(), e);
1260 }
1261 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001262 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001263
Craig Mautnerd44711d2013-02-23 11:24:36 -08001264 // Aggregate current change flags.
1265 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001266
Craig Mautnerd44711d2013-02-23 11:24:36 -08001267 if (r.fullscreen) {
1268 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001269 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1270 behindFullscreen = true;
Craig Mautner719e6212014-05-29 16:49:41 +00001271 } else if (!isHomeStack() && r.frontOfTask && task.mOnTopOfHome) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001272 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001273 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001274 }
1275 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001276 if (DEBUG_VISBILITY) Slog.v(
1277 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1278 + " state=" + r.state
1279 + " behindFullscreen=" + behindFullscreen);
1280 // Now for any activities that aren't visible to the user, make
1281 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001282 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001283 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001284 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001285 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001286 switch (r.state) {
1287 case STOPPING:
1288 case STOPPED:
1289 if (r.app != null && r.app.thread != null) {
1290 if (DEBUG_VISBILITY) Slog.v(
1291 TAG, "Scheduling invisibility: " + r);
1292 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1293 }
1294 break;
1295
1296 case INITIALIZING:
1297 case RESUMED:
1298 case PAUSING:
1299 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001300 // This case created for transitioning activities from
1301 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001302 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1303 mStackSupervisor.mStoppingActivities.add(r);
1304 }
1305 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001306 break;
1307
1308 default:
1309 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001310 }
1311 } catch (Exception e) {
1312 // Just skip on any failure; we'll make it
1313 // visible when it next restarts.
1314 Slog.w(TAG, "Exception thrown making hidden: "
1315 + r.intent.getComponent(), e);
1316 }
1317 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001318 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320 }
1321 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001322 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001323 }
Craig Mautner58547802013-03-05 08:23:53 -08001324
Craig Mautner233ceee2014-05-09 17:05:11 -07001325 void convertToTranslucent(ActivityRecord r, ActivityOptions options) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001326 mTranslucentActivityWaiting = r;
1327 mUndrawnActivitiesBelowTopTranslucent.clear();
Craig Mautner233ceee2014-05-09 17:05:11 -07001328 mReturningActivityOptions = options;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001329 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1330 }
1331
1332 /**
1333 * Called as activities below the top translucent activity are redrawn. When the last one is
1334 * redrawn notify the top activity by calling
1335 * {@link Activity#onTranslucentConversionComplete}.
1336 *
1337 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1338 * occurred and the activity will be notified immediately.
1339 */
1340 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001341 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001342 if ((r == null)
1343 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1344 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1345 // The last undrawn activity below the top has just been drawn. If there is an
1346 // opaque activity at the top, notify it that it can become translucent safely now.
1347 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1348 mTranslucentActivityWaiting = null;
1349 mUndrawnActivitiesBelowTopTranslucent.clear();
1350 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1351
Craig Mautner71dd1b62014-02-18 15:48:52 -08001352 if (waitingActivity != null) {
1353 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1354 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1355 try {
1356 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1357 waitingActivity.appToken, r != null);
1358 } catch (RemoteException e) {
1359 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001360 }
1361 }
1362 }
1363 }
1364
Craig Mautnera61bc652013-10-28 15:43:18 -07001365 /** If any activities below the top running one are in the INITIALIZING state and they have a
1366 * starting window displayed then remove that starting window. It is possible that the activity
1367 * in this state will never resumed in which case that starting window will be orphaned. */
1368 void cancelInitializingActivities() {
1369 final ActivityRecord topActivity = topRunningActivityLocked(null);
1370 boolean aboveTop = true;
1371 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1372 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1373 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1374 final ActivityRecord r = activities.get(activityNdx);
1375 if (aboveTop) {
1376 if (r == topActivity) {
1377 aboveTop = false;
1378 }
1379 continue;
1380 }
1381
1382 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1383 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1384 r.mStartingWindowShown = false;
1385 mWindowManager.removeAppStartingWindow(r.appToken);
1386 }
1387 }
1388 }
1389 }
1390
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001391 /**
1392 * Ensure that the top activity in the stack is resumed.
1393 *
1394 * @param prev The previously resumed activity, for when in the process
1395 * of pausing; can be null to call from elsewhere.
1396 *
1397 * @return Returns true if something is being resumed, or false if
1398 * nothing happened.
1399 */
1400 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001401 return resumeTopActivityLocked(prev, null);
1402 }
1403
1404 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001405 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1406
Craig Mautnerdf88d732014-01-27 09:21:32 -08001407 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001408 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001409 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001410 // Do not resume this stack if its parent is not resumed.
1411 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1412 return false;
1413 }
1414
Craig Mautnera61bc652013-10-28 15:43:18 -07001415 cancelInitializingActivities();
1416
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417 // Find the first activity that is not finishing.
1418 ActivityRecord next = topRunningActivityLocked(null);
1419
1420 // Remember how we'll process this pause/resume situation, and ensure
1421 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001422 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1423 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424
1425 if (next == null) {
1426 // There are no more activities! Let's just start up the
1427 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001428 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001429 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001430 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001431 // Only resume home if on home display
Craig Mautner719e6212014-05-29 16:49:41 +00001432 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 }
1434
1435 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001436
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001437 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001438 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1439 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001440 // Make sure we have executed any pending transitions, since there
1441 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001442 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001443 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001444 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001445 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001446 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001447 return false;
1448 }
1449
Craig Mautner525f3d92013-05-07 14:01:50 -07001450 final TaskRecord nextTask = next.task;
Craig Mautner719e6212014-05-29 16:49:41 +00001451 final TaskRecord prevTask = prev != null ? prev.task : null;
bulicccd230712014-05-12 14:34:50 -07001452 if (prevTask != null && prevTask.stack == this &&
Craig Mautner719e6212014-05-29 16:49:41 +00001453 prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001454 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001455 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001456 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001457 } else if (prevTask != topTask()) {
Craig Mautner719e6212014-05-29 16:49:41 +00001458 // This task is going away but it was supposed to return to the home task.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001459 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001460 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner719e6212014-05-29 16:49:41 +00001461 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001462 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001463 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1464 "resumeTopActivityLocked: Launching home next");
1465 // Only resume home if on home display
Craig Mautner719e6212014-05-29 16:49:41 +00001466 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001468 }
1469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 // If we are sleeping, and there is no resumed activity, and the top
1471 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001472 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001473 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001474 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001475 // Make sure we have executed any pending transitions, since there
1476 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001477 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001479 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001480 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001481 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 return false;
1483 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001484
1485 // Make sure that the user who owns this activity is started. If not,
1486 // we will just leave it as is because someone should be bringing
1487 // another user's activities to the top of the stack.
1488 if (mService.mStartedUsers.get(next.userId) == null) {
1489 Slog.w(TAG, "Skipping resume of top activity " + next
1490 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001491 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001492 return false;
1493 }
1494
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 // The activity may be waiting for stop, but that is no longer
1496 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001497 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001498 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001499 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001500 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501
1502 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1503
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001504 // If we are currently pausing an activity, then don't do anything
1505 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001506 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001507 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1508 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001509 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001510 return false;
1511 }
1512
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001513 // Okay we are now going to start a switch, to 'next'. We may first
1514 // have to pause the current activity, but this is an important point
1515 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001516 // XXX "App Redirected" dialog is getting too many false positives
1517 // at this point, so turn off for now.
1518 if (false) {
1519 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1520 long now = SystemClock.uptimeMillis();
1521 final boolean inTime = mLastStartedActivity.startTime != 0
1522 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1523 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1524 final int nextUid = next.info.applicationInfo.uid;
1525 if (inTime && lastUid != nextUid
1526 && lastUid != next.launchedFromUid
1527 && mService.checkPermission(
1528 android.Manifest.permission.STOP_APP_SWITCHES,
1529 -1, next.launchedFromUid)
1530 != PackageManager.PERMISSION_GRANTED) {
1531 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1532 } else {
1533 next.startTime = now;
1534 mLastStartedActivity = next;
1535 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001536 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001537 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001538 mLastStartedActivity = next;
1539 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001540 }
Craig Mautner58547802013-03-05 08:23:53 -08001541
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 // We need to start pausing the current activity so the top one
1543 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001544 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001545 if (mResumedActivity != null) {
1546 pausing = true;
1547 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001548 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001549 }
1550 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001551 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1552 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001553 // At this point we want to put the upcoming activity's process
1554 // at the top of the LRU list, since we know we will be needing it
1555 // very soon and it would be a waste to let it get killed if it
1556 // happens to be sitting towards the end.
1557 if (next.app != null && next.app.thread != null) {
1558 // No reason to do full oom adj update here; we'll let that
1559 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001560 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001561 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001562 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 return true;
1564 }
1565
Christopher Tated3f175c2012-06-14 14:16:54 -07001566 // If the most recent activity was noHistory but was only stopped rather
1567 // than stopped+finished because the device went to sleep, we need to make
1568 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001569 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001570 !mLastNoHistoryActivity.finishing) {
1571 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1572 " on new resume");
1573 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1574 null, "no-history", false);
1575 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001576 }
1577
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 if (prev != null && prev != next) {
1579 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1580 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001581 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 if (DEBUG_SWITCH) Slog.v(
1583 TAG, "Resuming top, waiting visible to hide: " + prev);
1584 } else {
1585 // The next activity is already visible, so hide the previous
1586 // activity's windows right now so we can show the new one ASAP.
1587 // We only do this if the previous is finishing, which should mean
1588 // it is on top of the one being resumed so hiding it quickly
1589 // is good. Otherwise, we want to do the normal route of allowing
1590 // the resumed activity to be shown so we can decide if the
1591 // previous should actually be hidden depending on whether the
1592 // new one is found to be full-screen or not.
1593 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001594 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1596 + prev + ", waitingVisible="
1597 + (prev != null ? prev.waitingVisible : null)
1598 + ", nowVisible=" + next.nowVisible);
1599 } else {
1600 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1601 + prev + ", waitingVisible="
1602 + (prev != null ? prev.waitingVisible : null)
1603 + ", nowVisible=" + next.nowVisible);
1604 }
1605 }
1606 }
1607
Dianne Hackborne7f97212011-02-24 14:40:20 -08001608 // Launching this app's activity, make sure the app is no longer
1609 // considered stopped.
1610 try {
1611 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001612 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001613 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001614 } catch (IllegalArgumentException e) {
1615 Slog.w(TAG, "Failed trying to unstop package "
1616 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001617 }
1618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 // We are starting up the next activity, so tell the window manager
1620 // that the previous one will be hidden soon. This way it can know
1621 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001622 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 if (prev != null) {
1624 if (prev.finishing) {
1625 if (DEBUG_TRANSITION) Slog.v(TAG,
1626 "Prepare close transition: prev=" + prev);
1627 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001628 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001629 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001631 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001632 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1633 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001635 mWindowManager.setAppWillBeHidden(prev.appToken);
1636 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001638 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001639 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001640 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001641 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001642 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001643 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001644 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1645 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646 }
1647 }
1648 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001649 mWindowManager.setAppWillBeHidden(prev.appToken);
1650 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 }
Craig Mautner967212c2013-04-13 21:10:58 -07001652 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001653 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001654 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001655 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001656 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001657 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001658 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 }
1660 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001661
1662 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001663 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001664 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1665 if (opts != null) {
1666 resumeAnimOptions = opts.toBundle();
1667 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001668 next.applyOptionsLocked();
1669 } else {
1670 next.clearOptionsLocked();
1671 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001672
Craig Mautnercf910b02013-04-23 11:23:27 -07001673 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001674 if (next.app != null && next.app.thread != null) {
1675 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1676
1677 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001678 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001680 // schedule launch ticks to collect information about slow apps.
1681 next.startLaunchTickingLocked();
1682
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001683 ActivityRecord lastResumedActivity =
1684 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 ActivityState lastState = next.state;
1686
1687 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001688
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001689 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 next.state = ActivityState.RESUMED;
1691 mResumedActivity = next;
1692 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001693 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001694 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001695 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001696 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001697
1698 // Have the window manager re-evaluate the orientation of
1699 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001700 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001701 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001702 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001703 mService.mConfiguration,
1704 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1705 if (config != null) {
1706 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001708 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001709 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001710
Craig Mautner525f3d92013-05-07 14:01:50 -07001711 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001712 // The configuration update wasn't able to keep the existing
1713 // instance of the activity, and instead started a new one.
1714 // We should be all done, but let's just make sure our activity
1715 // is still at the top and schedule another run if something
1716 // weird happened.
1717 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001718 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 "Activity config changed during resume: " + next
1720 + ", new next: " + nextNext);
1721 if (nextNext != next) {
1722 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001723 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001725 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001726 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001727 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001728 return true;
1729 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001730 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001731 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 }
Craig Mautner58547802013-03-05 08:23:53 -08001733
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734 try {
1735 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001736 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001737 if (a != null) {
1738 final int N = a.size();
1739 if (!next.finishing && N > 0) {
1740 if (DEBUG_RESULTS) Slog.v(
1741 TAG, "Delivering results to " + next
1742 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001743 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 }
1745 }
1746
1747 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001748 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 }
1750
1751 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001752 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001754
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001755 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001756 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001757 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001758 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001759 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001760 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001761 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001762
Craig Mautner0eea92c2013-05-16 13:35:39 -07001763 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001764
Craig Mautnerac6f8432013-07-17 13:24:59 -07001765 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001766 } catch (Exception e) {
1767 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001768 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1769 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001771 if (lastStack != null) {
1772 lastStack.mResumedActivity = lastResumedActivity;
1773 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 Slog.i(TAG, "Restarting because process died: " + next);
1775 if (!next.hasBeenLaunched) {
1776 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001777 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1778 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001780 next.appToken, next.packageName, next.theme,
1781 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001782 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1783 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 }
George Mount2c92c972014-03-20 09:38:23 -07001785 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001786 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 return true;
1788 }
1789
1790 // From this point on, if something goes wrong there is no way
1791 // to recover the activity.
1792 try {
1793 next.visible = true;
1794 completeResumeLocked(next);
1795 } catch (Exception e) {
1796 // If any exception gets thrown, toss away this
1797 // activity and try the next one.
1798 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001799 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001800 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001801 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001802 return true;
1803 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 next.stopped = false;
1805
1806 } else {
1807 // Whoops, need to restart this activity!
1808 if (!next.hasBeenLaunched) {
1809 next.hasBeenLaunched = true;
1810 } else {
1811 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001812 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001813 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001814 mService.compatibilityInfoForPackageLocked(
1815 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001816 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001817 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001818 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819 }
1820 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1821 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001822 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001823 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 }
1825
Craig Mautnercf910b02013-04-23 11:23:27 -07001826 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001827 return true;
1828 }
1829
Craig Mautnerac6f8432013-07-17 13:24:59 -07001830 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001831 // If this is being moved to the top by another activity or being launched from the home
1832 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001833 if (isOnHomeDisplay()) {
1834 ActivityStack lastStack = mStackSupervisor.getLastStack();
1835 final boolean fromHome = lastStack.isHomeStack();
1836 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner719e6212014-05-29 16:49:41 +00001837 task.mOnTopOfHome = fromHome;
Craig Mautnere0a38842013-12-16 16:14:02 -08001838 }
1839 } else {
Craig Mautner719e6212014-05-29 16:49:41 +00001840 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001841 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001842
Craig Mautnerac6f8432013-07-17 13:24:59 -07001843 mTaskHistory.remove(task);
1844 // Now put task at top.
1845 int stackNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001846 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001847 // Put non-current user tasks below current user tasks.
1848 while (--stackNdx >= 0) {
Kenny Guy2a764942014-04-02 13:29:20 +01001849 if (!isCurrentProfileLocked(mTaskHistory.get(stackNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001850 break;
1851 }
1852 }
1853 ++stackNdx;
1854 }
1855 mTaskHistory.add(stackNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001856 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001857 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001858
Craig Mautner8849a5e2013-04-02 16:41:03 -07001859 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001860 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001861 TaskRecord rTask = r.task;
1862 final int taskId = rTask.taskId;
1863 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001864 // Last activity in task had been removed or ActivityManagerService is reusing task.
1865 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001866 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001867 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001868 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001869 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001870 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 if (!newTask) {
1872 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001873 boolean startIt = true;
1874 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1875 task = mTaskHistory.get(taskNdx);
1876 if (task == r.task) {
1877 // Here it is! Now, if this is not yet visible to the
1878 // user, then just add it without starting; it will
1879 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001880 if (!startIt) {
1881 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1882 + task, new RuntimeException("here").fillInStackTrace());
1883 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001884 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001885 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1886 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001887 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001888 r.userId, r.info.configChanges, task.voiceSession != null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001889 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001890 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001891 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001892 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001893 return;
1894 }
1895 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001896 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 startIt = false;
1898 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001899 }
1900 }
1901
1902 // Place a new activity at top of stack, so it is next to interact
1903 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001904
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 // If we are not placing the new activity frontmost, we do not want
1906 // to deliver the onUserLeaving callback to the actual frontmost
1907 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001908 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001909 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001910 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1911 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 }
Craig Mautner70a86932013-02-28 22:37:44 -08001913
1914 task = r.task;
1915
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001916 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001917 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001918 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001919 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001920 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001921
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001922 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001923 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001924 // We want to show the starting preview window if we are
1925 // switching to a new task, or the next activity's process is
1926 // not currently running.
1927 boolean showStartingIcon = newTask;
1928 ProcessRecord proc = r.app;
1929 if (proc == null) {
1930 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1931 }
1932 if (proc == null || proc.thread == null) {
1933 showStartingIcon = true;
1934 }
1935 if (DEBUG_TRANSITION) Slog.v(TAG,
1936 "Prepare open transition: starting " + r);
1937 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001938 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001939 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001940 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001941 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001942 ? AppTransition.TRANSIT_TASK_OPEN
1943 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001944 mNoAnimActivities.remove(r);
1945 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001946 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001947 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001948 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001949 r.info.configChanges, task.voiceSession != null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001950 boolean doShow = true;
1951 if (newTask) {
1952 // Even though this activity is starting fresh, we still need
1953 // to reset it to make sure we apply affinities to move any
1954 // existing activities from other tasks in to it.
1955 // If the caller has requested that the target task be
1956 // reset, then do so.
1957 if ((r.intent.getFlags()
Craig Mautnerd00f4742014-03-12 14:17:26 -07001958 & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 resetTaskIfNeededLocked(r, r);
1960 doShow = topRunningNonDelayedActivityLocked(null) == r;
1961 }
1962 }
1963 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1964 // Figure out if we are transitioning from another activity that is
1965 // "has the same starting icon" as the next one. This allows the
1966 // window manager to keep the previous window it had previously
1967 // created, if it still had one.
1968 ActivityRecord prev = mResumedActivity;
1969 if (prev != null) {
1970 // We don't want to reuse the previous starting preview if:
1971 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001972 if (prev.task != r.task) {
1973 prev = null;
1974 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001975 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001976 else if (prev.nowVisible) {
1977 prev = null;
1978 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001979 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001980 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001981 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001982 mService.compatibilityInfoForPackageLocked(
1983 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001984 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001985 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07001986 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001987 }
1988 } else {
1989 // If this is the first activity, don't do any fancy animations,
1990 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001992 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001993 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001994 r.info.configChanges, task.voiceSession != null);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001995 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07001996 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 }
1998 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001999 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 }
2001
2002 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002003 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002004 }
2005 }
2006
Dianne Hackbornbe707852011-11-11 14:32:10 -08002007 final void validateAppTokensLocked() {
2008 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002009 mValidateAppTokens.ensureCapacity(numActivities());
2010 final int numTasks = mTaskHistory.size();
2011 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2012 TaskRecord task = mTaskHistory.get(taskNdx);
2013 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002014 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002015 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002016 }
Craig Mautner000f0022013-02-26 15:04:29 -08002017 TaskGroup group = new TaskGroup();
2018 group.taskId = task.taskId;
2019 mValidateAppTokens.add(group);
2020 final int numActivities = activities.size();
2021 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2022 final ActivityRecord r = activities.get(activityNdx);
2023 group.tokens.add(r.appToken);
2024 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002025 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002026 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002027 }
2028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 /**
2030 * Perform a reset of the given task, if needed as part of launching it.
2031 * Returns the new HistoryRecord at the top of the task.
2032 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002033 /**
2034 * Helper method for #resetTaskIfNeededLocked.
2035 * We are inside of the task being reset... we'll either finish this activity, push it out
2036 * for another task, or leave it as-is.
2037 * @param task The task containing the Activity (taskTop) that might be reset.
2038 * @param forceReset
2039 * @return An ActivityOptions that needs to be processed.
2040 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002041 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002042 ActivityOptions topOptions = null;
2043
2044 int replyChainEnd = -1;
2045 boolean canMoveOptions = true;
2046
2047 // We only do this for activities that are not the root of the task (since if we finish
2048 // the root, we may no longer have the task!).
2049 final ArrayList<ActivityRecord> activities = task.mActivities;
2050 final int numActivities = activities.size();
2051 for (int i = numActivities - 1; i > 0; --i ) {
2052 ActivityRecord target = activities.get(i);
2053
2054 final int flags = target.info.flags;
2055 final boolean finishOnTaskLaunch =
2056 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2057 final boolean allowTaskReparenting =
2058 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2059 final boolean clearWhenTaskReset =
2060 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2061
2062 if (!finishOnTaskLaunch
2063 && !clearWhenTaskReset
2064 && target.resultTo != null) {
2065 // If this activity is sending a reply to a previous
2066 // activity, we can't do anything with it now until
2067 // we reach the start of the reply chain.
2068 // XXX note that we are assuming the result is always
2069 // to the previous activity, which is almost always
2070 // the case but we really shouldn't count on.
2071 if (replyChainEnd < 0) {
2072 replyChainEnd = i;
2073 }
2074 } else if (!finishOnTaskLaunch
2075 && !clearWhenTaskReset
2076 && allowTaskReparenting
2077 && target.taskAffinity != null
2078 && !target.taskAffinity.equals(task.affinity)) {
2079 // If this activity has an affinity for another
2080 // task, then we need to move it out of here. We will
2081 // move it as far out of the way as possible, to the
2082 // bottom of the activity stack. This also keeps it
2083 // correctly ordered with any activities we previously
2084 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002085 final ThumbnailHolder newThumbHolder;
2086 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002087 final ActivityRecord bottom =
2088 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002089 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002090 if (bottom != null && target.taskAffinity != null
2091 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002092 // If the activity currently at the bottom has the
2093 // same task affinity as the one we are moving,
2094 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002095 targetTask = bottom.task;
2096 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002097 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002098 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002099 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002100 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002101 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002102 newThumbHolder = targetTask;
2103 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2105 + " out to new task " + target.task);
2106 }
2107
Craig Mautnerd00f4742014-03-12 14:17:26 -07002108 target.thumbHolder = newThumbHolder;
Craig Mautner329f4122013-11-07 09:10:42 -08002109
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002111 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002112
Craig Mautner525f3d92013-05-07 14:01:50 -07002113 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002114 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2115 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002116 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002117 if (p.finishing) {
2118 continue;
2119 }
2120
Craig Mautner525f3d92013-05-07 14:01:50 -07002121 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002122 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002123 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002124 topOptions = p.takeOptionsLocked();
2125 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002126 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002127 }
2128 }
2129 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002130 + task + " adding to task=" + targetTask
2131 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002132 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2133 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002134 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002135 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002136
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002137 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002138 }
2139
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002140 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002141 if (VALIDATE_TOKENS) {
2142 validateAppTokensLocked();
2143 }
2144
2145 replyChainEnd = -1;
2146 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2147 // If the activity should just be removed -- either
2148 // because it asks for it, or the task should be
2149 // cleared -- then finish it and anything that is
2150 // part of its reply chain.
2151 int end;
2152 if (clearWhenTaskReset) {
2153 // In this case, we want to finish this activity
2154 // and everything above it, so be sneaky and pretend
2155 // like these are all in the reply chain.
2156 end = numActivities - 1;
2157 } else if (replyChainEnd < 0) {
2158 end = i;
2159 } else {
2160 end = replyChainEnd;
2161 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002162 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002163 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002164 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002165 if (p.finishing) {
2166 continue;
2167 }
2168 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002169 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002170 topOptions = p.takeOptionsLocked();
2171 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002172 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002173 }
2174 }
Craig Mautner58547802013-03-05 08:23:53 -08002175 if (DEBUG_TASKS) Slog.w(TAG,
2176 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002177 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002178 end--;
2179 srcPos--;
2180 }
2181 }
2182 replyChainEnd = -1;
2183 } else {
2184 // If we were in the middle of a chain, well the
2185 // activity that started it all doesn't want anything
2186 // special, so leave it all as-is.
2187 replyChainEnd = -1;
2188 }
2189 }
2190
2191 return topOptions;
2192 }
2193
2194 /**
2195 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2196 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2197 * @param affinityTask The task we are looking for an affinity to.
2198 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2199 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2200 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2201 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002202 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002203 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002204 int replyChainEnd = -1;
2205 final int taskId = task.taskId;
2206 final String taskAffinity = task.affinity;
2207
2208 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2209 final int numActivities = activities.size();
2210 // Do not operate on the root Activity.
2211 for (int i = numActivities - 1; i > 0; --i) {
2212 ActivityRecord target = activities.get(i);
2213
2214 final int flags = target.info.flags;
2215 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2216 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2217
2218 if (target.resultTo != null) {
2219 // If this activity is sending a reply to a previous
2220 // activity, we can't do anything with it now until
2221 // we reach the start of the reply chain.
2222 // XXX note that we are assuming the result is always
2223 // to the previous activity, which is almost always
2224 // the case but we really shouldn't count on.
2225 if (replyChainEnd < 0) {
2226 replyChainEnd = i;
2227 }
2228 } else if (topTaskIsHigher
2229 && allowTaskReparenting
2230 && taskAffinity != null
2231 && taskAffinity.equals(target.taskAffinity)) {
2232 // This activity has an affinity for our task. Either remove it if we are
2233 // clearing or move it over to our task. Note that
2234 // we currently punt on the case where we are resetting a
2235 // task that is not at the top but who has activities above
2236 // with an affinity to it... this is really not a normal
2237 // case, and we will need to later pull that task to the front
2238 // and usually at that point we will do the reset and pick
2239 // up those remaining activities. (This only happens if
2240 // someone starts an activity in a new task from an activity
2241 // in a task that is not currently on top.)
2242 if (forceReset || finishOnTaskLaunch) {
2243 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2244 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2245 for (int srcPos = start; srcPos >= i; --srcPos) {
2246 final ActivityRecord p = activities.get(srcPos);
2247 if (p.finishing) {
2248 continue;
2249 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002250 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002251 }
2252 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002253 if (taskInsertionPoint < 0) {
2254 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002255
Craig Mautner77878772013-03-04 19:46:24 -08002256 }
Craig Mautner77878772013-03-04 19:46:24 -08002257
2258 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2259 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2260 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2261 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002262 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002263 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002264 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002265
Craig Mautnere3a74d52013-02-22 14:14:58 -08002266 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2267 + " to stack at " + task,
2268 new RuntimeException("here").fillInStackTrace());
2269 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2270 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002271 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002272 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002273 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002274 if (VALIDATE_TOKENS) {
2275 validateAppTokensLocked();
2276 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002277
2278 // Now we've moved it in to place... but what if this is
2279 // a singleTop activity and we have put it on top of another
2280 // instance of the same activity? Then we drop the instance
2281 // below so it remains singleTop.
2282 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2283 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002284 int targetNdx = taskActivities.indexOf(target);
2285 if (targetNdx > 0) {
2286 ActivityRecord p = taskActivities.get(targetNdx - 1);
2287 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002288 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2289 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002290 }
2291 }
2292 }
2293 }
2294
2295 replyChainEnd = -1;
2296 }
2297 }
Craig Mautner77878772013-03-04 19:46:24 -08002298 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002299 }
2300
Craig Mautner8849a5e2013-04-02 16:41:03 -07002301 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002302 ActivityRecord newActivity) {
2303 boolean forceReset =
2304 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2305 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2306 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2307 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2308 forceReset = true;
2309 }
2310 }
2311
2312 final TaskRecord task = taskTop.task;
2313
2314 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2315 * for remaining tasks. Used for later tasks to reparent to task. */
2316 boolean taskFound = false;
2317
2318 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2319 ActivityOptions topOptions = null;
2320
Craig Mautner77878772013-03-04 19:46:24 -08002321 // Preserve the location for reparenting in the new task.
2322 int reparentInsertionPoint = -1;
2323
Craig Mautnere3a74d52013-02-22 14:14:58 -08002324 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2325 final TaskRecord targetTask = mTaskHistory.get(i);
2326
2327 if (targetTask == task) {
2328 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2329 taskFound = true;
2330 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002331 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2332 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002333 }
2334 }
2335
Craig Mautner70a86932013-02-28 22:37:44 -08002336 int taskNdx = mTaskHistory.indexOf(task);
2337 do {
2338 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2339 } while (taskTop == null && taskNdx >= 0);
2340
Craig Mautnere3a74d52013-02-22 14:14:58 -08002341 if (topOptions != null) {
2342 // If we got some ActivityOptions from an activity on top that
2343 // was removed from the task, propagate them to the new real top.
2344 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002345 taskTop.updateOptionsLocked(topOptions);
2346 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002347 topOptions.abort();
2348 }
2349 }
2350
2351 return taskTop;
2352 }
2353
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002354 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2355 String resultWho, int requestCode, int resultCode, Intent data) {
2356
2357 if (callingUid > 0) {
2358 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002359 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 }
2361
2362 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2363 + " : who=" + resultWho + " req=" + requestCode
2364 + " res=" + resultCode + " data=" + data);
2365 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2366 try {
2367 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2368 list.add(new ResultInfo(resultWho, requestCode,
2369 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002370 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 return;
2372 } catch (Exception e) {
2373 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2374 }
2375 }
2376
2377 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2378 }
2379
Craig Mautner04f0b702013-10-22 12:31:01 -07002380 private void adjustFocusedActivityLocked(ActivityRecord r) {
2381 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2382 ActivityRecord next = topRunningActivityLocked(null);
2383 if (next != r) {
2384 final TaskRecord task = r.task;
Craig Mautner719e6212014-05-29 16:49:41 +00002385 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2386 mStackSupervisor.moveHomeToTop();
Craig Mautner04f0b702013-10-22 12:31:01 -07002387 }
2388 }
Winson Chung648c83b2014-04-28 15:11:56 -07002389 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2390 if (top != null) {
2391 mService.setFocusedActivityLocked(top);
2392 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002393 }
2394 }
2395
Craig Mautnerf3333272013-04-22 10:55:53 -07002396 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2398 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2399 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2400 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002401 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002402 if (DEBUG_STATES) {
2403 Slog.d(TAG, "no-history finish of " + r);
2404 }
2405 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002406 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002407 } else {
2408 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2409 + " on stop because we're just sleeping");
2410 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002412 }
2413
2414 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002415 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002416 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 try {
2418 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002419 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2420 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 r.state = ActivityState.STOPPING;
2422 if (DEBUG_VISBILITY) Slog.v(
2423 TAG, "Stopping visible=" + r.visible + " for " + r);
2424 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002425 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002426 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002427 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002428 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002429 r.setSleeping(true);
2430 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002431 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002432 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 } catch (Exception e) {
2434 // Maybe just ignore exceptions here... if the process
2435 // has crashed, our death notification will clean things
2436 // up.
2437 Slog.w(TAG, "Exception thrown during pause", e);
2438 // Just in case, assume it to be stopped.
2439 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002440 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 r.state = ActivityState.STOPPED;
2442 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002443 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 }
2445 }
2446 }
2447 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002448
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 /**
2450 * @return Returns true if the activity is being finished, false if for
2451 * some reason it is being left as-is.
2452 */
2453 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002454 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002455 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002456 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002457 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002458 + ", result=" + resultCode + ", data=" + resultData
2459 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002460 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002461 return false;
2462 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463
Craig Mautnerd44711d2013-02-23 11:24:36 -08002464 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465 return true;
2466 }
2467
Craig Mautnerd2328952013-03-05 12:46:26 -08002468 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002469 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2470 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2471 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2472 ActivityRecord r = activities.get(activityNdx);
2473 if (r.resultTo == self && r.requestCode == requestCode) {
2474 if ((r.resultWho == null && resultWho == null) ||
2475 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2476 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2477 false);
2478 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002479 }
2480 }
2481 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002482 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002483 }
2484
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002485 final void finishTopRunningActivityLocked(ProcessRecord app) {
2486 ActivityRecord r = topRunningActivityLocked(null);
2487 if (r != null && r.app == app) {
2488 // If the top running activity is from this crashing
2489 // process, then terminate it to avoid getting in a loop.
2490 Slog.w(TAG, " Force finishing activity "
2491 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002492 int taskNdx = mTaskHistory.indexOf(r.task);
2493 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002494 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002495 // Also terminate any activities below it that aren't yet
2496 // stopped, to avoid a situation where one will get
2497 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002498 --activityNdx;
2499 if (activityNdx < 0) {
2500 do {
2501 --taskNdx;
2502 if (taskNdx < 0) {
2503 break;
2504 }
2505 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2506 } while (activityNdx < 0);
2507 }
2508 if (activityNdx >= 0) {
2509 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002510 if (r.state == ActivityState.RESUMED
2511 || r.state == ActivityState.PAUSING
2512 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002513 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002514 Slog.w(TAG, " Force finishing activity "
2515 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002516 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002517 }
2518 }
2519 }
2520 }
2521 }
2522
Craig Mautnerd2328952013-03-05 12:46:26 -08002523 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002524 ArrayList<ActivityRecord> activities = r.task.mActivities;
2525 for (int index = activities.indexOf(r); index >= 0; --index) {
2526 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002527 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002528 break;
2529 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002530 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002531 }
2532 return true;
2533 }
2534
Dianne Hackborn5c607432012-02-28 14:44:19 -08002535 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2536 // send the result
2537 ActivityRecord resultTo = r.resultTo;
2538 if (resultTo != null) {
2539 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2540 + " who=" + r.resultWho + " req=" + r.requestCode
2541 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002542 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002543 if (resultData != null) {
2544 resultData.prepareToLeaveUser(r.userId);
2545 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002546 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002547 if (r.info.applicationInfo.uid > 0) {
2548 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2549 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002550 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002551 }
2552 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2553 resultData);
2554 r.resultTo = null;
2555 }
2556 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2557
2558 // Make sure this HistoryRecord is not holding on to other resources,
2559 // because clients have remote IPC references to this object so we
2560 // can't assume that will go away and want to avoid circular IPC refs.
2561 r.results = null;
2562 r.pendingResults = null;
2563 r.newIntents = null;
2564 r.icicle = null;
2565 }
2566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 /**
2568 * @return Returns true if this activity has been removed from the history
2569 * list, or false if it is still in the list and will be removed later.
2570 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002571 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2572 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002573 if (r.finishing) {
2574 Slog.w(TAG, "Duplicate finish request for " + r);
2575 return false;
2576 }
2577
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002578 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002579 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002580 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002581 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002582 task.taskId, r.shortComponentName, reason);
2583 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002584 final int index = activities.indexOf(r);
2585 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002586 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002587 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002588 // If the caller asked that this activity (and all above it)
2589 // be cleared when the task is reset, don't lose that information,
2590 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002591 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002592 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 }
2594 }
2595
2596 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002597
2598 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599
Dianne Hackborn5c607432012-02-28 14:44:19 -08002600 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002601
Craig Mautnerde4ef022013-04-07 19:01:33 -07002602 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002603 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002604 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002606 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002607 ? AppTransition.TRANSIT_TASK_CLOSE
2608 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002609
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002610 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002611 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002612
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002613 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2615 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2616 startPausingLocked(false, false);
2617 }
2618
Craig Mautneraea74a52014-03-08 14:23:10 -08002619 if (endTask) {
2620 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2621 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002622 } else if (r.state != ActivityState.PAUSING) {
2623 // If the activity is PAUSING, we will complete the finish once
2624 // it is done pausing; else we can just directly finish it here.
2625 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002626 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 } else {
2628 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2629 }
2630
2631 return false;
2632 }
2633
Craig Mautnerf3333272013-04-22 10:55:53 -07002634 static final int FINISH_IMMEDIATELY = 0;
2635 static final int FINISH_AFTER_PAUSE = 1;
2636 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637
Craig Mautnerf3333272013-04-22 10:55:53 -07002638 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002639 // First things first: if this activity is currently visible,
2640 // and the resumed activity is not yet visible, then hold off on
2641 // finishing until the resumed one becomes visible.
2642 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002643 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2644 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002645 if (mStackSupervisor.mStoppingActivities.size() > 3
2646 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 // If we already have a few activities waiting to stop,
2648 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002649 // them out. Or if r is the last of activity of the last task the stack
2650 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002651 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002652 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002653 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 }
2655 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002656 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2657 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002659 if (oomAdj) {
2660 mService.updateOomAdjLocked();
2661 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 return r;
2663 }
2664
2665 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002666 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002667 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002668 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002669 if (mResumedActivity == r) {
2670 mResumedActivity = null;
2671 }
2672 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002673 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 r.state = ActivityState.FINISHING;
2675
2676 if (mode == FINISH_IMMEDIATELY
2677 || prevState == ActivityState.STOPPED
2678 || prevState == ActivityState.INITIALIZING) {
2679 // If this activity is already stopped, we can just finish
2680 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002681 r.makeFinishing();
2682 boolean activityRemoved = destroyActivityLocked(r, true, oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002683 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002684 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002685 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002686 if (DEBUG_CONTAINERS) Slog.d(TAG,
2687 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2688 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002689 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002691
2692 // Need to go through the full pause cycle to get this
2693 // activity into the stopped state and then finish it.
2694 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002695 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002696 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002697 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698 return r;
2699 }
2700
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002701 void finishAllActivitiesLocked() {
2702 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2703 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2704 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2705 final ActivityRecord r = activities.get(activityNdx);
2706 if (r.finishing) {
2707 continue;
2708 }
2709 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
2710 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2711 }
2712 }
2713 }
2714
Craig Mautnerd2328952013-03-05 12:46:26 -08002715 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002716 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002717 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002718 final TaskRecord task = srec.task;
2719 final ArrayList<ActivityRecord> activities = task.mActivities;
2720 final int start = activities.indexOf(srec);
2721 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002722 return false;
2723 }
2724 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002725 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002726 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002727 final ComponentName dest = destIntent.getComponent();
2728 if (start > 0 && dest != null) {
2729 for (int i = finishTo; i >= 0; i--) {
2730 ActivityRecord r = activities.get(i);
2731 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002732 r.info.name.equals(dest.getClassName())) {
2733 finishTo = i;
2734 parent = r;
2735 foundParentInTask = true;
2736 break;
2737 }
2738 }
2739 }
2740
2741 IActivityController controller = mService.mController;
2742 if (controller != null) {
2743 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2744 if (next != null) {
2745 // ask watcher if this is allowed
2746 boolean resumeOK = true;
2747 try {
2748 resumeOK = controller.activityResuming(next.packageName);
2749 } catch (RemoteException e) {
2750 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002751 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002752 }
2753
2754 if (!resumeOK) {
2755 return false;
2756 }
2757 }
2758 }
2759 final long origId = Binder.clearCallingIdentity();
2760 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002761 ActivityRecord r = activities.get(i);
2762 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002763 // Only return the supplied result for the first activity finished
2764 resultCode = Activity.RESULT_CANCELED;
2765 resultData = null;
2766 }
2767
2768 if (parent != null && foundParentInTask) {
2769 final int parentLaunchMode = parent.info.launchMode;
2770 final int destIntentFlags = destIntent.getFlags();
2771 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2772 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2773 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2774 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2775 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2776 } else {
2777 try {
2778 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2779 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002780 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002781 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002782 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002783 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002784 foundParentInTask = res == ActivityManager.START_SUCCESS;
2785 } catch (RemoteException e) {
2786 foundParentInTask = false;
2787 }
2788 requestFinishActivityLocked(parent.appToken, resultCode,
2789 resultData, "navigate-up", true);
2790 }
2791 }
2792 Binder.restoreCallingIdentity(origId);
2793 return foundParentInTask;
2794 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 /**
2796 * Perform the common clean-up of an activity record. This is called both
2797 * as part of destroyActivityLocked() (when destroying the client-side
2798 * representation) and cleaning things up as a result of its hosting
2799 * processing going away, in which case there is no remaining client-side
2800 * state to destroy so only the cleanup here is needed.
2801 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002802 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2803 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002804 if (mResumedActivity == r) {
2805 mResumedActivity = null;
2806 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002807 if (mPausingActivity == r) {
2808 mPausingActivity = null;
2809 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002810 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002811
2812 r.configDestroy = false;
2813 r.frozenBeforeDestroy = false;
2814
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002815 if (setState) {
2816 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2817 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002818 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002819 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002820 }
2821
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002822 // Make sure this record is no longer in the pending finishes list.
2823 // This could happen, for example, if we are trimming activities
2824 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002825 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002826 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002827
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828 // Remove any pending results.
2829 if (r.finishing && r.pendingResults != null) {
2830 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2831 PendingIntentRecord rec = apr.get();
2832 if (rec != null) {
2833 mService.cancelIntentSenderLocked(rec, false);
2834 }
2835 }
2836 r.pendingResults = null;
2837 }
2838
2839 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002840 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002841 }
2842
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002844 removeTimeoutsForActivityLocked(r);
2845 }
2846
2847 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002848 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002850 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002851 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002852 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002853 }
2854
Craig Mautner04a0ea62014-01-13 12:51:26 -08002855 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002856 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002857 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2858 r.makeFinishing();
2859 if (DEBUG_ADD_REMOVE) {
2860 RuntimeException here = new RuntimeException("here");
2861 here.fillInStackTrace();
2862 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002863 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002864 r.takeFromHistory();
2865 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002866 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002867 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002868 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002869 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002870 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002871 if (VALIDATE_TOKENS) {
2872 validateAppTokensLocked();
2873 }
Craig Mautner312ba862014-02-10 17:55:01 -08002874 final TaskRecord task = r.task;
2875 if (task != null && task.removeActivity(r)) {
2876 if (DEBUG_STACK) Slog.i(TAG,
2877 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner719e6212014-05-29 16:49:41 +00002878 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
2879 mStackSupervisor.moveHomeToTop();
Craig Mautner312ba862014-02-10 17:55:01 -08002880 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002881 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08002882 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002883 cleanUpActivityServicesLocked(r);
2884 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002886
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887 /**
2888 * Perform clean-up of service connections in an activity record.
2889 */
2890 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2891 // Throw away any services that have been bound by this activity.
2892 if (r.connections != null) {
2893 Iterator<ConnectionRecord> it = r.connections.iterator();
2894 while (it.hasNext()) {
2895 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002896 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 }
2898 r.connections = null;
2899 }
2900 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002901
2902 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2903 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2904 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2905 mHandler.sendMessage(msg);
2906 }
2907
Dianne Hackborn28695e02011-11-02 21:59:51 -07002908 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002909 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002910 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002911 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2912 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2913 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2914 final ActivityRecord r = activities.get(activityNdx);
2915 if (r.finishing) {
2916 continue;
2917 }
2918 if (r.fullscreen) {
2919 lastIsOpaque = true;
2920 }
2921 if (owner != null && r.app != owner) {
2922 continue;
2923 }
2924 if (!lastIsOpaque) {
2925 continue;
2926 }
2927 // We can destroy this one if we have its icicle saved and
2928 // it is not in the process of pausing/stopping/finishing.
2929 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2930 && r.haveState && !r.visible && r.stopped
2931 && r.state != ActivityState.DESTROYING
2932 && r.state != ActivityState.DESTROYED) {
2933 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2934 + " resumed=" + mResumedActivity
2935 + " pausing=" + mPausingActivity);
2936 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2937 activityRemoved = true;
2938 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002939 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002940 }
2941 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002942 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002943 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002944 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002945 }
2946
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 /**
2948 * Destroy the current CLIENT SIDE instance of an activity. This may be
2949 * called both when actually finishing an activity, or when performing
2950 * a configuration switch where we destroy the current client-side object
2951 * but then create a new client-side object for this same HistoryRecord.
2952 */
2953 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002954 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002955 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002956 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2958 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002959 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002960 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002961
2962 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002963
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002964 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965
2966 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002967
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002968 if (hadApp) {
2969 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002970 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2972 mService.mHeavyWeightProcess = null;
2973 mService.mHandler.sendEmptyMessage(
2974 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2975 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002976 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002977 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002978 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002979 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980 }
2981 }
2982
2983 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002984
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 try {
2986 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002987 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 r.configChangeFlags);
2989 } catch (Exception e) {
2990 // We can just ignore exceptions here... if the process
2991 // has crashed, our death notification will clean things
2992 // up.
2993 //Slog.w(TAG, "Exception thrown during finish", e);
2994 if (r.finishing) {
2995 removeActivityFromHistoryLocked(r);
2996 removedFromHistory = true;
2997 skipDestroy = true;
2998 }
2999 }
3000
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003001 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003002
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003003 // If the activity is finishing, we need to wait on removing it
3004 // from the list to give it a chance to do its cleanup. During
3005 // that time it may make calls back with its token so we need to
3006 // be able to find it on the list and so we don't want to remove
3007 // it from the list yet. Otherwise, we can just immediately put
3008 // it in the destroyed state since we are not removing it from the
3009 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003010 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003011 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3012 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003014 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3016 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003017 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003018 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003019 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003020 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
3022 } else {
3023 // remove this record from the history.
3024 if (r.finishing) {
3025 removeActivityFromHistoryLocked(r);
3026 removedFromHistory = true;
3027 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003028 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003030 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003031 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003032 }
3033 }
3034
3035 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003036
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 if (!mLRUActivities.remove(r) && hadApp) {
3038 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3039 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003041 return removedFromHistory;
3042 }
3043
Craig Mautnerd2328952013-03-05 12:46:26 -08003044 final void activityDestroyedLocked(IBinder token) {
3045 final long origId = Binder.clearCallingIdentity();
3046 try {
3047 ActivityRecord r = ActivityRecord.forToken(token);
3048 if (r != null) {
3049 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003051 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003052
3053 if (isInStackLocked(token) != null) {
3054 if (r.state == ActivityState.DESTROYING) {
3055 cleanUpActivityLocked(r, true, false);
3056 removeActivityFromHistoryLocked(r);
3057 }
3058 }
Craig Mautner05d29032013-05-03 13:40:13 -07003059 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003060 } finally {
3061 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 }
3063 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003064
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003065 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3066 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003068 if (DEBUG_CLEANUP) Slog.v(
3069 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 + " with " + i + " entries");
3071 while (i > 0) {
3072 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003073 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003074 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003076 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003078 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 }
3080 }
3081 }
3082
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003083 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3084 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003085 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3086 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003087 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3088 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003089 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003090 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003091 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3092 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003093
3094 boolean hasVisibleActivities = false;
3095
3096 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003097 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003098 if (DEBUG_CLEANUP) Slog.v(
3099 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003100 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3101 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3102 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3103 final ActivityRecord r = activities.get(activityNdx);
3104 --i;
3105 if (DEBUG_CLEANUP) Slog.v(
3106 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3107 if (r.app == app) {
3108 boolean remove;
3109 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3110 // Don't currently have state for the activity, or
3111 // it is finishing -- always remove it.
3112 remove = true;
3113 } else if (r.launchCount > 2 &&
3114 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3115 // We have launched this activity too many times since it was
3116 // able to run, so give up and remove it.
3117 remove = true;
3118 } else {
3119 // The process may be gone, but the activity lives on!
3120 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003121 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003122 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003123 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003124 RuntimeException here = new RuntimeException("here");
3125 here.fillInStackTrace();
3126 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3127 + ": haveState=" + r.haveState
3128 + " stateNotNeeded=" + r.stateNotNeeded
3129 + " finishing=" + r.finishing
3130 + " state=" + r.state, here);
3131 }
3132 if (!r.finishing) {
3133 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3134 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3135 r.userId, System.identityHashCode(r),
3136 r.task.taskId, r.shortComponentName,
3137 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003138 if (r.state == ActivityState.RESUMED) {
3139 mService.updateUsageStats(r, false);
3140 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003141 }
3142 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003143
Craig Mautner0247fc82013-02-28 14:32:06 -08003144 } else {
3145 // We have the current state for this activity, so
3146 // it can be restarted later when needed.
3147 if (localLOGV) Slog.v(
3148 TAG, "Keeping entry, setting app to null");
3149 if (r.visible) {
3150 hasVisibleActivities = true;
3151 }
3152 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3153 + r);
3154 r.app = null;
3155 r.nowVisible = false;
3156 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003157 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003158 "App died, clearing saved state of " + r);
3159 r.icicle = null;
3160 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003161 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003162
Craig Mautnerd2328952013-03-05 12:46:26 -08003163 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003164 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003165 }
3166 }
3167
3168 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003170
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003171 final void updateTransitLocked(int transit, Bundle options) {
3172 if (options != null) {
3173 ActivityRecord r = topRunningActivityLocked(null);
3174 if (r != null && r.state != ActivityState.RESUMED) {
3175 r.updateOptionsLocked(options);
3176 } else {
3177 ActivityOptions.abort(options);
3178 }
3179 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003180 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003181 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003182
Craig Mautner21d24a22014-04-23 11:45:37 -07003183 void updateTaskMovement(TaskRecord task, boolean toFront) {
3184 if (task.isPersistable) {
3185 task.mLastTimeMoved = System.currentTimeMillis();
3186 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3187 // recently will be most negative, tasks sent to the bottom before that will be less
3188 // negative. Similarly for recent tasks moved to the top which will be most positive.
3189 if (!toFront) {
3190 task.mLastTimeMoved *= -1;
3191 }
3192 }
3193 }
3194
Craig Mautner719e6212014-05-29 16:49:41 +00003195 void moveHomeTaskToTop() {
Craig Mautnera82aa092013-09-13 15:34:08 -07003196 final int top = mTaskHistory.size() - 1;
3197 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3198 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner719e6212014-05-29 16:49:41 +00003199 if (task.isHomeTask()) {
3200 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003201 mTaskHistory.remove(taskNdx);
3202 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003203 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003204 mWindowManager.moveTaskToTop(task.taskId);
3205 return;
3206 }
3207 }
3208 }
3209
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003210 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003211 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003212
Craig Mautner11bf9a52013-02-19 14:08:51 -08003213 final int numTasks = mTaskHistory.size();
3214 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003215 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003216 // nothing to do!
3217 if (reason != null &&
3218 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3219 ActivityOptions.abort(options);
3220 } else {
3221 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3222 }
3223 return;
3224 }
3225
Craig Mautnere0a38842013-12-16 16:14:02 -08003226 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003227
Craig Mautner11bf9a52013-02-19 14:08:51 -08003228 // Shift all activities with this task up to the top
3229 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003230 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003231
3232 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 if (reason != null &&
3234 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003235 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 ActivityRecord r = topRunningActivityLocked(null);
3237 if (r != null) {
3238 mNoAnimActivities.add(r);
3239 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003240 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003241 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003242 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003243 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003244
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003245 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003246
Craig Mautner05d29032013-05-03 13:40:13 -07003247 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003248 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003249
3250 if (VALIDATE_TOKENS) {
3251 validateAppTokensLocked();
3252 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 }
3254
3255 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003256 * Worker method for rearranging history stack. Implements the function of moving all
3257 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003259 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003260 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3261 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003262 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003263 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 * @return Returns true if the move completed, false if not.
3265 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003266 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003267 final TaskRecord tr = taskForIdLocked(taskId);
3268 if (tr == null) {
3269 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3270 return false;
3271 }
3272
3273 Slog.i(TAG, "moveTaskToBack: " + tr);
3274
3275 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003276
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003277 // If we have a watcher, preflight the move before committing to it. First check
3278 // for *other* available tasks, but if none are available, then try again allowing the
3279 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003280 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003281 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 if (next == null) {
3283 next = topRunningActivityLocked(null, 0);
3284 }
3285 if (next != null) {
3286 // ask watcher if this is allowed
3287 boolean moveOK = true;
3288 try {
3289 moveOK = mService.mController.activityResuming(next.packageName);
3290 } catch (RemoteException e) {
3291 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003292 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003293 }
3294 if (!moveOK) {
3295 return false;
3296 }
3297 }
3298 }
3299
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003301 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003302
Craig Mautner11bf9a52013-02-19 14:08:51 -08003303 mTaskHistory.remove(tr);
3304 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003305 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003306
Craig Mautnerc8143c62013-09-03 12:15:57 -07003307 // There is an assumption that moving a task to the back moves it behind the home activity.
3308 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003309 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003310 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3311 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner719e6212014-05-29 16:49:41 +00003312 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003313 break;
3314 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003315 if (taskNdx == 1) {
3316 // Set the last task before tr to go to home.
Craig Mautner719e6212014-05-29 16:49:41 +00003317 task.mOnTopOfHome = true;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003318 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003319 }
3320
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003322 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3323 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 ActivityRecord r = topRunningActivityLocked(null);
3325 if (r != null) {
3326 mNoAnimActivities.add(r);
3327 }
3328 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003329 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003331 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003332
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003334 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 }
3336
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003337 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner719e6212014-05-29 16:49:41 +00003338 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
3339 tr.mOnTopOfHome = false;
3340 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003341 }
3342
Craig Mautner05d29032013-05-03 13:40:13 -07003343 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 return true;
3345 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003346
Craig Mautner8849a5e2013-04-02 16:41:03 -07003347 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003348 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003349 final Uri data = r.intent.getData();
3350 final String strData = data != null ? data.toSafeString() : null;
3351
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003353 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003354 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003355 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003356 }
3357
3358 /**
3359 * Make sure the given activity matches the current configuration. Returns
3360 * false if the activity had to be destroyed. Returns true if the
3361 * configuration is the same, or the activity will remain running as-is
3362 * for whatever reason. Ensures the HistoryRecord is updated with the
3363 * correct configuration and all other bookkeeping is handled.
3364 */
3365 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3366 int globalChanges) {
3367 if (mConfigWillChange) {
3368 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3369 "Skipping config check (will change): " + r);
3370 return true;
3371 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003373 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3374 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003375
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003376 // Short circuit: if the two configurations are the exact same
3377 // object (the common case), then there is nothing to do.
3378 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003379 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003380 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3381 "Configuration unchanged in " + r);
3382 return true;
3383 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003385 // We don't worry about activities that are finishing.
3386 if (r.finishing) {
3387 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3388 "Configuration doesn't matter in finishing " + r);
3389 r.stopFreezingScreenLocked(false);
3390 return true;
3391 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003392
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003393 // Okay we now are going to make this activity have the new config.
3394 // But then we need to figure out how it needs to deal with that.
3395 Configuration oldConfig = r.configuration;
3396 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003397
3398 // Determine what has changed. May be nothing, if this is a config
3399 // that has come back from the app after going idle. In that case
3400 // we just want to leave the official config object now in the
3401 // activity and do nothing else.
3402 final int changes = oldConfig.diff(newConfig);
3403 if (changes == 0 && !r.forceNewConfig) {
3404 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3405 "Configuration no differences in " + r);
3406 return true;
3407 }
3408
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003409 // If the activity isn't currently running, just leave the new
3410 // configuration and it will pick that up next time it starts.
3411 if (r.app == null || r.app.thread == null) {
3412 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3413 "Configuration doesn't matter not running " + r);
3414 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003415 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003416 return true;
3417 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003418
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003419 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003420 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3421 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3422 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003423 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003424 + ", newConfig=" + newConfig);
3425 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003426 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003427 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3428 r.configChangeFlags |= changes;
3429 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003430 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003431 if (r.app == null || r.app.thread == null) {
3432 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003433 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003434 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003435 } else if (r.state == ActivityState.PAUSING) {
3436 // A little annoying: we are waiting for this activity to
3437 // finish pausing. Let's not do anything now, but just
3438 // flag that it needs to be restarted when done pausing.
3439 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003440 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003441 r.configDestroy = true;
3442 return true;
3443 } else if (r.state == ActivityState.RESUMED) {
3444 // Try to optimize this case: the configuration is changing
3445 // and we need to restart the top, resumed activity.
3446 // Instead of doing the normal handshaking, just say
3447 // "restart!".
3448 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003449 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003450 relaunchActivityLocked(r, r.configChangeFlags, true);
3451 r.configChangeFlags = 0;
3452 } else {
3453 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003454 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003455 relaunchActivityLocked(r, r.configChangeFlags, false);
3456 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003458
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003459 // All done... tell the caller we weren't able to keep this
3460 // activity around.
3461 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003462 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003463
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003464 // Default case: the activity can handle this new configuration, so
3465 // hand it over. Note that we don't need to give it the new
3466 // configuration, since we always send configuration changes to all
3467 // process when they happen so it can just use whatever configuration
3468 // it last got.
3469 if (r.app != null && r.app.thread != null) {
3470 try {
3471 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003472 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 } catch (RemoteException e) {
3474 // If process died, whatever.
3475 }
3476 }
3477 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003478
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479 return true;
3480 }
3481
Craig Mautnerc8143c62013-09-03 12:15:57 -07003482 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003483 int changes, boolean andResume) {
3484 List<ResultInfo> results = null;
3485 List<Intent> newIntents = null;
3486 if (andResume) {
3487 results = r.results;
3488 newIntents = r.newIntents;
3489 }
3490 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3491 + " with results=" + results + " newIntents=" + newIntents
3492 + " andResume=" + andResume);
3493 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003494 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003496
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003497 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003498
Craig Mautner34b73df2014-01-12 21:11:08 -08003499 mStackSupervisor.removeChildActivityContainers(r);
3500
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003501 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003502 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3503 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3504 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003505 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003506 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003507 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 // Note: don't need to call pauseIfSleepingLocked() here, because
3509 // the caller will only pass in 'andResume' if this activity is
3510 // currently resumed, which implies we aren't sleeping.
3511 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003512 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003513 }
3514
3515 if (andResume) {
3516 r.results = null;
3517 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003518 r.state = ActivityState.RESUMED;
3519 } else {
3520 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3521 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 }
3523
3524 return true;
3525 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003526
3527 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003528 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3529 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3530 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3531 final ActivityRecord r = activities.get(activityNdx);
3532 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003533 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003534 }
3535 if (r.fullscreen && !r.finishing) {
3536 return false;
3537 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003538 }
3539 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003540 final ActivityRecord r = ActivityRecord.forToken(token);
3541 if (r == null) {
3542 return false;
3543 }
3544 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3545 + " would have returned true for r=" + r);
3546 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003547 }
3548
3549 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003550 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3551 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3552 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3553 final ActivityRecord r = activities.get(activityNdx);
3554 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003555 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003556 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003557 }
3558 }
3559 }
3560
3561 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3562 boolean didSomething = false;
3563 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003564 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3565 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3566 int numActivities = activities.size();
3567 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3568 ActivityRecord r = activities.get(activityNdx);
3569 final boolean samePackage = r.packageName.equals(name)
3570 || (name == null && r.userId == userId);
3571 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3572 && (samePackage || r.task == lastTask)
3573 && (r.app == null || evenPersistent || !r.app.persistent)) {
3574 if (!doit) {
3575 if (r.finishing) {
3576 // If this activity is just finishing, then it is not
3577 // interesting as far as something to stop.
3578 continue;
3579 }
3580 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003581 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003582 didSomething = true;
3583 Slog.i(TAG, " Force finishing activity " + r);
3584 if (samePackage) {
3585 if (r.app != null) {
3586 r.app.removed = true;
3587 }
3588 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003589 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003590 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003591 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3592 true)) {
3593 // r has been deleted from mActivities, accommodate.
3594 --numActivities;
3595 --activityNdx;
3596 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003597 }
3598 }
3599 }
3600 return didSomething;
3601 }
3602
Dianne Hackborn09233282014-04-30 11:33:59 -07003603 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003604 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003605 final TaskRecord task = mTaskHistory.get(taskNdx);
3606 ActivityRecord r = null;
3607 ActivityRecord top = null;
3608 int numActivities = 0;
3609 int numRunning = 0;
3610 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003611 if (activities.isEmpty()) {
3612 continue;
3613 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003614 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3615 continue;
3616 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003617 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3618 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003619
Craig Mautneraab647e2013-02-28 16:31:36 -08003620 // Initialize state for next task if needed.
3621 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3622 top = r;
3623 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003624 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003625
3626 // Add 'r' into the current task.
3627 numActivities++;
3628 if (r.app != null && r.app.thread != null) {
3629 numRunning++;
3630 }
3631
3632 if (localLOGV) Slog.v(
3633 TAG, r.intent.getComponent().flattenToShortString()
3634 + ": task=" + r.task);
3635 }
3636
3637 RunningTaskInfo ci = new RunningTaskInfo();
3638 ci.id = task.taskId;
3639 ci.baseActivity = r.intent.getComponent();
3640 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003641 ci.lastActiveTime = task.lastActiveTime;
3642
Craig Mautneraab647e2013-02-28 16:31:36 -08003643 if (top.thumbHolder != null) {
3644 ci.description = top.thumbHolder.lastDescription;
3645 }
3646 ci.numActivities = numActivities;
3647 ci.numRunning = numRunning;
3648 //System.out.println(
3649 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003650 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003651 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003652 }
3653
3654 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003655 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003656 if (DEBUG_SWITCH) Slog.d(
3657 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003658 if (top >= 0) {
3659 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3660 int activityTop = activities.size() - 1;
3661 if (activityTop > 0) {
3662 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3663 "unhandled-back", true);
3664 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003665 }
3666 }
3667
Craig Mautner6b74cb52013-09-27 17:02:21 -07003668 /**
3669 * Reset local parameters because an app's activity died.
3670 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003671 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003672 */
3673 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003674 if (mPausingActivity != null && mPausingActivity.app == app) {
3675 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3676 "App died while pausing: " + mPausingActivity);
3677 mPausingActivity = null;
3678 }
3679 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3680 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003681 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003682 }
3683
Craig Mautner19091252013-10-05 00:03:53 -07003684 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003685 }
3686
Craig Mautnercae015f2013-02-08 14:31:27 -08003687 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003688 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3689 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3690 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3691 final ActivityRecord r = activities.get(activityNdx);
3692 if (r.app == app) {
3693 Slog.w(TAG, " Force finishing activity "
3694 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003695 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003696 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003697 }
3698 }
3699 }
3700
Dianne Hackborn390517b2013-05-30 15:03:32 -07003701 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003702 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003703 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003704 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3705 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003706 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3707 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003708 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003709 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003710 if (printed) {
3711 header = null;
3712 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003713 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003714 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003715 }
3716
3717 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3718 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3719
3720 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003721 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3722 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003723 }
3724 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003725 final int top = mTaskHistory.size() - 1;
3726 if (top >= 0) {
3727 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3728 int listTop = list.size() - 1;
3729 if (listTop >= 0) {
3730 activities.add(list.get(listTop));
3731 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003732 }
3733 } else {
3734 ItemMatcher matcher = new ItemMatcher();
3735 matcher.build(name);
3736
Craig Mautneraab647e2013-02-28 16:31:36 -08003737 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3738 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3739 if (matcher.match(r1, r1.intent.getComponent())) {
3740 activities.add(r1);
3741 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003742 }
3743 }
3744 }
3745
3746 return activities;
3747 }
3748
3749 ActivityRecord restartPackage(String packageName) {
3750 ActivityRecord starting = topRunningActivityLocked(null);
3751
3752 // All activities that came from the package must be
3753 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003754 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3755 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3756 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3757 final ActivityRecord a = activities.get(activityNdx);
3758 if (a.info.packageName.equals(packageName)) {
3759 a.forceNewConfig = true;
3760 if (starting != null && a == starting && a.visible) {
3761 a.startFreezingScreenLocked(starting.app,
3762 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3763 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003764 }
3765 }
3766 }
3767
3768 return starting;
3769 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003770
Craig Mautner41db4a72014-05-07 17:20:56 -07003771 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003772 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003773 mWindowManager.removeTask(task.taskId);
3774 final ActivityRecord r = mResumedActivity;
3775 if (r != null && r.task == task) {
3776 mResumedActivity = null;
3777 }
3778
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003779 final int taskNdx = mTaskHistory.indexOf(task);
3780 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner719e6212014-05-29 16:49:41 +00003781 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3782 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003783 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003784 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003785 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07003786
3787 if (task.mActivities.isEmpty()) {
3788 final boolean isVoiceSession = task.voiceSession != null;
3789 if (isVoiceSession) {
3790 try {
3791 task.voiceSession.taskFinished(task.intent, task.taskId);
3792 } catch (RemoteException e) {
3793 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003794 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003795 if (task.autoRemoveFromRecents() || isVoiceSession) {
3796 // Task creator asked to remove this when done, or this task was a voice
3797 // interaction, so it should not remain on the recent tasks list.
3798 mService.mRecentTasks.remove(task);
3799 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003800 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003801
3802 if (mTaskHistory.isEmpty()) {
3803 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3804 if (isOnHomeDisplay()) {
3805 mStackSupervisor.moveHomeStack(!isHomeStack());
3806 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003807 if (mStacks != null) {
3808 mStacks.remove(this);
3809 mStacks.add(0, this);
3810 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003811 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08003812 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003813 }
3814
Dianne Hackborn91097de2014-04-04 18:02:06 -07003815 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
3816 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
3817 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07003818 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
3819 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003820 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003821 return task;
3822 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003823
3824 ArrayList<TaskRecord> getAllTasks() {
3825 return new ArrayList<TaskRecord>(mTaskHistory);
3826 }
3827
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003828 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003829 task.stack = this;
3830 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003831 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003832 } else {
3833 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003834 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003835 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003836 if (!moving && task.voiceSession != null) {
3837 try {
3838 task.voiceSession.taskStarted(task.intent, task.taskId);
3839 } catch (RemoteException e) {
3840 }
3841 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003842 }
3843
3844 public int getStackId() {
3845 return mStackId;
3846 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003847
3848 @Override
3849 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003850 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3851 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003852 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003853}