blob: fe2a473e3173aecac5b5091e99a6ec66818f1915 [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 Mautnere3a00d72014-04-16 08:31:19 -07001069 private void setVisibile(ActivityRecord r, boolean visible) {
1070 r.visible = visible;
1071 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001072 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001073 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001074 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001075 container.setVisible(visible);
1076 }
1077 }
1078
Jose Lima7ba71252014-04-30 12:59:27 -07001079 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1080 // If so, this stack is hidden, otherwise it is visible.
1081 private boolean isStackVisible() {
1082 if (!isAttached()) {
1083 return false;
1084 }
1085
1086 if (mStackSupervisor.isFrontStack(this)) {
1087 return true;
1088 }
1089
Jose Lima729cb232014-05-05 15:59:40 -07001090 /**
1091 * Start at the task above this one and go up, looking for a visible
1092 * fullscreen activity, or a translucent activity that requested the
1093 * wallpaper to be shown behind it.
1094 */
Jose Lima7ba71252014-04-30 12:59:27 -07001095 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1096 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1097 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner719e6212014-05-29 16:49:41 +00001098 final ArrayList<ActivityRecord> activities = tasks.get(taskNdx).mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001099 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1100 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001101
1102 // Conditions for an activity to obscure the stack we're
1103 // examining:
1104 // 1. Not Finishing AND Visible AND:
1105 // 2. Either:
1106 // - Full Screen Activity OR
1107 // - On top of Home and our stack is NOT home
1108 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner719e6212014-05-29 16:49:41 +00001109 (!isHomeStack() && r.frontOfTask && tasks.get(taskNdx).mOnTopOfHome))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001110 return false;
1111 }
1112 }
1113 }
1114 }
1115
1116 return true;
1117 }
1118
Jose Lima729cb232014-05-05 15:59:40 -07001119 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1120 ActivityRecord r = topRunningActivityLocked(null);
1121 if (r != null) {
1122 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1123 }
1124 }
1125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126 /**
1127 * Make sure that all activities that need to be visible (that is, they
1128 * currently can be seen by the user) actually are.
1129 */
Jose Lima729cb232014-05-05 15:59:40 -07001130 final void ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1131 String onlyThisProcess, int configChanges) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001132 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001133 TAG, "ensureActivitiesVisible behind " + top
1134 + " configChanges=0x" + Integer.toHexString(configChanges));
1135
Craig Mautner5eda9b32013-07-02 11:58:16 -07001136 if (mTranslucentActivityWaiting != top) {
1137 mUndrawnActivitiesBelowTopTranslucent.clear();
1138 if (mTranslucentActivityWaiting != null) {
1139 // Call the callback with a timeout indication.
1140 notifyActivityDrawnLocked(null);
1141 mTranslucentActivityWaiting = null;
1142 }
1143 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1144 }
1145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 // If the top activity is not fullscreen, then we need to
1147 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001148 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001149 boolean behindFullscreen = !isStackVisible();
1150
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001151 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001152 final TaskRecord task = mTaskHistory.get(taskNdx);
1153 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001154 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1155 final ActivityRecord r = activities.get(activityNdx);
1156 if (r.finishing) {
1157 continue;
1158 }
1159 if (aboveTop && r != top) {
1160 continue;
1161 }
1162 aboveTop = false;
1163 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001165 TAG, "Make visible? " + r + " finishing=" + r.finishing
1166 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001167
Craig Mautnerd44711d2013-02-23 11:24:36 -08001168 final boolean doThisProcess = onlyThisProcess == null
1169 || onlyThisProcess.equals(r.processName);
1170
1171 // First: if this is not the current activity being started, make
1172 // sure it matches the current configuration.
1173 if (r != starting && doThisProcess) {
1174 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001176
1177 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001178 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001179 // This activity needs to be visible, but isn't even
1180 // running... get it started, but don't resume it
1181 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001182 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001183 if (r != starting) {
1184 r.startFreezingScreenLocked(r.app, configChanges);
1185 }
1186 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001187 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001188 TAG, "Starting and making visible: " + r);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001189 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001190 }
1191 if (r != starting) {
George Mount2c92c972014-03-20 09:38:23 -07001192 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001193 }
1194 }
1195
1196 } else if (r.visible) {
1197 // If this activity is already visible, then there is nothing
1198 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001199 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001200 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001201
1202 } else if (onlyThisProcess == null) {
1203 // This activity is not currently visible, but is running.
1204 // Tell it to become visible.
1205 r.visible = true;
1206 if (r.state != ActivityState.RESUMED && r != starting) {
1207 // If this activity is paused, tell it
1208 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001209 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001210 TAG, "Making visible and scheduling visibility: " + r);
1211 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001212 if (mTranslucentActivityWaiting != null) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001213 r.updateOptionsLocked(mReturningActivityOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001214 mUndrawnActivitiesBelowTopTranslucent.add(r);
1215 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001216 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001217 r.sleeping = false;
1218 r.app.pendingUiClean = true;
1219 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1220 r.stopFreezingScreenLocked(false);
1221 } catch (Exception e) {
1222 // Just skip on any failure; we'll make it
1223 // visible when it next restarts.
1224 Slog.w(TAG, "Exception thrown making visibile: "
1225 + r.intent.getComponent(), e);
1226 }
1227 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001229
Craig Mautnerd44711d2013-02-23 11:24:36 -08001230 // Aggregate current change flags.
1231 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001232
Craig Mautnerd44711d2013-02-23 11:24:36 -08001233 if (r.fullscreen) {
1234 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001235 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1236 behindFullscreen = true;
Craig Mautner719e6212014-05-29 16:49:41 +00001237 } else if (!isHomeStack() && r.frontOfTask && task.mOnTopOfHome) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001238 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001239 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001240 }
1241 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001242 if (DEBUG_VISBILITY) Slog.v(
1243 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1244 + " state=" + r.state
1245 + " behindFullscreen=" + behindFullscreen);
1246 // Now for any activities that aren't visible to the user, make
1247 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001249 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001250 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001251 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001252 switch (r.state) {
1253 case STOPPING:
1254 case STOPPED:
1255 if (r.app != null && r.app.thread != null) {
1256 if (DEBUG_VISBILITY) Slog.v(
1257 TAG, "Scheduling invisibility: " + r);
1258 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1259 }
1260 break;
1261
1262 case INITIALIZING:
1263 case RESUMED:
1264 case PAUSING:
1265 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001266 // This case created for transitioning activities from
1267 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001268 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1269 mStackSupervisor.mStoppingActivities.add(r);
1270 }
1271 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001272 break;
1273
1274 default:
1275 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001276 }
1277 } catch (Exception e) {
1278 // Just skip on any failure; we'll make it
1279 // visible when it next restarts.
1280 Slog.w(TAG, "Exception thrown making hidden: "
1281 + r.intent.getComponent(), e);
1282 }
1283 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001284 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 }
1287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001288 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001289 }
Craig Mautner58547802013-03-05 08:23:53 -08001290
Craig Mautner233ceee2014-05-09 17:05:11 -07001291 void convertToTranslucent(ActivityRecord r, ActivityOptions options) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001292 mTranslucentActivityWaiting = r;
1293 mUndrawnActivitiesBelowTopTranslucent.clear();
Craig Mautner233ceee2014-05-09 17:05:11 -07001294 mReturningActivityOptions = options;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001295 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1296 }
1297
1298 /**
1299 * Called as activities below the top translucent activity are redrawn. When the last one is
1300 * redrawn notify the top activity by calling
1301 * {@link Activity#onTranslucentConversionComplete}.
1302 *
1303 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1304 * occurred and the activity will be notified immediately.
1305 */
1306 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001307 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001308 if ((r == null)
1309 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1310 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1311 // The last undrawn activity below the top has just been drawn. If there is an
1312 // opaque activity at the top, notify it that it can become translucent safely now.
1313 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1314 mTranslucentActivityWaiting = null;
1315 mUndrawnActivitiesBelowTopTranslucent.clear();
1316 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1317
Craig Mautner71dd1b62014-02-18 15:48:52 -08001318 if (waitingActivity != null) {
1319 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1320 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1321 try {
1322 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1323 waitingActivity.appToken, r != null);
1324 } catch (RemoteException e) {
1325 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001326 }
1327 }
1328 }
1329 }
1330
Craig Mautnera61bc652013-10-28 15:43:18 -07001331 /** If any activities below the top running one are in the INITIALIZING state and they have a
1332 * starting window displayed then remove that starting window. It is possible that the activity
1333 * in this state will never resumed in which case that starting window will be orphaned. */
1334 void cancelInitializingActivities() {
1335 final ActivityRecord topActivity = topRunningActivityLocked(null);
1336 boolean aboveTop = true;
1337 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1338 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1339 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1340 final ActivityRecord r = activities.get(activityNdx);
1341 if (aboveTop) {
1342 if (r == topActivity) {
1343 aboveTop = false;
1344 }
1345 continue;
1346 }
1347
1348 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1349 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1350 r.mStartingWindowShown = false;
1351 mWindowManager.removeAppStartingWindow(r.appToken);
1352 }
1353 }
1354 }
1355 }
1356
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 /**
1358 * Ensure that the top activity in the stack is resumed.
1359 *
1360 * @param prev The previously resumed activity, for when in the process
1361 * of pausing; can be null to call from elsewhere.
1362 *
1363 * @return Returns true if something is being resumed, or false if
1364 * nothing happened.
1365 */
1366 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001367 return resumeTopActivityLocked(prev, null);
1368 }
1369
1370 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001371 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1372
Craig Mautnerdf88d732014-01-27 09:21:32 -08001373 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001374 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001375 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001376 // Do not resume this stack if its parent is not resumed.
1377 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1378 return false;
1379 }
1380
Craig Mautnera61bc652013-10-28 15:43:18 -07001381 cancelInitializingActivities();
1382
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001383 // Find the first activity that is not finishing.
1384 ActivityRecord next = topRunningActivityLocked(null);
1385
1386 // Remember how we'll process this pause/resume situation, and ensure
1387 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001388 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1389 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390
1391 if (next == null) {
1392 // There are no more activities! Let's just start up the
1393 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001394 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001395 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001396 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001397 // Only resume home if on home display
Craig Mautner719e6212014-05-29 16:49:41 +00001398 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001399 }
1400
1401 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001402
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001403 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001404 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1405 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001406 // Make sure we have executed any pending transitions, since there
1407 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001408 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001410 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001411 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001412 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 return false;
1414 }
1415
Craig Mautner525f3d92013-05-07 14:01:50 -07001416 final TaskRecord nextTask = next.task;
Craig Mautner719e6212014-05-29 16:49:41 +00001417 final TaskRecord prevTask = prev != null ? prev.task : null;
bulicccd230712014-05-12 14:34:50 -07001418 if (prevTask != null && prevTask.stack == this &&
Craig Mautner719e6212014-05-29 16:49:41 +00001419 prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001420 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001421 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001422 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001423 } else if (prevTask != topTask()) {
Craig Mautner719e6212014-05-29 16:49:41 +00001424 // This task is going away but it was supposed to return to the home task.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001425 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001426 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner719e6212014-05-29 16:49:41 +00001427 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001428 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001429 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1430 "resumeTopActivityLocked: Launching home next");
1431 // Only resume home if on home display
Craig Mautner719e6212014-05-29 16:49:41 +00001432 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001433 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001434 }
1435
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 // If we are sleeping, and there is no resumed activity, and the top
1437 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001438 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001439 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001440 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 // Make sure we have executed any pending transitions, since there
1442 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001443 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001445 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001446 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001447 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001448 return false;
1449 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001450
1451 // Make sure that the user who owns this activity is started. If not,
1452 // we will just leave it as is because someone should be bringing
1453 // another user's activities to the top of the stack.
1454 if (mService.mStartedUsers.get(next.userId) == null) {
1455 Slog.w(TAG, "Skipping resume of top activity " + next
1456 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001457 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001458 return false;
1459 }
1460
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 // The activity may be waiting for stop, but that is no longer
1462 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001463 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001464 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001465 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001466 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001467
1468 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1469
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001470 // If we are currently pausing an activity, then don't do anything
1471 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001472 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001473 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1474 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001475 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001476 return false;
1477 }
1478
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001479 // Okay we are now going to start a switch, to 'next'. We may first
1480 // have to pause the current activity, but this is an important point
1481 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001482 // XXX "App Redirected" dialog is getting too many false positives
1483 // at this point, so turn off for now.
1484 if (false) {
1485 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1486 long now = SystemClock.uptimeMillis();
1487 final boolean inTime = mLastStartedActivity.startTime != 0
1488 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1489 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1490 final int nextUid = next.info.applicationInfo.uid;
1491 if (inTime && lastUid != nextUid
1492 && lastUid != next.launchedFromUid
1493 && mService.checkPermission(
1494 android.Manifest.permission.STOP_APP_SWITCHES,
1495 -1, next.launchedFromUid)
1496 != PackageManager.PERMISSION_GRANTED) {
1497 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1498 } else {
1499 next.startTime = now;
1500 mLastStartedActivity = next;
1501 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001502 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001503 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001504 mLastStartedActivity = next;
1505 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001506 }
Craig Mautner58547802013-03-05 08:23:53 -08001507
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 // We need to start pausing the current activity so the top one
1509 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001510 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001511 if (mResumedActivity != null) {
1512 pausing = true;
1513 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001514 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001515 }
1516 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001517 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1518 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001519 // At this point we want to put the upcoming activity's process
1520 // at the top of the LRU list, since we know we will be needing it
1521 // very soon and it would be a waste to let it get killed if it
1522 // happens to be sitting towards the end.
1523 if (next.app != null && next.app.thread != null) {
1524 // No reason to do full oom adj update here; we'll let that
1525 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001526 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001527 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001528 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 return true;
1530 }
1531
Christopher Tated3f175c2012-06-14 14:16:54 -07001532 // If the most recent activity was noHistory but was only stopped rather
1533 // than stopped+finished because the device went to sleep, we need to make
1534 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001535 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001536 !mLastNoHistoryActivity.finishing) {
1537 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1538 " on new resume");
1539 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1540 null, "no-history", false);
1541 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001542 }
1543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 if (prev != null && prev != next) {
1545 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1546 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001547 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001548 if (DEBUG_SWITCH) Slog.v(
1549 TAG, "Resuming top, waiting visible to hide: " + prev);
1550 } else {
1551 // The next activity is already visible, so hide the previous
1552 // activity's windows right now so we can show the new one ASAP.
1553 // We only do this if the previous is finishing, which should mean
1554 // it is on top of the one being resumed so hiding it quickly
1555 // is good. Otherwise, we want to do the normal route of allowing
1556 // the resumed activity to be shown so we can decide if the
1557 // previous should actually be hidden depending on whether the
1558 // new one is found to be full-screen or not.
1559 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001560 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1562 + prev + ", waitingVisible="
1563 + (prev != null ? prev.waitingVisible : null)
1564 + ", nowVisible=" + next.nowVisible);
1565 } else {
1566 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1567 + prev + ", waitingVisible="
1568 + (prev != null ? prev.waitingVisible : null)
1569 + ", nowVisible=" + next.nowVisible);
1570 }
1571 }
1572 }
1573
Dianne Hackborne7f97212011-02-24 14:40:20 -08001574 // Launching this app's activity, make sure the app is no longer
1575 // considered stopped.
1576 try {
1577 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001578 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001579 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001580 } catch (IllegalArgumentException e) {
1581 Slog.w(TAG, "Failed trying to unstop package "
1582 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001583 }
1584
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 // We are starting up the next activity, so tell the window manager
1586 // that the previous one will be hidden soon. This way it can know
1587 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001588 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 if (prev != null) {
1590 if (prev.finishing) {
1591 if (DEBUG_TRANSITION) Slog.v(TAG,
1592 "Prepare close transition: prev=" + prev);
1593 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001594 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001595 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001597 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001598 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1599 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001600 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001601 mWindowManager.setAppWillBeHidden(prev.appToken);
1602 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001604 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001605 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001606 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001607 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001609 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001610 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1611 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 }
1613 }
1614 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001615 mWindowManager.setAppWillBeHidden(prev.appToken);
1616 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001617 }
Craig Mautner967212c2013-04-13 21:10:58 -07001618 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001619 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001621 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001622 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001624 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001625 }
1626 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001627
1628 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001629 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001630 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1631 if (opts != null) {
1632 resumeAnimOptions = opts.toBundle();
1633 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001634 next.applyOptionsLocked();
1635 } else {
1636 next.clearOptionsLocked();
1637 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638
Craig Mautnercf910b02013-04-23 11:23:27 -07001639 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 if (next.app != null && next.app.thread != null) {
1641 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1642
1643 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001644 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001645
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001646 // schedule launch ticks to collect information about slow apps.
1647 next.startLaunchTickingLocked();
1648
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001649 ActivityRecord lastResumedActivity =
1650 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 ActivityState lastState = next.state;
1652
1653 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001654
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001655 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001656 next.state = ActivityState.RESUMED;
1657 mResumedActivity = next;
1658 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001659 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001660 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001662 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663
1664 // Have the window manager re-evaluate the orientation of
1665 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001666 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001667 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001668 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001669 mService.mConfiguration,
1670 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1671 if (config != null) {
1672 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001674 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001676
Craig Mautner525f3d92013-05-07 14:01:50 -07001677 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001678 // The configuration update wasn't able to keep the existing
1679 // instance of the activity, and instead started a new one.
1680 // We should be all done, but let's just make sure our activity
1681 // is still at the top and schedule another run if something
1682 // weird happened.
1683 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001684 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 "Activity config changed during resume: " + next
1686 + ", new next: " + nextNext);
1687 if (nextNext != next) {
1688 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001689 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001691 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001692 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001693 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001694 return true;
1695 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001696 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001697 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 }
Craig Mautner58547802013-03-05 08:23:53 -08001699
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 try {
1701 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001702 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 if (a != null) {
1704 final int N = a.size();
1705 if (!next.finishing && N > 0) {
1706 if (DEBUG_RESULTS) Slog.v(
1707 TAG, "Delivering results to " + next
1708 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001709 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 }
1711 }
1712
1713 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001714 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 }
1716
1717 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001718 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001720
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001721 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001722 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001723 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001724 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001725 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001726 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001727 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001728
Craig Mautner0eea92c2013-05-16 13:35:39 -07001729 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730
Craig Mautnerac6f8432013-07-17 13:24:59 -07001731 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 } catch (Exception e) {
1733 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001734 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1735 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001737 if (lastStack != null) {
1738 lastStack.mResumedActivity = lastResumedActivity;
1739 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 Slog.i(TAG, "Restarting because process died: " + next);
1741 if (!next.hasBeenLaunched) {
1742 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001743 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1744 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001745 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001746 next.appToken, next.packageName, next.theme,
1747 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001748 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1749 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 }
George Mount2c92c972014-03-20 09:38:23 -07001751 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001752 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 return true;
1754 }
1755
1756 // From this point on, if something goes wrong there is no way
1757 // to recover the activity.
1758 try {
1759 next.visible = true;
1760 completeResumeLocked(next);
1761 } catch (Exception e) {
1762 // If any exception gets thrown, toss away this
1763 // activity and try the next one.
1764 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001765 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001766 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001767 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768 return true;
1769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 next.stopped = false;
1771
1772 } else {
1773 // Whoops, need to restart this activity!
1774 if (!next.hasBeenLaunched) {
1775 next.hasBeenLaunched = true;
1776 } else {
1777 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001778 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001779 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001780 mService.compatibilityInfoForPackageLocked(
1781 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001783 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001784 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 }
1786 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1787 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001788 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001789 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 }
1791
Craig Mautnercf910b02013-04-23 11:23:27 -07001792 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 return true;
1794 }
1795
Craig Mautnerac6f8432013-07-17 13:24:59 -07001796 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001797 // If this is being moved to the top by another activity or being launched from the home
1798 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001799 if (isOnHomeDisplay()) {
1800 ActivityStack lastStack = mStackSupervisor.getLastStack();
1801 final boolean fromHome = lastStack.isHomeStack();
1802 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner719e6212014-05-29 16:49:41 +00001803 task.mOnTopOfHome = fromHome;
Craig Mautnere0a38842013-12-16 16:14:02 -08001804 }
1805 } else {
Craig Mautner719e6212014-05-29 16:49:41 +00001806 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001807 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001808
Craig Mautnerac6f8432013-07-17 13:24:59 -07001809 mTaskHistory.remove(task);
1810 // Now put task at top.
1811 int stackNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001812 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001813 // Put non-current user tasks below current user tasks.
1814 while (--stackNdx >= 0) {
Kenny Guy2a764942014-04-02 13:29:20 +01001815 if (!isCurrentProfileLocked(mTaskHistory.get(stackNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001816 break;
1817 }
1818 }
1819 ++stackNdx;
1820 }
1821 mTaskHistory.add(stackNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001822 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001823 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001824
Craig Mautner8849a5e2013-04-02 16:41:03 -07001825 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001826 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001827 TaskRecord rTask = r.task;
1828 final int taskId = rTask.taskId;
1829 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001830 // Last activity in task had been removed or ActivityManagerService is reusing task.
1831 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001832 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001833 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001834 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001835 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001836 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837 if (!newTask) {
1838 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001839 boolean startIt = true;
1840 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1841 task = mTaskHistory.get(taskNdx);
1842 if (task == r.task) {
1843 // Here it is! Now, if this is not yet visible to the
1844 // user, then just add it without starting; it will
1845 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001846 if (!startIt) {
1847 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1848 + task, new RuntimeException("here").fillInStackTrace());
1849 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001850 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001851 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1852 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001853 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001854 r.userId, r.info.configChanges, task.voiceSession != null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001855 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001856 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001858 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001859 return;
1860 }
1861 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001862 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001863 startIt = false;
1864 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001865 }
1866 }
1867
1868 // Place a new activity at top of stack, so it is next to interact
1869 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001870
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 // If we are not placing the new activity frontmost, we do not want
1872 // to deliver the onUserLeaving callback to the actual frontmost
1873 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001874 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001875 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001876 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1877 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001878 }
Craig Mautner70a86932013-02-28 22:37:44 -08001879
1880 task = r.task;
1881
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001882 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001883 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001884 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001885 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001886 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001887
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001888 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001889 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001890 // We want to show the starting preview window if we are
1891 // switching to a new task, or the next activity's process is
1892 // not currently running.
1893 boolean showStartingIcon = newTask;
1894 ProcessRecord proc = r.app;
1895 if (proc == null) {
1896 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1897 }
1898 if (proc == null || proc.thread == null) {
1899 showStartingIcon = true;
1900 }
1901 if (DEBUG_TRANSITION) Slog.v(TAG,
1902 "Prepare open transition: starting " + r);
1903 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001904 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001907 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001908 ? AppTransition.TRANSIT_TASK_OPEN
1909 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001910 mNoAnimActivities.remove(r);
1911 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001912 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001913 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001914 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001915 r.info.configChanges, task.voiceSession != null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001916 boolean doShow = true;
1917 if (newTask) {
1918 // Even though this activity is starting fresh, we still need
1919 // to reset it to make sure we apply affinities to move any
1920 // existing activities from other tasks in to it.
1921 // If the caller has requested that the target task be
1922 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001923 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001924 resetTaskIfNeededLocked(r, r);
1925 doShow = topRunningNonDelayedActivityLocked(null) == r;
1926 }
1927 }
1928 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1929 // Figure out if we are transitioning from another activity that is
1930 // "has the same starting icon" as the next one. This allows the
1931 // window manager to keep the previous window it had previously
1932 // created, if it still had one.
1933 ActivityRecord prev = mResumedActivity;
1934 if (prev != null) {
1935 // We don't want to reuse the previous starting preview if:
1936 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001937 if (prev.task != r.task) {
1938 prev = null;
1939 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001940 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001941 else if (prev.nowVisible) {
1942 prev = null;
1943 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001944 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001945 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001946 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001947 mService.compatibilityInfoForPackageLocked(
1948 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001949 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001950 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07001951 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001952 }
1953 } else {
1954 // If this is the first activity, don't do any fancy animations,
1955 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001956 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001957 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001958 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001959 r.info.configChanges, task.voiceSession != null);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001960 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07001961 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001962 }
1963 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001964 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001965 }
1966
1967 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001968 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001969 }
1970 }
1971
Dianne Hackbornbe707852011-11-11 14:32:10 -08001972 final void validateAppTokensLocked() {
1973 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001974 mValidateAppTokens.ensureCapacity(numActivities());
1975 final int numTasks = mTaskHistory.size();
1976 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1977 TaskRecord task = mTaskHistory.get(taskNdx);
1978 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001979 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001980 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001981 }
Craig Mautner000f0022013-02-26 15:04:29 -08001982 TaskGroup group = new TaskGroup();
1983 group.taskId = task.taskId;
1984 mValidateAppTokens.add(group);
1985 final int numActivities = activities.size();
1986 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1987 final ActivityRecord r = activities.get(activityNdx);
1988 group.tokens.add(r.appToken);
1989 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001990 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001992 }
1993
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001994 /**
1995 * Perform a reset of the given task, if needed as part of launching it.
1996 * Returns the new HistoryRecord at the top of the task.
1997 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001998 /**
1999 * Helper method for #resetTaskIfNeededLocked.
2000 * We are inside of the task being reset... we'll either finish this activity, push it out
2001 * for another task, or leave it as-is.
2002 * @param task The task containing the Activity (taskTop) that might be reset.
2003 * @param forceReset
2004 * @return An ActivityOptions that needs to be processed.
2005 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002006 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002007 ActivityOptions topOptions = null;
2008
2009 int replyChainEnd = -1;
2010 boolean canMoveOptions = true;
2011
2012 // We only do this for activities that are not the root of the task (since if we finish
2013 // the root, we may no longer have the task!).
2014 final ArrayList<ActivityRecord> activities = task.mActivities;
2015 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002016 final int rootActivityNdx = task.findEffectiveRootIndex();
2017 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002018 ActivityRecord target = activities.get(i);
2019
2020 final int flags = target.info.flags;
2021 final boolean finishOnTaskLaunch =
2022 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2023 final boolean allowTaskReparenting =
2024 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2025 final boolean clearWhenTaskReset =
2026 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2027
2028 if (!finishOnTaskLaunch
2029 && !clearWhenTaskReset
2030 && target.resultTo != null) {
2031 // If this activity is sending a reply to a previous
2032 // activity, we can't do anything with it now until
2033 // we reach the start of the reply chain.
2034 // XXX note that we are assuming the result is always
2035 // to the previous activity, which is almost always
2036 // the case but we really shouldn't count on.
2037 if (replyChainEnd < 0) {
2038 replyChainEnd = i;
2039 }
2040 } else if (!finishOnTaskLaunch
2041 && !clearWhenTaskReset
2042 && allowTaskReparenting
2043 && target.taskAffinity != null
2044 && !target.taskAffinity.equals(task.affinity)) {
2045 // If this activity has an affinity for another
2046 // task, then we need to move it out of here. We will
2047 // move it as far out of the way as possible, to the
2048 // bottom of the activity stack. This also keeps it
2049 // correctly ordered with any activities we previously
2050 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002051 final ThumbnailHolder newThumbHolder;
2052 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002053 final ActivityRecord bottom =
2054 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002055 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002056 if (bottom != null && target.taskAffinity != null
2057 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002058 // If the activity currently at the bottom has the
2059 // same task affinity as the one we are moving,
2060 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002061 targetTask = bottom.task;
2062 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002063 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002064 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002065 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002066 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002067 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002068 newThumbHolder = targetTask;
2069 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002070 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2071 + " out to new task " + target.task);
2072 }
2073
Craig Mautnerd00f4742014-03-12 14:17:26 -07002074 target.thumbHolder = newThumbHolder;
Craig Mautner329f4122013-11-07 09:10:42 -08002075
Craig Mautnere3a74d52013-02-22 14:14:58 -08002076 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002077 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002078
Craig Mautner525f3d92013-05-07 14:01:50 -07002079 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002080 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2081 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002082 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002083 if (p.finishing) {
2084 continue;
2085 }
2086
Craig Mautner525f3d92013-05-07 14:01:50 -07002087 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002088 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002089 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002090 topOptions = p.takeOptionsLocked();
2091 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002092 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002093 }
2094 }
2095 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002096 + task + " adding to task=" + targetTask
2097 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002098 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2099 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002100 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002101 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002102
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002103 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 }
2105
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002106 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002107 if (VALIDATE_TOKENS) {
2108 validateAppTokensLocked();
2109 }
2110
2111 replyChainEnd = -1;
2112 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2113 // If the activity should just be removed -- either
2114 // because it asks for it, or the task should be
2115 // cleared -- then finish it and anything that is
2116 // part of its reply chain.
2117 int end;
2118 if (clearWhenTaskReset) {
2119 // In this case, we want to finish this activity
2120 // and everything above it, so be sneaky and pretend
2121 // like these are all in the reply chain.
2122 end = numActivities - 1;
2123 } else if (replyChainEnd < 0) {
2124 end = i;
2125 } else {
2126 end = replyChainEnd;
2127 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002128 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002129 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002130 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002131 if (p.finishing) {
2132 continue;
2133 }
2134 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002135 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002136 topOptions = p.takeOptionsLocked();
2137 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002138 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002139 }
2140 }
Craig Mautner58547802013-03-05 08:23:53 -08002141 if (DEBUG_TASKS) Slog.w(TAG,
2142 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002143 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002144 end--;
2145 srcPos--;
2146 }
2147 }
2148 replyChainEnd = -1;
2149 } else {
2150 // If we were in the middle of a chain, well the
2151 // activity that started it all doesn't want anything
2152 // special, so leave it all as-is.
2153 replyChainEnd = -1;
2154 }
2155 }
2156
2157 return topOptions;
2158 }
2159
2160 /**
2161 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2162 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2163 * @param affinityTask The task we are looking for an affinity to.
2164 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2165 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2166 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2167 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002168 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002169 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002170 int replyChainEnd = -1;
2171 final int taskId = task.taskId;
2172 final String taskAffinity = task.affinity;
2173
2174 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2175 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002176 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2177
2178 // Do not operate on or below the effective root Activity.
2179 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002180 ActivityRecord target = activities.get(i);
2181
2182 final int flags = target.info.flags;
2183 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2184 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2185
2186 if (target.resultTo != null) {
2187 // If this activity is sending a reply to a previous
2188 // activity, we can't do anything with it now until
2189 // we reach the start of the reply chain.
2190 // XXX note that we are assuming the result is always
2191 // to the previous activity, which is almost always
2192 // the case but we really shouldn't count on.
2193 if (replyChainEnd < 0) {
2194 replyChainEnd = i;
2195 }
2196 } else if (topTaskIsHigher
2197 && allowTaskReparenting
2198 && taskAffinity != null
2199 && taskAffinity.equals(target.taskAffinity)) {
2200 // This activity has an affinity for our task. Either remove it if we are
2201 // clearing or move it over to our task. Note that
2202 // we currently punt on the case where we are resetting a
2203 // task that is not at the top but who has activities above
2204 // with an affinity to it... this is really not a normal
2205 // case, and we will need to later pull that task to the front
2206 // and usually at that point we will do the reset and pick
2207 // up those remaining activities. (This only happens if
2208 // someone starts an activity in a new task from an activity
2209 // in a task that is not currently on top.)
2210 if (forceReset || finishOnTaskLaunch) {
2211 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2212 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2213 for (int srcPos = start; srcPos >= i; --srcPos) {
2214 final ActivityRecord p = activities.get(srcPos);
2215 if (p.finishing) {
2216 continue;
2217 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002218 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002219 }
2220 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002221 if (taskInsertionPoint < 0) {
2222 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002223
Craig Mautner77878772013-03-04 19:46:24 -08002224 }
Craig Mautner77878772013-03-04 19:46:24 -08002225
2226 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2227 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2228 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2229 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002231 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002232 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002233
Craig Mautnere3a74d52013-02-22 14:14:58 -08002234 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2235 + " to stack at " + task,
2236 new RuntimeException("here").fillInStackTrace());
2237 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2238 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002239 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002240 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002241 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002242 if (VALIDATE_TOKENS) {
2243 validateAppTokensLocked();
2244 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002245
2246 // Now we've moved it in to place... but what if this is
2247 // a singleTop activity and we have put it on top of another
2248 // instance of the same activity? Then we drop the instance
2249 // below so it remains singleTop.
2250 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2251 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002252 int targetNdx = taskActivities.indexOf(target);
2253 if (targetNdx > 0) {
2254 ActivityRecord p = taskActivities.get(targetNdx - 1);
2255 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002256 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2257 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002258 }
2259 }
2260 }
2261 }
2262
2263 replyChainEnd = -1;
2264 }
2265 }
Craig Mautner77878772013-03-04 19:46:24 -08002266 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002267 }
2268
Craig Mautner8849a5e2013-04-02 16:41:03 -07002269 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002270 ActivityRecord newActivity) {
2271 boolean forceReset =
2272 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2273 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2274 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2275 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2276 forceReset = true;
2277 }
2278 }
2279
2280 final TaskRecord task = taskTop.task;
2281
2282 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2283 * for remaining tasks. Used for later tasks to reparent to task. */
2284 boolean taskFound = false;
2285
2286 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2287 ActivityOptions topOptions = null;
2288
Craig Mautner77878772013-03-04 19:46:24 -08002289 // Preserve the location for reparenting in the new task.
2290 int reparentInsertionPoint = -1;
2291
Craig Mautnere3a74d52013-02-22 14:14:58 -08002292 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2293 final TaskRecord targetTask = mTaskHistory.get(i);
2294
2295 if (targetTask == task) {
2296 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2297 taskFound = true;
2298 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002299 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2300 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002301 }
2302 }
2303
Craig Mautner70a86932013-02-28 22:37:44 -08002304 int taskNdx = mTaskHistory.indexOf(task);
2305 do {
2306 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2307 } while (taskTop == null && taskNdx >= 0);
2308
Craig Mautnere3a74d52013-02-22 14:14:58 -08002309 if (topOptions != null) {
2310 // If we got some ActivityOptions from an activity on top that
2311 // was removed from the task, propagate them to the new real top.
2312 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002313 taskTop.updateOptionsLocked(topOptions);
2314 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002315 topOptions.abort();
2316 }
2317 }
2318
2319 return taskTop;
2320 }
2321
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2323 String resultWho, int requestCode, int resultCode, Intent data) {
2324
2325 if (callingUid > 0) {
2326 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002327 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002328 }
2329
2330 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2331 + " : who=" + resultWho + " req=" + requestCode
2332 + " res=" + resultCode + " data=" + data);
2333 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2334 try {
2335 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2336 list.add(new ResultInfo(resultWho, requestCode,
2337 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002338 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 return;
2340 } catch (Exception e) {
2341 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2342 }
2343 }
2344
2345 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2346 }
2347
Craig Mautner04f0b702013-10-22 12:31:01 -07002348 private void adjustFocusedActivityLocked(ActivityRecord r) {
2349 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2350 ActivityRecord next = topRunningActivityLocked(null);
2351 if (next != r) {
2352 final TaskRecord task = r.task;
Craig Mautner719e6212014-05-29 16:49:41 +00002353 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2354 mStackSupervisor.moveHomeToTop();
Craig Mautner04f0b702013-10-22 12:31:01 -07002355 }
2356 }
Winson Chung648c83b2014-04-28 15:11:56 -07002357 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2358 if (top != null) {
2359 mService.setFocusedActivityLocked(top);
2360 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002361 }
2362 }
2363
Craig Mautnerf3333272013-04-22 10:55:53 -07002364 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2366 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2367 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2368 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002369 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002370 if (DEBUG_STATES) {
2371 Slog.d(TAG, "no-history finish of " + r);
2372 }
2373 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002374 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002375 } else {
2376 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2377 + " on stop because we're just sleeping");
2378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002380 }
2381
2382 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002383 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002384 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 try {
2386 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002387 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2388 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 r.state = ActivityState.STOPPING;
2390 if (DEBUG_VISBILITY) Slog.v(
2391 TAG, "Stopping visible=" + r.visible + " for " + r);
2392 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002393 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002395 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002396 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002397 r.setSleeping(true);
2398 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002399 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002400 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 } catch (Exception e) {
2402 // Maybe just ignore exceptions here... if the process
2403 // has crashed, our death notification will clean things
2404 // up.
2405 Slog.w(TAG, "Exception thrown during pause", e);
2406 // Just in case, assume it to be stopped.
2407 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002408 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 r.state = ActivityState.STOPPED;
2410 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002411 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002412 }
2413 }
2414 }
2415 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002416
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 /**
2418 * @return Returns true if the activity is being finished, false if for
2419 * some reason it is being left as-is.
2420 */
2421 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002422 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002423 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002424 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002425 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002426 + ", result=" + resultCode + ", data=" + resultData
2427 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002428 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 return false;
2430 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431
Craig Mautnerd44711d2013-02-23 11:24:36 -08002432 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 return true;
2434 }
2435
Craig Mautnerd2328952013-03-05 12:46:26 -08002436 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002437 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2438 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2439 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2440 ActivityRecord r = activities.get(activityNdx);
2441 if (r.resultTo == self && r.requestCode == requestCode) {
2442 if ((r.resultWho == null && resultWho == null) ||
2443 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2444 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2445 false);
2446 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002447 }
2448 }
2449 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002450 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002451 }
2452
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002453 final void finishTopRunningActivityLocked(ProcessRecord app) {
2454 ActivityRecord r = topRunningActivityLocked(null);
2455 if (r != null && r.app == app) {
2456 // If the top running activity is from this crashing
2457 // process, then terminate it to avoid getting in a loop.
2458 Slog.w(TAG, " Force finishing activity "
2459 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002460 int taskNdx = mTaskHistory.indexOf(r.task);
2461 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002462 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002463 // Also terminate any activities below it that aren't yet
2464 // stopped, to avoid a situation where one will get
2465 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002466 --activityNdx;
2467 if (activityNdx < 0) {
2468 do {
2469 --taskNdx;
2470 if (taskNdx < 0) {
2471 break;
2472 }
2473 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2474 } while (activityNdx < 0);
2475 }
2476 if (activityNdx >= 0) {
2477 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002478 if (r.state == ActivityState.RESUMED
2479 || r.state == ActivityState.PAUSING
2480 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002481 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002482 Slog.w(TAG, " Force finishing activity "
2483 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002484 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002485 }
2486 }
2487 }
2488 }
2489 }
2490
Craig Mautnerd2328952013-03-05 12:46:26 -08002491 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002492 ArrayList<ActivityRecord> activities = r.task.mActivities;
2493 for (int index = activities.indexOf(r); index >= 0; --index) {
2494 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002495 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002496 break;
2497 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002498 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002499 }
2500 return true;
2501 }
2502
Dianne Hackborn5c607432012-02-28 14:44:19 -08002503 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2504 // send the result
2505 ActivityRecord resultTo = r.resultTo;
2506 if (resultTo != null) {
2507 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2508 + " who=" + r.resultWho + " req=" + r.requestCode
2509 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002510 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002511 if (resultData != null) {
2512 resultData.prepareToLeaveUser(r.userId);
2513 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002514 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002515 if (r.info.applicationInfo.uid > 0) {
2516 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2517 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002518 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002519 }
2520 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2521 resultData);
2522 r.resultTo = null;
2523 }
2524 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2525
2526 // Make sure this HistoryRecord is not holding on to other resources,
2527 // because clients have remote IPC references to this object so we
2528 // can't assume that will go away and want to avoid circular IPC refs.
2529 r.results = null;
2530 r.pendingResults = null;
2531 r.newIntents = null;
2532 r.icicle = null;
2533 }
2534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 /**
2536 * @return Returns true if this activity has been removed from the history
2537 * list, or false if it is still in the list and will be removed later.
2538 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002539 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2540 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002541 if (r.finishing) {
2542 Slog.w(TAG, "Duplicate finish request for " + r);
2543 return false;
2544 }
2545
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002546 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002547 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002548 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002549 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002550 task.taskId, r.shortComponentName, reason);
2551 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002552 final int index = activities.indexOf(r);
2553 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002554 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002555 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002556 // If the caller asked that this activity (and all above it)
2557 // be cleared when the task is reset, don't lose that information,
2558 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002559 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002560 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002561 }
2562 }
2563
2564 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002565
2566 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567
Dianne Hackborn5c607432012-02-28 14:44:19 -08002568 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002569
Craig Mautnerde4ef022013-04-07 19:01:33 -07002570 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002571 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002572 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002573 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002574 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002575 ? AppTransition.TRANSIT_TASK_CLOSE
2576 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002577
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002579 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002580
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002581 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002582 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2583 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2584 startPausingLocked(false, false);
2585 }
2586
Craig Mautneraea74a52014-03-08 14:23:10 -08002587 if (endTask) {
2588 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2589 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002590 } else if (r.state != ActivityState.PAUSING) {
2591 // If the activity is PAUSING, we will complete the finish once
2592 // it is done pausing; else we can just directly finish it here.
2593 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002594 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002595 } else {
2596 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2597 }
2598
2599 return false;
2600 }
2601
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 static final int FINISH_IMMEDIATELY = 0;
2603 static final int FINISH_AFTER_PAUSE = 1;
2604 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605
Craig Mautnerf3333272013-04-22 10:55:53 -07002606 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 // First things first: if this activity is currently visible,
2608 // and the resumed activity is not yet visible, then hold off on
2609 // finishing until the resumed one becomes visible.
2610 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002611 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2612 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002613 if (mStackSupervisor.mStoppingActivities.size() > 3
2614 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002615 // If we already have a few activities waiting to stop,
2616 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002617 // them out. Or if r is the last of activity of the last task the stack
2618 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002619 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002620 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002621 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002622 }
2623 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002624 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2625 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002626 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002627 if (oomAdj) {
2628 mService.updateOomAdjLocked();
2629 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 return r;
2631 }
2632
2633 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002634 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002635 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002636 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637 if (mResumedActivity == r) {
2638 mResumedActivity = null;
2639 }
2640 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002641 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002642 r.state = ActivityState.FINISHING;
2643
2644 if (mode == FINISH_IMMEDIATELY
2645 || prevState == ActivityState.STOPPED
2646 || prevState == ActivityState.INITIALIZING) {
2647 // If this activity is already stopped, we can just finish
2648 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002649 r.makeFinishing();
2650 boolean activityRemoved = destroyActivityLocked(r, true, oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002651 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002652 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002653 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002654 if (DEBUG_CONTAINERS) Slog.d(TAG,
2655 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2656 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002657 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002659
2660 // Need to go through the full pause cycle to get this
2661 // activity into the stopped state and then finish it.
2662 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002663 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002664 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002665 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 return r;
2667 }
2668
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002669 void finishAllActivitiesLocked() {
2670 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2671 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2672 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2673 final ActivityRecord r = activities.get(activityNdx);
2674 if (r.finishing) {
2675 continue;
2676 }
2677 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
2678 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2679 }
2680 }
2681 }
2682
Craig Mautnerd2328952013-03-05 12:46:26 -08002683 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002684 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002685 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002686 final TaskRecord task = srec.task;
2687 final ArrayList<ActivityRecord> activities = task.mActivities;
2688 final int start = activities.indexOf(srec);
2689 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002690 return false;
2691 }
2692 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002693 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002694 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002695 final ComponentName dest = destIntent.getComponent();
2696 if (start > 0 && dest != null) {
2697 for (int i = finishTo; i >= 0; i--) {
2698 ActivityRecord r = activities.get(i);
2699 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002700 r.info.name.equals(dest.getClassName())) {
2701 finishTo = i;
2702 parent = r;
2703 foundParentInTask = true;
2704 break;
2705 }
2706 }
2707 }
2708
2709 IActivityController controller = mService.mController;
2710 if (controller != null) {
2711 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2712 if (next != null) {
2713 // ask watcher if this is allowed
2714 boolean resumeOK = true;
2715 try {
2716 resumeOK = controller.activityResuming(next.packageName);
2717 } catch (RemoteException e) {
2718 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002719 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002720 }
2721
2722 if (!resumeOK) {
2723 return false;
2724 }
2725 }
2726 }
2727 final long origId = Binder.clearCallingIdentity();
2728 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002729 ActivityRecord r = activities.get(i);
2730 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002731 // Only return the supplied result for the first activity finished
2732 resultCode = Activity.RESULT_CANCELED;
2733 resultData = null;
2734 }
2735
2736 if (parent != null && foundParentInTask) {
2737 final int parentLaunchMode = parent.info.launchMode;
2738 final int destIntentFlags = destIntent.getFlags();
2739 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2740 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2741 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2742 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2743 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2744 } else {
2745 try {
2746 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2747 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002748 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002749 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002750 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002751 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002752 foundParentInTask = res == ActivityManager.START_SUCCESS;
2753 } catch (RemoteException e) {
2754 foundParentInTask = false;
2755 }
2756 requestFinishActivityLocked(parent.appToken, resultCode,
2757 resultData, "navigate-up", true);
2758 }
2759 }
2760 Binder.restoreCallingIdentity(origId);
2761 return foundParentInTask;
2762 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 /**
2764 * Perform the common clean-up of an activity record. This is called both
2765 * as part of destroyActivityLocked() (when destroying the client-side
2766 * representation) and cleaning things up as a result of its hosting
2767 * processing going away, in which case there is no remaining client-side
2768 * state to destroy so only the cleanup here is needed.
2769 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002770 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2771 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 if (mResumedActivity == r) {
2773 mResumedActivity = null;
2774 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002775 if (mPausingActivity == r) {
2776 mPausingActivity = null;
2777 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002778 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779
2780 r.configDestroy = false;
2781 r.frozenBeforeDestroy = false;
2782
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002783 if (setState) {
2784 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2785 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002786 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002787 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002788 }
2789
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002790 // Make sure this record is no longer in the pending finishes list.
2791 // This could happen, for example, if we are trimming activities
2792 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002793 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002794 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002795
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 // Remove any pending results.
2797 if (r.finishing && r.pendingResults != null) {
2798 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2799 PendingIntentRecord rec = apr.get();
2800 if (rec != null) {
2801 mService.cancelIntentSenderLocked(rec, false);
2802 }
2803 }
2804 r.pendingResults = null;
2805 }
2806
2807 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002808 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809 }
2810
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002811 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002812 removeTimeoutsForActivityLocked(r);
2813 }
2814
2815 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002816 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002817 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002818 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002819 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002820 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002821 }
2822
Craig Mautner04a0ea62014-01-13 12:51:26 -08002823 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002824 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002825 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2826 r.makeFinishing();
2827 if (DEBUG_ADD_REMOVE) {
2828 RuntimeException here = new RuntimeException("here");
2829 here.fillInStackTrace();
2830 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002832 r.takeFromHistory();
2833 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002834 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002835 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002836 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002837 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002838 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002839 if (VALIDATE_TOKENS) {
2840 validateAppTokensLocked();
2841 }
Craig Mautner312ba862014-02-10 17:55:01 -08002842 final TaskRecord task = r.task;
2843 if (task != null && task.removeActivity(r)) {
2844 if (DEBUG_STACK) Slog.i(TAG,
2845 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner719e6212014-05-29 16:49:41 +00002846 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
2847 mStackSupervisor.moveHomeToTop();
Craig Mautner312ba862014-02-10 17:55:01 -08002848 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002849 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08002850 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002851 cleanUpActivityServicesLocked(r);
2852 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002853 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002854
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002855 /**
2856 * Perform clean-up of service connections in an activity record.
2857 */
2858 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2859 // Throw away any services that have been bound by this activity.
2860 if (r.connections != null) {
2861 Iterator<ConnectionRecord> it = r.connections.iterator();
2862 while (it.hasNext()) {
2863 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002864 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002865 }
2866 r.connections = null;
2867 }
2868 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002869
2870 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2871 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2872 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2873 mHandler.sendMessage(msg);
2874 }
2875
Dianne Hackborn28695e02011-11-02 21:59:51 -07002876 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002877 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002878 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002879 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2880 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2881 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2882 final ActivityRecord r = activities.get(activityNdx);
2883 if (r.finishing) {
2884 continue;
2885 }
2886 if (r.fullscreen) {
2887 lastIsOpaque = true;
2888 }
2889 if (owner != null && r.app != owner) {
2890 continue;
2891 }
2892 if (!lastIsOpaque) {
2893 continue;
2894 }
2895 // We can destroy this one if we have its icicle saved and
2896 // it is not in the process of pausing/stopping/finishing.
2897 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2898 && r.haveState && !r.visible && r.stopped
2899 && r.state != ActivityState.DESTROYING
2900 && r.state != ActivityState.DESTROYED) {
2901 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2902 + " resumed=" + mResumedActivity
2903 + " pausing=" + mPausingActivity);
2904 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2905 activityRemoved = true;
2906 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002907 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002908 }
2909 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002910 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002911 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002912 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002913 }
2914
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002915 /**
2916 * Destroy the current CLIENT SIDE instance of an activity. This may be
2917 * called both when actually finishing an activity, or when performing
2918 * a configuration switch where we destroy the current client-side object
2919 * but then create a new client-side object for this same HistoryRecord.
2920 */
2921 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002922 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002923 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002924 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2926 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002927 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002928 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929
2930 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002931
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002932 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002933
2934 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002935
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 if (hadApp) {
2937 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002938 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2940 mService.mHeavyWeightProcess = null;
2941 mService.mHandler.sendEmptyMessage(
2942 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2943 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002944 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002945 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002946 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002947 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 }
2949 }
2950
2951 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002952
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002953 try {
2954 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002955 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 r.configChangeFlags);
2957 } catch (Exception e) {
2958 // We can just ignore exceptions here... if the process
2959 // has crashed, our death notification will clean things
2960 // up.
2961 //Slog.w(TAG, "Exception thrown during finish", e);
2962 if (r.finishing) {
2963 removeActivityFromHistoryLocked(r);
2964 removedFromHistory = true;
2965 skipDestroy = true;
2966 }
2967 }
2968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002970
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002971 // If the activity is finishing, we need to wait on removing it
2972 // from the list to give it a chance to do its cleanup. During
2973 // that time it may make calls back with its token so we need to
2974 // be able to find it on the list and so we don't want to remove
2975 // it from the list yet. Otherwise, we can just immediately put
2976 // it in the destroyed state since we are not removing it from the
2977 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002978 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002979 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2980 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002981 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002982 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002983 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2984 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002985 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002986 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002987 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002988 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 }
2990 } else {
2991 // remove this record from the history.
2992 if (r.finishing) {
2993 removeActivityFromHistoryLocked(r);
2994 removedFromHistory = true;
2995 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002996 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002997 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002998 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002999 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003000 }
3001 }
3002
3003 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003004
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003005 if (!mLRUActivities.remove(r) && hadApp) {
3006 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3007 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003008
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 return removedFromHistory;
3010 }
3011
Craig Mautnerd2328952013-03-05 12:46:26 -08003012 final void activityDestroyedLocked(IBinder token) {
3013 final long origId = Binder.clearCallingIdentity();
3014 try {
3015 ActivityRecord r = ActivityRecord.forToken(token);
3016 if (r != null) {
3017 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003018 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003019 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003020
3021 if (isInStackLocked(token) != null) {
3022 if (r.state == ActivityState.DESTROYING) {
3023 cleanUpActivityLocked(r, true, false);
3024 removeActivityFromHistoryLocked(r);
3025 }
3026 }
Craig Mautner05d29032013-05-03 13:40:13 -07003027 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003028 } finally {
3029 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003030 }
3031 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003032
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003033 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3034 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003036 if (DEBUG_CLEANUP) Slog.v(
3037 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 + " with " + i + " entries");
3039 while (i > 0) {
3040 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003041 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003042 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003043 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003044 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003046 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 }
3048 }
3049 }
3050
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003051 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3052 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003053 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3054 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003055 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3056 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003057 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003058 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003059 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3060 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003061
3062 boolean hasVisibleActivities = false;
3063
3064 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003065 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003066 if (DEBUG_CLEANUP) Slog.v(
3067 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003068 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3069 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3070 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3071 final ActivityRecord r = activities.get(activityNdx);
3072 --i;
3073 if (DEBUG_CLEANUP) Slog.v(
3074 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3075 if (r.app == app) {
3076 boolean remove;
3077 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3078 // Don't currently have state for the activity, or
3079 // it is finishing -- always remove it.
3080 remove = true;
3081 } else if (r.launchCount > 2 &&
3082 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3083 // We have launched this activity too many times since it was
3084 // able to run, so give up and remove it.
3085 remove = true;
3086 } else {
3087 // The process may be gone, but the activity lives on!
3088 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003089 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003090 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003091 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003092 RuntimeException here = new RuntimeException("here");
3093 here.fillInStackTrace();
3094 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3095 + ": haveState=" + r.haveState
3096 + " stateNotNeeded=" + r.stateNotNeeded
3097 + " finishing=" + r.finishing
3098 + " state=" + r.state, here);
3099 }
3100 if (!r.finishing) {
3101 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3102 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3103 r.userId, System.identityHashCode(r),
3104 r.task.taskId, r.shortComponentName,
3105 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003106 if (r.state == ActivityState.RESUMED) {
3107 mService.updateUsageStats(r, false);
3108 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003109 }
3110 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003111
Craig Mautner0247fc82013-02-28 14:32:06 -08003112 } else {
3113 // We have the current state for this activity, so
3114 // it can be restarted later when needed.
3115 if (localLOGV) Slog.v(
3116 TAG, "Keeping entry, setting app to null");
3117 if (r.visible) {
3118 hasVisibleActivities = true;
3119 }
3120 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3121 + r);
3122 r.app = null;
3123 r.nowVisible = false;
3124 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003125 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003126 "App died, clearing saved state of " + r);
3127 r.icicle = null;
3128 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003129 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003130
Craig Mautnerd2328952013-03-05 12:46:26 -08003131 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003132 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003133 }
3134 }
3135
3136 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003138
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003139 final void updateTransitLocked(int transit, Bundle options) {
3140 if (options != null) {
3141 ActivityRecord r = topRunningActivityLocked(null);
3142 if (r != null && r.state != ActivityState.RESUMED) {
3143 r.updateOptionsLocked(options);
3144 } else {
3145 ActivityOptions.abort(options);
3146 }
3147 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003148 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003149 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003150
Craig Mautner21d24a22014-04-23 11:45:37 -07003151 void updateTaskMovement(TaskRecord task, boolean toFront) {
3152 if (task.isPersistable) {
3153 task.mLastTimeMoved = System.currentTimeMillis();
3154 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3155 // recently will be most negative, tasks sent to the bottom before that will be less
3156 // negative. Similarly for recent tasks moved to the top which will be most positive.
3157 if (!toFront) {
3158 task.mLastTimeMoved *= -1;
3159 }
3160 }
3161 }
3162
Craig Mautner719e6212014-05-29 16:49:41 +00003163 void moveHomeTaskToTop() {
Craig Mautnera82aa092013-09-13 15:34:08 -07003164 final int top = mTaskHistory.size() - 1;
3165 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3166 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner719e6212014-05-29 16:49:41 +00003167 if (task.isHomeTask()) {
3168 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003169 mTaskHistory.remove(taskNdx);
3170 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003171 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003172 mWindowManager.moveTaskToTop(task.taskId);
3173 return;
3174 }
3175 }
3176 }
3177
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003178 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003179 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180
Craig Mautner11bf9a52013-02-19 14:08:51 -08003181 final int numTasks = mTaskHistory.size();
3182 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003183 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003184 // nothing to do!
3185 if (reason != null &&
3186 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3187 ActivityOptions.abort(options);
3188 } else {
3189 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3190 }
3191 return;
3192 }
3193
Craig Mautnere0a38842013-12-16 16:14:02 -08003194 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003195
Craig Mautner11bf9a52013-02-19 14:08:51 -08003196 // Shift all activities with this task up to the top
3197 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003198 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003199
3200 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 if (reason != null &&
3202 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003203 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 ActivityRecord r = topRunningActivityLocked(null);
3205 if (r != null) {
3206 mNoAnimActivities.add(r);
3207 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003208 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003209 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003210 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003212
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003213 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003214
Craig Mautner05d29032013-05-03 13:40:13 -07003215 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003216 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003217
3218 if (VALIDATE_TOKENS) {
3219 validateAppTokensLocked();
3220 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221 }
3222
3223 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003224 * Worker method for rearranging history stack. Implements the function of moving all
3225 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003227 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3229 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003230 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003231 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003232 * @return Returns true if the move completed, false if not.
3233 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003234 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003235 final TaskRecord tr = taskForIdLocked(taskId);
3236 if (tr == null) {
3237 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3238 return false;
3239 }
3240
3241 Slog.i(TAG, "moveTaskToBack: " + tr);
3242
3243 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003244
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 // If we have a watcher, preflight the move before committing to it. First check
3246 // for *other* available tasks, but if none are available, then try again allowing the
3247 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003248 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003249 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 if (next == null) {
3251 next = topRunningActivityLocked(null, 0);
3252 }
3253 if (next != null) {
3254 // ask watcher if this is allowed
3255 boolean moveOK = true;
3256 try {
3257 moveOK = mService.mController.activityResuming(next.packageName);
3258 } catch (RemoteException e) {
3259 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003260 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 }
3262 if (!moveOK) {
3263 return false;
3264 }
3265 }
3266 }
3267
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003269 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270
Craig Mautner11bf9a52013-02-19 14:08:51 -08003271 mTaskHistory.remove(tr);
3272 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003273 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003274
Craig Mautnerc8143c62013-09-03 12:15:57 -07003275 // There is an assumption that moving a task to the back moves it behind the home activity.
3276 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003277 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003278 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3279 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner719e6212014-05-29 16:49:41 +00003280 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003281 break;
3282 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003283 if (taskNdx == 1) {
3284 // Set the last task before tr to go to home.
Craig Mautner719e6212014-05-29 16:49:41 +00003285 task.mOnTopOfHome = true;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003286 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003287 }
3288
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003289 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003290 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3291 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292 ActivityRecord r = topRunningActivityLocked(null);
3293 if (r != null) {
3294 mNoAnimActivities.add(r);
3295 }
3296 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003297 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003298 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003299 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003300
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003301 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003302 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 }
3304
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003305 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner719e6212014-05-29 16:49:41 +00003306 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
3307 tr.mOnTopOfHome = false;
3308 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003309 }
3310
Craig Mautner05d29032013-05-03 13:40:13 -07003311 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 return true;
3313 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003314
Craig Mautner8849a5e2013-04-02 16:41:03 -07003315 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003316 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003317 final Uri data = r.intent.getData();
3318 final String strData = data != null ? data.toSafeString() : null;
3319
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003321 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003323 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 }
3325
3326 /**
3327 * Make sure the given activity matches the current configuration. Returns
3328 * false if the activity had to be destroyed. Returns true if the
3329 * configuration is the same, or the activity will remain running as-is
3330 * for whatever reason. Ensures the HistoryRecord is updated with the
3331 * correct configuration and all other bookkeeping is handled.
3332 */
3333 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3334 int globalChanges) {
3335 if (mConfigWillChange) {
3336 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3337 "Skipping config check (will change): " + r);
3338 return true;
3339 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003340
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3342 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003343
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 // Short circuit: if the two configurations are the exact same
3345 // object (the common case), then there is nothing to do.
3346 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003347 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003348 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3349 "Configuration unchanged in " + r);
3350 return true;
3351 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003352
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 // We don't worry about activities that are finishing.
3354 if (r.finishing) {
3355 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3356 "Configuration doesn't matter in finishing " + r);
3357 r.stopFreezingScreenLocked(false);
3358 return true;
3359 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003360
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 // Okay we now are going to make this activity have the new config.
3362 // But then we need to figure out how it needs to deal with that.
3363 Configuration oldConfig = r.configuration;
3364 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003365
3366 // Determine what has changed. May be nothing, if this is a config
3367 // that has come back from the app after going idle. In that case
3368 // we just want to leave the official config object now in the
3369 // activity and do nothing else.
3370 final int changes = oldConfig.diff(newConfig);
3371 if (changes == 0 && !r.forceNewConfig) {
3372 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3373 "Configuration no differences in " + r);
3374 return true;
3375 }
3376
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003377 // If the activity isn't currently running, just leave the new
3378 // configuration and it will pick that up next time it starts.
3379 if (r.app == null || r.app.thread == null) {
3380 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3381 "Configuration doesn't matter not running " + r);
3382 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003383 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003384 return true;
3385 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003386
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003387 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003388 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3389 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3390 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003391 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003392 + ", newConfig=" + newConfig);
3393 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003394 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003395 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3396 r.configChangeFlags |= changes;
3397 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003398 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003399 if (r.app == null || r.app.thread == null) {
3400 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003401 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003402 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003403 } else if (r.state == ActivityState.PAUSING) {
3404 // A little annoying: we are waiting for this activity to
3405 // finish pausing. Let's not do anything now, but just
3406 // flag that it needs to be restarted when done pausing.
3407 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003408 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003409 r.configDestroy = true;
3410 return true;
3411 } else if (r.state == ActivityState.RESUMED) {
3412 // Try to optimize this case: the configuration is changing
3413 // and we need to restart the top, resumed activity.
3414 // Instead of doing the normal handshaking, just say
3415 // "restart!".
3416 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003417 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003418 relaunchActivityLocked(r, r.configChangeFlags, true);
3419 r.configChangeFlags = 0;
3420 } else {
3421 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003422 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003423 relaunchActivityLocked(r, r.configChangeFlags, false);
3424 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003425 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003426
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003427 // All done... tell the caller we weren't able to keep this
3428 // activity around.
3429 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003430 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003431
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003432 // Default case: the activity can handle this new configuration, so
3433 // hand it over. Note that we don't need to give it the new
3434 // configuration, since we always send configuration changes to all
3435 // process when they happen so it can just use whatever configuration
3436 // it last got.
3437 if (r.app != null && r.app.thread != null) {
3438 try {
3439 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003440 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003441 } catch (RemoteException e) {
3442 // If process died, whatever.
3443 }
3444 }
3445 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003446
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003447 return true;
3448 }
3449
Craig Mautnerc8143c62013-09-03 12:15:57 -07003450 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 int changes, boolean andResume) {
3452 List<ResultInfo> results = null;
3453 List<Intent> newIntents = null;
3454 if (andResume) {
3455 results = r.results;
3456 newIntents = r.newIntents;
3457 }
3458 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3459 + " with results=" + results + " newIntents=" + newIntents
3460 + " andResume=" + andResume);
3461 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003462 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003463 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003464
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003465 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003466
Craig Mautner34b73df2014-01-12 21:11:08 -08003467 mStackSupervisor.removeChildActivityContainers(r);
3468
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003469 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003470 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3471 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3472 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003473 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003474 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003475 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003476 // Note: don't need to call pauseIfSleepingLocked() here, because
3477 // the caller will only pass in 'andResume' if this activity is
3478 // currently resumed, which implies we aren't sleeping.
3479 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003480 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003481 }
3482
3483 if (andResume) {
3484 r.results = null;
3485 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003486 r.state = ActivityState.RESUMED;
3487 } else {
3488 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3489 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003490 }
3491
3492 return true;
3493 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003494
3495 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003496 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3497 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3498 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3499 final ActivityRecord r = activities.get(activityNdx);
3500 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003501 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003502 }
3503 if (r.fullscreen && !r.finishing) {
3504 return false;
3505 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003506 }
3507 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003508 final ActivityRecord r = ActivityRecord.forToken(token);
3509 if (r == null) {
3510 return false;
3511 }
3512 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3513 + " would have returned true for r=" + r);
3514 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003515 }
3516
3517 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003518 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3519 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3520 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3521 final ActivityRecord r = activities.get(activityNdx);
3522 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003523 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003524 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003525 }
3526 }
3527 }
3528
3529 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3530 boolean didSomething = false;
3531 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003532 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3533 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3534 int numActivities = activities.size();
3535 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3536 ActivityRecord r = activities.get(activityNdx);
3537 final boolean samePackage = r.packageName.equals(name)
3538 || (name == null && r.userId == userId);
3539 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3540 && (samePackage || r.task == lastTask)
3541 && (r.app == null || evenPersistent || !r.app.persistent)) {
3542 if (!doit) {
3543 if (r.finishing) {
3544 // If this activity is just finishing, then it is not
3545 // interesting as far as something to stop.
3546 continue;
3547 }
3548 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003549 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003550 didSomething = true;
3551 Slog.i(TAG, " Force finishing activity " + r);
3552 if (samePackage) {
3553 if (r.app != null) {
3554 r.app.removed = true;
3555 }
3556 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003557 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003558 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003559 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3560 true)) {
3561 // r has been deleted from mActivities, accommodate.
3562 --numActivities;
3563 --activityNdx;
3564 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003565 }
3566 }
3567 }
3568 return didSomething;
3569 }
3570
Dianne Hackborn09233282014-04-30 11:33:59 -07003571 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003572 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003573 final TaskRecord task = mTaskHistory.get(taskNdx);
3574 ActivityRecord r = null;
3575 ActivityRecord top = null;
3576 int numActivities = 0;
3577 int numRunning = 0;
3578 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003579 if (activities.isEmpty()) {
3580 continue;
3581 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003582 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3583 continue;
3584 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003585 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3586 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003587
Craig Mautneraab647e2013-02-28 16:31:36 -08003588 // Initialize state for next task if needed.
3589 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3590 top = r;
3591 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003592 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003593
3594 // Add 'r' into the current task.
3595 numActivities++;
3596 if (r.app != null && r.app.thread != null) {
3597 numRunning++;
3598 }
3599
3600 if (localLOGV) Slog.v(
3601 TAG, r.intent.getComponent().flattenToShortString()
3602 + ": task=" + r.task);
3603 }
3604
3605 RunningTaskInfo ci = new RunningTaskInfo();
3606 ci.id = task.taskId;
3607 ci.baseActivity = r.intent.getComponent();
3608 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003609 ci.lastActiveTime = task.lastActiveTime;
3610
Craig Mautneraab647e2013-02-28 16:31:36 -08003611 if (top.thumbHolder != null) {
3612 ci.description = top.thumbHolder.lastDescription;
3613 }
3614 ci.numActivities = numActivities;
3615 ci.numRunning = numRunning;
3616 //System.out.println(
3617 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003618 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003619 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003620 }
3621
3622 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003623 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003624 if (DEBUG_SWITCH) Slog.d(
3625 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003626 if (top >= 0) {
3627 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3628 int activityTop = activities.size() - 1;
3629 if (activityTop > 0) {
3630 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3631 "unhandled-back", true);
3632 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003633 }
3634 }
3635
Craig Mautner6b74cb52013-09-27 17:02:21 -07003636 /**
3637 * Reset local parameters because an app's activity died.
3638 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003639 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003640 */
3641 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003642 if (mPausingActivity != null && mPausingActivity.app == app) {
3643 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3644 "App died while pausing: " + mPausingActivity);
3645 mPausingActivity = null;
3646 }
3647 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3648 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003649 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003650 }
3651
Craig Mautner19091252013-10-05 00:03:53 -07003652 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003653 }
3654
Craig Mautnercae015f2013-02-08 14:31:27 -08003655 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3657 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3658 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3659 final ActivityRecord r = activities.get(activityNdx);
3660 if (r.app == app) {
3661 Slog.w(TAG, " Force finishing activity "
3662 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003663 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003664 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003665 }
3666 }
3667 }
3668
Dianne Hackborn390517b2013-05-30 15:03:32 -07003669 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003670 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003671 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003672 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3673 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003674 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3675 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003676 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003677 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003678 if (printed) {
3679 header = null;
3680 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003681 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003682 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003683 }
3684
3685 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3686 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3687
3688 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003689 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3690 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003691 }
3692 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003693 final int top = mTaskHistory.size() - 1;
3694 if (top >= 0) {
3695 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3696 int listTop = list.size() - 1;
3697 if (listTop >= 0) {
3698 activities.add(list.get(listTop));
3699 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003700 }
3701 } else {
3702 ItemMatcher matcher = new ItemMatcher();
3703 matcher.build(name);
3704
Craig Mautneraab647e2013-02-28 16:31:36 -08003705 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3706 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3707 if (matcher.match(r1, r1.intent.getComponent())) {
3708 activities.add(r1);
3709 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003710 }
3711 }
3712 }
3713
3714 return activities;
3715 }
3716
3717 ActivityRecord restartPackage(String packageName) {
3718 ActivityRecord starting = topRunningActivityLocked(null);
3719
3720 // All activities that came from the package must be
3721 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003722 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3723 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3724 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3725 final ActivityRecord a = activities.get(activityNdx);
3726 if (a.info.packageName.equals(packageName)) {
3727 a.forceNewConfig = true;
3728 if (starting != null && a == starting && a.visible) {
3729 a.startFreezingScreenLocked(starting.app,
3730 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3731 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003732 }
3733 }
3734 }
3735
3736 return starting;
3737 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003738
Craig Mautner41db4a72014-05-07 17:20:56 -07003739 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003740 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003741 mWindowManager.removeTask(task.taskId);
3742 final ActivityRecord r = mResumedActivity;
3743 if (r != null && r.task == task) {
3744 mResumedActivity = null;
3745 }
3746
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003747 final int taskNdx = mTaskHistory.indexOf(task);
3748 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner719e6212014-05-29 16:49:41 +00003749 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3750 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003751 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003752 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003753 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07003754
3755 if (task.mActivities.isEmpty()) {
3756 final boolean isVoiceSession = task.voiceSession != null;
3757 if (isVoiceSession) {
3758 try {
3759 task.voiceSession.taskFinished(task.intent, task.taskId);
3760 } catch (RemoteException e) {
3761 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003762 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003763 if (task.autoRemoveFromRecents() || isVoiceSession) {
3764 // Task creator asked to remove this when done, or this task was a voice
3765 // interaction, so it should not remain on the recent tasks list.
3766 mService.mRecentTasks.remove(task);
3767 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003768 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003769
3770 if (mTaskHistory.isEmpty()) {
3771 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3772 if (isOnHomeDisplay()) {
3773 mStackSupervisor.moveHomeStack(!isHomeStack());
3774 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003775 if (mStacks != null) {
3776 mStacks.remove(this);
3777 mStacks.add(0, this);
3778 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003779 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08003780 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003781 }
3782
Dianne Hackborn91097de2014-04-04 18:02:06 -07003783 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
3784 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
3785 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07003786 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
3787 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003788 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003789 return task;
3790 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003791
3792 ArrayList<TaskRecord> getAllTasks() {
3793 return new ArrayList<TaskRecord>(mTaskHistory);
3794 }
3795
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003796 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003797 task.stack = this;
3798 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003799 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003800 } else {
3801 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003802 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003803 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003804 if (!moving && task.voiceSession != null) {
3805 try {
3806 task.voiceSession.taskStarted(task.intent, task.taskId);
3807 } catch (RemoteException e) {
3808 }
3809 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003810 }
3811
3812 public int getStackId() {
3813 return mStackId;
3814 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003815
3816 @Override
3817 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003818 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3819 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003820 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003821}