blob: efed0b99dc04d117b83683d29840b1ea5b3aa776 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
Craig Mautner84984fa2014-06-19 11:19:20 -070033import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070035
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
37import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070041import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070042import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070043import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
44
Dianne Hackborn89ad4562014-08-24 16:45:38 -070045import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070046import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080047import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070049import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080050import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080051import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080052import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070054import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055
56import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070057import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070058import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080060import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080062import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080068import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080069import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070071import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.Handler;
74import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090075import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070077import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.os.RemoteException;
79import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080080import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070081import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070082import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070085import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080088import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
91import java.util.Iterator;
92import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080093import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094
95/**
96 * State and management of a single stack of activities.
97 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070098final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080099
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700100 // Ticks during which we check progress while waiting for an app to launch.
101 static final int LAUNCH_TICK = 500;
102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700103 // How long we wait until giving up on the last activity to pause. This
104 // is short because it directly impacts the responsiveness of starting the
105 // next activity.
106 static final int PAUSE_TIMEOUT = 500;
107
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700108 // How long we wait for the activity to tell us it has stopped before
109 // giving up. This is a good amount of time because we really need this
110 // from the application in order to get its saved state.
111 static final int STOP_TIMEOUT = 10*1000;
112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700113 // How long we wait until giving up on an activity telling us it has
114 // finished destroying itself.
115 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800118 // disabled.
119 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700121 // How long between activity launches that we consider safe to not warn
122 // the user about an unexpected activity being launched on top.
123 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 // Set to false to disable the preview that is shown while a new activity
126 // is being started.
127 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800128
Craig Mautner5eda9b32013-07-02 11:58:16 -0700129 // How long to wait for all background Activities to redraw following a call to
130 // convertToTranslucent().
131 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
132
Craig Mautnera61bc652013-10-28 15:43:18 -0700133 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135 enum ActivityState {
136 INITIALIZING,
137 RESUMED,
138 PAUSING,
139 PAUSED,
140 STOPPING,
141 STOPPED,
142 FINISHING,
143 DESTROYING,
144 DESTROYED
145 }
146
147 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700148 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700150 /**
151 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152 * running) activities. It contains #TaskRecord objects.
153 */
154 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
155
156 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800157 * Used for validating app tokens with window manager.
158 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800159 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 /**
162 * List of running activities, sorted by recent usage.
163 * The first entry in the list is the least recently used.
164 * It contains HistoryRecord objects.
165 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700166 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * Animations that for the current transition have requested not to
170 * be considered for the transition animation.
171 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 * When we are in the process of pausing an activity, before starting the
176 * next one, this variable holds the activity that is currently being paused.
177 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800178 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179
180 /**
181 * This is the last activity that we put into the paused state. This is
182 * used to determine if we need to do an activity transition while sleeping,
183 * when we normally hold the top activity paused.
184 */
185 ActivityRecord mLastPausedActivity = null;
186
187 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700188 * Activities that specify No History must be removed once the user navigates away from them.
189 * If the device goes to sleep with such an activity in the paused state then we save it here
190 * and finish it later if another activity replaces it on wakeup.
191 */
192 ActivityRecord mLastNoHistoryActivity = null;
193
194 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195 * Current activity that is resumed, or null if there is none.
196 */
197 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700200 * This is the last activity that has been started. It is only used to
201 * identify when multiple activities are started at once so that the user
202 * can be warned they may not be in the activity they think they are.
203 */
204 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800205
Craig Mautner5eda9b32013-07-02 11:58:16 -0700206 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
207 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
208 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
209 // Activity in mTranslucentActivityWaiting is notified via
210 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
211 // background activity being drawn then the same call will be made with a true value.
212 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700213 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700214 new ArrayList<ActivityRecord>();
215
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700216 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700217 * Set when we know we are going to be calling updateConfiguration()
218 * soon, so want to skip intermediate config checks.
219 */
220 boolean mConfigWillChange;
221
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700222 long mLaunchStartTime = 0;
223 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800224
Craig Mautner858d8a62013-04-23 17:08:34 -0700225 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700226
Craig Mautnerc00204b2013-03-05 15:02:14 -0800227 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800228 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800229 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
230 ArrayList<ActivityStack> mStacks;
231 /** The attached Display's unique identifier, or -1 if detached */
232 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800233
Craig Mautner27084302013-03-25 08:05:25 -0700234 /** Run all ActivityStacks through this */
235 final ActivityStackSupervisor mStackSupervisor;
236
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700237 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700238 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
239 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
240 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
241 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700242 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700243 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700244 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700245
246 static class ScheduleDestroyArgs {
247 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700248 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700249 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700250 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700251 mReason = reason;
252 }
253 }
254
Zoran Marcetaf958b322012-08-09 20:27:12 +0900255 final Handler mHandler;
256
257 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700258 //public Handler() {
259 // if (localLOGV) Slog.v(TAG, "Handler started!");
260 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700261 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900262 super(looper);
263 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700264
Zoran Marcetaf958b322012-08-09 20:27:12 +0900265 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700266 public void handleMessage(Message msg) {
267 switch (msg.what) {
268 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800269 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 // We don't at this point know if the activity is fullscreen,
271 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800272 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700273 synchronized (mService) {
274 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700275 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700276 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700277 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800278 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700279 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700280 case LAUNCH_TICK_MSG: {
281 ActivityRecord r = (ActivityRecord)msg.obj;
282 synchronized (mService) {
283 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700284 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 }
286 }
287 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800289 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700290 // We don't at this point know if the activity is fullscreen,
291 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800292 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800293 synchronized (mService) {
294 activityDestroyedLocked(r != null ? r.appToken : null);
295 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700297 case STOP_TIMEOUT_MSG: {
298 ActivityRecord r = (ActivityRecord)msg.obj;
299 // We don't at this point know if the activity is fullscreen,
300 // so we need to be conservative and assume it isn't.
301 Slog.w(TAG, "Activity stop timeout for " + r);
302 synchronized (mService) {
303 if (r.isInHistory()) {
304 activityStoppedLocked(r, null, null, null);
305 }
306 }
307 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700308 case DESTROY_ACTIVITIES_MSG: {
309 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
310 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700311 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700312 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700313 } break;
314 case TRANSLUCENT_TIMEOUT_MSG: {
315 synchronized (mService) {
316 notifyActivityDrawnLocked(null);
317 }
318 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700319 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700320 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700321 final ActivityRecord r = getVisibleBehindActivity();
322 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700323 if (r != null) {
324 mService.killAppAtUsersRequest(r.app, null);
325 }
326 }
327 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700328 }
329 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800330 }
331
Craig Mautner34b73df2014-01-12 21:11:08 -0800332 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800333 int count = 0;
334 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
335 count += mTaskHistory.get(taskNdx).mActivities.size();
336 }
337 return count;
338 }
339
Craig Mautner4a1cb222013-12-04 16:14:06 -0800340 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
341 mActivityContainer = activityContainer;
342 mStackSupervisor = activityContainer.getOuter();
343 mService = mStackSupervisor.mService;
344 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
345 mWindowManager = mService.mWindowManager;
346 mStackId = activityContainer.mStackId;
347 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700348 }
Craig Mautner5962b122012-10-05 14:45:52 -0700349
Amith Yamasani734983f2014-03-04 16:48:05 -0800350 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100351 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800352 */
Kenny Guy2a764942014-04-02 13:29:20 +0100353 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100354 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100355 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
356 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800357 }
358 return false;
359 }
360
361 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100362 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700363 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
364 }
365
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800367 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700368 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
369 if (r != null) {
370 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800371 }
372 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 return null;
374 }
375
376 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800377 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
378 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800379 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800380 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
381 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800382 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800383 return r;
384 }
385 }
386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 return null;
388 }
389
390 /**
391 * This is a simplified version of topRunningActivityLocked that provides a number of
392 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800393 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 * @param token If non-null, any history records matching this token will be skipped.
395 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800396 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 * @return Returns the HistoryRecord of the next activity on the stack.
398 */
399 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800400 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
401 TaskRecord task = mTaskHistory.get(taskNdx);
402 if (task.taskId == taskId) {
403 continue;
404 }
405 ArrayList<ActivityRecord> activities = task.mActivities;
406 for (int i = activities.size() - 1; i >= 0; --i) {
407 final ActivityRecord r = activities.get(i);
408 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800409 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800410 return r;
411 }
412 }
413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 return null;
415 }
416
Craig Mautner8849a5e2013-04-02 16:41:03 -0700417 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700420 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
421 final ActivityRecord r = activities.get(activityNdx);
422 if (!r.finishing) {
423 return r;
424 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700425 }
426 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700427 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700428 }
429
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700430 final TaskRecord topTask() {
431 final int size = mTaskHistory.size();
432 if (size > 0) {
433 return mTaskHistory.get(size - 1);
434 }
435 return null;
436 }
437
Craig Mautnerd2328952013-03-05 12:46:26 -0800438 TaskRecord taskForIdLocked(int id) {
439 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
440 final TaskRecord task = mTaskHistory.get(taskNdx);
441 if (task.taskId == id) {
442 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800443 }
444 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700445 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700446 }
447
Craig Mautnerd2328952013-03-05 12:46:26 -0800448 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800449 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800450 if (r != null) {
451 final TaskRecord task = r.task;
Dianne Hackborn925e2b32014-09-03 16:40:49 -0700452 if (task != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800453 if (task.stack != this) Slog.w(TAG,
454 "Illegal state! task does not point to stack it is in.");
455 return r;
456 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800457 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800458 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800459 }
460
Craig Mautner2420ead2013-04-01 17:13:20 -0700461 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 final boolean hadit = mLRUActivities.remove(r);
463 mLRUActivities.add(r);
464 return hadit;
465 }
466
Craig Mautnerde4ef022013-04-07 19:01:33 -0700467 final boolean isHomeStack() {
468 return mStackId == HOME_STACK_ID;
469 }
470
Craig Mautnere0a38842013-12-16 16:14:02 -0800471 final boolean isOnHomeDisplay() {
472 return isAttached() &&
473 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
474 }
475
476 final void moveToFront() {
477 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800478 if (isOnHomeDisplay()) {
479 mStackSupervisor.moveHomeStack(isHomeStack());
480 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800481 mStacks.remove(this);
482 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800483 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800484 }
485
486 final boolean isAttached() {
487 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800488 }
489
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700490 /**
491 * Returns the top activity in any existing task matching the given
492 * Intent. Returns null if no such task is found.
493 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700494 ActivityRecord findTaskLocked(ActivityRecord target) {
495 Intent intent = target.intent;
496 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 ComponentName cls = intent.getComponent();
498 if (info.targetActivity != null) {
499 cls = new ComponentName(info.packageName, info.targetActivity);
500 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700501 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700502 boolean isDocument = intent != null & intent.isDocument();
503 // If documentData is non-null then it must match the existing task data.
504 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800505
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700506 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800507 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
508 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700509 if (task.voiceSession != null) {
510 // We never match voice sessions; those always run independently.
511 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
512 continue;
513 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700514 if (task.userId != userId) {
515 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700516 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700517 continue;
518 }
Craig Mautner000f0022013-02-26 15:04:29 -0800519 final ActivityRecord r = task.getTopActivity();
520 if (r == null || r.finishing || r.userId != userId ||
521 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700522 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800523 continue;
524 }
525
Craig Mautnerd00f4742014-03-12 14:17:26 -0700526 final Intent taskIntent = task.intent;
527 final Intent affinityIntent = task.affinityIntent;
528 final boolean taskIsDocument;
529 final Uri taskDocumentData;
530 if (taskIntent != null && taskIntent.isDocument()) {
531 taskIsDocument = true;
532 taskDocumentData = taskIntent.getData();
533 } else if (affinityIntent != null && affinityIntent.isDocument()) {
534 taskIsDocument = true;
535 taskDocumentData = affinityIntent.getData();
536 } else {
537 taskIsDocument = false;
538 taskDocumentData = null;
539 }
540
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700541 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700542 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700543 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700544 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700545 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
546 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700547 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700548 return r;
549 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700550 } else if (taskIntent != null && taskIntent.getComponent() != null &&
551 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700552 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700553 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800554 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700555 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
556 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800557 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700558 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
559 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700560 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700561 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800562 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700563 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
564 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800565 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700566 } else if (DEBUG_TASKS) {
567 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700568 }
569 }
570
571 return null;
572 }
573
574 /**
575 * Returns the first activity (starting from the top of the stack) that
576 * is the same as the given activity. Returns null if no such activity
577 * is found.
578 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700579 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700580 ComponentName cls = intent.getComponent();
581 if (info.targetActivity != null) {
582 cls = new ComponentName(info.packageName, info.targetActivity);
583 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700584 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700585
Craig Mautner000f0022013-02-26 15:04:29 -0800586 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700587 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100588 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700589 return null;
590 }
591 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800592 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
593 ActivityRecord r = activities.get(activityNdx);
594 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700595 //Slog.i(TAG, "Found matching class!");
596 //dump();
597 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
598 return r;
599 }
600 }
601 }
602
603 return null;
604 }
605
Amith Yamasani742a6712011-05-04 14:49:28 -0700606 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700607 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700608 */
Craig Mautner93529a42013-10-04 15:03:13 -0700609 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800610 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700611 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800612 }
613 mCurrentUser = userId;
614
615 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800616 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800617 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700618 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100619 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700620 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
621 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800622 mTaskHistory.remove(i);
623 mTaskHistory.add(task);
624 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800625 // Use same value for i.
626 } else {
627 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 }
629 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700630 if (VALIDATE_TOKENS) {
631 validateAppTokensLocked();
632 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700633 }
634
Craig Mautner2420ead2013-04-01 17:13:20 -0700635 void minimalResumeActivityLocked(ActivityRecord r) {
636 r.state = ActivityState.RESUMED;
637 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
638 + " (starting new instance)");
639 r.stopped = false;
640 mResumedActivity = r;
641 r.task.touchActiveTime();
642 mService.addRecentTaskLocked(r.task);
643 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700644 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700645 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700646 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700647 }
648
Dianne Hackborncee04b52013-07-03 17:01:28 -0700649 private void startLaunchTraces() {
650 if (mFullyDrawnStartTime != 0) {
651 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
652 }
653 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
654 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
655 }
656
657 private void stopFullyDrawnTraceIfNeeded() {
658 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
659 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
660 mFullyDrawnStartTime = 0;
661 }
662 }
663
Craig Mautnere79d42682013-04-01 19:01:53 -0700664 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700665 if (r.displayStartTime == 0) {
666 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
667 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700668 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700669 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700670 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700671 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700672 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700673 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700674 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700675 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800676
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700677 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700678 // Make sure that there is no activity waiting for this to launch.
679 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
680 r.displayStartTime = r.fullyDrawnStartTime = 0;
681 } else {
682 mStackSupervisor.removeTimeoutsForActivityLocked(r);
683 mStackSupervisor.scheduleIdleTimeoutLocked(r);
684 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700685 }
686
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800687 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800688 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800689 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
690 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
691 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
692 activities.get(activityNdx).setSleeping(false);
693 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800694 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800695 if (mPausingActivity != null) {
696 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
697 activityPausedLocked(mPausingActivity.appToken, true);
698 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800699 }
700
Craig Mautner0eea92c2013-05-16 13:35:39 -0700701 /**
702 * @return true if something must be done before going to sleep.
703 */
704 boolean checkReadyForSleepLocked() {
705 if (mResumedActivity != null) {
706 // Still have something resumed; can't sleep until it is paused.
707 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
708 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700709 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700710 return true;
711 }
712 if (mPausingActivity != null) {
713 // Still waiting for something to pause; can't sleep yet.
714 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
715 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800716 }
717
Craig Mautner0eea92c2013-05-16 13:35:39 -0700718 return false;
719 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800720
Craig Mautner0eea92c2013-05-16 13:35:39 -0700721 void goToSleep() {
722 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800723
Craig Mautner0eea92c2013-05-16 13:35:39 -0700724 // Make sure any stopped but visible activities are now sleeping.
725 // This ensures that the activity's onStop() is called.
726 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
727 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
728 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
729 final ActivityRecord r = activities.get(activityNdx);
730 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
731 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800732 }
733 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800734 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700735 }
Craig Mautner59c00972012-07-30 12:10:24 -0700736
Dianne Hackbornd2835932010-12-13 16:28:46 -0800737 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700738 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800739 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700740 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800741 return null;
742 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800743
Winson Chung083baf92014-07-11 10:32:42 -0700744 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700745 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800746 // we can just go ahead and skip taking the screenshot if this is the home stack.
747 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700748 return null;
749 }
750
Winson Chung48a10a52014-08-27 14:36:51 -0700751 int w = mService.mThumbnailWidth;
752 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800753 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800754 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
755 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
756 w, h, SCREENSHOT_FORCE_565);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800757 }
Winson Chung376543b2014-05-21 17:43:28 -0700758 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800759 return null;
760 }
761
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700762 /**
763 * Start pausing the currently resumed activity. It is an error to call this if there
764 * is already an activity being paused or there is no resumed activity.
765 *
766 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
767 * @param uiSleeping True if this is happening with the user interface going to sleep (the
768 * screen turning off).
769 * @param resuming True if this is being called as part of resuming the top activity, so
770 * we shouldn't try to instigate a resume here.
771 * @param dontWait True if the caller does not want to wait for the pause to complete. If
772 * set to true, we will immediately complete the pause here before returning.
773 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
774 * it to tell us when it is done.
775 */
776 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
777 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800778 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700779 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
780 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800781 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 ActivityRecord prev = mResumedActivity;
783 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700784 if (!resuming) {
785 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
786 mStackSupervisor.resumeTopActivitiesLocked();
787 }
788 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800790
791 if (mActivityContainer.mParentActivity == null) {
792 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700793 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800794 }
795
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700796 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
797 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800799 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700801 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
802 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700803 prev.state = ActivityState.PAUSING;
804 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700805 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700806 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800807 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800808 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700809 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700810 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700811
812 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800813
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 if (prev.app != null && prev.app.thread != null) {
815 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
816 try {
817 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700818 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700820 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800821 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700822 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 } catch (Exception e) {
824 // Ignore exception, if process died other code will cleanup.
825 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800826 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700828 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829 }
830 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800831 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700833 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 }
835
836 // If we are not going to sleep, we want to ensure the device is
837 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700838 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700839 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 }
841
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800842 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700843 // Have the window manager pause its key dispatching until the new
844 // activity has started. If we're pausing the activity just because
845 // the screen is being turned off and the UI is sleeping, don't interrupt
846 // key dispatch; the same activity will pick it up again on wakeup.
847 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800848 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 } else {
850 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
851 }
852
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700853 if (dontWait) {
854 // If the caller said they don't want to wait for the pause, then complete
855 // the pause now.
856 completePauseLocked(false);
857 return false;
858
859 } else {
860 // Schedule a pause timeout in case the app doesn't respond.
861 // We don't give it much time because this directly impacts the
862 // responsiveness seen by the user.
863 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
864 msg.obj = prev;
865 prev.pauseTime = SystemClock.uptimeMillis();
866 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
867 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
868 return true;
869 }
870
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700871 } else {
872 // This activity failed to schedule the
873 // pause, so just treat it as being paused now.
874 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700875 if (!resuming) {
876 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
877 }
878 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 }
880 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700881
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700882 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800884 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885
Craig Mautnerd2328952013-03-05 12:46:26 -0800886 final ActivityRecord r = isInStackLocked(token);
887 if (r != null) {
888 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
889 if (mPausingActivity == r) {
890 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
891 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700892 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800893 } else {
894 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
895 r.userId, System.identityHashCode(r), r.shortComponentName,
896 mPausingActivity != null
897 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 }
899 }
900 }
901
Craig Mautnera0026042014-04-23 11:45:37 -0700902 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
903 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700904 if (r.state != ActivityState.STOPPING) {
905 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
906 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
907 return;
908 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700909 if (persistentState != null) {
910 r.persistentState = persistentState;
911 mService.notifyTaskPersisterLocked(r.task, false);
912 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700913 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700914 if (icicle != null) {
915 // If icicle is null, this is happening due to a timeout, so we
916 // haven't really saved the state.
917 r.icicle = icicle;
918 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800919 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800920 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700921 }
922 if (!r.stopped) {
923 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
924 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
925 r.stopped = true;
926 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700927 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
928 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700929 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700930 if (r.finishing) {
931 r.clearOptionsLocked();
932 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700933 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700934 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700935 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700936 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700937 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800938 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700939 }
940 }
941 }
942
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700943 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800944 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700945 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800946
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800947 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700948 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800949 if (prev.finishing) {
950 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700951 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800952 } else if (prev.app != null) {
953 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
954 if (prev.waitingVisible) {
955 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800957 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
958 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800959 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800960 if (prev.configDestroy) {
961 // The previous is being paused because the configuration
962 // is changing, which means it is actually stopping...
963 // To juggle the fact that we are also starting a new
964 // instance right now, we need to first completely stop
965 // the current instance before starting the new one.
966 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700967 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700968 } else if (!hasVisibleBehindActivity()) {
969 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700970 // stopping.
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
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700991 if (resumeNext) {
992 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
993 if (!mService.isSleepingOrShuttingDown()) {
994 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
995 } else {
996 mStackSupervisor.checkReadyForSleepLocked();
997 ActivityRecord top = topStack.topRunningActivityLocked(null);
998 if (top == null || (prev != null && top != prev)) {
999 // If there are no more activities available to run,
1000 // do resume anyway to start something. Also if the top
1001 // activity on the stack is not the just paused activity,
1002 // we need to go ahead and resume it to ensure we complete
1003 // an in-flight app switch.
1004 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1005 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001006 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001007 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001008
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001009 if (prev != null) {
1010 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001011
Craig Mautner525f3d92013-05-07 14:01:50 -07001012 if (prev.app != null && prev.cpuTimeAtResume > 0
1013 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001014 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1015 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001016 if (diff > 0) {
1017 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1018 synchronized (bsi) {
1019 BatteryStatsImpl.Uid.Proc ps =
1020 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001021 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001022 if (ps != null) {
1023 ps.addForegroundTimeLocked(diff);
1024 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001025 }
1026 }
1027 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001028 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001030
1031 // Notfiy when the task stack has changed
1032 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001033 }
1034
1035 /**
1036 * Once we know that we have asked an application to put an activity in
1037 * the resumed state (either by launching it or explicitly telling it),
1038 * this function updates the rest of our state to match that fact.
1039 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001040 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 next.idle = false;
1042 next.results = null;
1043 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001044
1045 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1046 ProcessRecord app = next.task.mActivities.get(0).app;
1047 if (app != null && app != mService.mHomeProcess) {
1048 mService.mHomeProcess = app;
1049 }
1050 }
1051
Craig Mautner07566322013-09-26 16:42:55 -07001052 if (next.nowVisible) {
1053 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001054 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001055 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001056
1057 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001058 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001060 mStackSupervisor.reportResumedActivityLocked(next);
1061
1062 next.resumeKeyDispatchingLocked();
1063 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064
1065 // Mark the point when the activity is resuming
1066 // TODO: To be more accurate, the mark should be before the onCreate,
1067 // not after the onResume. But for subsequent starts, onResume is fine.
1068 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001069 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070 } else {
1071 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1072 }
Winson Chung376543b2014-05-21 17:43:28 -07001073
George Mount6ba042b2014-07-28 11:12:28 -07001074 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001075
1076 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1077 // When resuming an activity, require it to call requestVisibleBehind() again.
1078 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1079 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001080 }
1081
Craig Mautnere3a00d72014-04-16 08:31:19 -07001082 private void setVisibile(ActivityRecord r, boolean visible) {
1083 r.visible = visible;
1084 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001085 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001086 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001087 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001088 container.setVisible(visible);
1089 }
1090 }
1091
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001092 // Find the first visible activity above the passed activity and if it is translucent return it
1093 // otherwise return null;
1094 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1095 TaskRecord task = r.task;
1096 if (task == null) {
1097 return null;
1098 }
1099
1100 ActivityStack stack = task.stack;
1101 if (stack == null) {
1102 return null;
1103 }
1104
1105 int stackNdx = mStacks.indexOf(stack);
1106
1107 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1108 int taskNdx = tasks.indexOf(task);
1109
1110 ArrayList<ActivityRecord> activities = task.mActivities;
1111 int activityNdx = activities.indexOf(r) + 1;
1112
1113 final int numStacks = mStacks.size();
1114 while (stackNdx < numStacks) {
1115 tasks = mStacks.get(stackNdx).mTaskHistory;
1116 final int numTasks = tasks.size();
1117 while (taskNdx < numTasks) {
1118 activities = tasks.get(taskNdx).mActivities;
1119 final int numActivities = activities.size();
1120 while (activityNdx < numActivities) {
1121 final ActivityRecord activity = activities.get(activityNdx);
1122 if (!activity.finishing) {
1123 return activity.fullscreen ? null : activity;
1124 }
1125 ++activityNdx;
1126 }
1127 activityNdx = 0;
1128 ++taskNdx;
1129 }
1130 taskNdx = 0;
1131 ++stackNdx;
1132 }
1133
1134 return null;
1135 }
1136
Jose Lima7ba71252014-04-30 12:59:27 -07001137 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1138 // If so, this stack is hidden, otherwise it is visible.
1139 private boolean isStackVisible() {
1140 if (!isAttached()) {
1141 return false;
1142 }
1143
1144 if (mStackSupervisor.isFrontStack(this)) {
1145 return true;
1146 }
1147
Jose Lima729cb232014-05-05 15:59:40 -07001148 /**
1149 * Start at the task above this one and go up, looking for a visible
1150 * fullscreen activity, or a translucent activity that requested the
1151 * wallpaper to be shown behind it.
1152 */
Jose Lima7ba71252014-04-30 12:59:27 -07001153 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1154 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1155 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001156 final TaskRecord task = tasks.get(taskNdx);
1157 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001158 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1159 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001160
1161 // Conditions for an activity to obscure the stack we're
1162 // examining:
1163 // 1. Not Finishing AND Visible AND:
1164 // 2. Either:
1165 // - Full Screen Activity OR
1166 // - On top of Home and our stack is NOT home
1167 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001168 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001169 return false;
1170 }
1171 }
1172 }
1173 }
1174
1175 return true;
1176 }
1177
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001178 /**
1179 * Make sure that all activities that need to be visible (that is, they
1180 * currently can be seen by the user) actually are.
1181 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001182 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1183 ActivityRecord top = topRunningActivityLocked(null);
1184 if (top == null) {
1185 return;
1186 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001187 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001188 TAG, "ensureActivitiesVisible behind " + top
1189 + " configChanges=0x" + Integer.toHexString(configChanges));
1190
Craig Mautner5eda9b32013-07-02 11:58:16 -07001191 if (mTranslucentActivityWaiting != top) {
1192 mUndrawnActivitiesBelowTopTranslucent.clear();
1193 if (mTranslucentActivityWaiting != null) {
1194 // Call the callback with a timeout indication.
1195 notifyActivityDrawnLocked(null);
1196 mTranslucentActivityWaiting = null;
1197 }
1198 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1199 }
1200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201 // If the top activity is not fullscreen, then we need to
1202 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001203 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001204 boolean behindFullscreen = !isStackVisible();
1205
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001206 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001207 final TaskRecord task = mTaskHistory.get(taskNdx);
1208 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001209 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1210 final ActivityRecord r = activities.get(activityNdx);
1211 if (r.finishing) {
1212 continue;
1213 }
1214 if (aboveTop && r != top) {
1215 continue;
1216 }
1217 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001218 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1219 // but must be drawn initially for the animation as though they were visible.
1220 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001221 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001222 TAG, "Make visible? " + r + " finishing=" + r.finishing
1223 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001224
Craig Mautnerd44711d2013-02-23 11:24:36 -08001225 // First: if this is not the current activity being started, make
1226 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001227 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001228 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001229 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001230
1231 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001232 // This activity needs to be visible, but isn't even
1233 // running... get it started, but don't resume it
1234 // at this point.
1235 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1236 if (r != starting) {
1237 r.startFreezingScreenLocked(r.app, configChanges);
1238 }
1239 if (!r.visible || r.mLaunchTaskBehind) {
1240 if (DEBUG_VISBILITY) Slog.v(
1241 TAG, "Starting and making visible: " + r);
1242 setVisibile(r, true);
1243 }
1244 if (r != starting) {
1245 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001246 }
1247
1248 } else if (r.visible) {
1249 // If this activity is already visible, then there is nothing
1250 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001251 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001252 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001253 try {
George Mount6ba042b2014-07-28 11:12:28 -07001254 if (r.returningOptions != null) {
1255 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1256 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001257 }
1258 } catch(RemoteException e) {
1259 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001260 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001261 // This activity is not currently visible, but is running.
1262 // Tell it to become visible.
1263 r.visible = true;
1264 if (r.state != ActivityState.RESUMED && r != starting) {
1265 // If this activity is paused, tell it
1266 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001267 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001268 TAG, "Making visible and scheduling visibility: " + r);
1269 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001270 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001271 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001272 mUndrawnActivitiesBelowTopTranslucent.add(r);
1273 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001274 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001275 r.sleeping = false;
1276 r.app.pendingUiClean = true;
1277 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1278 r.stopFreezingScreenLocked(false);
1279 } catch (Exception e) {
1280 // Just skip on any failure; we'll make it
1281 // visible when it next restarts.
1282 Slog.w(TAG, "Exception thrown making visibile: "
1283 + r.intent.getComponent(), e);
1284 }
1285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001287
Craig Mautnerd44711d2013-02-23 11:24:36 -08001288 // Aggregate current change flags.
1289 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001290
Craig Mautnerd44711d2013-02-23 11:24:36 -08001291 if (r.fullscreen) {
1292 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001293 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1294 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001295 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001296 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001297 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001298 }
1299 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001300 if (DEBUG_VISBILITY) Slog.v(
1301 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1302 + " state=" + r.state
1303 + " behindFullscreen=" + behindFullscreen);
1304 // Now for any activities that aren't visible to the user, make
1305 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001306 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001307 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001308 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001309 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001310 switch (r.state) {
1311 case STOPPING:
1312 case STOPPED:
1313 if (r.app != null && r.app.thread != null) {
1314 if (DEBUG_VISBILITY) Slog.v(
1315 TAG, "Scheduling invisibility: " + r);
1316 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1317 }
1318 break;
1319
1320 case INITIALIZING:
1321 case RESUMED:
1322 case PAUSING:
1323 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001324 // This case created for transitioning activities from
1325 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001326 if (getVisibleBehindActivity() == r) {
1327 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001328 } else {
1329 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1330 mStackSupervisor.mStoppingActivities.add(r);
1331 }
1332 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001333 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001334 break;
1335
1336 default:
1337 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001338 }
1339 } catch (Exception e) {
1340 // Just skip on any failure; we'll make it
1341 // visible when it next restarts.
1342 Slog.w(TAG, "Exception thrown making hidden: "
1343 + r.intent.getComponent(), e);
1344 }
1345 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001346 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001347 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001348 }
1349 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001351
1352 if (mTranslucentActivityWaiting != null &&
1353 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1354 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1355 notifyActivityDrawnLocked(null);
1356 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 }
Craig Mautner58547802013-03-05 08:23:53 -08001358
George Mount6ba042b2014-07-28 11:12:28 -07001359 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001360 mTranslucentActivityWaiting = r;
1361 mUndrawnActivitiesBelowTopTranslucent.clear();
1362 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1363 }
1364
1365 /**
1366 * Called as activities below the top translucent activity are redrawn. When the last one is
1367 * redrawn notify the top activity by calling
1368 * {@link Activity#onTranslucentConversionComplete}.
1369 *
1370 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1371 * occurred and the activity will be notified immediately.
1372 */
1373 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001374 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001375 if ((r == null)
1376 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1377 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1378 // The last undrawn activity below the top has just been drawn. If there is an
1379 // opaque activity at the top, notify it that it can become translucent safely now.
1380 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1381 mTranslucentActivityWaiting = null;
1382 mUndrawnActivitiesBelowTopTranslucent.clear();
1383 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1384
Craig Mautner71dd1b62014-02-18 15:48:52 -08001385 if (waitingActivity != null) {
1386 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1387 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1388 try {
1389 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1390 waitingActivity.appToken, r != null);
1391 } catch (RemoteException e) {
1392 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001393 }
1394 }
1395 }
1396 }
1397
Craig Mautnera61bc652013-10-28 15:43:18 -07001398 /** If any activities below the top running one are in the INITIALIZING state and they have a
1399 * starting window displayed then remove that starting window. It is possible that the activity
1400 * in this state will never resumed in which case that starting window will be orphaned. */
1401 void cancelInitializingActivities() {
1402 final ActivityRecord topActivity = topRunningActivityLocked(null);
1403 boolean aboveTop = true;
1404 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1405 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1406 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1407 final ActivityRecord r = activities.get(activityNdx);
1408 if (aboveTop) {
1409 if (r == topActivity) {
1410 aboveTop = false;
1411 }
1412 continue;
1413 }
1414
1415 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1416 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1417 r.mStartingWindowShown = false;
1418 mWindowManager.removeAppStartingWindow(r.appToken);
1419 }
1420 }
1421 }
1422 }
1423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 /**
1425 * Ensure that the top activity in the stack is resumed.
1426 *
1427 * @param prev The previously resumed activity, for when in the process
1428 * of pausing; can be null to call from elsewhere.
1429 *
1430 * @return Returns true if something is being resumed, or false if
1431 * nothing happened.
1432 */
1433 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001434 return resumeTopActivityLocked(prev, null);
1435 }
1436
1437 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001438 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001439 // Don't even start recursing.
1440 return false;
1441 }
1442
1443 boolean result = false;
1444 try {
1445 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001446 mStackSupervisor.inResumeTopActivity = true;
1447 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1448 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001449 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001450 }
Craig Mautner544efa72014-09-04 16:41:20 -07001451 result = resumeTopActivityInnerLocked(prev, options);
1452 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001453 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001454 }
1455 return result;
1456 }
1457
1458 final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001459 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1460
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001461 if (!mService.mBooting && !mService.mBooted) {
1462 // Not ready yet!
1463 return false;
1464 }
1465
Craig Mautnerdf88d732014-01-27 09:21:32 -08001466 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001467 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001468 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001469 // Do not resume this stack if its parent is not resumed.
1470 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1471 return false;
1472 }
1473
Craig Mautnera61bc652013-10-28 15:43:18 -07001474 cancelInitializingActivities();
1475
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 // Find the first activity that is not finishing.
1477 ActivityRecord next = topRunningActivityLocked(null);
1478
1479 // Remember how we'll process this pause/resume situation, and ensure
1480 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001481 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1482 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483
Craig Mautner84984fa2014-06-19 11:19:20 -07001484 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001485 if (next == null) {
1486 // There are no more activities! Let's just start up the
1487 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001488 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001489 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001490 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001491 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001492 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1493 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1494 return isOnHomeDisplay() &&
1495 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 }
1497
1498 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001499
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001501 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1502 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 // Make sure we have executed any pending transitions, since there
1504 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001505 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001507 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001508 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001509 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510 return false;
1511 }
1512
Craig Mautner525f3d92013-05-07 14:01:50 -07001513 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001514 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001515 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001516 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001517 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001518 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001519 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001520 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001521 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001522 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001523 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001524 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001525 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1526 "resumeTopActivityLocked: Launching home next");
1527 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001528 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1529 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1530 return isOnHomeDisplay() &&
1531 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001532 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001533 }
1534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 // If we are sleeping, and there is no resumed activity, and the top
1536 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001537 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001538 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001539 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540 // Make sure we have executed any pending transitions, since there
1541 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001542 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001544 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001545 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001546 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 return false;
1548 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001549
1550 // Make sure that the user who owns this activity is started. If not,
1551 // we will just leave it as is because someone should be bringing
1552 // another user's activities to the top of the stack.
1553 if (mService.mStartedUsers.get(next.userId) == null) {
1554 Slog.w(TAG, "Skipping resume of top activity " + next
1555 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001556 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001557 return false;
1558 }
1559
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001560 // The activity may be waiting for stop, but that is no longer
1561 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001562 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001563 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001564 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001565 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566
1567 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1568
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001569 // If we are currently pausing an activity, then don't do anything
1570 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001571 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001572 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1573 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001574 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001575 return false;
1576 }
1577
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001578 // Okay we are now going to start a switch, to 'next'. We may first
1579 // have to pause the current activity, but this is an important point
1580 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001581 // XXX "App Redirected" dialog is getting too many false positives
1582 // at this point, so turn off for now.
1583 if (false) {
1584 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1585 long now = SystemClock.uptimeMillis();
1586 final boolean inTime = mLastStartedActivity.startTime != 0
1587 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1588 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1589 final int nextUid = next.info.applicationInfo.uid;
1590 if (inTime && lastUid != nextUid
1591 && lastUid != next.launchedFromUid
1592 && mService.checkPermission(
1593 android.Manifest.permission.STOP_APP_SWITCHES,
1594 -1, next.launchedFromUid)
1595 != PackageManager.PERMISSION_GRANTED) {
1596 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1597 } else {
1598 next.startTime = now;
1599 mLastStartedActivity = next;
1600 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001601 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001602 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001603 mLastStartedActivity = next;
1604 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001605 }
Craig Mautner58547802013-03-05 08:23:53 -08001606
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 // We need to start pausing the current activity so the top one
1608 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001609 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1610 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001611 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001612 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001613 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001614 }
1615 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001616 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1617 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001618 // At this point we want to put the upcoming activity's process
1619 // at the top of the LRU list, since we know we will be needing it
1620 // very soon and it would be a waste to let it get killed if it
1621 // happens to be sitting towards the end.
1622 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001623 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001624 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001625 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 return true;
1627 }
1628
Christopher Tated3f175c2012-06-14 14:16:54 -07001629 // If the most recent activity was noHistory but was only stopped rather
1630 // than stopped+finished because the device went to sleep, we need to make
1631 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001632 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001633 !mLastNoHistoryActivity.finishing) {
1634 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1635 " on new resume");
1636 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1637 null, "no-history", false);
1638 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001639 }
1640
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 if (prev != null && prev != next) {
1642 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1643 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001644 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001645 if (DEBUG_SWITCH) Slog.v(
1646 TAG, "Resuming top, waiting visible to hide: " + prev);
1647 } else {
1648 // The next activity is already visible, so hide the previous
1649 // activity's windows right now so we can show the new one ASAP.
1650 // We only do this if the previous is finishing, which should mean
1651 // it is on top of the one being resumed so hiding it quickly
1652 // is good. Otherwise, we want to do the normal route of allowing
1653 // the resumed activity to be shown so we can decide if the
1654 // previous should actually be hidden depending on whether the
1655 // new one is found to be full-screen or not.
1656 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001657 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001658 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1659 + prev + ", waitingVisible="
1660 + (prev != null ? prev.waitingVisible : null)
1661 + ", nowVisible=" + next.nowVisible);
1662 } else {
1663 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1664 + prev + ", waitingVisible="
1665 + (prev != null ? prev.waitingVisible : null)
1666 + ", nowVisible=" + next.nowVisible);
1667 }
1668 }
1669 }
1670
Dianne Hackborne7f97212011-02-24 14:40:20 -08001671 // Launching this app's activity, make sure the app is no longer
1672 // considered stopped.
1673 try {
1674 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001675 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001676 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001677 } catch (IllegalArgumentException e) {
1678 Slog.w(TAG, "Failed trying to unstop package "
1679 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001680 }
1681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001682 // We are starting up the next activity, so tell the window manager
1683 // that the previous one will be hidden soon. This way it can know
1684 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001685 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001686 if (prev != null) {
1687 if (prev.finishing) {
1688 if (DEBUG_TRANSITION) Slog.v(TAG,
1689 "Prepare close transition: prev=" + prev);
1690 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001691 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001692 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001694 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001695 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1696 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001697 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001698 mWindowManager.setAppWillBeHidden(prev.appToken);
1699 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001701 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001703 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001704 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001706 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001707 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001708 : next.mLaunchTaskBehind
1709 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1710 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 }
1712 }
1713 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001714 mWindowManager.setAppWillBeHidden(prev.appToken);
1715 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001716 }
Craig Mautner967212c2013-04-13 21:10:58 -07001717 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001718 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001720 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001721 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001723 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 }
1725 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001726
1727 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001728 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001729 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1730 if (opts != null) {
1731 resumeAnimOptions = opts.toBundle();
1732 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001733 next.applyOptionsLocked();
1734 } else {
1735 next.clearOptionsLocked();
1736 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001737
Craig Mautnercf910b02013-04-23 11:23:27 -07001738 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001739 if (next.app != null && next.app.thread != null) {
1740 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1741
1742 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001743 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001745 // schedule launch ticks to collect information about slow apps.
1746 next.startLaunchTickingLocked();
1747
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001748 ActivityRecord lastResumedActivity =
1749 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 ActivityState lastState = next.state;
1751
1752 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001753
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001754 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 next.state = ActivityState.RESUMED;
1756 mResumedActivity = next;
1757 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001758 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001759 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001760 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001761 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762
1763 // Have the window manager re-evaluate the orientation of
1764 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001765 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001766 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001767 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001768 mService.mConfiguration,
1769 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1770 if (config != null) {
1771 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001773 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001775
Craig Mautner525f3d92013-05-07 14:01:50 -07001776 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 // The configuration update wasn't able to keep the existing
1778 // instance of the activity, and instead started a new one.
1779 // We should be all done, but let's just make sure our activity
1780 // is still at the top and schedule another run if something
1781 // weird happened.
1782 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001783 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 "Activity config changed during resume: " + next
1785 + ", new next: " + nextNext);
1786 if (nextNext != next) {
1787 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001788 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001790 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001791 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001792 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001793 return true;
1794 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001795 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001796 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
Craig Mautner58547802013-03-05 08:23:53 -08001798
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 try {
1800 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001801 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001802 if (a != null) {
1803 final int N = a.size();
1804 if (!next.finishing && N > 0) {
1805 if (DEBUG_RESULTS) Slog.v(
1806 TAG, "Delivering results to " + next
1807 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001808 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 }
1810 }
1811
1812 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001813 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 }
1815
1816 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001817 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001819
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001820 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001821 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001822 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001823 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001824 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001825 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001826 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001827
Craig Mautner0eea92c2013-05-16 13:35:39 -07001828 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829
Craig Mautnerac6f8432013-07-17 13:24:59 -07001830 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001831 } catch (Exception e) {
1832 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001833 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1834 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001836 if (lastStack != null) {
1837 lastStack.mResumedActivity = lastResumedActivity;
1838 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 Slog.i(TAG, "Restarting because process died: " + next);
1840 if (!next.hasBeenLaunched) {
1841 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001842 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1843 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001844 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001845 next.appToken, next.packageName, next.theme,
1846 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001847 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1848 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 }
George Mount2c92c972014-03-20 09:38:23 -07001850 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001851 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001852 return true;
1853 }
1854
1855 // From this point on, if something goes wrong there is no way
1856 // to recover the activity.
1857 try {
1858 next.visible = true;
1859 completeResumeLocked(next);
1860 } catch (Exception e) {
1861 // If any exception gets thrown, toss away this
1862 // activity and try the next one.
1863 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001864 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001865 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001866 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001867 return true;
1868 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 next.stopped = false;
1870
1871 } else {
1872 // Whoops, need to restart this activity!
1873 if (!next.hasBeenLaunched) {
1874 next.hasBeenLaunched = true;
1875 } else {
1876 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001877 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001878 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001879 mService.compatibilityInfoForPackageLocked(
1880 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001882 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001883 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001884 }
1885 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1886 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001887 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001888 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001889 }
1890
Craig Mautnercf910b02013-04-23 11:23:27 -07001891 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001892 return true;
1893 }
1894
Craig Mautnerac6f8432013-07-17 13:24:59 -07001895 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001896 // If this is being moved to the top by another activity or being launched from the home
1897 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001898 if (isOnHomeDisplay()) {
1899 ActivityStack lastStack = mStackSupervisor.getLastStack();
1900 final boolean fromHome = lastStack.isHomeStack();
1901 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001902 task.setTaskToReturnTo(fromHome
1903 ? lastStack.topTask() == null
1904 ? HOME_ACTIVITY_TYPE
1905 : lastStack.topTask().taskType
1906 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001907 }
1908 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001909 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001910 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001911
Craig Mautnerac6f8432013-07-17 13:24:59 -07001912 mTaskHistory.remove(task);
1913 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001914 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001915 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001916 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001917 while (--taskNdx >= 0) {
1918 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001919 break;
1920 }
1921 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001922 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001923 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001924 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001925 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001926 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001927
Craig Mautner8849a5e2013-04-02 16:41:03 -07001928 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001929 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001930 TaskRecord rTask = r.task;
1931 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001932 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1933 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001934 // Last activity in task had been removed or ActivityManagerService is reusing task.
1935 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001936 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001937 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001938 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001939 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001940 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 if (!newTask) {
1942 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001943 boolean startIt = true;
1944 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1945 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001946 if (task.getTopActivity() == null) {
1947 // All activities in task are finishing.
1948 continue;
1949 }
Craig Mautner70a86932013-02-28 22:37:44 -08001950 if (task == r.task) {
1951 // Here it is! Now, if this is not yet visible to the
1952 // user, then just add it without starting; it will
1953 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001954 if (!startIt) {
1955 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1956 + task, new RuntimeException("here").fillInStackTrace());
1957 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001958 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001959 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1960 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001961 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001962 r.userId, r.info.configChanges, task.voiceSession != null,
1963 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001964 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001965 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001967 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001968 return;
1969 }
1970 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001971 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 startIt = false;
1973 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 }
1975 }
1976
1977 // Place a new activity at top of stack, so it is next to interact
1978 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 // If we are not placing the new activity frontmost, we do not want
1981 // to deliver the onUserLeaving callback to the actual frontmost
1982 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001983 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001984 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001985 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1986 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001987 }
Craig Mautner70a86932013-02-28 22:37:44 -08001988
1989 task = r.task;
1990
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001992 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001993 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001994 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001995 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001996
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001997 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001998 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999 // We want to show the starting preview window if we are
2000 // switching to a new task, or the next activity's process is
2001 // not currently running.
2002 boolean showStartingIcon = newTask;
2003 ProcessRecord proc = r.app;
2004 if (proc == null) {
2005 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2006 }
2007 if (proc == null || proc.thread == null) {
2008 showStartingIcon = true;
2009 }
2010 if (DEBUG_TRANSITION) Slog.v(TAG,
2011 "Prepare open transition: starting " + r);
2012 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002013 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002014 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002015 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002016 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002017 ? r.mLaunchTaskBehind
2018 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2019 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002020 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002021 mNoAnimActivities.remove(r);
2022 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002023 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002024 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002025 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002026 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 boolean doShow = true;
2028 if (newTask) {
2029 // Even though this activity is starting fresh, we still need
2030 // to reset it to make sure we apply affinities to move any
2031 // existing activities from other tasks in to it.
2032 // If the caller has requested that the target task be
2033 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002034 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 resetTaskIfNeededLocked(r, r);
2036 doShow = topRunningNonDelayedActivityLocked(null) == r;
2037 }
George Mount70778d72014-07-01 16:33:45 -07002038 } else if (options != null && new ActivityOptions(options).getAnimationType()
2039 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2040 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002042 if (r.mLaunchTaskBehind) {
2043 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2044 // tell WindowManager that r is visible even though it is at the back of the stack.
2045 mWindowManager.setAppVisibility(r.appToken, true);
2046 ensureActivitiesVisibleLocked(null, 0);
2047 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 // Figure out if we are transitioning from another activity that is
2049 // "has the same starting icon" as the next one. This allows the
2050 // window manager to keep the previous window it had previously
2051 // created, if it still had one.
2052 ActivityRecord prev = mResumedActivity;
2053 if (prev != null) {
2054 // We don't want to reuse the previous starting preview if:
2055 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002056 if (prev.task != r.task) {
2057 prev = null;
2058 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002059 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002060 else if (prev.nowVisible) {
2061 prev = null;
2062 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002063 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002064 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002065 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002066 mService.compatibilityInfoForPackageLocked(
2067 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002068 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002069 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002070 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 }
2072 } else {
2073 // If this is the first activity, don't do any fancy animations,
2074 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002075 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002076 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002077 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002078 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002079 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002080 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 }
2082 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002083 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 }
2085
2086 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002087 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 }
2089 }
2090
Dianne Hackbornbe707852011-11-11 14:32:10 -08002091 final void validateAppTokensLocked() {
2092 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002093 mValidateAppTokens.ensureCapacity(numActivities());
2094 final int numTasks = mTaskHistory.size();
2095 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2096 TaskRecord task = mTaskHistory.get(taskNdx);
2097 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002098 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002099 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002100 }
Craig Mautner000f0022013-02-26 15:04:29 -08002101 TaskGroup group = new TaskGroup();
2102 group.taskId = task.taskId;
2103 mValidateAppTokens.add(group);
2104 final int numActivities = activities.size();
2105 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2106 final ActivityRecord r = activities.get(activityNdx);
2107 group.tokens.add(r.appToken);
2108 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002109 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002110 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002111 }
2112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 /**
2114 * Perform a reset of the given task, if needed as part of launching it.
2115 * Returns the new HistoryRecord at the top of the task.
2116 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002117 /**
2118 * Helper method for #resetTaskIfNeededLocked.
2119 * We are inside of the task being reset... we'll either finish this activity, push it out
2120 * for another task, or leave it as-is.
2121 * @param task The task containing the Activity (taskTop) that might be reset.
2122 * @param forceReset
2123 * @return An ActivityOptions that needs to be processed.
2124 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002125 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002126 ActivityOptions topOptions = null;
2127
2128 int replyChainEnd = -1;
2129 boolean canMoveOptions = true;
2130
2131 // We only do this for activities that are not the root of the task (since if we finish
2132 // the root, we may no longer have the task!).
2133 final ArrayList<ActivityRecord> activities = task.mActivities;
2134 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002135 final int rootActivityNdx = task.findEffectiveRootIndex();
2136 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002137 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002138 if (target.frontOfTask)
2139 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140
2141 final int flags = target.info.flags;
2142 final boolean finishOnTaskLaunch =
2143 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2144 final boolean allowTaskReparenting =
2145 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2146 final boolean clearWhenTaskReset =
2147 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2148
2149 if (!finishOnTaskLaunch
2150 && !clearWhenTaskReset
2151 && target.resultTo != null) {
2152 // If this activity is sending a reply to a previous
2153 // activity, we can't do anything with it now until
2154 // we reach the start of the reply chain.
2155 // XXX note that we are assuming the result is always
2156 // to the previous activity, which is almost always
2157 // the case but we really shouldn't count on.
2158 if (replyChainEnd < 0) {
2159 replyChainEnd = i;
2160 }
2161 } else if (!finishOnTaskLaunch
2162 && !clearWhenTaskReset
2163 && allowTaskReparenting
2164 && target.taskAffinity != null
2165 && !target.taskAffinity.equals(task.affinity)) {
2166 // If this activity has an affinity for another
2167 // task, then we need to move it out of here. We will
2168 // move it as far out of the way as possible, to the
2169 // bottom of the activity stack. This also keeps it
2170 // correctly ordered with any activities we previously
2171 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002172 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002173 final ActivityRecord bottom =
2174 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002175 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002176 if (bottom != null && target.taskAffinity != null
2177 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002178 // If the activity currently at the bottom has the
2179 // same task affinity as the one we are moving,
2180 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002181 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002182 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002183 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002184 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002185 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002186 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002187 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002188 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2189 + " out to new task " + target.task);
2190 }
2191
Craig Mautnere3a74d52013-02-22 14:14:58 -08002192 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002193 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002194
Craig Mautner525f3d92013-05-07 14:01:50 -07002195 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002196 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2197 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002198 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002199 if (p.finishing) {
2200 continue;
2201 }
2202
Craig Mautnere3a74d52013-02-22 14:14:58 -08002203 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002204 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002205 topOptions = p.takeOptionsLocked();
2206 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002207 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002208 }
2209 }
2210 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002211 + task + " adding to task=" + targetTask
2212 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002213 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2214 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002215 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002216 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002217
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002218 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002219 }
2220
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002221 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002222 if (VALIDATE_TOKENS) {
2223 validateAppTokensLocked();
2224 }
2225
2226 replyChainEnd = -1;
2227 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2228 // If the activity should just be removed -- either
2229 // because it asks for it, or the task should be
2230 // cleared -- then finish it and anything that is
2231 // part of its reply chain.
2232 int end;
2233 if (clearWhenTaskReset) {
2234 // In this case, we want to finish this activity
2235 // and everything above it, so be sneaky and pretend
2236 // like these are all in the reply chain.
2237 end = numActivities - 1;
2238 } else if (replyChainEnd < 0) {
2239 end = i;
2240 } else {
2241 end = replyChainEnd;
2242 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002243 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002244 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002245 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002246 if (p.finishing) {
2247 continue;
2248 }
2249 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002250 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002251 topOptions = p.takeOptionsLocked();
2252 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002253 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002254 }
2255 }
Craig Mautner58547802013-03-05 08:23:53 -08002256 if (DEBUG_TASKS) Slog.w(TAG,
2257 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002258 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002259 end--;
2260 srcPos--;
2261 }
2262 }
2263 replyChainEnd = -1;
2264 } else {
2265 // If we were in the middle of a chain, well the
2266 // activity that started it all doesn't want anything
2267 // special, so leave it all as-is.
2268 replyChainEnd = -1;
2269 }
2270 }
2271
2272 return topOptions;
2273 }
2274
2275 /**
2276 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2277 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2278 * @param affinityTask The task we are looking for an affinity to.
2279 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2280 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2281 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2282 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002283 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002284 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002285 int replyChainEnd = -1;
2286 final int taskId = task.taskId;
2287 final String taskAffinity = task.affinity;
2288
2289 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2290 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002291 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2292
2293 // Do not operate on or below the effective root Activity.
2294 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002295 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002296 if (target.frontOfTask)
2297 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002298
2299 final int flags = target.info.flags;
2300 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2301 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2302
2303 if (target.resultTo != null) {
2304 // If this activity is sending a reply to a previous
2305 // activity, we can't do anything with it now until
2306 // we reach the start of the reply chain.
2307 // XXX note that we are assuming the result is always
2308 // to the previous activity, which is almost always
2309 // the case but we really shouldn't count on.
2310 if (replyChainEnd < 0) {
2311 replyChainEnd = i;
2312 }
2313 } else if (topTaskIsHigher
2314 && allowTaskReparenting
2315 && taskAffinity != null
2316 && taskAffinity.equals(target.taskAffinity)) {
2317 // This activity has an affinity for our task. Either remove it if we are
2318 // clearing or move it over to our task. Note that
2319 // we currently punt on the case where we are resetting a
2320 // task that is not at the top but who has activities above
2321 // with an affinity to it... this is really not a normal
2322 // case, and we will need to later pull that task to the front
2323 // and usually at that point we will do the reset and pick
2324 // up those remaining activities. (This only happens if
2325 // someone starts an activity in a new task from an activity
2326 // in a task that is not currently on top.)
2327 if (forceReset || finishOnTaskLaunch) {
2328 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2329 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2330 for (int srcPos = start; srcPos >= i; --srcPos) {
2331 final ActivityRecord p = activities.get(srcPos);
2332 if (p.finishing) {
2333 continue;
2334 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002335 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002336 }
2337 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002338 if (taskInsertionPoint < 0) {
2339 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002340
Craig Mautner77878772013-03-04 19:46:24 -08002341 }
Craig Mautner77878772013-03-04 19:46:24 -08002342
2343 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2344 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2345 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2346 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002347 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002348 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002349 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002350
Craig Mautnere3a74d52013-02-22 14:14:58 -08002351 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2352 + " to stack at " + task,
2353 new RuntimeException("here").fillInStackTrace());
2354 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2355 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002356 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002357 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002358 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002359 if (VALIDATE_TOKENS) {
2360 validateAppTokensLocked();
2361 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002362
2363 // Now we've moved it in to place... but what if this is
2364 // a singleTop activity and we have put it on top of another
2365 // instance of the same activity? Then we drop the instance
2366 // below so it remains singleTop.
2367 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2368 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002369 int targetNdx = taskActivities.indexOf(target);
2370 if (targetNdx > 0) {
2371 ActivityRecord p = taskActivities.get(targetNdx - 1);
2372 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002373 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2374 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002375 }
2376 }
2377 }
2378 }
2379
2380 replyChainEnd = -1;
2381 }
2382 }
Craig Mautner77878772013-03-04 19:46:24 -08002383 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002384 }
2385
Craig Mautner8849a5e2013-04-02 16:41:03 -07002386 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002387 ActivityRecord newActivity) {
2388 boolean forceReset =
2389 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2390 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2391 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2392 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2393 forceReset = true;
2394 }
2395 }
2396
2397 final TaskRecord task = taskTop.task;
2398
2399 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2400 * for remaining tasks. Used for later tasks to reparent to task. */
2401 boolean taskFound = false;
2402
2403 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2404 ActivityOptions topOptions = null;
2405
Craig Mautner77878772013-03-04 19:46:24 -08002406 // Preserve the location for reparenting in the new task.
2407 int reparentInsertionPoint = -1;
2408
Craig Mautnere3a74d52013-02-22 14:14:58 -08002409 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2410 final TaskRecord targetTask = mTaskHistory.get(i);
2411
2412 if (targetTask == task) {
2413 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2414 taskFound = true;
2415 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002416 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2417 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002418 }
2419 }
2420
Craig Mautner70a86932013-02-28 22:37:44 -08002421 int taskNdx = mTaskHistory.indexOf(task);
2422 do {
2423 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2424 } while (taskTop == null && taskNdx >= 0);
2425
Craig Mautnere3a74d52013-02-22 14:14:58 -08002426 if (topOptions != null) {
2427 // If we got some ActivityOptions from an activity on top that
2428 // was removed from the task, propagate them to the new real top.
2429 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002430 taskTop.updateOptionsLocked(topOptions);
2431 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002432 topOptions.abort();
2433 }
2434 }
2435
2436 return taskTop;
2437 }
2438
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2440 String resultWho, int requestCode, int resultCode, Intent data) {
2441
2442 if (callingUid > 0) {
2443 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002444 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002445 }
2446
2447 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2448 + " : who=" + resultWho + " req=" + requestCode
2449 + " res=" + resultCode + " data=" + data);
2450 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2451 try {
2452 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2453 list.add(new ResultInfo(resultWho, requestCode,
2454 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002455 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002456 return;
2457 } catch (Exception e) {
2458 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2459 }
2460 }
2461
2462 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2463 }
2464
Craig Mautner04f0b702013-10-22 12:31:01 -07002465 private void adjustFocusedActivityLocked(ActivityRecord r) {
2466 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2467 ActivityRecord next = topRunningActivityLocked(null);
2468 if (next != r) {
2469 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002470 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2471 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002472 }
2473 }
Winson Chung648c83b2014-04-28 15:11:56 -07002474 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2475 if (top != null) {
2476 mService.setFocusedActivityLocked(top);
2477 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002478 }
2479 }
2480
Craig Mautnerf3333272013-04-22 10:55:53 -07002481 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002482 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2483 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2484 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2485 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002486 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002487 if (DEBUG_STATES) {
2488 Slog.d(TAG, "no-history finish of " + r);
2489 }
2490 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002491 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002492 } else {
2493 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2494 + " on stop because we're just sleeping");
2495 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002497 }
2498
2499 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002500 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002501 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002502 try {
2503 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002504 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2505 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 r.state = ActivityState.STOPPING;
2507 if (DEBUG_VISBILITY) Slog.v(
2508 TAG, "Stopping visible=" + r.visible + " for " + r);
2509 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002510 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002512 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002513 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002514 r.setSleeping(true);
2515 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002516 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002517 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002518 } catch (Exception e) {
2519 // Maybe just ignore exceptions here... if the process
2520 // has crashed, our death notification will clean things
2521 // up.
2522 Slog.w(TAG, "Exception thrown during pause", e);
2523 // Just in case, assume it to be stopped.
2524 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002525 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002526 r.state = ActivityState.STOPPED;
2527 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002528 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002529 }
2530 }
2531 }
2532 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002533
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 /**
2535 * @return Returns true if the activity is being finished, false if for
2536 * some reason it is being left as-is.
2537 */
2538 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002539 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002540 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002541 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002542 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002543 + ", result=" + resultCode + ", data=" + resultData
2544 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002545 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 return false;
2547 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002548
Craig Mautnerd44711d2013-02-23 11:24:36 -08002549 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 return true;
2551 }
2552
Craig Mautnerd2328952013-03-05 12:46:26 -08002553 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002554 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2555 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2556 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2557 ActivityRecord r = activities.get(activityNdx);
2558 if (r.resultTo == self && r.requestCode == requestCode) {
2559 if ((r.resultWho == null && resultWho == null) ||
2560 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2561 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2562 false);
2563 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002564 }
2565 }
2566 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002567 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002568 }
2569
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002570 final void finishTopRunningActivityLocked(ProcessRecord app) {
2571 ActivityRecord r = topRunningActivityLocked(null);
2572 if (r != null && r.app == app) {
2573 // If the top running activity is from this crashing
2574 // process, then terminate it to avoid getting in a loop.
2575 Slog.w(TAG, " Force finishing activity "
2576 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002577 int taskNdx = mTaskHistory.indexOf(r.task);
2578 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002579 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002580 // Also terminate any activities below it that aren't yet
2581 // stopped, to avoid a situation where one will get
2582 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002583 --activityNdx;
2584 if (activityNdx < 0) {
2585 do {
2586 --taskNdx;
2587 if (taskNdx < 0) {
2588 break;
2589 }
2590 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2591 } while (activityNdx < 0);
2592 }
2593 if (activityNdx >= 0) {
2594 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002595 if (r.state == ActivityState.RESUMED
2596 || r.state == ActivityState.PAUSING
2597 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002598 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002599 Slog.w(TAG, " Force finishing activity "
2600 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002601 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002602 }
2603 }
2604 }
2605 }
2606 }
2607
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002608 final void finishVoiceTask(IVoiceInteractionSession session) {
2609 IBinder sessionBinder = session.asBinder();
2610 boolean didOne = false;
2611 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2612 TaskRecord tr = mTaskHistory.get(taskNdx);
2613 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2614 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2615 ActivityRecord r = tr.mActivities.get(activityNdx);
2616 if (!r.finishing) {
2617 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2618 false);
2619 didOne = true;
2620 }
2621 }
2622 }
2623 }
2624 if (didOne) {
2625 mService.updateOomAdjLocked();
2626 }
2627 }
2628
Craig Mautnerd2328952013-03-05 12:46:26 -08002629 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002630 ArrayList<ActivityRecord> activities = r.task.mActivities;
2631 for (int index = activities.indexOf(r); index >= 0; --index) {
2632 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002633 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002634 break;
2635 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002636 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002637 }
2638 return true;
2639 }
2640
Dianne Hackborn5c607432012-02-28 14:44:19 -08002641 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2642 // send the result
2643 ActivityRecord resultTo = r.resultTo;
2644 if (resultTo != null) {
2645 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2646 + " who=" + r.resultWho + " req=" + r.requestCode
2647 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002648 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002649 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002650 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002651 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002652 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002653 if (r.info.applicationInfo.uid > 0) {
2654 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2655 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002656 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002657 }
2658 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2659 resultData);
2660 r.resultTo = null;
2661 }
2662 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2663
2664 // Make sure this HistoryRecord is not holding on to other resources,
2665 // because clients have remote IPC references to this object so we
2666 // can't assume that will go away and want to avoid circular IPC refs.
2667 r.results = null;
2668 r.pendingResults = null;
2669 r.newIntents = null;
2670 r.icicle = null;
2671 }
2672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 /**
2674 * @return Returns true if this activity has been removed from the history
2675 * list, or false if it is still in the list and will be removed later.
2676 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002677 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2678 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002679 if (r.finishing) {
2680 Slog.w(TAG, "Duplicate finish request for " + r);
2681 return false;
2682 }
2683
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002684 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002685 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002686 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002687 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002688 task.taskId, r.shortComponentName, reason);
2689 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002690 final int index = activities.indexOf(r);
2691 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002692 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002693 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002694 // If the caller asked that this activity (and all above it)
2695 // be cleared when the task is reset, don't lose that information,
2696 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002697 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002698 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 }
2700 }
2701
2702 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002703
2704 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002705
Dianne Hackborn5c607432012-02-28 14:44:19 -08002706 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002707
Craig Mautnerde4ef022013-04-07 19:01:33 -07002708 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002709 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002710 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002712 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002713 ? AppTransition.TRANSIT_TASK_CLOSE
2714 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002715
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002717 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002718
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002719 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002720 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2721 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002722 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 }
2724
Craig Mautneraea74a52014-03-08 14:23:10 -08002725 if (endTask) {
2726 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2727 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 } else if (r.state != ActivityState.PAUSING) {
2729 // If the activity is PAUSING, we will complete the finish once
2730 // it is done pausing; else we can just directly finish it here.
2731 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002732 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002733 } else {
2734 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2735 }
2736
2737 return false;
2738 }
2739
Craig Mautnerf3333272013-04-22 10:55:53 -07002740 static final int FINISH_IMMEDIATELY = 0;
2741 static final int FINISH_AFTER_PAUSE = 1;
2742 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743
Craig Mautnerf3333272013-04-22 10:55:53 -07002744 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 // First things first: if this activity is currently visible,
2746 // and the resumed activity is not yet visible, then hold off on
2747 // finishing until the resumed one becomes visible.
2748 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002749 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2750 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002751 if (mStackSupervisor.mStoppingActivities.size() > 3
2752 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002753 // If we already have a few activities waiting to stop,
2754 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002755 // them out. Or if r is the last of activity of the last task the stack
2756 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002757 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002758 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002759 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 }
2761 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002762 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2763 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002765 if (oomAdj) {
2766 mService.updateOomAdjLocked();
2767 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002768 return r;
2769 }
2770
2771 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002772 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002773 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002774 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002775 if (mResumedActivity == r) {
2776 mResumedActivity = null;
2777 }
2778 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002779 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002780 r.state = ActivityState.FINISHING;
2781
2782 if (mode == FINISH_IMMEDIATELY
2783 || prevState == ActivityState.STOPPED
2784 || prevState == ActivityState.INITIALIZING) {
2785 // If this activity is already stopped, we can just finish
2786 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002787 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002788 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002789 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002790 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002791 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002792 if (DEBUG_CONTAINERS) Slog.d(TAG,
2793 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2794 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002795 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002797
2798 // Need to go through the full pause cycle to get this
2799 // activity into the stopped state and then finish it.
2800 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002801 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002802 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002803 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002804 return r;
2805 }
2806
Craig Mautneree36c772014-07-16 14:56:05 -07002807 void finishAllActivitiesLocked(boolean immediately) {
2808 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002809 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2810 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2811 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2812 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002813 noActivitiesInStack = false;
2814 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002815 continue;
2816 }
Craig Mautneree36c772014-07-16 14:56:05 -07002817 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002818 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2819 }
2820 }
Craig Mautneree36c772014-07-16 14:56:05 -07002821 if (noActivitiesInStack) {
2822 mActivityContainer.onTaskListEmptyLocked();
2823 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002824 }
2825
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002826 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2827 // Basic case: for simple app-centric recents, we need to recreate
2828 // the task if the affinity has changed.
2829 if (srec == null || srec.task.affinity == null ||
2830 !srec.task.affinity.equals(destAffinity)) {
2831 return true;
2832 }
2833 // Document-centric case: an app may be split in to multiple documents;
2834 // they need to re-create their task if this current activity is the root
2835 // of a document, unless simply finishing it will return them to the the
2836 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002837 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2838 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002839 // Okay, this activity is at the root of its task. What to do, what to do...
2840 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2841 // Finishing won't return to an application, so we need to recreate.
2842 return true;
2843 }
2844 // We now need to get the task below it to determine what to do.
2845 int taskIdx = mTaskHistory.indexOf(srec.task);
2846 if (taskIdx <= 0) {
2847 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2848 return false;
2849 }
2850 if (taskIdx == 0) {
2851 // At the bottom of the stack, nothing to go back to.
2852 return true;
2853 }
2854 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2855 if (!srec.task.affinity.equals(prevTask.affinity)) {
2856 // These are different apps, so need to recreate.
2857 return true;
2858 }
2859 }
2860 return false;
2861 }
2862
Craig Mautnerd2328952013-03-05 12:46:26 -08002863 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002864 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002865 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002866 final TaskRecord task = srec.task;
2867 final ArrayList<ActivityRecord> activities = task.mActivities;
2868 final int start = activities.indexOf(srec);
2869 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002870 return false;
2871 }
2872 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002873 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002874 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002875 final ComponentName dest = destIntent.getComponent();
2876 if (start > 0 && dest != null) {
2877 for (int i = finishTo; i >= 0; i--) {
2878 ActivityRecord r = activities.get(i);
2879 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002880 r.info.name.equals(dest.getClassName())) {
2881 finishTo = i;
2882 parent = r;
2883 foundParentInTask = true;
2884 break;
2885 }
2886 }
2887 }
2888
2889 IActivityController controller = mService.mController;
2890 if (controller != null) {
2891 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2892 if (next != null) {
2893 // ask watcher if this is allowed
2894 boolean resumeOK = true;
2895 try {
2896 resumeOK = controller.activityResuming(next.packageName);
2897 } catch (RemoteException e) {
2898 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002899 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002900 }
2901
2902 if (!resumeOK) {
2903 return false;
2904 }
2905 }
2906 }
2907 final long origId = Binder.clearCallingIdentity();
2908 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002909 ActivityRecord r = activities.get(i);
2910 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002911 // Only return the supplied result for the first activity finished
2912 resultCode = Activity.RESULT_CANCELED;
2913 resultData = null;
2914 }
2915
2916 if (parent != null && foundParentInTask) {
2917 final int parentLaunchMode = parent.info.launchMode;
2918 final int destIntentFlags = destIntent.getFlags();
2919 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2920 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2921 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2922 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002923 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
2924 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002925 } else {
2926 try {
2927 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2928 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002929 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002930 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002931 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07002932 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002933 foundParentInTask = res == ActivityManager.START_SUCCESS;
2934 } catch (RemoteException e) {
2935 foundParentInTask = false;
2936 }
2937 requestFinishActivityLocked(parent.appToken, resultCode,
2938 resultData, "navigate-up", true);
2939 }
2940 }
2941 Binder.restoreCallingIdentity(origId);
2942 return foundParentInTask;
2943 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002944 /**
2945 * Perform the common clean-up of an activity record. This is called both
2946 * as part of destroyActivityLocked() (when destroying the client-side
2947 * representation) and cleaning things up as a result of its hosting
2948 * processing going away, in which case there is no remaining client-side
2949 * state to destroy so only the cleanup here is needed.
2950 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002951 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2952 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002953 if (mResumedActivity == r) {
2954 mResumedActivity = null;
2955 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002956 if (mPausingActivity == r) {
2957 mPausingActivity = null;
2958 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002959 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960
2961 r.configDestroy = false;
2962 r.frozenBeforeDestroy = false;
2963
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002964 if (setState) {
2965 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2966 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002967 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002968 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002969 }
2970
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971 // Make sure this record is no longer in the pending finishes list.
2972 // This could happen, for example, if we are trimming activities
2973 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002974 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002975 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002976
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977 // Remove any pending results.
2978 if (r.finishing && r.pendingResults != null) {
2979 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2980 PendingIntentRecord rec = apr.get();
2981 if (rec != null) {
2982 mService.cancelIntentSenderLocked(rec, false);
2983 }
2984 }
2985 r.pendingResults = null;
2986 }
2987
2988 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002989 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002990 }
2991
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002993 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07002994 if (getVisibleBehindActivity() == r) {
2995 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002996 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002997 }
2998
2999 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003000 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003001 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003002 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003003 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003004 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003005 }
3006
Craig Mautner04a0ea62014-01-13 12:51:26 -08003007 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003008 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003009 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3010 r.makeFinishing();
3011 if (DEBUG_ADD_REMOVE) {
3012 RuntimeException here = new RuntimeException("here");
3013 here.fillInStackTrace();
3014 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003016 r.takeFromHistory();
3017 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003018 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003019 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003020 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003021 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003022 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003023 if (VALIDATE_TOKENS) {
3024 validateAppTokensLocked();
3025 }
Craig Mautner312ba862014-02-10 17:55:01 -08003026 final TaskRecord task = r.task;
3027 if (task != null && task.removeActivity(r)) {
3028 if (DEBUG_STACK) Slog.i(TAG,
3029 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003030 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3031 task.isOverHomeStack()) {
3032 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08003033 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003034 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08003035 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003036 cleanUpActivityServicesLocked(r);
3037 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003039
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 /**
3041 * Perform clean-up of service connections in an activity record.
3042 */
3043 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3044 // Throw away any services that have been bound by this activity.
3045 if (r.connections != null) {
3046 Iterator<ConnectionRecord> it = r.connections.iterator();
3047 while (it.hasNext()) {
3048 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003049 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 }
3051 r.connections = null;
3052 }
3053 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003054
Craig Mautneree2e45a2014-06-27 12:10:03 -07003055 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003056 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003057 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003058 mHandler.sendMessage(msg);
3059 }
3060
Craig Mautneree2e45a2014-06-27 12:10:03 -07003061 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003062 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003063 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003064 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3065 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3066 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3067 final ActivityRecord r = activities.get(activityNdx);
3068 if (r.finishing) {
3069 continue;
3070 }
3071 if (r.fullscreen) {
3072 lastIsOpaque = true;
3073 }
3074 if (owner != null && r.app != owner) {
3075 continue;
3076 }
3077 if (!lastIsOpaque) {
3078 continue;
3079 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003080 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003081 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3082 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003083 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003084 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003085 activityRemoved = true;
3086 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003087 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003088 }
3089 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003090 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003091 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003092 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003093 }
3094
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003095 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3096 if (r.isDestroyable()) {
3097 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3098 + " resumed=" + mResumedActivity
3099 + " pausing=" + mPausingActivity + " for reason " + reason);
3100 return destroyActivityLocked(r, true, reason);
3101 }
3102 return false;
3103 }
3104
3105 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3106 String reason) {
3107 // Iterate over tasks starting at the back (oldest) first.
3108 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3109 int maxTasks = tasks.size() / 4;
3110 if (maxTasks < 1) {
3111 maxTasks = 1;
3112 }
3113 int numReleased = 0;
3114 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3115 final TaskRecord task = mTaskHistory.get(taskNdx);
3116 if (!tasks.contains(task)) {
3117 continue;
3118 }
3119 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3120 int curNum = 0;
3121 final ArrayList<ActivityRecord> activities = task.mActivities;
3122 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3123 final ActivityRecord activity = activities.get(actNdx);
3124 if (activity.app == app && activity.isDestroyable()) {
3125 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3126 + " in state " + activity.state + " resumed=" + mResumedActivity
3127 + " pausing=" + mPausingActivity + " for reason " + reason);
3128 destroyActivityLocked(activity, true, reason);
3129 if (activities.get(actNdx) != activity) {
3130 // Was removed from list, back up so we don't miss the next one.
3131 actNdx--;
3132 }
3133 curNum++;
3134 }
3135 }
3136 if (curNum > 0) {
3137 numReleased += curNum;
3138 maxTasks--;
3139 if (mTaskHistory.get(taskNdx) != task) {
3140 // The entire task got removed, back up so we don't miss the next one.
3141 taskNdx--;
3142 }
3143 }
3144 }
3145 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3146 return numReleased;
3147 }
3148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 /**
3150 * Destroy the current CLIENT SIDE instance of an activity. This may be
3151 * called both when actually finishing an activity, or when performing
3152 * a configuration switch where we destroy the current client-side object
3153 * but then create a new client-side object for this same HistoryRecord.
3154 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003155 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003156 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003157 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3159 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003160 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003161 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162
3163 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003164
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003165 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166
3167 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003168
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 if (hadApp) {
3170 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003171 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3173 mService.mHeavyWeightProcess = null;
3174 mService.mHandler.sendEmptyMessage(
3175 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3176 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003177 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003178 // Update any services we are bound to that might care about whether
3179 // their client may have activities.
3180 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003181 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003182 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003183 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 }
3185 }
3186
3187 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003188
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 try {
3190 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003191 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003192 r.configChangeFlags);
3193 } catch (Exception e) {
3194 // We can just ignore exceptions here... if the process
3195 // has crashed, our death notification will clean things
3196 // up.
3197 //Slog.w(TAG, "Exception thrown during finish", e);
3198 if (r.finishing) {
3199 removeActivityFromHistoryLocked(r);
3200 removedFromHistory = true;
3201 skipDestroy = true;
3202 }
3203 }
3204
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003206
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003207 // If the activity is finishing, we need to wait on removing it
3208 // from the list to give it a chance to do its cleanup. During
3209 // that time it may make calls back with its token so we need to
3210 // be able to find it on the list and so we don't want to remove
3211 // it from the list yet. Otherwise, we can just immediately put
3212 // it in the destroyed state since we are not removing it from the
3213 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003214 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003215 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3216 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003218 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003219 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3220 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003221 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003223 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003224 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 }
3226 } else {
3227 // remove this record from the history.
3228 if (r.finishing) {
3229 removeActivityFromHistoryLocked(r);
3230 removedFromHistory = true;
3231 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003232 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003234 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003235 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 }
3237 }
3238
3239 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003240
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003241 if (!mLRUActivities.remove(r) && hadApp) {
3242 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3243 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003244
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 return removedFromHistory;
3246 }
3247
Craig Mautnerd2328952013-03-05 12:46:26 -08003248 final void activityDestroyedLocked(IBinder token) {
3249 final long origId = Binder.clearCallingIdentity();
3250 try {
3251 ActivityRecord r = ActivityRecord.forToken(token);
3252 if (r != null) {
3253 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003254 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003255 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003256
3257 if (isInStackLocked(token) != null) {
3258 if (r.state == ActivityState.DESTROYING) {
3259 cleanUpActivityLocked(r, true, false);
3260 removeActivityFromHistoryLocked(r);
3261 }
3262 }
Craig Mautner05d29032013-05-03 13:40:13 -07003263 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003264 } finally {
3265 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 }
3267 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003268
Jose Lima4b6c6692014-08-12 17:41:12 -07003269 void releaseBackgroundResources() {
3270 if (hasVisibleBehindActivity() &&
3271 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3272 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautner64ccb702014-10-01 09:38:40 -07003273 if (r == topRunningActivityLocked(null)) {
3274 // Don't release the top activity if it has requested to run behind the next
3275 // activity.
3276 return;
3277 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003278 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3279 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003280 " thread=" + r.app.thread);
3281 if (r != null && r.app != null && r.app.thread != null) {
3282 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003283 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003284 } catch (RemoteException e) {
3285 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003286 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003287 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003288 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003289 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003290 }
3291 }
3292 }
3293
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003294 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003295 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3296 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003297 if (r != null) {
3298 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003299 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003300 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003301 }
3302 mStackSupervisor.resumeTopActivitiesLocked();
3303 }
3304
Jose Lima4b6c6692014-08-12 17:41:12 -07003305 boolean hasVisibleBehindActivity() {
3306 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003307 }
3308
Jose Lima4b6c6692014-08-12 17:41:12 -07003309 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003310 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003311 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003312 }
3313 }
3314
Jose Lima4b6c6692014-08-12 17:41:12 -07003315 ActivityRecord getVisibleBehindActivity() {
3316 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003317 }
3318
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003319 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3320 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003322 if (DEBUG_CLEANUP) Slog.v(
3323 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 + " with " + i + " entries");
3325 while (i > 0) {
3326 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003327 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003328 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003329 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003330 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003332 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 }
3334 }
3335 }
3336
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003337 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3338 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003339 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3340 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003341 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3342 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003343 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003344 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003345 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3346 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003347
3348 boolean hasVisibleActivities = false;
3349
3350 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003351 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003352 if (DEBUG_CLEANUP) Slog.v(
3353 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003354 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3355 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3356 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3357 final ActivityRecord r = activities.get(activityNdx);
3358 --i;
3359 if (DEBUG_CLEANUP) Slog.v(
3360 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3361 if (r.app == app) {
3362 boolean remove;
3363 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3364 // Don't currently have state for the activity, or
3365 // it is finishing -- always remove it.
3366 remove = true;
3367 } else if (r.launchCount > 2 &&
3368 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3369 // We have launched this activity too many times since it was
3370 // able to run, so give up and remove it.
3371 remove = true;
3372 } else {
3373 // The process may be gone, but the activity lives on!
3374 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003375 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003376 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003377 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003378 RuntimeException here = new RuntimeException("here");
3379 here.fillInStackTrace();
3380 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3381 + ": haveState=" + r.haveState
3382 + " stateNotNeeded=" + r.stateNotNeeded
3383 + " finishing=" + r.finishing
3384 + " state=" + r.state, here);
3385 }
3386 if (!r.finishing) {
3387 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3388 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3389 r.userId, System.identityHashCode(r),
3390 r.task.taskId, r.shortComponentName,
3391 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003392 if (r.state == ActivityState.RESUMED) {
3393 mService.updateUsageStats(r, false);
3394 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003395 }
3396 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003397
Craig Mautner0247fc82013-02-28 14:32:06 -08003398 } else {
3399 // We have the current state for this activity, so
3400 // it can be restarted later when needed.
3401 if (localLOGV) Slog.v(
3402 TAG, "Keeping entry, setting app to null");
3403 if (r.visible) {
3404 hasVisibleActivities = true;
3405 }
3406 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3407 + r);
3408 r.app = null;
3409 r.nowVisible = false;
3410 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003411 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003412 "App died, clearing saved state of " + r);
3413 r.icicle = null;
3414 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003415 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003416
Craig Mautnerd2328952013-03-05 12:46:26 -08003417 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003418 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003419 }
3420 }
3421
3422 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003423 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003424
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003425 final void updateTransitLocked(int transit, Bundle options) {
3426 if (options != null) {
3427 ActivityRecord r = topRunningActivityLocked(null);
3428 if (r != null && r.state != ActivityState.RESUMED) {
3429 r.updateOptionsLocked(options);
3430 } else {
3431 ActivityOptions.abort(options);
3432 }
3433 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003434 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003435 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003436
Craig Mautner21d24a22014-04-23 11:45:37 -07003437 void updateTaskMovement(TaskRecord task, boolean toFront) {
3438 if (task.isPersistable) {
3439 task.mLastTimeMoved = System.currentTimeMillis();
3440 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3441 // recently will be most negative, tasks sent to the bottom before that will be less
3442 // negative. Similarly for recent tasks moved to the top which will be most positive.
3443 if (!toFront) {
3444 task.mLastTimeMoved *= -1;
3445 }
3446 }
3447 }
3448
Craig Mautner84984fa2014-06-19 11:19:20 -07003449 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003450 final int top = mTaskHistory.size() - 1;
3451 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3452 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003453 if (task.taskType == homeStackTaskType) {
3454 if (DEBUG_TASKS || DEBUG_STACK)
3455 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003456 mTaskHistory.remove(taskNdx);
3457 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003458 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003459 mWindowManager.moveTaskToTop(task.taskId);
3460 return;
3461 }
3462 }
3463 }
3464
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003465 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003466 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003467
Craig Mautner11bf9a52013-02-19 14:08:51 -08003468 final int numTasks = mTaskHistory.size();
3469 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003470 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003471 // nothing to do!
3472 if (reason != null &&
3473 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3474 ActivityOptions.abort(options);
3475 } else {
3476 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3477 }
3478 return;
3479 }
3480
Craig Mautnere0a38842013-12-16 16:14:02 -08003481 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003482
Craig Mautner11bf9a52013-02-19 14:08:51 -08003483 // Shift all activities with this task up to the top
3484 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003485 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003486
3487 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003488 if (reason != null &&
3489 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003490 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003491 ActivityRecord r = topRunningActivityLocked(null);
3492 if (r != null) {
3493 mNoAnimActivities.add(r);
3494 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003495 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003496 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003497 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003499
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003500 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003501
Craig Mautner05d29032013-05-03 13:40:13 -07003502 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003503 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003504
3505 if (VALIDATE_TOKENS) {
3506 validateAppTokensLocked();
3507 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 }
3509
3510 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003511 * Worker method for rearranging history stack. Implements the function of moving all
3512 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003513 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003514 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003515 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3516 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003517 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003518 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003519 * @return Returns true if the move completed, false if not.
3520 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003521 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003522 final TaskRecord tr = taskForIdLocked(taskId);
3523 if (tr == null) {
3524 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3525 return false;
3526 }
3527
3528 Slog.i(TAG, "moveTaskToBack: " + tr);
3529
3530 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003531
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003532 // If we have a watcher, preflight the move before committing to it. First check
3533 // for *other* available tasks, but if none are available, then try again allowing the
3534 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003535 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003536 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003537 if (next == null) {
3538 next = topRunningActivityLocked(null, 0);
3539 }
3540 if (next != null) {
3541 // ask watcher if this is allowed
3542 boolean moveOK = true;
3543 try {
3544 moveOK = mService.mController.activityResuming(next.packageName);
3545 } catch (RemoteException e) {
3546 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003547 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003548 }
3549 if (!moveOK) {
3550 return false;
3551 }
3552 }
3553 }
3554
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003555 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003556 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557
Craig Mautner11bf9a52013-02-19 14:08:51 -08003558 mTaskHistory.remove(tr);
3559 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003560 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561
Craig Mautnerc8143c62013-09-03 12:15:57 -07003562 // There is an assumption that moving a task to the back moves it behind the home activity.
3563 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003564 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003565 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3566 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003567 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003568 break;
3569 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003570 if (taskNdx == 1) {
3571 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003572 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003573 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003574 }
3575
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003577 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3578 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003579 ActivityRecord r = topRunningActivityLocked(null);
3580 if (r != null) {
3581 mNoAnimActivities.add(r);
3582 }
3583 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003584 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003586 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003589 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003590 }
3591
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003592 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003593 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003594 if (!mService.mBooting && !mService.mBooted) {
3595 // Not ready yet!
3596 return false;
3597 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003598 final int taskToReturnTo = tr.getTaskToReturnTo();
3599 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3600 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003601 }
3602
Craig Mautner05d29032013-05-03 13:40:13 -07003603 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003604 return true;
3605 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003606
Craig Mautner8849a5e2013-04-02 16:41:03 -07003607 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003609 final Uri data = r.intent.getData();
3610 final String strData = data != null ? data.toSafeString() : null;
3611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003612 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003613 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003615 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003616 }
3617
3618 /**
3619 * Make sure the given activity matches the current configuration. Returns
3620 * false if the activity had to be destroyed. Returns true if the
3621 * configuration is the same, or the activity will remain running as-is
3622 * for whatever reason. Ensures the HistoryRecord is updated with the
3623 * correct configuration and all other bookkeeping is handled.
3624 */
3625 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3626 int globalChanges) {
3627 if (mConfigWillChange) {
3628 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3629 "Skipping config check (will change): " + r);
3630 return true;
3631 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003632
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003633 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3634 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003635
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 // Short circuit: if the two configurations are the exact same
3637 // object (the common case), then there is nothing to do.
3638 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003639 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3641 "Configuration unchanged in " + r);
3642 return true;
3643 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003644
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003645 // We don't worry about activities that are finishing.
3646 if (r.finishing) {
3647 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3648 "Configuration doesn't matter in finishing " + r);
3649 r.stopFreezingScreenLocked(false);
3650 return true;
3651 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003652
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003653 // Okay we now are going to make this activity have the new config.
3654 // But then we need to figure out how it needs to deal with that.
3655 Configuration oldConfig = r.configuration;
3656 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003657
3658 // Determine what has changed. May be nothing, if this is a config
3659 // that has come back from the app after going idle. In that case
3660 // we just want to leave the official config object now in the
3661 // activity and do nothing else.
3662 final int changes = oldConfig.diff(newConfig);
3663 if (changes == 0 && !r.forceNewConfig) {
3664 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3665 "Configuration no differences in " + r);
3666 return true;
3667 }
3668
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003669 // If the activity isn't currently running, just leave the new
3670 // configuration and it will pick that up next time it starts.
3671 if (r.app == null || r.app.thread == null) {
3672 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3673 "Configuration doesn't matter not running " + r);
3674 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003675 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 return true;
3677 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003678
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003679 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003680 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3681 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3682 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003683 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003684 + ", newConfig=" + newConfig);
3685 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003686 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003687 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3688 r.configChangeFlags |= changes;
3689 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003690 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003691 if (r.app == null || r.app.thread == null) {
3692 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003693 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003694 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003695 } else if (r.state == ActivityState.PAUSING) {
3696 // A little annoying: we are waiting for this activity to
3697 // finish pausing. Let's not do anything now, but just
3698 // flag that it needs to be restarted when done pausing.
3699 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003700 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003701 r.configDestroy = true;
3702 return true;
3703 } else if (r.state == ActivityState.RESUMED) {
3704 // Try to optimize this case: the configuration is changing
3705 // and we need to restart the top, resumed activity.
3706 // Instead of doing the normal handshaking, just say
3707 // "restart!".
3708 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003709 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003710 relaunchActivityLocked(r, r.configChangeFlags, true);
3711 r.configChangeFlags = 0;
3712 } else {
3713 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003714 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003715 relaunchActivityLocked(r, r.configChangeFlags, false);
3716 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003717 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003718
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003719 // All done... tell the caller we weren't able to keep this
3720 // activity around.
3721 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003722 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003723
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003724 // Default case: the activity can handle this new configuration, so
3725 // hand it over. Note that we don't need to give it the new
3726 // configuration, since we always send configuration changes to all
3727 // process when they happen so it can just use whatever configuration
3728 // it last got.
3729 if (r.app != null && r.app.thread != null) {
3730 try {
3731 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003732 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 } catch (RemoteException e) {
3734 // If process died, whatever.
3735 }
3736 }
3737 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003738
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739 return true;
3740 }
3741
Craig Mautnerc8143c62013-09-03 12:15:57 -07003742 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003743 int changes, boolean andResume) {
3744 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003745 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003746 if (andResume) {
3747 results = r.results;
3748 newIntents = r.newIntents;
3749 }
3750 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3751 + " with results=" + results + " newIntents=" + newIntents
3752 + " andResume=" + andResume);
3753 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003754 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003756
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003757 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003758
Craig Mautner34b73df2014-01-12 21:11:08 -08003759 mStackSupervisor.removeChildActivityContainers(r);
3760
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003761 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003762 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3763 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3764 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003765 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003766 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003767 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003768 // Note: don't need to call pauseIfSleepingLocked() here, because
3769 // the caller will only pass in 'andResume' if this activity is
3770 // currently resumed, which implies we aren't sleeping.
3771 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003772 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 }
3774
3775 if (andResume) {
3776 r.results = null;
3777 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003778 r.state = ActivityState.RESUMED;
3779 } else {
3780 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3781 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003782 }
3783
3784 return true;
3785 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003786
3787 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003788 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3789 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3790 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3791 final ActivityRecord r = activities.get(activityNdx);
3792 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003793 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003794 }
3795 if (r.fullscreen && !r.finishing) {
3796 return false;
3797 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003798 }
3799 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003800 final ActivityRecord r = ActivityRecord.forToken(token);
3801 if (r == null) {
3802 return false;
3803 }
3804 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3805 + " would have returned true for r=" + r);
3806 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003807 }
3808
3809 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003810 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3811 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3812 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3813 final ActivityRecord r = activities.get(activityNdx);
3814 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003815 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003816 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003817 }
3818 }
3819 }
3820
3821 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3822 boolean didSomething = false;
3823 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003824 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003825 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3826 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3827 int numActivities = activities.size();
3828 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3829 ActivityRecord r = activities.get(activityNdx);
3830 final boolean samePackage = r.packageName.equals(name)
3831 || (name == null && r.userId == userId);
3832 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3833 && (samePackage || r.task == lastTask)
3834 && (r.app == null || evenPersistent || !r.app.persistent)) {
3835 if (!doit) {
3836 if (r.finishing) {
3837 // If this activity is just finishing, then it is not
3838 // interesting as far as something to stop.
3839 continue;
3840 }
3841 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003842 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003843 if (r.isHomeActivity()) {
3844 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3845 Slog.i(TAG, "Skip force-stop again " + r);
3846 continue;
3847 } else {
3848 homeActivity = r.realActivity;
3849 }
3850 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003851 didSomething = true;
3852 Slog.i(TAG, " Force finishing activity " + r);
3853 if (samePackage) {
3854 if (r.app != null) {
3855 r.app.removed = true;
3856 }
3857 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003858 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003859 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003860 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3861 true)) {
3862 // r has been deleted from mActivities, accommodate.
3863 --numActivities;
3864 --activityNdx;
3865 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003866 }
3867 }
3868 }
3869 return didSomething;
3870 }
3871
Dianne Hackborn09233282014-04-30 11:33:59 -07003872 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003873 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003874 final TaskRecord task = mTaskHistory.get(taskNdx);
3875 ActivityRecord r = null;
3876 ActivityRecord top = null;
3877 int numActivities = 0;
3878 int numRunning = 0;
3879 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003880 if (activities.isEmpty()) {
3881 continue;
3882 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07003883 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07003884 continue;
3885 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003886 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3887 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003888
Craig Mautneraab647e2013-02-28 16:31:36 -08003889 // Initialize state for next task if needed.
3890 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3891 top = r;
3892 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003893 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003894
3895 // Add 'r' into the current task.
3896 numActivities++;
3897 if (r.app != null && r.app.thread != null) {
3898 numRunning++;
3899 }
3900
3901 if (localLOGV) Slog.v(
3902 TAG, r.intent.getComponent().flattenToShortString()
3903 + ": task=" + r.task);
3904 }
3905
3906 RunningTaskInfo ci = new RunningTaskInfo();
3907 ci.id = task.taskId;
3908 ci.baseActivity = r.intent.getComponent();
3909 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003910 ci.lastActiveTime = task.lastActiveTime;
3911
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003912 if (top.task != null) {
3913 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003914 }
3915 ci.numActivities = numActivities;
3916 ci.numRunning = numRunning;
3917 //System.out.println(
3918 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003919 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003920 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003921 }
3922
3923 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003924 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003925 if (DEBUG_SWITCH) Slog.d(
3926 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003927 if (top >= 0) {
3928 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3929 int activityTop = activities.size() - 1;
3930 if (activityTop > 0) {
3931 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3932 "unhandled-back", true);
3933 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003934 }
3935 }
3936
Craig Mautner6b74cb52013-09-27 17:02:21 -07003937 /**
3938 * Reset local parameters because an app's activity died.
3939 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003940 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003941 */
3942 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003943 if (mPausingActivity != null && mPausingActivity.app == app) {
3944 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3945 "App died while pausing: " + mPausingActivity);
3946 mPausingActivity = null;
3947 }
3948 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3949 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003950 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003951 }
3952
Craig Mautner19091252013-10-05 00:03:53 -07003953 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003954 }
3955
Craig Mautnercae015f2013-02-08 14:31:27 -08003956 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003957 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3958 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3959 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3960 final ActivityRecord r = activities.get(activityNdx);
3961 if (r.app == app) {
3962 Slog.w(TAG, " Force finishing activity "
3963 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07003964 // Force the destroy to skip right to removal.
3965 r.app = null;
3966 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003967 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003968 }
3969 }
3970 }
3971
Dianne Hackborn390517b2013-05-30 15:03:32 -07003972 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003973 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003974 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003975 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3976 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003977 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3978 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003979 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003980 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003981 if (printed) {
3982 header = null;
3983 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003984 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003985 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003986 }
3987
3988 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3989 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3990
3991 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003992 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3993 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003994 }
3995 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003996 final int top = mTaskHistory.size() - 1;
3997 if (top >= 0) {
3998 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3999 int listTop = list.size() - 1;
4000 if (listTop >= 0) {
4001 activities.add(list.get(listTop));
4002 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004003 }
4004 } else {
4005 ItemMatcher matcher = new ItemMatcher();
4006 matcher.build(name);
4007
Craig Mautneraab647e2013-02-28 16:31:36 -08004008 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4009 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4010 if (matcher.match(r1, r1.intent.getComponent())) {
4011 activities.add(r1);
4012 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004013 }
4014 }
4015 }
4016
4017 return activities;
4018 }
4019
4020 ActivityRecord restartPackage(String packageName) {
4021 ActivityRecord starting = topRunningActivityLocked(null);
4022
4023 // All activities that came from the package must be
4024 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004025 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4026 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4027 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4028 final ActivityRecord a = activities.get(activityNdx);
4029 if (a.info.packageName.equals(packageName)) {
4030 a.forceNewConfig = true;
4031 if (starting != null && a == starting && a.visible) {
4032 a.startFreezingScreenLocked(starting.app,
4033 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4034 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004035 }
4036 }
4037 }
4038
4039 return starting;
4040 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004041
Craig Mautner41db4a72014-05-07 17:20:56 -07004042 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004043 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08004044 mWindowManager.removeTask(task.taskId);
4045 final ActivityRecord r = mResumedActivity;
4046 if (r != null && r.task == task) {
4047 mResumedActivity = null;
4048 }
4049
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004050 final int taskNdx = mTaskHistory.indexOf(task);
4051 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004052 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4053 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4054 if (!nextTask.isOverHomeStack()) {
4055 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4056 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004057 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004058 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004059 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004060
4061 if (task.mActivities.isEmpty()) {
4062 final boolean isVoiceSession = task.voiceSession != null;
4063 if (isVoiceSession) {
4064 try {
4065 task.voiceSession.taskFinished(task.intent, task.taskId);
4066 } catch (RemoteException e) {
4067 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004068 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004069 if (task.autoRemoveFromRecents() || isVoiceSession) {
4070 // Task creator asked to remove this when done, or this task was a voice
4071 // interaction, so it should not remain on the recent tasks list.
4072 mService.mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004073 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004074 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004075 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004076
4077 if (mTaskHistory.isEmpty()) {
4078 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
4079 if (isOnHomeDisplay()) {
4080 mStackSupervisor.moveHomeStack(!isHomeStack());
4081 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004082 if (mStacks != null) {
4083 mStacks.remove(this);
4084 mStacks.add(0, this);
4085 }
Craig Mautnerd163e752014-06-13 17:18:47 -07004086 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004087 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004088 }
4089
Dianne Hackborn91097de2014-04-04 18:02:06 -07004090 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4091 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4092 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004093 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4094 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004095 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004096 return task;
4097 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004098
4099 ArrayList<TaskRecord> getAllTasks() {
4100 return new ArrayList<TaskRecord>(mTaskHistory);
4101 }
4102
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004103 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004104 task.stack = this;
4105 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004106 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004107 } else {
4108 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004109 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004110 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004111 if (!moving && task.voiceSession != null) {
4112 try {
4113 task.voiceSession.taskStarted(task.intent, task.taskId);
4114 } catch (RemoteException e) {
4115 }
4116 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004117 }
4118
4119 public int getStackId() {
4120 return mStackId;
4121 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004122
4123 @Override
4124 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004125 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4126 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004127 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004128}