blob: 545423b7e4994c96ada786e0d361ee97cf7405a6 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
Craig Mautner84984fa2014-06-19 11:19:20 -070033import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36
Craig Mautner0eea92c2013-05-16 13:35:39 -070037import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
38import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070041import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070042import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070043import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
44
Dianne Hackborn91097de2014-04-04 18:02:06 -070045import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070047import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080048import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080049import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080050import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070052import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053
54import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070055import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070056import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080058import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080060import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080066import android.content.res.Resources;
67import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080068import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070070import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080071import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.Handler;
73import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090074import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070076import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.os.RemoteException;
78import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080079import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070080import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070081import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070084import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
Craig Mautnercae015f2013-02-08 14:31:27 -080086import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088import java.lang.ref.WeakReference;
89import java.util.ArrayList;
90import java.util.Iterator;
91import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080092import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093
94/**
95 * State and management of a single stack of activities.
96 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070097final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080098
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070099 // Ticks during which we check progress while waiting for an app to launch.
100 static final int LAUNCH_TICK = 500;
101
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700102 // How long we wait until giving up on the last activity to pause. This
103 // is short because it directly impacts the responsiveness of starting the
104 // next activity.
105 static final int PAUSE_TIMEOUT = 500;
106
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700107 // How long we wait for the activity to tell us it has stopped before
108 // giving up. This is a good amount of time because we really need this
109 // from the application in order to get its saved state.
110 static final int STOP_TIMEOUT = 10*1000;
111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112 // How long we wait until giving up on an activity telling us it has
113 // finished destroying itself.
114 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800117 // disabled.
118 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700120 // How long between activity launches that we consider safe to not warn
121 // the user about an unexpected activity being launched on top.
122 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124 // Set to false to disable the preview that is shown while a new activity
125 // is being started.
126 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800127
Craig Mautner5eda9b32013-07-02 11:58:16 -0700128 // How long to wait for all background Activities to redraw following a call to
129 // convertToTranslucent().
130 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
131
Craig Mautnera61bc652013-10-28 15:43:18 -0700132 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700133
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134 enum ActivityState {
135 INITIALIZING,
136 RESUMED,
137 PAUSING,
138 PAUSED,
139 STOPPING,
140 STOPPED,
141 FINISHING,
142 DESTROYING,
143 DESTROYED
144 }
145
146 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700147 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 /**
150 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151 * running) activities. It contains #TaskRecord objects.
152 */
153 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
154
155 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156 * Used for validating app tokens with window manager.
157 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800158 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 /**
161 * List of running activities, sorted by recent usage.
162 * The first entry in the list is the least recently used.
163 * It contains HistoryRecord objects.
164 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700165 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168 * Animations that for the current transition have requested not to
169 * be considered for the transition animation.
170 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172
173 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 * When we are in the process of pausing an activity, before starting the
175 * next one, this variable holds the activity that is currently being paused.
176 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800177 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700178
179 /**
180 * This is the last activity that we put into the paused state. This is
181 * used to determine if we need to do an activity transition while sleeping,
182 * when we normally hold the top activity paused.
183 */
184 ActivityRecord mLastPausedActivity = null;
185
186 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700187 * Activities that specify No History must be removed once the user navigates away from them.
188 * If the device goes to sleep with such an activity in the paused state then we save it here
189 * and finish it later if another activity replaces it on wakeup.
190 */
191 ActivityRecord mLastNoHistoryActivity = null;
192
193 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 * Current activity that is resumed, or null if there is none.
195 */
196 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700199 * This is the last activity that has been started. It is only used to
200 * identify when multiple activities are started at once so that the user
201 * can be warned they may not be in the activity they think they are.
202 */
203 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800204
Craig Mautner5eda9b32013-07-02 11:58:16 -0700205 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
206 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
207 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
208 // Activity in mTranslucentActivityWaiting is notified via
209 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
210 // background activity being drawn then the same call will be made with a true value.
211 ActivityRecord mTranslucentActivityWaiting = null;
212 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
213 new ArrayList<ActivityRecord>();
Craig Mautner233ceee2014-05-09 17:05:11 -0700214 // Options passed from the caller of the convertToTranslucent to the activity that will
215 // appear below it.
216 ActivityOptions mReturningActivityOptions = null;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700217
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700218 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700219 * Set when we know we are going to be calling updateConfiguration()
220 * soon, so want to skip intermediate config checks.
221 */
222 boolean mConfigWillChange;
223
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700224 long mLaunchStartTime = 0;
225 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800226
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800227 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800228 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
229 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
230 */
231 private ActivityRecord mLastScreenshotActivity = null;
232 private Bitmap mLastScreenshotBitmap = null;
233
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800234 int mThumbnailWidth = -1;
235 int mThumbnailHeight = -1;
236
Craig Mautner858d8a62013-04-23 17:08:34 -0700237 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700238
Craig Mautnerc00204b2013-03-05 15:02:14 -0800239 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800241 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
242 ArrayList<ActivityStack> mStacks;
243 /** The attached Display's unique identifier, or -1 if detached */
244 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800245
Craig Mautner27084302013-03-25 08:05:25 -0700246 /** Run all ActivityStacks through this */
247 final ActivityStackSupervisor mStackSupervisor;
248
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700249 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700250 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
251 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
252 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
253 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700254 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700255
256 static class ScheduleDestroyArgs {
257 final ProcessRecord mOwner;
258 final boolean mOomAdj;
259 final String mReason;
260 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
261 mOwner = owner;
262 mOomAdj = oomAdj;
263 mReason = reason;
264 }
265 }
266
Zoran Marcetaf958b322012-08-09 20:27:12 +0900267 final Handler mHandler;
268
269 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 //public Handler() {
271 // if (localLOGV) Slog.v(TAG, "Handler started!");
272 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700273 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 super(looper);
275 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276
Zoran Marcetaf958b322012-08-09 20:27:12 +0900277 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 public void handleMessage(Message msg) {
279 switch (msg.what) {
280 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800281 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282 // We don't at this point know if the activity is fullscreen,
283 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800284 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 synchronized (mService) {
286 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700287 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700288 }
Craig Mautnera0026042014-04-23 11:45:37 -0700289 activityPausedLocked(r.appToken, true, r.persistentState);
Craig Mautnerd2328952013-03-05 12:46:26 -0800290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700292 case LAUNCH_TICK_MSG: {
293 ActivityRecord r = (ActivityRecord)msg.obj;
294 synchronized (mService) {
295 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700296 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700297 }
298 }
299 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800301 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700302 // We don't at this point know if the activity is fullscreen,
303 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800304 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800305 synchronized (mService) {
306 activityDestroyedLocked(r != null ? r.appToken : null);
307 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700309 case STOP_TIMEOUT_MSG: {
310 ActivityRecord r = (ActivityRecord)msg.obj;
311 // We don't at this point know if the activity is fullscreen,
312 // so we need to be conservative and assume it isn't.
313 Slog.w(TAG, "Activity stop timeout for " + r);
314 synchronized (mService) {
315 if (r.isInHistory()) {
316 activityStoppedLocked(r, null, null, null);
317 }
318 }
319 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700320 case DESTROY_ACTIVITIES_MSG: {
321 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
322 synchronized (mService) {
323 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
324 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700325 } break;
326 case TRANSLUCENT_TIMEOUT_MSG: {
327 synchronized (mService) {
328 notifyActivityDrawnLocked(null);
329 }
330 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700331 }
332 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800333 }
334
Craig Mautner34b73df2014-01-12 21:11:08 -0800335 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800336 int count = 0;
337 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
338 count += mTaskHistory.get(taskNdx).mActivities.size();
339 }
340 return count;
341 }
342
Craig Mautner4a1cb222013-12-04 16:14:06 -0800343 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
344 mActivityContainer = activityContainer;
345 mStackSupervisor = activityContainer.getOuter();
346 mService = mStackSupervisor.mService;
347 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
348 mWindowManager = mService.mWindowManager;
349 mStackId = activityContainer.mStackId;
350 mCurrentUser = mService.mCurrentUserId;
Winson Chung376543b2014-05-21 17:43:28 -0700351 // Get the activity screenshot thumbnail dimensions
352 Resources res = mService.mContext.getResources();
Winson Chung2eb4f9c2014-05-22 11:22:41 -0700353 mThumbnailWidth = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
354 mThumbnailHeight = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700355 }
Craig Mautner5962b122012-10-05 14:45:52 -0700356
Amith Yamasani734983f2014-03-04 16:48:05 -0800357 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100358 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800359 */
Kenny Guy2a764942014-04-02 13:29:20 +0100360 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100361 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100362 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
363 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800364 }
365 return false;
366 }
367
368 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100369 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700370 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
371 }
372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800374 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700375 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
376 if (r != null) {
377 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800378 }
379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 return null;
381 }
382
383 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800384 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
385 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800386 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800387 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
388 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800389 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800390 return r;
391 }
392 }
393 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 return null;
395 }
396
397 /**
398 * This is a simplified version of topRunningActivityLocked that provides a number of
399 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800400 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 * @param token If non-null, any history records matching this token will be skipped.
402 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800403 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700404 * @return Returns the HistoryRecord of the next activity on the stack.
405 */
406 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
408 TaskRecord task = mTaskHistory.get(taskNdx);
409 if (task.taskId == taskId) {
410 continue;
411 }
412 ArrayList<ActivityRecord> activities = task.mActivities;
413 for (int i = activities.size() - 1; i >= 0; --i) {
414 final ActivityRecord r = activities.get(i);
415 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800416 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800417 return r;
418 }
419 }
420 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700421 return null;
422 }
423
Craig Mautner8849a5e2013-04-02 16:41:03 -0700424 final ActivityRecord topActivity() {
425 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700426 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700427 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
428 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700429 final int topActivityNdx = activities.size() - 1;
430 if (topActivityNdx >= 0) {
431 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700432 }
433 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700434 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700435 }
436
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700437 final TaskRecord topTask() {
438 final int size = mTaskHistory.size();
439 if (size > 0) {
440 return mTaskHistory.get(size - 1);
441 }
442 return null;
443 }
444
Craig Mautnerd2328952013-03-05 12:46:26 -0800445 TaskRecord taskForIdLocked(int id) {
446 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
447 final TaskRecord task = mTaskHistory.get(taskNdx);
448 if (task.taskId == id) {
449 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800450 }
451 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700452 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700453 }
454
Craig Mautnerd2328952013-03-05 12:46:26 -0800455 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800456 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800457 if (r != null) {
458 final TaskRecord task = r.task;
459 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
460 if (task.stack != this) Slog.w(TAG,
461 "Illegal state! task does not point to stack it is in.");
462 return r;
463 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800464 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800465 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800466 }
467
Craig Mautner2420ead2013-04-01 17:13:20 -0700468 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700469 final boolean hadit = mLRUActivities.remove(r);
470 mLRUActivities.add(r);
471 return hadit;
472 }
473
Craig Mautnerde4ef022013-04-07 19:01:33 -0700474 final boolean isHomeStack() {
475 return mStackId == HOME_STACK_ID;
476 }
477
Craig Mautnere0a38842013-12-16 16:14:02 -0800478 final boolean isOnHomeDisplay() {
479 return isAttached() &&
480 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
481 }
482
483 final void moveToFront() {
484 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800485 if (isOnHomeDisplay()) {
486 mStackSupervisor.moveHomeStack(isHomeStack());
487 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800488 mStacks.remove(this);
489 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800490 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 }
492
493 final boolean isAttached() {
494 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800495 }
496
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 /**
498 * Returns the top activity in any existing task matching the given
499 * Intent. Returns null if no such task is found.
500 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700501 ActivityRecord findTaskLocked(ActivityRecord target) {
502 Intent intent = target.intent;
503 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 ComponentName cls = intent.getComponent();
505 if (info.targetActivity != null) {
506 cls = new ComponentName(info.packageName, info.targetActivity);
507 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700508 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700509 boolean isDocument = intent != null & intent.isDocument();
510 // If documentData is non-null then it must match the existing task data.
511 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800512
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700513 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800514 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
515 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700516 if (task.voiceSession != null) {
517 // We never match voice sessions; those always run independently.
518 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
519 continue;
520 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521 if (task.userId != userId) {
522 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700523 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700524 continue;
525 }
Craig Mautner000f0022013-02-26 15:04:29 -0800526 final ActivityRecord r = task.getTopActivity();
527 if (r == null || r.finishing || r.userId != userId ||
528 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700529 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800530 continue;
531 }
532
Craig Mautnerd00f4742014-03-12 14:17:26 -0700533 final Intent taskIntent = task.intent;
534 final Intent affinityIntent = task.affinityIntent;
535 final boolean taskIsDocument;
536 final Uri taskDocumentData;
537 if (taskIntent != null && taskIntent.isDocument()) {
538 taskIsDocument = true;
539 taskDocumentData = taskIntent.getData();
540 } else if (affinityIntent != null && affinityIntent.isDocument()) {
541 taskIsDocument = true;
542 taskDocumentData = affinityIntent.getData();
543 } else {
544 taskIsDocument = false;
545 taskDocumentData = null;
546 }
547
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700548 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700549 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700550 + "/aff=" + r.task.affinity + " to new cls="
551 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700552 if (!isDocument && !taskIsDocument && task.affinity != null) {
553 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700554 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700555 return r;
556 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700557 } else if (taskIntent != null && taskIntent.getComponent() != null &&
558 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700559 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700560 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800561 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700562 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
563 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800564 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700565 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
566 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700567 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700568 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800569 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700570 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
571 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800572 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700573 } else if (DEBUG_TASKS) {
574 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 }
576 }
577
578 return null;
579 }
580
581 /**
582 * Returns the first activity (starting from the top of the stack) that
583 * is the same as the given activity. Returns null if no such activity
584 * is found.
585 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700586 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587 ComponentName cls = intent.getComponent();
588 if (info.targetActivity != null) {
589 cls = new ComponentName(info.packageName, info.targetActivity);
590 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700591 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700592
Craig Mautner000f0022013-02-26 15:04:29 -0800593 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100595 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700596 return null;
597 }
598 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800599 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
600 ActivityRecord r = activities.get(activityNdx);
601 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602 //Slog.i(TAG, "Found matching class!");
603 //dump();
604 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
605 return r;
606 }
607 }
608 }
609
610 return null;
611 }
612
Amith Yamasani742a6712011-05-04 14:49:28 -0700613 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700614 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700615 */
Craig Mautner93529a42013-10-04 15:03:13 -0700616 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800617 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700618 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800619 }
620 mCurrentUser = userId;
621
622 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800623 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800624 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700625 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100626 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700627 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
628 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800629 mTaskHistory.remove(i);
630 mTaskHistory.add(task);
631 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800632 // Use same value for i.
633 } else {
634 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800635 }
636 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700637 if (VALIDATE_TOKENS) {
638 validateAppTokensLocked();
639 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700640 }
641
Craig Mautner2420ead2013-04-01 17:13:20 -0700642 void minimalResumeActivityLocked(ActivityRecord r) {
643 r.state = ActivityState.RESUMED;
644 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
645 + " (starting new instance)");
646 r.stopped = false;
647 mResumedActivity = r;
648 r.task.touchActiveTime();
649 mService.addRecentTaskLocked(r.task);
650 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700651 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700652 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700653 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700654 }
655
Dianne Hackborncee04b52013-07-03 17:01:28 -0700656 private void startLaunchTraces() {
657 if (mFullyDrawnStartTime != 0) {
658 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
659 }
660 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
661 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
662 }
663
664 private void stopFullyDrawnTraceIfNeeded() {
665 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
666 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
667 mFullyDrawnStartTime = 0;
668 }
669 }
670
Craig Mautnere79d42682013-04-01 19:01:53 -0700671 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700672 if (r.displayStartTime == 0) {
673 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
674 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700675 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700676 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700678 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700679 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700680 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800683
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700684 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700685 // Make sure that there is no activity waiting for this to launch.
686 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
687 r.displayStartTime = r.fullyDrawnStartTime = 0;
688 } else {
689 mStackSupervisor.removeTimeoutsForActivityLocked(r);
690 mStackSupervisor.scheduleIdleTimeoutLocked(r);
691 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700692 }
693
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800694 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800696 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
697 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
698 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
699 activities.get(activityNdx).setSleeping(false);
700 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800701 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800702 }
703
Craig Mautner0eea92c2013-05-16 13:35:39 -0700704 /**
705 * @return true if something must be done before going to sleep.
706 */
707 boolean checkReadyForSleepLocked() {
708 if (mResumedActivity != null) {
709 // Still have something resumed; can't sleep until it is paused.
710 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
711 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
712 startPausingLocked(false, true);
713 return true;
714 }
715 if (mPausingActivity != null) {
716 // Still waiting for something to pause; can't sleep yet.
717 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
718 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800719 }
720
Craig Mautner0eea92c2013-05-16 13:35:39 -0700721 return false;
722 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800723
Craig Mautner0eea92c2013-05-16 13:35:39 -0700724 void goToSleep() {
725 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800726
Craig Mautner0eea92c2013-05-16 13:35:39 -0700727 // Make sure any stopped but visible activities are now sleeping.
728 // This ensures that the activity's onStop() is called.
729 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
730 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
731 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
732 final ActivityRecord r = activities.get(activityNdx);
733 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
734 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800735 }
736 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800737 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700738 }
Craig Mautner59c00972012-07-30 12:10:24 -0700739
Winson Chung376543b2014-05-21 17:43:28 -0700740 /**
741 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
742 * again when requested.
743 */
744 private void invalidateLastScreenshot() {
745 mLastScreenshotActivity = null;
746 if (mLastScreenshotBitmap != null) {
747 mLastScreenshotBitmap.recycle();
748 }
749 mLastScreenshotBitmap = null;
750 }
751
Dianne Hackbornd2835932010-12-13 16:28:46 -0800752 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700753 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800754 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700755 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800756 return null;
757 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800758
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700759 TaskRecord tr = who.task;
Winson Chung376543b2014-05-21 17:43:28 -0700760 if (mService.getMostRecentTask() != tr || isHomeStack()) {
761 // This is an optimization -- since we never show Home or Recents within Recents itself,
762 // we can just go ahead and skip taking the screenshot if this is the home stack. In
763 // the case where the most recent task is not the task that was supplied, then the stack
764 // has changed, so invalidate the last screenshot().
765 invalidateLastScreenshot();
766 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700767 return null;
768 }
769
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800770 int w = mThumbnailWidth;
771 int h = mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800772 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800773 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700774 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800775 || mLastScreenshotBitmap.getWidth() != w
776 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700777 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800778 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700779 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700780 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800781 }
782 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700783 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700784 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800785 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800786 }
Winson Chung376543b2014-05-21 17:43:28 -0700787 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800788 return null;
789 }
790
Craig Mautnercf910b02013-04-23 11:23:27 -0700791 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800792 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800793 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700794 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800795 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700796 ActivityRecord prev = mResumedActivity;
797 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700798 Slog.e(TAG, "Trying to pause when nothing is resumed",
799 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700800 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700801 return;
802 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800803
804 if (mActivityContainer.mParentActivity == null) {
805 // Top level stack, not a child. Look for child stacks.
806 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
807 }
808
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700809 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
810 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700811 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800812 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700813 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700814 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
815 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 prev.state = ActivityState.PAUSING;
817 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700818 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700819 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Craig Mautnerf6ead5d62014-04-24 10:19:20 -0700820 if (next == null || next.noDisplay || next.task != prev.task) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700821 prev.updateThumbnail(screenshotActivities(prev), null);
822 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700823 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700824
825 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800826
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 if (prev.app != null && prev.app.thread != null) {
828 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
829 try {
830 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700831 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700833 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800834 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
835 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 } catch (Exception e) {
837 // Ignore exception, if process died other code will cleanup.
838 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800839 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700841 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842 }
843 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800844 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700846 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 }
848
849 // If we are not going to sleep, we want to ensure the device is
850 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700851 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700852 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 }
854
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800855 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700856 // Have the window manager pause its key dispatching until the new
857 // activity has started. If we're pausing the activity just because
858 // the screen is being turned off and the UI is sleeping, don't interrupt
859 // key dispatch; the same activity will pick it up again on wakeup.
860 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800861 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 } else {
863 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
864 }
865
866 // Schedule a pause timeout in case the app doesn't respond.
867 // We don't give it much time because this directly impacts the
868 // responsiveness seen by the user.
869 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
870 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700871 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700872 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
873 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
874 } else {
875 // This activity failed to schedule the
876 // pause, so just treat it as being paused now.
877 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700878 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 }
880 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700881
Craig Mautnera0026042014-04-23 11:45:37 -0700882 final void activityPausedLocked(IBinder token, boolean timeout,
883 PersistableBundle persistentState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700884 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800885 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886
Craig Mautnerd2328952013-03-05 12:46:26 -0800887 final ActivityRecord r = isInStackLocked(token);
888 if (r != null) {
889 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Craig Mautner21d24a22014-04-23 11:45:37 -0700890 if (persistentState != null) {
891 r.persistentState = persistentState;
892 mService.notifyTaskPersisterLocked(r.task, false);
893 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800894 if (mPausingActivity == r) {
895 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
896 + (timeout ? " (due to timeout)" : " (pause complete)"));
897 r.state = ActivityState.PAUSED;
898 completePauseLocked();
899 } else {
900 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
901 r.userId, System.identityHashCode(r), r.shortComponentName,
902 mPausingActivity != null
903 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700904 }
905 }
906 }
907
Craig Mautnera0026042014-04-23 11:45:37 -0700908 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
909 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700910 if (r.state != ActivityState.STOPPING) {
911 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
912 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
913 return;
914 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700915 if (persistentState != null) {
916 r.persistentState = persistentState;
917 mService.notifyTaskPersisterLocked(r.task, false);
918 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700919 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700920 if (icicle != null) {
921 // If icicle is null, this is happening due to a timeout, so we
922 // haven't really saved the state.
923 r.icicle = icicle;
924 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800925 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700926 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700927 }
928 if (!r.stopped) {
929 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
930 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
931 r.stopped = true;
932 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700933 if (r.finishing) {
934 r.clearOptionsLocked();
935 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700936 if (r.configDestroy) {
937 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700938 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700939 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700940 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800941 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700942 }
943 }
944 }
945
Craig Mautnerc8143c62013-09-03 12:15:57 -0700946 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800947 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700948 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800949
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800950 if (prev != null) {
951 if (prev.finishing) {
952 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700953 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800954 } else if (prev.app != null) {
955 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
956 if (prev.waitingVisible) {
957 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
960 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800961 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800962 if (prev.configDestroy) {
963 // The previous is being paused because the configuration
964 // is changing, which means it is actually stopping...
965 // To juggle the fact that we are also starting a new
966 // instance right now, we need to first completely stop
967 // the current instance before starting the new one.
968 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
969 destroyActivityLocked(prev, true, false, "pause-config");
970 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700971 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700972 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
973 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800974 // If we already have a few activities waiting to stop,
975 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700976 // them out. Or if r is the last of activity of the last task the stack
977 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700979 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800980 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700981 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800982 }
983 }
984 } else {
985 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
986 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800987 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800990
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700991 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700992 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700993 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800994 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700995 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700996 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700997 if (top == null || (prev != null && top != prev)) {
998 // If there are no more activities available to run,
999 // do resume anyway to start something. Also if the top
1000 // activity on the stack is not the just paused activity,
1001 // we need to go ahead and resume it to ensure we complete
1002 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -07001003 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001004 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001005 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001006
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 if (prev != null) {
1008 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009
Craig Mautner525f3d92013-05-07 14:01:50 -07001010 if (prev.app != null && prev.cpuTimeAtResume > 0
1011 && mService.mBatteryStatsService.isOnBattery()) {
1012 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -07001013 synchronized (mService.mProcessCpuThread) {
1014 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -07001015 - prev.cpuTimeAtResume;
1016 }
1017 if (diff > 0) {
1018 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1019 synchronized (bsi) {
1020 BatteryStatsImpl.Uid.Proc ps =
1021 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001022 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001023 if (ps != null) {
1024 ps.addForegroundTimeLocked(diff);
1025 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
1027 }
1028 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001029 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001030 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001031 }
1032
1033 /**
1034 * Once we know that we have asked an application to put an activity in
1035 * the resumed state (either by launching it or explicitly telling it),
1036 * this function updates the rest of our state to match that fact.
1037 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001038 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001039 next.idle = false;
1040 next.results = null;
1041 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -07001042 if (next.nowVisible) {
1043 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1044 mStackSupervisor.dismissKeyguard();
1045 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001046
1047 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001048 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001049
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001050 mStackSupervisor.reportResumedActivityLocked(next);
1051
1052 next.resumeKeyDispatchingLocked();
1053 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001054
1055 // Mark the point when the activity is resuming
1056 // TODO: To be more accurate, the mark should be before the onCreate,
1057 // not after the onResume. But for subsequent starts, onResume is fine.
1058 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001059 synchronized (mService.mProcessCpuThread) {
1060 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061 }
1062 } else {
1063 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1064 }
Winson Chung376543b2014-05-21 17:43:28 -07001065
1066 // If we are resuming the activity that we had last screenshotted, then we know it will be
1067 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1068 if (next == mLastScreenshotActivity) {
1069 invalidateLastScreenshot();
1070 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071 }
1072
Craig Mautnere3a00d72014-04-16 08:31:19 -07001073 private void setVisibile(ActivityRecord r, boolean visible) {
1074 r.visible = visible;
1075 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001076 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001077 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001078 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001079 container.setVisible(visible);
1080 }
1081 }
1082
Jose Lima7ba71252014-04-30 12:59:27 -07001083 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1084 // If so, this stack is hidden, otherwise it is visible.
1085 private boolean isStackVisible() {
1086 if (!isAttached()) {
1087 return false;
1088 }
1089
1090 if (mStackSupervisor.isFrontStack(this)) {
1091 return true;
1092 }
1093
Jose Lima729cb232014-05-05 15:59:40 -07001094 /**
1095 * Start at the task above this one and go up, looking for a visible
1096 * fullscreen activity, or a translucent activity that requested the
1097 * wallpaper to be shown behind it.
1098 */
Jose Lima7ba71252014-04-30 12:59:27 -07001099 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1100 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1101 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001102 final TaskRecord task = tasks.get(taskNdx);
1103 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001104 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1105 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001106
1107 // Conditions for an activity to obscure the stack we're
1108 // examining:
1109 // 1. Not Finishing AND Visible AND:
1110 // 2. Either:
1111 // - Full Screen Activity OR
1112 // - On top of Home and our stack is NOT home
1113 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001114 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001115 return false;
1116 }
1117 }
1118 }
1119 }
1120
1121 return true;
1122 }
1123
Jose Lima729cb232014-05-05 15:59:40 -07001124 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1125 ActivityRecord r = topRunningActivityLocked(null);
1126 if (r != null) {
1127 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1128 }
1129 }
1130
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131 /**
1132 * Make sure that all activities that need to be visible (that is, they
1133 * currently can be seen by the user) actually are.
1134 */
Jose Lima729cb232014-05-05 15:59:40 -07001135 final void ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1136 String onlyThisProcess, int configChanges) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001137 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 TAG, "ensureActivitiesVisible behind " + top
1139 + " configChanges=0x" + Integer.toHexString(configChanges));
1140
Craig Mautner5eda9b32013-07-02 11:58:16 -07001141 if (mTranslucentActivityWaiting != top) {
1142 mUndrawnActivitiesBelowTopTranslucent.clear();
1143 if (mTranslucentActivityWaiting != null) {
1144 // Call the callback with a timeout indication.
1145 notifyActivityDrawnLocked(null);
1146 mTranslucentActivityWaiting = null;
1147 }
1148 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1149 }
1150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151 // If the top activity is not fullscreen, then we need to
1152 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001153 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001154 boolean behindFullscreen = !isStackVisible();
1155
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001156 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001157 final TaskRecord task = mTaskHistory.get(taskNdx);
1158 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001159 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1160 final ActivityRecord r = activities.get(activityNdx);
1161 if (r.finishing) {
1162 continue;
1163 }
1164 if (aboveTop && r != top) {
1165 continue;
1166 }
1167 aboveTop = false;
1168 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001169 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001170 TAG, "Make visible? " + r + " finishing=" + r.finishing
1171 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001172
Craig Mautnerd44711d2013-02-23 11:24:36 -08001173 final boolean doThisProcess = onlyThisProcess == null
1174 || onlyThisProcess.equals(r.processName);
1175
1176 // First: if this is not the current activity being started, make
1177 // sure it matches the current configuration.
1178 if (r != starting && doThisProcess) {
1179 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001181
1182 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001183 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001184 // This activity needs to be visible, but isn't even
1185 // running... get it started, but don't resume it
1186 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001187 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001188 if (r != starting) {
1189 r.startFreezingScreenLocked(r.app, configChanges);
1190 }
1191 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001192 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001193 TAG, "Starting and making visible: " + r);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001194 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001195 }
1196 if (r != starting) {
George Mount2c92c972014-03-20 09:38:23 -07001197 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001198 }
1199 }
1200
1201 } else if (r.visible) {
1202 // If this activity is already visible, then there is nothing
1203 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001204 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001206
1207 } else if (onlyThisProcess == null) {
1208 // This activity is not currently visible, but is running.
1209 // Tell it to become visible.
1210 r.visible = true;
1211 if (r.state != ActivityState.RESUMED && r != starting) {
1212 // If this activity is paused, tell it
1213 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001214 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001215 TAG, "Making visible and scheduling visibility: " + r);
1216 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001217 if (mTranslucentActivityWaiting != null) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001218 r.updateOptionsLocked(mReturningActivityOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001219 mUndrawnActivitiesBelowTopTranslucent.add(r);
1220 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001221 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001222 r.sleeping = false;
1223 r.app.pendingUiClean = true;
1224 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1225 r.stopFreezingScreenLocked(false);
1226 } catch (Exception e) {
1227 // Just skip on any failure; we'll make it
1228 // visible when it next restarts.
1229 Slog.w(TAG, "Exception thrown making visibile: "
1230 + r.intent.getComponent(), e);
1231 }
1232 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001233 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234
Craig Mautnerd44711d2013-02-23 11:24:36 -08001235 // Aggregate current change flags.
1236 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237
Craig Mautnerd44711d2013-02-23 11:24:36 -08001238 if (r.fullscreen) {
1239 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001240 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1241 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001242 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001243 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001244 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001245 }
1246 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001247 if (DEBUG_VISBILITY) Slog.v(
1248 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1249 + " state=" + r.state
1250 + " behindFullscreen=" + behindFullscreen);
1251 // Now for any activities that aren't visible to the user, make
1252 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001254 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001255 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001256 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001257 switch (r.state) {
1258 case STOPPING:
1259 case STOPPED:
1260 if (r.app != null && r.app.thread != null) {
1261 if (DEBUG_VISBILITY) Slog.v(
1262 TAG, "Scheduling invisibility: " + r);
1263 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1264 }
1265 break;
1266
1267 case INITIALIZING:
1268 case RESUMED:
1269 case PAUSING:
1270 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001271 // This case created for transitioning activities from
1272 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001273 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1274 mStackSupervisor.mStoppingActivities.add(r);
1275 }
1276 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001277 break;
1278
1279 default:
1280 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001281 }
1282 } catch (Exception e) {
1283 // Just skip on any failure; we'll make it
1284 // visible when it next restarts.
1285 Slog.w(TAG, "Exception thrown making hidden: "
1286 + r.intent.getComponent(), e);
1287 }
1288 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001289 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291 }
1292 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001293 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001294 }
Craig Mautner58547802013-03-05 08:23:53 -08001295
Craig Mautner233ceee2014-05-09 17:05:11 -07001296 void convertToTranslucent(ActivityRecord r, ActivityOptions options) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001297 mTranslucentActivityWaiting = r;
1298 mUndrawnActivitiesBelowTopTranslucent.clear();
Craig Mautner233ceee2014-05-09 17:05:11 -07001299 mReturningActivityOptions = options;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001300 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1301 }
1302
1303 /**
1304 * Called as activities below the top translucent activity are redrawn. When the last one is
1305 * redrawn notify the top activity by calling
1306 * {@link Activity#onTranslucentConversionComplete}.
1307 *
1308 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1309 * occurred and the activity will be notified immediately.
1310 */
1311 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001312 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001313 if ((r == null)
1314 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1315 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1316 // The last undrawn activity below the top has just been drawn. If there is an
1317 // opaque activity at the top, notify it that it can become translucent safely now.
1318 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1319 mTranslucentActivityWaiting = null;
1320 mUndrawnActivitiesBelowTopTranslucent.clear();
1321 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1322
Craig Mautner71dd1b62014-02-18 15:48:52 -08001323 if (waitingActivity != null) {
1324 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1325 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1326 try {
1327 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1328 waitingActivity.appToken, r != null);
1329 } catch (RemoteException e) {
1330 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001331 }
1332 }
1333 }
1334 }
1335
Craig Mautnera61bc652013-10-28 15:43:18 -07001336 /** If any activities below the top running one are in the INITIALIZING state and they have a
1337 * starting window displayed then remove that starting window. It is possible that the activity
1338 * in this state will never resumed in which case that starting window will be orphaned. */
1339 void cancelInitializingActivities() {
1340 final ActivityRecord topActivity = topRunningActivityLocked(null);
1341 boolean aboveTop = true;
1342 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1343 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1344 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1345 final ActivityRecord r = activities.get(activityNdx);
1346 if (aboveTop) {
1347 if (r == topActivity) {
1348 aboveTop = false;
1349 }
1350 continue;
1351 }
1352
1353 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1354 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1355 r.mStartingWindowShown = false;
1356 mWindowManager.removeAppStartingWindow(r.appToken);
1357 }
1358 }
1359 }
1360 }
1361
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001362 /**
1363 * Ensure that the top activity in the stack is resumed.
1364 *
1365 * @param prev The previously resumed activity, for when in the process
1366 * of pausing; can be null to call from elsewhere.
1367 *
1368 * @return Returns true if something is being resumed, or false if
1369 * nothing happened.
1370 */
1371 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001372 return resumeTopActivityLocked(prev, null);
1373 }
1374
1375 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001376 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1377
Craig Mautnerdf88d732014-01-27 09:21:32 -08001378 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001379 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001380 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001381 // Do not resume this stack if its parent is not resumed.
1382 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1383 return false;
1384 }
1385
Craig Mautnera61bc652013-10-28 15:43:18 -07001386 cancelInitializingActivities();
1387
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001388 // Find the first activity that is not finishing.
1389 ActivityRecord next = topRunningActivityLocked(null);
1390
1391 // Remember how we'll process this pause/resume situation, and ensure
1392 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001393 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1394 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395
Craig Mautner84984fa2014-06-19 11:19:20 -07001396 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001397 if (next == null) {
1398 // There are no more activities! Let's just start up the
1399 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001400 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001401 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001402 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001403 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001404 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1405 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1406 return isOnHomeDisplay() &&
1407 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 }
1409
1410 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001411
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001412 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001413 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1414 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001415 // Make sure we have executed any pending transitions, since there
1416 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001417 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001418 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001419 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001420 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001421 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 return false;
1423 }
1424
Craig Mautner525f3d92013-05-07 14:01:50 -07001425 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001426 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001427 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001428 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001429 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001430 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001431 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001432 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001433 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001434 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001435 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001436 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001437 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1438 "resumeTopActivityLocked: Launching home next");
1439 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001440 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1441 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1442 return isOnHomeDisplay() &&
1443 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001444 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001445 }
1446
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001447 // If we are sleeping, and there is no resumed activity, and the top
1448 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001449 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001450 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001451 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001452 // Make sure we have executed any pending transitions, since there
1453 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001454 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001455 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001456 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001457 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001458 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 return false;
1460 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001461
1462 // Make sure that the user who owns this activity is started. If not,
1463 // we will just leave it as is because someone should be bringing
1464 // another user's activities to the top of the stack.
1465 if (mService.mStartedUsers.get(next.userId) == null) {
1466 Slog.w(TAG, "Skipping resume of top activity " + next
1467 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001468 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001469 return false;
1470 }
1471
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001472 // The activity may be waiting for stop, but that is no longer
1473 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001474 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001475 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001476 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001477 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478
1479 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1480
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001481 // If we are currently pausing an activity, then don't do anything
1482 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001483 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001484 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1485 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001486 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001487 return false;
1488 }
1489
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001490 // Okay we are now going to start a switch, to 'next'. We may first
1491 // have to pause the current activity, but this is an important point
1492 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001493 // XXX "App Redirected" dialog is getting too many false positives
1494 // at this point, so turn off for now.
1495 if (false) {
1496 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1497 long now = SystemClock.uptimeMillis();
1498 final boolean inTime = mLastStartedActivity.startTime != 0
1499 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1500 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1501 final int nextUid = next.info.applicationInfo.uid;
1502 if (inTime && lastUid != nextUid
1503 && lastUid != next.launchedFromUid
1504 && mService.checkPermission(
1505 android.Manifest.permission.STOP_APP_SWITCHES,
1506 -1, next.launchedFromUid)
1507 != PackageManager.PERMISSION_GRANTED) {
1508 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1509 } else {
1510 next.startTime = now;
1511 mLastStartedActivity = next;
1512 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001513 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001514 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001515 mLastStartedActivity = next;
1516 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001517 }
Craig Mautner58547802013-03-05 08:23:53 -08001518
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 // We need to start pausing the current activity so the top one
1520 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001521 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001522 if (mResumedActivity != null) {
1523 pausing = true;
1524 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001525 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001526 }
1527 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001528 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1529 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001530 // At this point we want to put the upcoming activity's process
1531 // at the top of the LRU list, since we know we will be needing it
1532 // very soon and it would be a waste to let it get killed if it
1533 // happens to be sitting towards the end.
1534 if (next.app != null && next.app.thread != null) {
1535 // No reason to do full oom adj update here; we'll let that
1536 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001537 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001538 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001539 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540 return true;
1541 }
1542
Christopher Tated3f175c2012-06-14 14:16:54 -07001543 // If the most recent activity was noHistory but was only stopped rather
1544 // than stopped+finished because the device went to sleep, we need to make
1545 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001546 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001547 !mLastNoHistoryActivity.finishing) {
1548 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1549 " on new resume");
1550 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1551 null, "no-history", false);
1552 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001553 }
1554
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001555 if (prev != null && prev != next) {
1556 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1557 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001558 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 if (DEBUG_SWITCH) Slog.v(
1560 TAG, "Resuming top, waiting visible to hide: " + prev);
1561 } else {
1562 // The next activity is already visible, so hide the previous
1563 // activity's windows right now so we can show the new one ASAP.
1564 // We only do this if the previous is finishing, which should mean
1565 // it is on top of the one being resumed so hiding it quickly
1566 // is good. Otherwise, we want to do the normal route of allowing
1567 // the resumed activity to be shown so we can decide if the
1568 // previous should actually be hidden depending on whether the
1569 // new one is found to be full-screen or not.
1570 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001571 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1573 + prev + ", waitingVisible="
1574 + (prev != null ? prev.waitingVisible : null)
1575 + ", nowVisible=" + next.nowVisible);
1576 } else {
1577 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1578 + prev + ", waitingVisible="
1579 + (prev != null ? prev.waitingVisible : null)
1580 + ", nowVisible=" + next.nowVisible);
1581 }
1582 }
1583 }
1584
Dianne Hackborne7f97212011-02-24 14:40:20 -08001585 // Launching this app's activity, make sure the app is no longer
1586 // considered stopped.
1587 try {
1588 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001589 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001590 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001591 } catch (IllegalArgumentException e) {
1592 Slog.w(TAG, "Failed trying to unstop package "
1593 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001594 }
1595
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 // We are starting up the next activity, so tell the window manager
1597 // that the previous one will be hidden soon. This way it can know
1598 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001599 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001600 if (prev != null) {
1601 if (prev.finishing) {
1602 if (DEBUG_TRANSITION) Slog.v(TAG,
1603 "Prepare close transition: prev=" + prev);
1604 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001605 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001606 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001608 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001609 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1610 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001612 mWindowManager.setAppWillBeHidden(prev.appToken);
1613 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001615 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001617 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001618 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001620 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001621 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1622 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 }
1624 }
1625 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001626 mWindowManager.setAppWillBeHidden(prev.appToken);
1627 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001628 }
Craig Mautner967212c2013-04-13 21:10:58 -07001629 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001630 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001631 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001632 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001633 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001635 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 }
1637 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001638
1639 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001640 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001641 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1642 if (opts != null) {
1643 resumeAnimOptions = opts.toBundle();
1644 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001645 next.applyOptionsLocked();
1646 } else {
1647 next.clearOptionsLocked();
1648 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649
Craig Mautnercf910b02013-04-23 11:23:27 -07001650 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 if (next.app != null && next.app.thread != null) {
1652 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1653
1654 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001655 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001656
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001657 // schedule launch ticks to collect information about slow apps.
1658 next.startLaunchTickingLocked();
1659
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001660 ActivityRecord lastResumedActivity =
1661 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001662 ActivityState lastState = next.state;
1663
1664 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001665
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001666 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 next.state = ActivityState.RESUMED;
1668 mResumedActivity = next;
1669 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001670 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001671 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001672 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001673 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001674
1675 // Have the window manager re-evaluate the orientation of
1676 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001677 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001678 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001679 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001680 mService.mConfiguration,
1681 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1682 if (config != null) {
1683 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001685 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001686 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001687
Craig Mautner525f3d92013-05-07 14:01:50 -07001688 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001689 // The configuration update wasn't able to keep the existing
1690 // instance of the activity, and instead started a new one.
1691 // We should be all done, but let's just make sure our activity
1692 // is still at the top and schedule another run if something
1693 // weird happened.
1694 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001695 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 "Activity config changed during resume: " + next
1697 + ", new next: " + nextNext);
1698 if (nextNext != next) {
1699 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001700 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001701 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001702 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001703 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001704 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001705 return true;
1706 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001707 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001708 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001709 }
Craig Mautner58547802013-03-05 08:23:53 -08001710
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 try {
1712 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001713 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001714 if (a != null) {
1715 final int N = a.size();
1716 if (!next.finishing && N > 0) {
1717 if (DEBUG_RESULTS) Slog.v(
1718 TAG, "Delivering results to " + next
1719 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001720 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 }
1722 }
1723
1724 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001725 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 }
1727
1728 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001729 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001731
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001732 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001733 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001734 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001735 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001736 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001737 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001738 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001739
Craig Mautner0eea92c2013-05-16 13:35:39 -07001740 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741
Craig Mautnerac6f8432013-07-17 13:24:59 -07001742 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 } catch (Exception e) {
1744 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001745 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1746 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001748 if (lastStack != null) {
1749 lastStack.mResumedActivity = lastResumedActivity;
1750 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751 Slog.i(TAG, "Restarting because process died: " + next);
1752 if (!next.hasBeenLaunched) {
1753 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001754 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1755 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001756 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001757 next.appToken, next.packageName, next.theme,
1758 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001759 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1760 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001761 }
George Mount2c92c972014-03-20 09:38:23 -07001762 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001763 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001764 return true;
1765 }
1766
1767 // From this point on, if something goes wrong there is no way
1768 // to recover the activity.
1769 try {
1770 next.visible = true;
1771 completeResumeLocked(next);
1772 } catch (Exception e) {
1773 // If any exception gets thrown, toss away this
1774 // activity and try the next one.
1775 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001776 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001777 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001778 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001779 return true;
1780 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001781 next.stopped = false;
1782
1783 } else {
1784 // Whoops, need to restart this activity!
1785 if (!next.hasBeenLaunched) {
1786 next.hasBeenLaunched = true;
1787 } else {
1788 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001789 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001790 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001791 mService.compatibilityInfoForPackageLocked(
1792 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001794 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001795 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001796 }
1797 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1798 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001799 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001800 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 }
1802
Craig Mautnercf910b02013-04-23 11:23:27 -07001803 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 return true;
1805 }
1806
Craig Mautnerac6f8432013-07-17 13:24:59 -07001807 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001808 // If this is being moved to the top by another activity or being launched from the home
1809 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001810 if (isOnHomeDisplay()) {
1811 ActivityStack lastStack = mStackSupervisor.getLastStack();
1812 final boolean fromHome = lastStack.isHomeStack();
1813 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001814 task.setTaskToReturnTo(fromHome ?
1815 lastStack.topTask().taskType : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001816 }
1817 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001818 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001819 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001820
Craig Mautnerac6f8432013-07-17 13:24:59 -07001821 mTaskHistory.remove(task);
1822 // Now put task at top.
1823 int stackNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001824 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001825 // Put non-current user tasks below current user tasks.
1826 while (--stackNdx >= 0) {
Kenny Guy2a764942014-04-02 13:29:20 +01001827 if (!isCurrentProfileLocked(mTaskHistory.get(stackNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001828 break;
1829 }
1830 }
1831 ++stackNdx;
1832 }
1833 mTaskHistory.add(stackNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001834 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001835 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001836
Craig Mautner8849a5e2013-04-02 16:41:03 -07001837 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001838 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001839 TaskRecord rTask = r.task;
1840 final int taskId = rTask.taskId;
1841 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001842 // Last activity in task had been removed or ActivityManagerService is reusing task.
1843 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001844 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001845 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001846 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001847 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001848 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 if (!newTask) {
1850 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001851 boolean startIt = true;
1852 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1853 task = mTaskHistory.get(taskNdx);
1854 if (task == r.task) {
1855 // Here it is! Now, if this is not yet visible to the
1856 // user, then just add it without starting; it will
1857 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001858 if (!startIt) {
1859 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1860 + task, new RuntimeException("here").fillInStackTrace());
1861 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001862 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001863 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1864 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001865 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001866 r.userId, r.info.configChanges, task.voiceSession != null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001867 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001868 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001870 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 return;
1872 }
1873 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001874 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001875 startIt = false;
1876 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 }
1878 }
1879
1880 // Place a new activity at top of stack, so it is next to interact
1881 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001882
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883 // If we are not placing the new activity frontmost, we do not want
1884 // to deliver the onUserLeaving callback to the actual frontmost
1885 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001886 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001887 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001888 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1889 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001890 }
Craig Mautner70a86932013-02-28 22:37:44 -08001891
1892 task = r.task;
1893
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001894 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001895 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001896 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001897 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001898 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001899
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001900 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001901 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001902 // We want to show the starting preview window if we are
1903 // switching to a new task, or the next activity's process is
1904 // not currently running.
1905 boolean showStartingIcon = newTask;
1906 ProcessRecord proc = r.app;
1907 if (proc == null) {
1908 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1909 }
1910 if (proc == null || proc.thread == null) {
1911 showStartingIcon = true;
1912 }
1913 if (DEBUG_TRANSITION) Slog.v(TAG,
1914 "Prepare open transition: starting " + r);
1915 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001916 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001918 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001919 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001920 ? AppTransition.TRANSIT_TASK_OPEN
1921 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001922 mNoAnimActivities.remove(r);
1923 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001924 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001925 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001926 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001927 r.info.configChanges, task.voiceSession != null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001928 boolean doShow = true;
1929 if (newTask) {
1930 // Even though this activity is starting fresh, we still need
1931 // to reset it to make sure we apply affinities to move any
1932 // existing activities from other tasks in to it.
1933 // If the caller has requested that the target task be
1934 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001935 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001936 resetTaskIfNeededLocked(r, r);
1937 doShow = topRunningNonDelayedActivityLocked(null) == r;
1938 }
George Mount70778d72014-07-01 16:33:45 -07001939 } else if (options != null && new ActivityOptions(options).getAnimationType()
1940 == ActivityOptions.ANIM_SCENE_TRANSITION) {
1941 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001942 }
1943 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1944 // Figure out if we are transitioning from another activity that is
1945 // "has the same starting icon" as the next one. This allows the
1946 // window manager to keep the previous window it had previously
1947 // created, if it still had one.
1948 ActivityRecord prev = mResumedActivity;
1949 if (prev != null) {
1950 // We don't want to reuse the previous starting preview if:
1951 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001952 if (prev.task != r.task) {
1953 prev = null;
1954 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001956 else if (prev.nowVisible) {
1957 prev = null;
1958 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001961 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001962 mService.compatibilityInfoForPackageLocked(
1963 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001964 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001965 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07001966 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 }
1968 } else {
1969 // If this is the first activity, don't do any fancy animations,
1970 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001971 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001972 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001973 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001974 r.info.configChanges, task.voiceSession != null);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001975 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07001976 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001977 }
1978 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001979 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 }
1981
1982 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07001983 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 }
1985 }
1986
Dianne Hackbornbe707852011-11-11 14:32:10 -08001987 final void validateAppTokensLocked() {
1988 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001989 mValidateAppTokens.ensureCapacity(numActivities());
1990 final int numTasks = mTaskHistory.size();
1991 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1992 TaskRecord task = mTaskHistory.get(taskNdx);
1993 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001994 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001995 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001996 }
Craig Mautner000f0022013-02-26 15:04:29 -08001997 TaskGroup group = new TaskGroup();
1998 group.taskId = task.taskId;
1999 mValidateAppTokens.add(group);
2000 final int numActivities = activities.size();
2001 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2002 final ActivityRecord r = activities.get(activityNdx);
2003 group.tokens.add(r.appToken);
2004 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002005 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002006 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002007 }
2008
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002009 /**
2010 * Perform a reset of the given task, if needed as part of launching it.
2011 * Returns the new HistoryRecord at the top of the task.
2012 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002013 /**
2014 * Helper method for #resetTaskIfNeededLocked.
2015 * We are inside of the task being reset... we'll either finish this activity, push it out
2016 * for another task, or leave it as-is.
2017 * @param task The task containing the Activity (taskTop) that might be reset.
2018 * @param forceReset
2019 * @return An ActivityOptions that needs to be processed.
2020 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002021 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002022 ActivityOptions topOptions = null;
2023
2024 int replyChainEnd = -1;
2025 boolean canMoveOptions = true;
2026
2027 // We only do this for activities that are not the root of the task (since if we finish
2028 // the root, we may no longer have the task!).
2029 final ArrayList<ActivityRecord> activities = task.mActivities;
2030 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002031 final int rootActivityNdx = task.findEffectiveRootIndex();
2032 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002033 ActivityRecord target = activities.get(i);
2034
2035 final int flags = target.info.flags;
2036 final boolean finishOnTaskLaunch =
2037 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2038 final boolean allowTaskReparenting =
2039 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2040 final boolean clearWhenTaskReset =
2041 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2042
2043 if (!finishOnTaskLaunch
2044 && !clearWhenTaskReset
2045 && target.resultTo != null) {
2046 // If this activity is sending a reply to a previous
2047 // activity, we can't do anything with it now until
2048 // we reach the start of the reply chain.
2049 // XXX note that we are assuming the result is always
2050 // to the previous activity, which is almost always
2051 // the case but we really shouldn't count on.
2052 if (replyChainEnd < 0) {
2053 replyChainEnd = i;
2054 }
2055 } else if (!finishOnTaskLaunch
2056 && !clearWhenTaskReset
2057 && allowTaskReparenting
2058 && target.taskAffinity != null
2059 && !target.taskAffinity.equals(task.affinity)) {
2060 // If this activity has an affinity for another
2061 // task, then we need to move it out of here. We will
2062 // move it as far out of the way as possible, to the
2063 // bottom of the activity stack. This also keeps it
2064 // correctly ordered with any activities we previously
2065 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002066 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002067 final ActivityRecord bottom =
2068 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002069 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002070 if (bottom != null && target.taskAffinity != null
2071 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002072 // If the activity currently at the bottom has the
2073 // same task affinity as the one we are moving,
2074 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002075 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002076 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002077 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002078 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002079 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002080 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002081 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002082 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2083 + " out to new task " + target.task);
2084 }
2085
Craig Mautnere3a74d52013-02-22 14:14:58 -08002086 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002087 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002088
Craig Mautner525f3d92013-05-07 14:01:50 -07002089 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002090 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2091 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002092 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002093 if (p.finishing) {
2094 continue;
2095 }
2096
Craig Mautnere3a74d52013-02-22 14:14:58 -08002097 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002098 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002099 topOptions = p.takeOptionsLocked();
2100 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002101 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002102 }
2103 }
2104 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002105 + task + " adding to task=" + targetTask
2106 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002107 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2108 + " out to target's task " + target.task);
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002109 p.setTask(targetTask, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002111
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002112 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002113 }
2114
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002115 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002116 if (VALIDATE_TOKENS) {
2117 validateAppTokensLocked();
2118 }
2119
2120 replyChainEnd = -1;
2121 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2122 // If the activity should just be removed -- either
2123 // because it asks for it, or the task should be
2124 // cleared -- then finish it and anything that is
2125 // part of its reply chain.
2126 int end;
2127 if (clearWhenTaskReset) {
2128 // In this case, we want to finish this activity
2129 // and everything above it, so be sneaky and pretend
2130 // like these are all in the reply chain.
2131 end = numActivities - 1;
2132 } else if (replyChainEnd < 0) {
2133 end = i;
2134 } else {
2135 end = replyChainEnd;
2136 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002137 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002138 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002139 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140 if (p.finishing) {
2141 continue;
2142 }
2143 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002144 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002145 topOptions = p.takeOptionsLocked();
2146 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002147 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002148 }
2149 }
Craig Mautner58547802013-03-05 08:23:53 -08002150 if (DEBUG_TASKS) Slog.w(TAG,
2151 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002152 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002153 end--;
2154 srcPos--;
2155 }
2156 }
2157 replyChainEnd = -1;
2158 } else {
2159 // If we were in the middle of a chain, well the
2160 // activity that started it all doesn't want anything
2161 // special, so leave it all as-is.
2162 replyChainEnd = -1;
2163 }
2164 }
2165
2166 return topOptions;
2167 }
2168
2169 /**
2170 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2171 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2172 * @param affinityTask The task we are looking for an affinity to.
2173 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2174 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2175 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2176 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002177 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002178 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002179 int replyChainEnd = -1;
2180 final int taskId = task.taskId;
2181 final String taskAffinity = task.affinity;
2182
2183 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2184 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002185 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2186
2187 // Do not operate on or below the effective root Activity.
2188 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002189 ActivityRecord target = activities.get(i);
2190
2191 final int flags = target.info.flags;
2192 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2193 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2194
2195 if (target.resultTo != null) {
2196 // If this activity is sending a reply to a previous
2197 // activity, we can't do anything with it now until
2198 // we reach the start of the reply chain.
2199 // XXX note that we are assuming the result is always
2200 // to the previous activity, which is almost always
2201 // the case but we really shouldn't count on.
2202 if (replyChainEnd < 0) {
2203 replyChainEnd = i;
2204 }
2205 } else if (topTaskIsHigher
2206 && allowTaskReparenting
2207 && taskAffinity != null
2208 && taskAffinity.equals(target.taskAffinity)) {
2209 // This activity has an affinity for our task. Either remove it if we are
2210 // clearing or move it over to our task. Note that
2211 // we currently punt on the case where we are resetting a
2212 // task that is not at the top but who has activities above
2213 // with an affinity to it... this is really not a normal
2214 // case, and we will need to later pull that task to the front
2215 // and usually at that point we will do the reset and pick
2216 // up those remaining activities. (This only happens if
2217 // someone starts an activity in a new task from an activity
2218 // in a task that is not currently on top.)
2219 if (forceReset || finishOnTaskLaunch) {
2220 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2221 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2222 for (int srcPos = start; srcPos >= i; --srcPos) {
2223 final ActivityRecord p = activities.get(srcPos);
2224 if (p.finishing) {
2225 continue;
2226 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002227 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002228 }
2229 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002230 if (taskInsertionPoint < 0) {
2231 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002232
Craig Mautner77878772013-03-04 19:46:24 -08002233 }
Craig Mautner77878772013-03-04 19:46:24 -08002234
2235 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2236 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2237 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2238 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002239 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002240 p.setTask(task, false);
Craig Mautner77878772013-03-04 19:46:24 -08002241 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002242
Craig Mautnere3a74d52013-02-22 14:14:58 -08002243 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2244 + " to stack at " + task,
2245 new RuntimeException("here").fillInStackTrace());
2246 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2247 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002248 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002249 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002250 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002251 if (VALIDATE_TOKENS) {
2252 validateAppTokensLocked();
2253 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002254
2255 // Now we've moved it in to place... but what if this is
2256 // a singleTop activity and we have put it on top of another
2257 // instance of the same activity? Then we drop the instance
2258 // below so it remains singleTop.
2259 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2260 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002261 int targetNdx = taskActivities.indexOf(target);
2262 if (targetNdx > 0) {
2263 ActivityRecord p = taskActivities.get(targetNdx - 1);
2264 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002265 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2266 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002267 }
2268 }
2269 }
2270 }
2271
2272 replyChainEnd = -1;
2273 }
2274 }
Craig Mautner77878772013-03-04 19:46:24 -08002275 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002276 }
2277
Craig Mautner8849a5e2013-04-02 16:41:03 -07002278 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002279 ActivityRecord newActivity) {
2280 boolean forceReset =
2281 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2282 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2283 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2284 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2285 forceReset = true;
2286 }
2287 }
2288
2289 final TaskRecord task = taskTop.task;
2290
2291 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2292 * for remaining tasks. Used for later tasks to reparent to task. */
2293 boolean taskFound = false;
2294
2295 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2296 ActivityOptions topOptions = null;
2297
Craig Mautner77878772013-03-04 19:46:24 -08002298 // Preserve the location for reparenting in the new task.
2299 int reparentInsertionPoint = -1;
2300
Craig Mautnere3a74d52013-02-22 14:14:58 -08002301 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2302 final TaskRecord targetTask = mTaskHistory.get(i);
2303
2304 if (targetTask == task) {
2305 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2306 taskFound = true;
2307 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002308 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2309 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002310 }
2311 }
2312
Craig Mautner70a86932013-02-28 22:37:44 -08002313 int taskNdx = mTaskHistory.indexOf(task);
2314 do {
2315 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2316 } while (taskTop == null && taskNdx >= 0);
2317
Craig Mautnere3a74d52013-02-22 14:14:58 -08002318 if (topOptions != null) {
2319 // If we got some ActivityOptions from an activity on top that
2320 // was removed from the task, propagate them to the new real top.
2321 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002322 taskTop.updateOptionsLocked(topOptions);
2323 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002324 topOptions.abort();
2325 }
2326 }
2327
2328 return taskTop;
2329 }
2330
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002331 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2332 String resultWho, int requestCode, int resultCode, Intent data) {
2333
2334 if (callingUid > 0) {
2335 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002336 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 }
2338
2339 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2340 + " : who=" + resultWho + " req=" + requestCode
2341 + " res=" + resultCode + " data=" + data);
2342 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2343 try {
2344 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2345 list.add(new ResultInfo(resultWho, requestCode,
2346 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002347 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 return;
2349 } catch (Exception e) {
2350 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2351 }
2352 }
2353
2354 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2355 }
2356
Craig Mautner04f0b702013-10-22 12:31:01 -07002357 private void adjustFocusedActivityLocked(ActivityRecord r) {
2358 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2359 ActivityRecord next = topRunningActivityLocked(null);
2360 if (next != r) {
2361 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002362 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2363 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002364 }
2365 }
Winson Chung648c83b2014-04-28 15:11:56 -07002366 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2367 if (top != null) {
2368 mService.setFocusedActivityLocked(top);
2369 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002370 }
2371 }
2372
Craig Mautnerf3333272013-04-22 10:55:53 -07002373 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2375 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2376 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2377 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002378 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002379 if (DEBUG_STATES) {
2380 Slog.d(TAG, "no-history finish of " + r);
2381 }
2382 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002383 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002384 } else {
2385 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2386 + " on stop because we're just sleeping");
2387 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002388 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002389 }
2390
2391 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002392 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002393 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 try {
2395 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002396 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2397 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 r.state = ActivityState.STOPPING;
2399 if (DEBUG_VISBILITY) Slog.v(
2400 TAG, "Stopping visible=" + r.visible + " for " + r);
2401 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002402 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002403 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002404 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002405 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002406 r.setSleeping(true);
2407 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002408 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002409 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002410 } catch (Exception e) {
2411 // Maybe just ignore exceptions here... if the process
2412 // has crashed, our death notification will clean things
2413 // up.
2414 Slog.w(TAG, "Exception thrown during pause", e);
2415 // Just in case, assume it to be stopped.
2416 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002417 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002418 r.state = ActivityState.STOPPED;
2419 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002420 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 }
2422 }
2423 }
2424 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002425
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002426 /**
2427 * @return Returns true if the activity is being finished, false if for
2428 * some reason it is being left as-is.
2429 */
2430 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002431 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002432 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002433 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002434 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002435 + ", result=" + resultCode + ", data=" + resultData
2436 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002437 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 return false;
2439 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002440
Craig Mautnerd44711d2013-02-23 11:24:36 -08002441 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002442 return true;
2443 }
2444
Craig Mautnerd2328952013-03-05 12:46:26 -08002445 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002446 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2447 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2448 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2449 ActivityRecord r = activities.get(activityNdx);
2450 if (r.resultTo == self && r.requestCode == requestCode) {
2451 if ((r.resultWho == null && resultWho == null) ||
2452 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2453 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2454 false);
2455 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002456 }
2457 }
2458 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002459 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002460 }
2461
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002462 final void finishTopRunningActivityLocked(ProcessRecord app) {
2463 ActivityRecord r = topRunningActivityLocked(null);
2464 if (r != null && r.app == app) {
2465 // If the top running activity is from this crashing
2466 // process, then terminate it to avoid getting in a loop.
2467 Slog.w(TAG, " Force finishing activity "
2468 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002469 int taskNdx = mTaskHistory.indexOf(r.task);
2470 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002471 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002472 // Also terminate any activities below it that aren't yet
2473 // stopped, to avoid a situation where one will get
2474 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002475 --activityNdx;
2476 if (activityNdx < 0) {
2477 do {
2478 --taskNdx;
2479 if (taskNdx < 0) {
2480 break;
2481 }
2482 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2483 } while (activityNdx < 0);
2484 }
2485 if (activityNdx >= 0) {
2486 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002487 if (r.state == ActivityState.RESUMED
2488 || r.state == ActivityState.PAUSING
2489 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002490 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002491 Slog.w(TAG, " Force finishing activity "
2492 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002493 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002494 }
2495 }
2496 }
2497 }
2498 }
2499
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002500 final void finishVoiceTask(IVoiceInteractionSession session) {
2501 IBinder sessionBinder = session.asBinder();
2502 boolean didOne = false;
2503 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2504 TaskRecord tr = mTaskHistory.get(taskNdx);
2505 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2506 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2507 ActivityRecord r = tr.mActivities.get(activityNdx);
2508 if (!r.finishing) {
2509 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2510 false);
2511 didOne = true;
2512 }
2513 }
2514 }
2515 }
2516 if (didOne) {
2517 mService.updateOomAdjLocked();
2518 }
2519 }
2520
Craig Mautnerd2328952013-03-05 12:46:26 -08002521 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002522 ArrayList<ActivityRecord> activities = r.task.mActivities;
2523 for (int index = activities.indexOf(r); index >= 0; --index) {
2524 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002525 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002526 break;
2527 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002528 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002529 }
2530 return true;
2531 }
2532
Dianne Hackborn5c607432012-02-28 14:44:19 -08002533 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2534 // send the result
2535 ActivityRecord resultTo = r.resultTo;
2536 if (resultTo != null) {
2537 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2538 + " who=" + r.resultWho + " req=" + r.requestCode
2539 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002540 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002541 if (resultData != null) {
2542 resultData.prepareToLeaveUser(r.userId);
2543 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002544 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002545 if (r.info.applicationInfo.uid > 0) {
2546 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2547 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002548 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002549 }
2550 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2551 resultData);
2552 r.resultTo = null;
2553 }
2554 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2555
2556 // Make sure this HistoryRecord is not holding on to other resources,
2557 // because clients have remote IPC references to this object so we
2558 // can't assume that will go away and want to avoid circular IPC refs.
2559 r.results = null;
2560 r.pendingResults = null;
2561 r.newIntents = null;
2562 r.icicle = null;
2563 }
2564
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565 /**
2566 * @return Returns true if this activity has been removed from the history
2567 * list, or false if it is still in the list and will be removed later.
2568 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002569 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2570 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002571 if (r.finishing) {
2572 Slog.w(TAG, "Duplicate finish request for " + r);
2573 return false;
2574 }
2575
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002576 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002577 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002579 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002580 task.taskId, r.shortComponentName, reason);
2581 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002582 final int index = activities.indexOf(r);
2583 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002584 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002585 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002586 // If the caller asked that this activity (and all above it)
2587 // be cleared when the task is reset, don't lose that information,
2588 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002589 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002590 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002591 }
2592 }
2593
2594 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002595
2596 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597
Dianne Hackborn5c607432012-02-28 14:44:19 -08002598 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002599
Craig Mautnerde4ef022013-04-07 19:01:33 -07002600 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002601 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002602 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002603 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002604 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002605 ? AppTransition.TRANSIT_TASK_CLOSE
2606 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002607
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002608 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002609 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002610
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002611 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002612 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2613 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2614 startPausingLocked(false, false);
2615 }
2616
Craig Mautneraea74a52014-03-08 14:23:10 -08002617 if (endTask) {
2618 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2619 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002620 } else if (r.state != ActivityState.PAUSING) {
2621 // If the activity is PAUSING, we will complete the finish once
2622 // it is done pausing; else we can just directly finish it here.
2623 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002624 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 } else {
2626 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2627 }
2628
2629 return false;
2630 }
2631
Craig Mautnerf3333272013-04-22 10:55:53 -07002632 static final int FINISH_IMMEDIATELY = 0;
2633 static final int FINISH_AFTER_PAUSE = 1;
2634 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002635
Craig Mautnerf3333272013-04-22 10:55:53 -07002636 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637 // First things first: if this activity is currently visible,
2638 // and the resumed activity is not yet visible, then hold off on
2639 // finishing until the resumed one becomes visible.
2640 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002641 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2642 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002643 if (mStackSupervisor.mStoppingActivities.size() > 3
2644 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002645 // If we already have a few activities waiting to stop,
2646 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002647 // them out. Or if r is the last of activity of the last task the stack
2648 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002649 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002650 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002651 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 }
2653 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002654 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2655 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002657 if (oomAdj) {
2658 mService.updateOomAdjLocked();
2659 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002660 return r;
2661 }
2662
2663 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002664 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002665 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002666 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002667 if (mResumedActivity == r) {
2668 mResumedActivity = null;
2669 }
2670 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002671 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002672 r.state = ActivityState.FINISHING;
2673
2674 if (mode == FINISH_IMMEDIATELY
2675 || prevState == ActivityState.STOPPED
2676 || prevState == ActivityState.INITIALIZING) {
2677 // If this activity is already stopped, we can just finish
2678 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002679 r.makeFinishing();
2680 boolean activityRemoved = destroyActivityLocked(r, true, oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002681 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002682 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002683 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002684 if (DEBUG_CONTAINERS) Slog.d(TAG,
2685 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2686 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002687 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002689
2690 // Need to go through the full pause cycle to get this
2691 // activity into the stopped state and then finish it.
2692 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002693 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002694 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002695 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 return r;
2697 }
2698
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002699 void finishAllActivitiesLocked() {
2700 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2701 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2702 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2703 final ActivityRecord r = activities.get(activityNdx);
2704 if (r.finishing) {
2705 continue;
2706 }
2707 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
2708 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2709 }
2710 }
2711 }
2712
Craig Mautnerd2328952013-03-05 12:46:26 -08002713 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002714 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002715 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002716 final TaskRecord task = srec.task;
2717 final ArrayList<ActivityRecord> activities = task.mActivities;
2718 final int start = activities.indexOf(srec);
2719 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002720 return false;
2721 }
2722 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002723 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002724 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002725 final ComponentName dest = destIntent.getComponent();
2726 if (start > 0 && dest != null) {
2727 for (int i = finishTo; i >= 0; i--) {
2728 ActivityRecord r = activities.get(i);
2729 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002730 r.info.name.equals(dest.getClassName())) {
2731 finishTo = i;
2732 parent = r;
2733 foundParentInTask = true;
2734 break;
2735 }
2736 }
2737 }
2738
2739 IActivityController controller = mService.mController;
2740 if (controller != null) {
2741 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2742 if (next != null) {
2743 // ask watcher if this is allowed
2744 boolean resumeOK = true;
2745 try {
2746 resumeOK = controller.activityResuming(next.packageName);
2747 } catch (RemoteException e) {
2748 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002749 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002750 }
2751
2752 if (!resumeOK) {
2753 return false;
2754 }
2755 }
2756 }
2757 final long origId = Binder.clearCallingIdentity();
2758 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002759 ActivityRecord r = activities.get(i);
2760 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002761 // Only return the supplied result for the first activity finished
2762 resultCode = Activity.RESULT_CANCELED;
2763 resultData = null;
2764 }
2765
2766 if (parent != null && foundParentInTask) {
2767 final int parentLaunchMode = parent.info.launchMode;
2768 final int destIntentFlags = destIntent.getFlags();
2769 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2770 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2771 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2772 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2773 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2774 } else {
2775 try {
2776 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2777 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002778 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002779 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002780 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002781 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002782 foundParentInTask = res == ActivityManager.START_SUCCESS;
2783 } catch (RemoteException e) {
2784 foundParentInTask = false;
2785 }
2786 requestFinishActivityLocked(parent.appToken, resultCode,
2787 resultData, "navigate-up", true);
2788 }
2789 }
2790 Binder.restoreCallingIdentity(origId);
2791 return foundParentInTask;
2792 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002793 /**
2794 * Perform the common clean-up of an activity record. This is called both
2795 * as part of destroyActivityLocked() (when destroying the client-side
2796 * representation) and cleaning things up as a result of its hosting
2797 * processing going away, in which case there is no remaining client-side
2798 * state to destroy so only the cleanup here is needed.
2799 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002800 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2801 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002802 if (mResumedActivity == r) {
2803 mResumedActivity = null;
2804 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002805 if (mPausingActivity == r) {
2806 mPausingActivity = null;
2807 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002808 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809
2810 r.configDestroy = false;
2811 r.frozenBeforeDestroy = false;
2812
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002813 if (setState) {
2814 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2815 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002816 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002817 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002818 }
2819
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002820 // Make sure this record is no longer in the pending finishes list.
2821 // This could happen, for example, if we are trimming activities
2822 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002823 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002824 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002825
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 // Remove any pending results.
2827 if (r.finishing && r.pendingResults != null) {
2828 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2829 PendingIntentRecord rec = apr.get();
2830 if (rec != null) {
2831 mService.cancelIntentSenderLocked(rec, false);
2832 }
2833 }
2834 r.pendingResults = null;
2835 }
2836
2837 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002838 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 }
2840
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002841 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002842 removeTimeoutsForActivityLocked(r);
2843 }
2844
2845 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002846 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002848 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002849 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002850 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002851 }
2852
Craig Mautner04a0ea62014-01-13 12:51:26 -08002853 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002854 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002855 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2856 r.makeFinishing();
2857 if (DEBUG_ADD_REMOVE) {
2858 RuntimeException here = new RuntimeException("here");
2859 here.fillInStackTrace();
2860 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002861 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002862 r.takeFromHistory();
2863 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002864 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002865 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002866 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002867 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002868 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002869 if (VALIDATE_TOKENS) {
2870 validateAppTokensLocked();
2871 }
Craig Mautner312ba862014-02-10 17:55:01 -08002872 final TaskRecord task = r.task;
2873 if (task != null && task.removeActivity(r)) {
2874 if (DEBUG_STACK) Slog.i(TAG,
2875 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07002876 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
2877 task.isOverHomeStack()) {
2878 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08002879 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002880 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08002881 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002882 cleanUpActivityServicesLocked(r);
2883 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002885
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002886 /**
2887 * Perform clean-up of service connections in an activity record.
2888 */
2889 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2890 // Throw away any services that have been bound by this activity.
2891 if (r.connections != null) {
2892 Iterator<ConnectionRecord> it = r.connections.iterator();
2893 while (it.hasNext()) {
2894 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002895 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002896 }
2897 r.connections = null;
2898 }
2899 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002900
2901 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2902 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2903 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2904 mHandler.sendMessage(msg);
2905 }
2906
Dianne Hackborn28695e02011-11-02 21:59:51 -07002907 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002908 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002909 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002910 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2911 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2912 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2913 final ActivityRecord r = activities.get(activityNdx);
2914 if (r.finishing) {
2915 continue;
2916 }
2917 if (r.fullscreen) {
2918 lastIsOpaque = true;
2919 }
2920 if (owner != null && r.app != owner) {
2921 continue;
2922 }
2923 if (!lastIsOpaque) {
2924 continue;
2925 }
2926 // We can destroy this one if we have its icicle saved and
2927 // it is not in the process of pausing/stopping/finishing.
2928 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2929 && r.haveState && !r.visible && r.stopped
2930 && r.state != ActivityState.DESTROYING
2931 && r.state != ActivityState.DESTROYED) {
2932 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2933 + " resumed=" + mResumedActivity
2934 + " pausing=" + mPausingActivity);
2935 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2936 activityRemoved = true;
2937 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002938 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002939 }
2940 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002941 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002942 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002943 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002944 }
2945
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002946 /**
2947 * Destroy the current CLIENT SIDE instance of an activity. This may be
2948 * called both when actually finishing an activity, or when performing
2949 * a configuration switch where we destroy the current client-side object
2950 * but then create a new client-side object for this same HistoryRecord.
2951 */
2952 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002953 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002954 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002955 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2957 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002958 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002959 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960
2961 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002962
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002963 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964
2965 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002966
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002967 if (hadApp) {
2968 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002969 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2971 mService.mHeavyWeightProcess = null;
2972 mService.mHandler.sendEmptyMessage(
2973 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2974 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002975 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002976 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002977 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002978 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 }
2980 }
2981
2982 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002983
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002984 try {
2985 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002986 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987 r.configChangeFlags);
2988 } catch (Exception e) {
2989 // We can just ignore exceptions here... if the process
2990 // has crashed, our death notification will clean things
2991 // up.
2992 //Slog.w(TAG, "Exception thrown during finish", e);
2993 if (r.finishing) {
2994 removeActivityFromHistoryLocked(r);
2995 removedFromHistory = true;
2996 skipDestroy = true;
2997 }
2998 }
2999
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003000 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003001
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003002 // If the activity is finishing, we need to wait on removing it
3003 // from the list to give it a chance to do its cleanup. During
3004 // that time it may make calls back with its token so we need to
3005 // be able to find it on the list and so we don't want to remove
3006 // it from the list yet. Otherwise, we can just immediately put
3007 // it in the destroyed state since we are not removing it from the
3008 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003010 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3011 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003013 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003014 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3015 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003016 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003017 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003018 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003019 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 }
3021 } else {
3022 // remove this record from the history.
3023 if (r.finishing) {
3024 removeActivityFromHistoryLocked(r);
3025 removedFromHistory = true;
3026 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003027 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003029 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003030 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 }
3032 }
3033
3034 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003035
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 if (!mLRUActivities.remove(r) && hadApp) {
3037 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3038 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003039
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 return removedFromHistory;
3041 }
3042
Craig Mautnerd2328952013-03-05 12:46:26 -08003043 final void activityDestroyedLocked(IBinder token) {
3044 final long origId = Binder.clearCallingIdentity();
3045 try {
3046 ActivityRecord r = ActivityRecord.forToken(token);
3047 if (r != null) {
3048 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003049 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003050 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003051
3052 if (isInStackLocked(token) != null) {
3053 if (r.state == ActivityState.DESTROYING) {
3054 cleanUpActivityLocked(r, true, false);
3055 removeActivityFromHistoryLocked(r);
3056 }
3057 }
Craig Mautner05d29032013-05-03 13:40:13 -07003058 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003059 } finally {
3060 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061 }
3062 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003063
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003064 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3065 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003067 if (DEBUG_CLEANUP) Slog.v(
3068 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 + " with " + i + " entries");
3070 while (i > 0) {
3071 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003072 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003073 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003075 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003077 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 }
3079 }
3080 }
3081
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003082 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3083 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003084 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3085 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003086 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3087 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003088 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003089 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003090 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3091 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003092
3093 boolean hasVisibleActivities = false;
3094
3095 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003096 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003097 if (DEBUG_CLEANUP) Slog.v(
3098 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003099 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3100 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3101 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3102 final ActivityRecord r = activities.get(activityNdx);
3103 --i;
3104 if (DEBUG_CLEANUP) Slog.v(
3105 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3106 if (r.app == app) {
3107 boolean remove;
3108 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3109 // Don't currently have state for the activity, or
3110 // it is finishing -- always remove it.
3111 remove = true;
3112 } else if (r.launchCount > 2 &&
3113 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3114 // We have launched this activity too many times since it was
3115 // able to run, so give up and remove it.
3116 remove = true;
3117 } else {
3118 // The process may be gone, but the activity lives on!
3119 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003120 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003121 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003122 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003123 RuntimeException here = new RuntimeException("here");
3124 here.fillInStackTrace();
3125 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3126 + ": haveState=" + r.haveState
3127 + " stateNotNeeded=" + r.stateNotNeeded
3128 + " finishing=" + r.finishing
3129 + " state=" + r.state, here);
3130 }
3131 if (!r.finishing) {
3132 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3133 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3134 r.userId, System.identityHashCode(r),
3135 r.task.taskId, r.shortComponentName,
3136 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003137 if (r.state == ActivityState.RESUMED) {
3138 mService.updateUsageStats(r, false);
3139 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003140 }
3141 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003142
Craig Mautner0247fc82013-02-28 14:32:06 -08003143 } else {
3144 // We have the current state for this activity, so
3145 // it can be restarted later when needed.
3146 if (localLOGV) Slog.v(
3147 TAG, "Keeping entry, setting app to null");
3148 if (r.visible) {
3149 hasVisibleActivities = true;
3150 }
3151 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3152 + r);
3153 r.app = null;
3154 r.nowVisible = false;
3155 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003156 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003157 "App died, clearing saved state of " + r);
3158 r.icicle = null;
3159 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003160 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003161
Craig Mautnerd2328952013-03-05 12:46:26 -08003162 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003163 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003164 }
3165 }
3166
3167 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003169
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003170 final void updateTransitLocked(int transit, Bundle options) {
3171 if (options != null) {
3172 ActivityRecord r = topRunningActivityLocked(null);
3173 if (r != null && r.state != ActivityState.RESUMED) {
3174 r.updateOptionsLocked(options);
3175 } else {
3176 ActivityOptions.abort(options);
3177 }
3178 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003179 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003180 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003181
Craig Mautner21d24a22014-04-23 11:45:37 -07003182 void updateTaskMovement(TaskRecord task, boolean toFront) {
3183 if (task.isPersistable) {
3184 task.mLastTimeMoved = System.currentTimeMillis();
3185 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3186 // recently will be most negative, tasks sent to the bottom before that will be less
3187 // negative. Similarly for recent tasks moved to the top which will be most positive.
3188 if (!toFront) {
3189 task.mLastTimeMoved *= -1;
3190 }
3191 }
3192 }
3193
Craig Mautner84984fa2014-06-19 11:19:20 -07003194 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003195 final int top = mTaskHistory.size() - 1;
3196 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3197 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003198 if (task.taskType == homeStackTaskType) {
3199 if (DEBUG_TASKS || DEBUG_STACK)
3200 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003201 mTaskHistory.remove(taskNdx);
3202 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003203 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003204 mWindowManager.moveTaskToTop(task.taskId);
3205 return;
3206 }
3207 }
3208 }
3209
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003210 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003211 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003212
Craig Mautner11bf9a52013-02-19 14:08:51 -08003213 final int numTasks = mTaskHistory.size();
3214 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003215 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003216 // nothing to do!
3217 if (reason != null &&
3218 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3219 ActivityOptions.abort(options);
3220 } else {
3221 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3222 }
3223 return;
3224 }
3225
Craig Mautnere0a38842013-12-16 16:14:02 -08003226 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003227
Craig Mautner11bf9a52013-02-19 14:08:51 -08003228 // Shift all activities with this task up to the top
3229 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003230 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003231
3232 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 if (reason != null &&
3234 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003235 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 ActivityRecord r = topRunningActivityLocked(null);
3237 if (r != null) {
3238 mNoAnimActivities.add(r);
3239 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003240 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003241 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003242 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003243 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003244
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003245 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003246
Craig Mautner05d29032013-05-03 13:40:13 -07003247 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003248 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003249
3250 if (VALIDATE_TOKENS) {
3251 validateAppTokensLocked();
3252 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 }
3254
3255 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003256 * Worker method for rearranging history stack. Implements the function of moving all
3257 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003259 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003260 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3261 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003262 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003263 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 * @return Returns true if the move completed, false if not.
3265 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003266 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003267 final TaskRecord tr = taskForIdLocked(taskId);
3268 if (tr == null) {
3269 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3270 return false;
3271 }
3272
3273 Slog.i(TAG, "moveTaskToBack: " + tr);
3274
3275 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003276
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003277 // If we have a watcher, preflight the move before committing to it. First check
3278 // for *other* available tasks, but if none are available, then try again allowing the
3279 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003280 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003281 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 if (next == null) {
3283 next = topRunningActivityLocked(null, 0);
3284 }
3285 if (next != null) {
3286 // ask watcher if this is allowed
3287 boolean moveOK = true;
3288 try {
3289 moveOK = mService.mController.activityResuming(next.packageName);
3290 } catch (RemoteException e) {
3291 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003292 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003293 }
3294 if (!moveOK) {
3295 return false;
3296 }
3297 }
3298 }
3299
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003301 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003302
Craig Mautner11bf9a52013-02-19 14:08:51 -08003303 mTaskHistory.remove(tr);
3304 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003305 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003306
Craig Mautnerc8143c62013-09-03 12:15:57 -07003307 // There is an assumption that moving a task to the back moves it behind the home activity.
3308 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003309 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003310 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3311 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003312 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003313 break;
3314 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003315 if (taskNdx == 1) {
3316 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003317 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003318 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003319 }
3320
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003322 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3323 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 ActivityRecord r = topRunningActivityLocked(null);
3325 if (r != null) {
3326 mNoAnimActivities.add(r);
3327 }
3328 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003329 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003331 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003332
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003334 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 }
3336
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003337 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003338 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
3339 final int taskToReturnTo = tr.getTaskToReturnTo();
3340 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3341 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003342 }
3343
Craig Mautner05d29032013-05-03 13:40:13 -07003344 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 return true;
3346 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003347
Craig Mautner8849a5e2013-04-02 16:41:03 -07003348 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003350 final Uri data = r.intent.getData();
3351 final String strData = data != null ? data.toSafeString() : null;
3352
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003354 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003356 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 }
3358
3359 /**
3360 * Make sure the given activity matches the current configuration. Returns
3361 * false if the activity had to be destroyed. Returns true if the
3362 * configuration is the same, or the activity will remain running as-is
3363 * for whatever reason. Ensures the HistoryRecord is updated with the
3364 * correct configuration and all other bookkeeping is handled.
3365 */
3366 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3367 int globalChanges) {
3368 if (mConfigWillChange) {
3369 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3370 "Skipping config check (will change): " + r);
3371 return true;
3372 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003373
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003374 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3375 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003376
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003377 // Short circuit: if the two configurations are the exact same
3378 // object (the common case), then there is nothing to do.
3379 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003380 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003381 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3382 "Configuration unchanged in " + r);
3383 return true;
3384 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 // We don't worry about activities that are finishing.
3387 if (r.finishing) {
3388 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3389 "Configuration doesn't matter in finishing " + r);
3390 r.stopFreezingScreenLocked(false);
3391 return true;
3392 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003393
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003394 // Okay we now are going to make this activity have the new config.
3395 // But then we need to figure out how it needs to deal with that.
3396 Configuration oldConfig = r.configuration;
3397 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003398
3399 // Determine what has changed. May be nothing, if this is a config
3400 // that has come back from the app after going idle. In that case
3401 // we just want to leave the official config object now in the
3402 // activity and do nothing else.
3403 final int changes = oldConfig.diff(newConfig);
3404 if (changes == 0 && !r.forceNewConfig) {
3405 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3406 "Configuration no differences in " + r);
3407 return true;
3408 }
3409
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003410 // If the activity isn't currently running, just leave the new
3411 // configuration and it will pick that up next time it starts.
3412 if (r.app == null || r.app.thread == null) {
3413 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3414 "Configuration doesn't matter not running " + r);
3415 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003416 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003417 return true;
3418 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003419
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003420 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003421 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3422 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3423 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003424 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003425 + ", newConfig=" + newConfig);
3426 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003427 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003428 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3429 r.configChangeFlags |= changes;
3430 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003431 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003432 if (r.app == null || r.app.thread == null) {
3433 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003434 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003435 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003436 } else if (r.state == ActivityState.PAUSING) {
3437 // A little annoying: we are waiting for this activity to
3438 // finish pausing. Let's not do anything now, but just
3439 // flag that it needs to be restarted when done pausing.
3440 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003441 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003442 r.configDestroy = true;
3443 return true;
3444 } else if (r.state == ActivityState.RESUMED) {
3445 // Try to optimize this case: the configuration is changing
3446 // and we need to restart the top, resumed activity.
3447 // Instead of doing the normal handshaking, just say
3448 // "restart!".
3449 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003450 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003451 relaunchActivityLocked(r, r.configChangeFlags, true);
3452 r.configChangeFlags = 0;
3453 } else {
3454 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003455 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003456 relaunchActivityLocked(r, r.configChangeFlags, false);
3457 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003458 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003459
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003460 // All done... tell the caller we weren't able to keep this
3461 // activity around.
3462 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003463 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003464
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003465 // Default case: the activity can handle this new configuration, so
3466 // hand it over. Note that we don't need to give it the new
3467 // configuration, since we always send configuration changes to all
3468 // process when they happen so it can just use whatever configuration
3469 // it last got.
3470 if (r.app != null && r.app.thread != null) {
3471 try {
3472 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003473 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003474 } catch (RemoteException e) {
3475 // If process died, whatever.
3476 }
3477 }
3478 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003479
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480 return true;
3481 }
3482
Craig Mautnerc8143c62013-09-03 12:15:57 -07003483 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003484 int changes, boolean andResume) {
3485 List<ResultInfo> results = null;
3486 List<Intent> newIntents = null;
3487 if (andResume) {
3488 results = r.results;
3489 newIntents = r.newIntents;
3490 }
3491 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3492 + " with results=" + results + " newIntents=" + newIntents
3493 + " andResume=" + andResume);
3494 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003495 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003496 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003497
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003499
Craig Mautner34b73df2014-01-12 21:11:08 -08003500 mStackSupervisor.removeChildActivityContainers(r);
3501
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003503 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3504 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3505 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003506 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003507 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003508 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003509 // Note: don't need to call pauseIfSleepingLocked() here, because
3510 // the caller will only pass in 'andResume' if this activity is
3511 // currently resumed, which implies we aren't sleeping.
3512 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003513 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003514 }
3515
3516 if (andResume) {
3517 r.results = null;
3518 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003519 r.state = ActivityState.RESUMED;
3520 } else {
3521 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3522 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 }
3524
3525 return true;
3526 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003527
3528 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003529 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3530 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3531 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3532 final ActivityRecord r = activities.get(activityNdx);
3533 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003534 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003535 }
3536 if (r.fullscreen && !r.finishing) {
3537 return false;
3538 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003539 }
3540 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003541 final ActivityRecord r = ActivityRecord.forToken(token);
3542 if (r == null) {
3543 return false;
3544 }
3545 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3546 + " would have returned true for r=" + r);
3547 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003548 }
3549
3550 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003551 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3552 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3553 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3554 final ActivityRecord r = activities.get(activityNdx);
3555 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003556 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003557 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003558 }
3559 }
3560 }
3561
3562 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3563 boolean didSomething = false;
3564 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003565 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003566 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3567 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3568 int numActivities = activities.size();
3569 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3570 ActivityRecord r = activities.get(activityNdx);
3571 final boolean samePackage = r.packageName.equals(name)
3572 || (name == null && r.userId == userId);
3573 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3574 && (samePackage || r.task == lastTask)
3575 && (r.app == null || evenPersistent || !r.app.persistent)) {
3576 if (!doit) {
3577 if (r.finishing) {
3578 // If this activity is just finishing, then it is not
3579 // interesting as far as something to stop.
3580 continue;
3581 }
3582 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003583 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003584 if (r.isHomeActivity()) {
3585 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3586 Slog.i(TAG, "Skip force-stop again " + r);
3587 continue;
3588 } else {
3589 homeActivity = r.realActivity;
3590 }
3591 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003592 didSomething = true;
3593 Slog.i(TAG, " Force finishing activity " + r);
3594 if (samePackage) {
3595 if (r.app != null) {
3596 r.app.removed = true;
3597 }
3598 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003599 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003600 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003601 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3602 true)) {
3603 // r has been deleted from mActivities, accommodate.
3604 --numActivities;
3605 --activityNdx;
3606 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003607 }
3608 }
3609 }
3610 return didSomething;
3611 }
3612
Dianne Hackborn09233282014-04-30 11:33:59 -07003613 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003614 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003615 final TaskRecord task = mTaskHistory.get(taskNdx);
3616 ActivityRecord r = null;
3617 ActivityRecord top = null;
3618 int numActivities = 0;
3619 int numRunning = 0;
3620 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003621 if (activities.isEmpty()) {
3622 continue;
3623 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003624 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3625 continue;
3626 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003627 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3628 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003629
Craig Mautneraab647e2013-02-28 16:31:36 -08003630 // Initialize state for next task if needed.
3631 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3632 top = r;
3633 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003634 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003635
3636 // Add 'r' into the current task.
3637 numActivities++;
3638 if (r.app != null && r.app.thread != null) {
3639 numRunning++;
3640 }
3641
3642 if (localLOGV) Slog.v(
3643 TAG, r.intent.getComponent().flattenToShortString()
3644 + ": task=" + r.task);
3645 }
3646
3647 RunningTaskInfo ci = new RunningTaskInfo();
3648 ci.id = task.taskId;
3649 ci.baseActivity = r.intent.getComponent();
3650 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003651 ci.lastActiveTime = task.lastActiveTime;
3652
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003653 if (top.task != null) {
3654 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003655 }
3656 ci.numActivities = numActivities;
3657 ci.numRunning = numRunning;
3658 //System.out.println(
3659 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003660 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003661 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003662 }
3663
3664 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003665 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003666 if (DEBUG_SWITCH) Slog.d(
3667 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003668 if (top >= 0) {
3669 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3670 int activityTop = activities.size() - 1;
3671 if (activityTop > 0) {
3672 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3673 "unhandled-back", true);
3674 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003675 }
3676 }
3677
Craig Mautner6b74cb52013-09-27 17:02:21 -07003678 /**
3679 * Reset local parameters because an app's activity died.
3680 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003681 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003682 */
3683 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003684 if (mPausingActivity != null && mPausingActivity.app == app) {
3685 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3686 "App died while pausing: " + mPausingActivity);
3687 mPausingActivity = null;
3688 }
3689 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3690 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003691 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003692 }
3693
Craig Mautner19091252013-10-05 00:03:53 -07003694 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003695 }
3696
Craig Mautnercae015f2013-02-08 14:31:27 -08003697 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003698 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3699 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3700 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3701 final ActivityRecord r = activities.get(activityNdx);
3702 if (r.app == app) {
3703 Slog.w(TAG, " Force finishing activity "
3704 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003705 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003706 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003707 }
3708 }
3709 }
3710
Dianne Hackborn390517b2013-05-30 15:03:32 -07003711 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003712 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003713 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003714 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3715 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003716 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3717 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003718 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003719 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003720 if (printed) {
3721 header = null;
3722 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003723 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003724 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003725 }
3726
3727 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3728 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3729
3730 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003731 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3732 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003733 }
3734 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003735 final int top = mTaskHistory.size() - 1;
3736 if (top >= 0) {
3737 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3738 int listTop = list.size() - 1;
3739 if (listTop >= 0) {
3740 activities.add(list.get(listTop));
3741 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003742 }
3743 } else {
3744 ItemMatcher matcher = new ItemMatcher();
3745 matcher.build(name);
3746
Craig Mautneraab647e2013-02-28 16:31:36 -08003747 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3748 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3749 if (matcher.match(r1, r1.intent.getComponent())) {
3750 activities.add(r1);
3751 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003752 }
3753 }
3754 }
3755
3756 return activities;
3757 }
3758
3759 ActivityRecord restartPackage(String packageName) {
3760 ActivityRecord starting = topRunningActivityLocked(null);
3761
3762 // All activities that came from the package must be
3763 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003764 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3765 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3766 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3767 final ActivityRecord a = activities.get(activityNdx);
3768 if (a.info.packageName.equals(packageName)) {
3769 a.forceNewConfig = true;
3770 if (starting != null && a == starting && a.visible) {
3771 a.startFreezingScreenLocked(starting.app,
3772 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3773 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003774 }
3775 }
3776 }
3777
3778 return starting;
3779 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003780
Craig Mautner41db4a72014-05-07 17:20:56 -07003781 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003782 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003783 mWindowManager.removeTask(task.taskId);
3784 final ActivityRecord r = mResumedActivity;
3785 if (r != null && r.task == task) {
3786 mResumedActivity = null;
3787 }
3788
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003789 final int taskNdx = mTaskHistory.indexOf(task);
3790 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07003791 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
3792 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
3793 if (!nextTask.isOverHomeStack()) {
3794 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
3795 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003796 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003797 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003798 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07003799
3800 if (task.mActivities.isEmpty()) {
3801 final boolean isVoiceSession = task.voiceSession != null;
3802 if (isVoiceSession) {
3803 try {
3804 task.voiceSession.taskFinished(task.intent, task.taskId);
3805 } catch (RemoteException e) {
3806 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003807 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003808 if (task.autoRemoveFromRecents() || isVoiceSession) {
3809 // Task creator asked to remove this when done, or this task was a voice
3810 // interaction, so it should not remain on the recent tasks list.
3811 mService.mRecentTasks.remove(task);
3812 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003813 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003814
3815 if (mTaskHistory.isEmpty()) {
3816 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3817 if (isOnHomeDisplay()) {
3818 mStackSupervisor.moveHomeStack(!isHomeStack());
3819 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003820 if (mStacks != null) {
3821 mStacks.remove(this);
3822 mStacks.add(0, this);
3823 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003824 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08003825 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003826 }
3827
Dianne Hackborn91097de2014-04-04 18:02:06 -07003828 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
3829 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
3830 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07003831 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
3832 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003833 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003834 return task;
3835 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003836
3837 ArrayList<TaskRecord> getAllTasks() {
3838 return new ArrayList<TaskRecord>(mTaskHistory);
3839 }
3840
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003841 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003842 task.stack = this;
3843 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003844 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003845 } else {
3846 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003847 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003848 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003849 if (!moving && task.voiceSession != null) {
3850 try {
3851 task.voiceSession.taskStarted(task.intent, task.taskId);
3852 } catch (RemoteException e) {
3853 }
3854 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003855 }
3856
3857 public int getStackId() {
3858 return mStackId;
3859 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003860
3861 @Override
3862 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003863 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3864 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003865 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003866}