blob: 4766742a5639a0f363729fe1ade23c269fdcf72d [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
Craig Mautner84984fa2014-06-19 11:19:20 -070033import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070035
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
37import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070041import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070042import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
43
Dianne Hackborn91097de2014-04-04 18:02:06 -070044import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070045import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070046import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080047import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautneree2e45a2014-06-27 12:10:03 -070049import com.android.server.am.ActivityStackSupervisor.ActivityDisplay;
Craig Mautner4b71aa12012-12-27 17:20:01 -080050import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070052import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053
54import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070055import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070056import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080058import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080060import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080066import android.content.res.Resources;
67import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080068import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070070import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080071import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.Handler;
73import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090074import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070076import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.os.RemoteException;
78import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080079import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070080import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070081import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070084import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
Craig Mautnercae015f2013-02-08 14:31:27 -080086import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088import java.lang.ref.WeakReference;
89import java.util.ArrayList;
90import java.util.Iterator;
91import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080092import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093
94/**
95 * State and management of a single stack of activities.
96 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070097final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080098
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070099 // Ticks during which we check progress while waiting for an app to launch.
100 static final int LAUNCH_TICK = 500;
101
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700102 // How long we wait until giving up on the last activity to pause. This
103 // is short because it directly impacts the responsiveness of starting the
104 // next activity.
105 static final int PAUSE_TIMEOUT = 500;
106
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700107 // How long we wait for the activity to tell us it has stopped before
108 // giving up. This is a good amount of time because we really need this
109 // from the application in order to get its saved state.
110 static final int STOP_TIMEOUT = 10*1000;
111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112 // How long we wait until giving up on an activity telling us it has
113 // finished destroying itself.
114 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800117 // disabled.
118 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700120 // How long between activity launches that we consider safe to not warn
121 // the user about an unexpected activity being launched on top.
122 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124 // Set to false to disable the preview that is shown while a new activity
125 // is being started.
126 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800127
Craig Mautner5eda9b32013-07-02 11:58:16 -0700128 // How long to wait for all background Activities to redraw following a call to
129 // convertToTranslucent().
130 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
131
Craig Mautnera61bc652013-10-28 15:43:18 -0700132 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700133
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134 enum ActivityState {
135 INITIALIZING,
136 RESUMED,
137 PAUSING,
138 PAUSED,
139 STOPPING,
140 STOPPED,
141 FINISHING,
142 DESTROYING,
143 DESTROYED
144 }
145
146 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700147 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 /**
150 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151 * running) activities. It contains #TaskRecord objects.
152 */
153 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
154
155 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156 * Used for validating app tokens with window manager.
157 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800158 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 /**
161 * List of running activities, sorted by recent usage.
162 * The first entry in the list is the least recently used.
163 * It contains HistoryRecord objects.
164 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700165 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168 * Animations that for the current transition have requested not to
169 * be considered for the transition animation.
170 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172
173 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 * When we are in the process of pausing an activity, before starting the
175 * next one, this variable holds the activity that is currently being paused.
176 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800177 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700178
179 /**
180 * This is the last activity that we put into the paused state. This is
181 * used to determine if we need to do an activity transition while sleeping,
182 * when we normally hold the top activity paused.
183 */
184 ActivityRecord mLastPausedActivity = null;
185
186 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700187 * Activities that specify No History must be removed once the user navigates away from them.
188 * If the device goes to sleep with such an activity in the paused state then we save it here
189 * and finish it later if another activity replaces it on wakeup.
190 */
191 ActivityRecord mLastNoHistoryActivity = null;
192
193 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 * Current activity that is resumed, or null if there is none.
195 */
196 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700199 * This is the last activity that has been started. It is only used to
200 * identify when multiple activities are started at once so that the user
201 * can be warned they may not be in the activity they think they are.
202 */
203 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800204
Craig Mautner5eda9b32013-07-02 11:58:16 -0700205 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
206 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
207 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
208 // Activity in mTranslucentActivityWaiting is notified via
209 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
210 // background activity being drawn then the same call will be made with a true value.
211 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700212 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700213 new ArrayList<ActivityRecord>();
214
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700215 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700216 * Set when we know we are going to be calling updateConfiguration()
217 * soon, so want to skip intermediate config checks.
218 */
219 boolean mConfigWillChange;
220
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700221 long mLaunchStartTime = 0;
222 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800223
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800224 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800225 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
226 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
227 */
228 private ActivityRecord mLastScreenshotActivity = null;
229 private Bitmap mLastScreenshotBitmap = null;
230
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800231 int mThumbnailWidth = -1;
232 int mThumbnailHeight = -1;
233
Craig Mautner858d8a62013-04-23 17:08:34 -0700234 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700235
Craig Mautnerc00204b2013-03-05 15:02:14 -0800236 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800237 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800238 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
239 ArrayList<ActivityStack> mStacks;
240 /** The attached Display's unique identifier, or -1 if detached */
241 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800242
Craig Mautner27084302013-03-25 08:05:25 -0700243 /** Run all ActivityStacks through this */
244 final ActivityStackSupervisor mStackSupervisor;
245
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700246 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700247 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
248 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
249 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
250 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700251 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700252 static final int STOP_MEDIA_PLAYING_TIMEOUT_MSG =
253 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700254
255 static class ScheduleDestroyArgs {
256 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700258 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700259 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 mReason = reason;
261 }
262 }
263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 final Handler mHandler;
265
266 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267 //public Handler() {
268 // if (localLOGV) Slog.v(TAG, "Handler started!");
269 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700270 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900271 super(looper);
272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 public void handleMessage(Message msg) {
276 switch (msg.what) {
277 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800278 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700279 // We don't at this point know if the activity is fullscreen,
280 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800281 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700282 synchronized (mService) {
283 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700284 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 }
Craig Mautnera0026042014-04-23 11:45:37 -0700286 activityPausedLocked(r.appToken, true, r.persistentState);
Craig Mautnerd2328952013-03-05 12:46:26 -0800287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700289 case LAUNCH_TICK_MSG: {
290 ActivityRecord r = (ActivityRecord)msg.obj;
291 synchronized (mService) {
292 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700293 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 }
295 }
296 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800298 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700299 // We don't at this point know if the activity is fullscreen,
300 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800301 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800302 synchronized (mService) {
303 activityDestroyedLocked(r != null ? r.appToken : null);
304 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700306 case STOP_TIMEOUT_MSG: {
307 ActivityRecord r = (ActivityRecord)msg.obj;
308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
310 Slog.w(TAG, "Activity stop timeout for " + r);
311 synchronized (mService) {
312 if (r.isInHistory()) {
313 activityStoppedLocked(r, null, null, null);
314 }
315 }
316 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700317 case DESTROY_ACTIVITIES_MSG: {
318 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
319 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700320 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700321 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700322 } break;
323 case TRANSLUCENT_TIMEOUT_MSG: {
324 synchronized (mService) {
325 notifyActivityDrawnLocked(null);
326 }
327 } break;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700328 case STOP_MEDIA_PLAYING_TIMEOUT_MSG: {
329 synchronized (mService) {
330 final ActivityRecord r = getMediaPlayer();
331 Slog.e(TAG, "Timeout waiting for stopMediaPlaying player=" + r);
332 if (r != null) {
333 mService.killAppAtUsersRequest(r.app, null);
334 }
335 }
336 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700337 }
338 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800339 }
340
Craig Mautner34b73df2014-01-12 21:11:08 -0800341 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800342 int count = 0;
343 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
344 count += mTaskHistory.get(taskNdx).mActivities.size();
345 }
346 return count;
347 }
348
Craig Mautner4a1cb222013-12-04 16:14:06 -0800349 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
350 mActivityContainer = activityContainer;
351 mStackSupervisor = activityContainer.getOuter();
352 mService = mStackSupervisor.mService;
353 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
354 mWindowManager = mService.mWindowManager;
355 mStackId = activityContainer.mStackId;
356 mCurrentUser = mService.mCurrentUserId;
Winson Chung376543b2014-05-21 17:43:28 -0700357 // Get the activity screenshot thumbnail dimensions
358 Resources res = mService.mContext.getResources();
Winson Chung2eb4f9c2014-05-22 11:22:41 -0700359 mThumbnailWidth = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
360 mThumbnailHeight = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700361 }
Craig Mautner5962b122012-10-05 14:45:52 -0700362
Amith Yamasani734983f2014-03-04 16:48:05 -0800363 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100364 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800365 */
Kenny Guy2a764942014-04-02 13:29:20 +0100366 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100367 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100368 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
369 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800370 }
371 return false;
372 }
373
374 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100375 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700376 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
377 }
378
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700379 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800380 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700381 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
382 if (r != null) {
383 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800384 }
385 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386 return null;
387 }
388
389 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800390 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
391 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800392 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800393 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
394 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800395 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800396 return r;
397 }
398 }
399 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 return null;
401 }
402
403 /**
404 * This is a simplified version of topRunningActivityLocked that provides a number of
405 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800406 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700407 * @param token If non-null, any history records matching this token will be skipped.
408 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800409 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700410 * @return Returns the HistoryRecord of the next activity on the stack.
411 */
412 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
414 TaskRecord task = mTaskHistory.get(taskNdx);
415 if (task.taskId == taskId) {
416 continue;
417 }
418 ArrayList<ActivityRecord> activities = task.mActivities;
419 for (int i = activities.size() - 1; i >= 0; --i) {
420 final ActivityRecord r = activities.get(i);
421 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800422 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800423 return r;
424 }
425 }
426 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700427 return null;
428 }
429
Craig Mautner8849a5e2013-04-02 16:41:03 -0700430 final ActivityRecord topActivity() {
431 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700432 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700433 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
434 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700435 final int topActivityNdx = activities.size() - 1;
436 if (topActivityNdx >= 0) {
437 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700438 }
439 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700440 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700441 }
442
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700443 final TaskRecord topTask() {
444 final int size = mTaskHistory.size();
445 if (size > 0) {
446 return mTaskHistory.get(size - 1);
447 }
448 return null;
449 }
450
Craig Mautnerd2328952013-03-05 12:46:26 -0800451 TaskRecord taskForIdLocked(int id) {
452 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
453 final TaskRecord task = mTaskHistory.get(taskNdx);
454 if (task.taskId == id) {
455 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800456 }
457 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700458 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700459 }
460
Craig Mautnerd2328952013-03-05 12:46:26 -0800461 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800462 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800463 if (r != null) {
464 final TaskRecord task = r.task;
465 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
466 if (task.stack != this) Slog.w(TAG,
467 "Illegal state! task does not point to stack it is in.");
468 return r;
469 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800470 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800471 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800472 }
473
Craig Mautner2420ead2013-04-01 17:13:20 -0700474 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700475 final boolean hadit = mLRUActivities.remove(r);
476 mLRUActivities.add(r);
477 return hadit;
478 }
479
Craig Mautnerde4ef022013-04-07 19:01:33 -0700480 final boolean isHomeStack() {
481 return mStackId == HOME_STACK_ID;
482 }
483
Craig Mautnere0a38842013-12-16 16:14:02 -0800484 final boolean isOnHomeDisplay() {
485 return isAttached() &&
486 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
487 }
488
489 final void moveToFront() {
490 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 if (isOnHomeDisplay()) {
492 mStackSupervisor.moveHomeStack(isHomeStack());
493 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800494 mStacks.remove(this);
495 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800496 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800497 }
498
499 final boolean isAttached() {
500 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800501 }
502
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700503 /**
504 * Returns the top activity in any existing task matching the given
505 * Intent. Returns null if no such task is found.
506 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700507 ActivityRecord findTaskLocked(ActivityRecord target) {
508 Intent intent = target.intent;
509 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700510 ComponentName cls = intent.getComponent();
511 if (info.targetActivity != null) {
512 cls = new ComponentName(info.packageName, info.targetActivity);
513 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700514 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700515 boolean isDocument = intent != null & intent.isDocument();
516 // If documentData is non-null then it must match the existing task data.
517 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800518
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700519 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800520 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
521 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700522 if (task.voiceSession != null) {
523 // We never match voice sessions; those always run independently.
524 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
525 continue;
526 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700527 if (task.userId != userId) {
528 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700529 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700530 continue;
531 }
Craig Mautner000f0022013-02-26 15:04:29 -0800532 final ActivityRecord r = task.getTopActivity();
533 if (r == null || r.finishing || r.userId != userId ||
534 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700535 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800536 continue;
537 }
538
Craig Mautnerd00f4742014-03-12 14:17:26 -0700539 final Intent taskIntent = task.intent;
540 final Intent affinityIntent = task.affinityIntent;
541 final boolean taskIsDocument;
542 final Uri taskDocumentData;
543 if (taskIntent != null && taskIntent.isDocument()) {
544 taskIsDocument = true;
545 taskDocumentData = taskIntent.getData();
546 } else if (affinityIntent != null && affinityIntent.isDocument()) {
547 taskIsDocument = true;
548 taskDocumentData = affinityIntent.getData();
549 } else {
550 taskIsDocument = false;
551 taskDocumentData = null;
552 }
553
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700554 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700555 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700556 + "/aff=" + r.task.affinity + " to new cls="
557 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700558 if (!isDocument && !taskIsDocument && task.affinity != null) {
559 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700560 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700561 return r;
562 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700563 } else if (taskIntent != null && taskIntent.getComponent() != null &&
564 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700565 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700566 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800567 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700568 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
569 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800570 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700571 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
572 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700573 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700574 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800575 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700576 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
577 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800578 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700579 } else if (DEBUG_TASKS) {
580 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700581 }
582 }
583
584 return null;
585 }
586
587 /**
588 * Returns the first activity (starting from the top of the stack) that
589 * is the same as the given activity. Returns null if no such activity
590 * is found.
591 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700592 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700593 ComponentName cls = intent.getComponent();
594 if (info.targetActivity != null) {
595 cls = new ComponentName(info.packageName, info.targetActivity);
596 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700597 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700598
Craig Mautner000f0022013-02-26 15:04:29 -0800599 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700600 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100601 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700602 return null;
603 }
604 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800605 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
606 ActivityRecord r = activities.get(activityNdx);
607 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700608 //Slog.i(TAG, "Found matching class!");
609 //dump();
610 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
611 return r;
612 }
613 }
614 }
615
616 return null;
617 }
618
Amith Yamasani742a6712011-05-04 14:49:28 -0700619 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700620 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700621 */
Craig Mautner93529a42013-10-04 15:03:13 -0700622 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800623 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700624 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800625 }
626 mCurrentUser = userId;
627
628 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800629 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800630 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700631 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100632 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700633 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
634 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800635 mTaskHistory.remove(i);
636 mTaskHistory.add(task);
637 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800638 // Use same value for i.
639 } else {
640 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800641 }
642 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700643 if (VALIDATE_TOKENS) {
644 validateAppTokensLocked();
645 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700646 }
647
Craig Mautner2420ead2013-04-01 17:13:20 -0700648 void minimalResumeActivityLocked(ActivityRecord r) {
649 r.state = ActivityState.RESUMED;
650 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
651 + " (starting new instance)");
652 r.stopped = false;
653 mResumedActivity = r;
654 r.task.touchActiveTime();
655 mService.addRecentTaskLocked(r.task);
656 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700657 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700658 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700659 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700660 }
661
Dianne Hackborncee04b52013-07-03 17:01:28 -0700662 private void startLaunchTraces() {
663 if (mFullyDrawnStartTime != 0) {
664 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
665 }
666 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
667 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
668 }
669
670 private void stopFullyDrawnTraceIfNeeded() {
671 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
672 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
673 mFullyDrawnStartTime = 0;
674 }
675 }
676
Craig Mautnere79d42682013-04-01 19:01:53 -0700677 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700678 if (r.displayStartTime == 0) {
679 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
680 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700681 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700682 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700683 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700684 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700685 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700686 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700687 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700688 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800689
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700690 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700691 // Make sure that there is no activity waiting for this to launch.
692 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
693 r.displayStartTime = r.fullyDrawnStartTime = 0;
694 } else {
695 mStackSupervisor.removeTimeoutsForActivityLocked(r);
696 mStackSupervisor.scheduleIdleTimeoutLocked(r);
697 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700698 }
699
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800700 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800701 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800702 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
703 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
704 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
705 activities.get(activityNdx).setSleeping(false);
706 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800707 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800708 }
709
Craig Mautner0eea92c2013-05-16 13:35:39 -0700710 /**
711 * @return true if something must be done before going to sleep.
712 */
713 boolean checkReadyForSleepLocked() {
714 if (mResumedActivity != null) {
715 // Still have something resumed; can't sleep until it is paused.
716 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
717 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
718 startPausingLocked(false, true);
719 return true;
720 }
721 if (mPausingActivity != null) {
722 // Still waiting for something to pause; can't sleep yet.
723 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
724 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800725 }
726
Craig Mautner0eea92c2013-05-16 13:35:39 -0700727 return false;
728 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800729
Craig Mautner0eea92c2013-05-16 13:35:39 -0700730 void goToSleep() {
731 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800732
Craig Mautner0eea92c2013-05-16 13:35:39 -0700733 // Make sure any stopped but visible activities are now sleeping.
734 // This ensures that the activity's onStop() is called.
735 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
736 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
737 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
738 final ActivityRecord r = activities.get(activityNdx);
739 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
740 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800741 }
742 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800743 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700744 }
Craig Mautner59c00972012-07-30 12:10:24 -0700745
Winson Chung376543b2014-05-21 17:43:28 -0700746 /**
747 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
748 * again when requested.
749 */
750 private void invalidateLastScreenshot() {
751 mLastScreenshotActivity = null;
752 if (mLastScreenshotBitmap != null) {
753 mLastScreenshotBitmap.recycle();
754 }
755 mLastScreenshotBitmap = null;
756 }
757
Dianne Hackbornd2835932010-12-13 16:28:46 -0800758 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700759 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800760 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700761 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800762 return null;
763 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800764
Winson Chung083baf92014-07-11 10:32:42 -0700765 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700766 // This is an optimization -- since we never show Home or Recents within Recents itself,
767 // we can just go ahead and skip taking the screenshot if this is the home stack. In
768 // the case where the most recent task is not the task that was supplied, then the stack
769 // has changed, so invalidate the last screenshot().
770 invalidateLastScreenshot();
771 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700772 return null;
773 }
774
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800775 int w = mThumbnailWidth;
776 int h = mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800777 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800778 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700779 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800780 || mLastScreenshotBitmap.getWidth() != w
781 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700782 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800783 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700784 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700785 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800786 }
787 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700788 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700789 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800790 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800791 }
Winson Chung376543b2014-05-21 17:43:28 -0700792 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800793 return null;
794 }
795
Craig Mautnercf910b02013-04-23 11:23:27 -0700796 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800797 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800798 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700799 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800800 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700801 ActivityRecord prev = mResumedActivity;
802 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700803 Slog.e(TAG, "Trying to pause when nothing is resumed",
804 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700805 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700806 return;
807 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800808
809 if (mActivityContainer.mParentActivity == null) {
810 // Top level stack, not a child. Look for child stacks.
811 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
812 }
813
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700814 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
815 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800817 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700819 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
820 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 prev.state = ActivityState.PAUSING;
822 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700823 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700824 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Alan Viverettea2ff6112014-07-14 17:46:27 -0700825 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task)) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700826 prev.updateThumbnail(screenshotActivities(prev), null);
827 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700828 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829
830 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800831
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 if (prev.app != null && prev.app.thread != null) {
833 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
834 try {
835 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700836 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700838 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800839 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
840 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700841 } catch (Exception e) {
842 // Ignore exception, if process died other code will cleanup.
843 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800844 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700846 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 }
848 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800849 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700851 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700852 }
853
854 // If we are not going to sleep, we want to ensure the device is
855 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700856 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700857 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 }
859
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700861 // Have the window manager pause its key dispatching until the new
862 // activity has started. If we're pausing the activity just because
863 // the screen is being turned off and the UI is sleeping, don't interrupt
864 // key dispatch; the same activity will pick it up again on wakeup.
865 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800866 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700867 } else {
868 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
869 }
870
871 // Schedule a pause timeout in case the app doesn't respond.
872 // We don't give it much time because this directly impacts the
873 // responsiveness seen by the user.
874 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
875 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700876 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700877 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
878 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
879 } else {
880 // This activity failed to schedule the
881 // pause, so just treat it as being paused now.
882 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700883 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700884 }
885 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700886
Craig Mautnera0026042014-04-23 11:45:37 -0700887 final void activityPausedLocked(IBinder token, boolean timeout,
888 PersistableBundle persistentState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700889 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800890 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700891
Craig Mautnerd2328952013-03-05 12:46:26 -0800892 final ActivityRecord r = isInStackLocked(token);
893 if (r != null) {
894 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Craig Mautner21d24a22014-04-23 11:45:37 -0700895 if (persistentState != null) {
896 r.persistentState = persistentState;
897 mService.notifyTaskPersisterLocked(r.task, false);
898 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800899 if (mPausingActivity == r) {
900 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
901 + (timeout ? " (due to timeout)" : " (pause complete)"));
902 r.state = ActivityState.PAUSED;
903 completePauseLocked();
904 } else {
905 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
906 r.userId, System.identityHashCode(r), r.shortComponentName,
907 mPausingActivity != null
908 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 }
910 }
911 }
912
Craig Mautnera0026042014-04-23 11:45:37 -0700913 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
914 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700915 if (r.state != ActivityState.STOPPING) {
916 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
917 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
918 return;
919 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700920 if (persistentState != null) {
921 r.persistentState = persistentState;
922 mService.notifyTaskPersisterLocked(r.task, false);
923 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700924 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700925 if (icicle != null) {
926 // If icicle is null, this is happening due to a timeout, so we
927 // haven't really saved the state.
928 r.icicle = icicle;
929 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800930 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700931 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700932 }
933 if (!r.stopped) {
934 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
935 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
936 r.stopped = true;
937 r.state = ActivityState.STOPPED;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700938 if (mActivityContainer.mActivityDisplay.mMediaPlayingActivity == r) {
939 mStackSupervisor.setMediaPlayingLocked(r, false);
940 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700941 if (r.finishing) {
942 r.clearOptionsLocked();
943 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700944 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700945 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700946 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700947 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700948 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800949 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700950 }
951 }
952 }
953
Craig Mautnerc8143c62013-09-03 12:15:57 -0700954 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800955 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700956 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800957
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800958 if (prev != null) {
959 if (prev.finishing) {
960 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700961 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800962 } else if (prev.app != null) {
963 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
964 if (prev.waitingVisible) {
965 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700966 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800967 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
968 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800969 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800970 if (prev.configDestroy) {
971 // The previous is being paused because the configuration
972 // is changing, which means it is actually stopping...
973 // To juggle the fact that we are also starting a new
974 // instance right now, we need to first completely stop
975 // the current instance before starting the new one.
976 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700977 destroyActivityLocked(prev, true, "pause-config");
978 } else if (!isMediaPlaying()) {
979 // If we were playing then resumeTopActivities will release resources before
980 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700981 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700982 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
983 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 // If we already have a few activities waiting to stop,
985 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700986 // them out. Or if r is the last of activity of the last task the stack
987 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700989 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800990 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700991 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800992 }
993 }
994 } else {
995 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
996 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800997 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800998 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700999 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001000
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001001 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001002 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -07001003 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001004 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001005 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001006 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001007 if (top == null || (prev != null && top != prev)) {
1008 // If there are no more activities available to run,
1009 // do resume anyway to start something. Also if the top
1010 // activity on the stack is not the just paused activity,
1011 // we need to go ahead and resume it to ensure we complete
1012 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -07001013 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001014 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001016
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001017 if (prev != null) {
1018 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001019
Craig Mautner525f3d92013-05-07 14:01:50 -07001020 if (prev.app != null && prev.cpuTimeAtResume > 0
1021 && mService.mBatteryStatsService.isOnBattery()) {
1022 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -07001023 synchronized (mService.mProcessCpuThread) {
1024 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -07001025 - prev.cpuTimeAtResume;
1026 }
1027 if (diff > 0) {
1028 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1029 synchronized (bsi) {
1030 BatteryStatsImpl.Uid.Proc ps =
1031 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001032 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001033 if (ps != null) {
1034 ps.addForegroundTimeLocked(diff);
1035 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001036 }
1037 }
1038 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001039 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 }
1042
1043 /**
1044 * Once we know that we have asked an application to put an activity in
1045 * the resumed state (either by launching it or explicitly telling it),
1046 * this function updates the rest of our state to match that fact.
1047 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001048 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001049 next.idle = false;
1050 next.results = null;
1051 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001052
1053 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1054 ProcessRecord app = next.task.mActivities.get(0).app;
1055 if (app != null && app != mService.mHomeProcess) {
1056 mService.mHomeProcess = app;
1057 }
1058 }
1059
Craig Mautner07566322013-09-26 16:42:55 -07001060 if (next.nowVisible) {
1061 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1062 mStackSupervisor.dismissKeyguard();
1063 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064
1065 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001066 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001068 mStackSupervisor.reportResumedActivityLocked(next);
1069
1070 next.resumeKeyDispatchingLocked();
1071 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072
1073 // Mark the point when the activity is resuming
1074 // TODO: To be more accurate, the mark should be before the onCreate,
1075 // not after the onResume. But for subsequent starts, onResume is fine.
1076 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001077 synchronized (mService.mProcessCpuThread) {
1078 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079 }
1080 } else {
1081 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1082 }
Winson Chung376543b2014-05-21 17:43:28 -07001083
1084 // If we are resuming the activity that we had last screenshotted, then we know it will be
1085 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1086 if (next == mLastScreenshotActivity) {
1087 invalidateLastScreenshot();
1088 }
George Mount6ba042b2014-07-28 11:12:28 -07001089 next.returningOptions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090 }
1091
Craig Mautnere3a00d72014-04-16 08:31:19 -07001092 private void setVisibile(ActivityRecord r, boolean visible) {
1093 r.visible = visible;
1094 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001095 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001096 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001097 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001098 container.setVisible(visible);
1099 }
1100 }
1101
Jose Lima7ba71252014-04-30 12:59:27 -07001102 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1103 // If so, this stack is hidden, otherwise it is visible.
1104 private boolean isStackVisible() {
1105 if (!isAttached()) {
1106 return false;
1107 }
1108
1109 if (mStackSupervisor.isFrontStack(this)) {
1110 return true;
1111 }
1112
Jose Lima729cb232014-05-05 15:59:40 -07001113 /**
1114 * Start at the task above this one and go up, looking for a visible
1115 * fullscreen activity, or a translucent activity that requested the
1116 * wallpaper to be shown behind it.
1117 */
Jose Lima7ba71252014-04-30 12:59:27 -07001118 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1119 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1120 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001121 final TaskRecord task = tasks.get(taskNdx);
1122 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001123 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1124 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001125
1126 // Conditions for an activity to obscure the stack we're
1127 // examining:
1128 // 1. Not Finishing AND Visible AND:
1129 // 2. Either:
1130 // - Full Screen Activity OR
1131 // - On top of Home and our stack is NOT home
1132 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001133 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001134 return false;
1135 }
1136 }
1137 }
1138 }
1139
1140 return true;
1141 }
1142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001143 /**
1144 * Make sure that all activities that need to be visible (that is, they
1145 * currently can be seen by the user) actually are.
1146 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001147 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1148 ActivityRecord top = topRunningActivityLocked(null);
1149 if (top == null) {
1150 return;
1151 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001152 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 TAG, "ensureActivitiesVisible behind " + top
1154 + " configChanges=0x" + Integer.toHexString(configChanges));
1155
Craig Mautner5eda9b32013-07-02 11:58:16 -07001156 if (mTranslucentActivityWaiting != top) {
1157 mUndrawnActivitiesBelowTopTranslucent.clear();
1158 if (mTranslucentActivityWaiting != null) {
1159 // Call the callback with a timeout indication.
1160 notifyActivityDrawnLocked(null);
1161 mTranslucentActivityWaiting = null;
1162 }
1163 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1164 }
1165
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001166 // If the top activity is not fullscreen, then we need to
1167 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001168 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001169 boolean behindFullscreen = !isStackVisible();
1170
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001171 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001172 final TaskRecord task = mTaskHistory.get(taskNdx);
1173 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001174 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1175 final ActivityRecord r = activities.get(activityNdx);
1176 if (r.finishing) {
1177 continue;
1178 }
1179 if (aboveTop && r != top) {
1180 continue;
1181 }
1182 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001183 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1184 // but must be drawn initially for the animation as though they were visible.
1185 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001186 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001187 TAG, "Make visible? " + r + " finishing=" + r.finishing
1188 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001189
Craig Mautnerd44711d2013-02-23 11:24:36 -08001190 // First: if this is not the current activity being started, make
1191 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001192 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001193 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001195
1196 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001197 // This activity needs to be visible, but isn't even
1198 // running... get it started, but don't resume it
1199 // at this point.
1200 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1201 if (r != starting) {
1202 r.startFreezingScreenLocked(r.app, configChanges);
1203 }
1204 if (!r.visible || r.mLaunchTaskBehind) {
1205 if (DEBUG_VISBILITY) Slog.v(
1206 TAG, "Starting and making visible: " + r);
1207 setVisibile(r, true);
1208 }
1209 if (r != starting) {
1210 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001211 }
1212
1213 } else if (r.visible) {
1214 // If this activity is already visible, then there is nothing
1215 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001216 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001217 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001218 try {
George Mount6ba042b2014-07-28 11:12:28 -07001219 if (r.returningOptions != null) {
1220 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1221 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001222 }
1223 } catch(RemoteException e) {
1224 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001225 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001226 // This activity is not currently visible, but is running.
1227 // Tell it to become visible.
1228 r.visible = true;
1229 if (r.state != ActivityState.RESUMED && r != starting) {
1230 // If this activity is paused, tell it
1231 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001232 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001233 TAG, "Making visible and scheduling visibility: " + r);
1234 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001235 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001236 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001237 mUndrawnActivitiesBelowTopTranslucent.add(r);
1238 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001239 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001240 r.sleeping = false;
1241 r.app.pendingUiClean = true;
1242 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1243 r.stopFreezingScreenLocked(false);
1244 } catch (Exception e) {
1245 // Just skip on any failure; we'll make it
1246 // visible when it next restarts.
1247 Slog.w(TAG, "Exception thrown making visibile: "
1248 + r.intent.getComponent(), e);
1249 }
1250 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001251 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001252
Craig Mautnerd44711d2013-02-23 11:24:36 -08001253 // Aggregate current change flags.
1254 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001255
Craig Mautnerd44711d2013-02-23 11:24:36 -08001256 if (r.fullscreen) {
1257 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001258 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1259 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001260 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001261 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001262 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001263 }
1264 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001265 if (DEBUG_VISBILITY) Slog.v(
1266 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1267 + " state=" + r.state
1268 + " behindFullscreen=" + behindFullscreen);
1269 // Now for any activities that aren't visible to the user, make
1270 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001271 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001272 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001273 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001274 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001275 switch (r.state) {
1276 case STOPPING:
1277 case STOPPED:
1278 if (r.app != null && r.app.thread != null) {
1279 if (DEBUG_VISBILITY) Slog.v(
1280 TAG, "Scheduling invisibility: " + r);
1281 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1282 }
1283 break;
1284
1285 case INITIALIZING:
1286 case RESUMED:
1287 case PAUSING:
1288 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001289 // This case created for transitioning activities from
1290 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautneree2e45a2014-06-27 12:10:03 -07001291 if (getMediaPlayer() == r) {
1292 releaseMediaResources();
1293 } else {
1294 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1295 mStackSupervisor.mStoppingActivities.add(r);
1296 }
1297 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001298 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001299 break;
1300
1301 default:
1302 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001303 }
1304 } catch (Exception e) {
1305 // Just skip on any failure; we'll make it
1306 // visible when it next restarts.
1307 Slog.w(TAG, "Exception thrown making hidden: "
1308 + r.intent.getComponent(), e);
1309 }
1310 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001311 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 }
1314 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001315 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001316
1317 if (mTranslucentActivityWaiting != null &&
1318 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1319 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1320 notifyActivityDrawnLocked(null);
1321 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001322 }
Craig Mautner58547802013-03-05 08:23:53 -08001323
George Mount6ba042b2014-07-28 11:12:28 -07001324 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001325 mTranslucentActivityWaiting = r;
1326 mUndrawnActivitiesBelowTopTranslucent.clear();
1327 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1328 }
1329
1330 /**
1331 * Called as activities below the top translucent activity are redrawn. When the last one is
1332 * redrawn notify the top activity by calling
1333 * {@link Activity#onTranslucentConversionComplete}.
1334 *
1335 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1336 * occurred and the activity will be notified immediately.
1337 */
1338 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001339 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001340 if ((r == null)
1341 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1342 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1343 // The last undrawn activity below the top has just been drawn. If there is an
1344 // opaque activity at the top, notify it that it can become translucent safely now.
1345 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1346 mTranslucentActivityWaiting = null;
1347 mUndrawnActivitiesBelowTopTranslucent.clear();
1348 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1349
Craig Mautner71dd1b62014-02-18 15:48:52 -08001350 if (waitingActivity != null) {
1351 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1352 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1353 try {
1354 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1355 waitingActivity.appToken, r != null);
1356 } catch (RemoteException e) {
1357 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001358 }
1359 }
1360 }
1361 }
1362
Craig Mautnera61bc652013-10-28 15:43:18 -07001363 /** If any activities below the top running one are in the INITIALIZING state and they have a
1364 * starting window displayed then remove that starting window. It is possible that the activity
1365 * in this state will never resumed in which case that starting window will be orphaned. */
1366 void cancelInitializingActivities() {
1367 final ActivityRecord topActivity = topRunningActivityLocked(null);
1368 boolean aboveTop = true;
1369 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1370 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1371 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1372 final ActivityRecord r = activities.get(activityNdx);
1373 if (aboveTop) {
1374 if (r == topActivity) {
1375 aboveTop = false;
1376 }
1377 continue;
1378 }
1379
1380 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1381 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1382 r.mStartingWindowShown = false;
1383 mWindowManager.removeAppStartingWindow(r.appToken);
1384 }
1385 }
1386 }
1387 }
1388
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001389 /**
1390 * Ensure that the top activity in the stack is resumed.
1391 *
1392 * @param prev The previously resumed activity, for when in the process
1393 * of pausing; can be null to call from elsewhere.
1394 *
1395 * @return Returns true if something is being resumed, or false if
1396 * nothing happened.
1397 */
1398 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001399 return resumeTopActivityLocked(prev, null);
1400 }
1401
1402 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001403 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1404
Craig Mautnerdf88d732014-01-27 09:21:32 -08001405 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001406 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001407 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001408 // Do not resume this stack if its parent is not resumed.
1409 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1410 return false;
1411 }
1412
Craig Mautnera61bc652013-10-28 15:43:18 -07001413 cancelInitializingActivities();
1414
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001415 // Find the first activity that is not finishing.
1416 ActivityRecord next = topRunningActivityLocked(null);
1417
1418 // Remember how we'll process this pause/resume situation, and ensure
1419 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001420 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1421 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422
Craig Mautner84984fa2014-06-19 11:19:20 -07001423 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 if (next == null) {
1425 // There are no more activities! Let's just start up the
1426 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001427 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001428 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001429 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001430 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001431 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1432 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1433 return isOnHomeDisplay() &&
1434 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 }
1436
1437 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001438
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001440 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1441 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 // Make sure we have executed any pending transitions, since there
1443 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001444 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001446 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001447 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001448 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 return false;
1450 }
1451
Craig Mautner525f3d92013-05-07 14:01:50 -07001452 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001453 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001454 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001455 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001456 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001457 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001458 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001459 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001460 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001461 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001462 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001463 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001464 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1465 "resumeTopActivityLocked: Launching home next");
1466 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001467 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1468 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1469 return isOnHomeDisplay() &&
1470 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001471 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001472 }
1473
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 // If we are sleeping, and there is no resumed activity, and the top
1475 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001476 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001477 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001478 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 // Make sure we have executed any pending transitions, since there
1480 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001481 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001483 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001484 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001485 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 return false;
1487 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001488
1489 // Make sure that the user who owns this activity is started. If not,
1490 // we will just leave it as is because someone should be bringing
1491 // another user's activities to the top of the stack.
1492 if (mService.mStartedUsers.get(next.userId) == null) {
1493 Slog.w(TAG, "Skipping resume of top activity " + next
1494 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001495 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001496 return false;
1497 }
1498
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001499 // The activity may be waiting for stop, but that is no longer
1500 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001501 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001502 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001503 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001504 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505
1506 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1507
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001508 // If we are currently pausing an activity, then don't do anything
1509 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001510 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001511 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1512 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001513 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001514 return false;
1515 }
1516
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001517 // Okay we are now going to start a switch, to 'next'. We may first
1518 // have to pause the current activity, but this is an important point
1519 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001520 // XXX "App Redirected" dialog is getting too many false positives
1521 // at this point, so turn off for now.
1522 if (false) {
1523 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1524 long now = SystemClock.uptimeMillis();
1525 final boolean inTime = mLastStartedActivity.startTime != 0
1526 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1527 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1528 final int nextUid = next.info.applicationInfo.uid;
1529 if (inTime && lastUid != nextUid
1530 && lastUid != next.launchedFromUid
1531 && mService.checkPermission(
1532 android.Manifest.permission.STOP_APP_SWITCHES,
1533 -1, next.launchedFromUid)
1534 != PackageManager.PERMISSION_GRANTED) {
1535 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1536 } else {
1537 next.startTime = now;
1538 mLastStartedActivity = next;
1539 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001540 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001541 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001542 mLastStartedActivity = next;
1543 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001544 }
Craig Mautner58547802013-03-05 08:23:53 -08001545
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 // We need to start pausing the current activity so the top one
1547 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001548 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001549 if (mResumedActivity != null) {
1550 pausing = true;
1551 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001552 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001553 }
1554 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001555 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1556 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001557 // At this point we want to put the upcoming activity's process
1558 // at the top of the LRU list, since we know we will be needing it
1559 // very soon and it would be a waste to let it get killed if it
1560 // happens to be sitting towards the end.
1561 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001562 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001563 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001564 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 return true;
1566 }
1567
Christopher Tated3f175c2012-06-14 14:16:54 -07001568 // If the most recent activity was noHistory but was only stopped rather
1569 // than stopped+finished because the device went to sleep, we need to make
1570 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001571 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001572 !mLastNoHistoryActivity.finishing) {
1573 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1574 " on new resume");
1575 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1576 null, "no-history", false);
1577 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001578 }
1579
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001580 if (prev != null && prev != next) {
1581 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1582 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001583 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 if (DEBUG_SWITCH) Slog.v(
1585 TAG, "Resuming top, waiting visible to hide: " + prev);
1586 } else {
1587 // The next activity is already visible, so hide the previous
1588 // activity's windows right now so we can show the new one ASAP.
1589 // We only do this if the previous is finishing, which should mean
1590 // it is on top of the one being resumed so hiding it quickly
1591 // is good. Otherwise, we want to do the normal route of allowing
1592 // the resumed activity to be shown so we can decide if the
1593 // previous should actually be hidden depending on whether the
1594 // new one is found to be full-screen or not.
1595 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001596 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1598 + prev + ", waitingVisible="
1599 + (prev != null ? prev.waitingVisible : null)
1600 + ", nowVisible=" + next.nowVisible);
1601 } else {
1602 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1603 + prev + ", waitingVisible="
1604 + (prev != null ? prev.waitingVisible : null)
1605 + ", nowVisible=" + next.nowVisible);
1606 }
1607 }
1608 }
1609
Dianne Hackborne7f97212011-02-24 14:40:20 -08001610 // Launching this app's activity, make sure the app is no longer
1611 // considered stopped.
1612 try {
1613 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001614 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001615 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001616 } catch (IllegalArgumentException e) {
1617 Slog.w(TAG, "Failed trying to unstop package "
1618 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001619 }
1620
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 // We are starting up the next activity, so tell the window manager
1622 // that the previous one will be hidden soon. This way it can know
1623 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001624 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001625 if (prev != null) {
1626 if (prev.finishing) {
1627 if (DEBUG_TRANSITION) Slog.v(TAG,
1628 "Prepare close transition: prev=" + prev);
1629 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001630 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001631 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001633 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001634 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1635 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001637 mWindowManager.setAppWillBeHidden(prev.appToken);
1638 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001639 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001640 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001642 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001643 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001645 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001646 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001647 : next.mLaunchTaskBehind
1648 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1649 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001650 }
1651 }
1652 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001653 mWindowManager.setAppWillBeHidden(prev.appToken);
1654 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 }
Craig Mautner967212c2013-04-13 21:10:58 -07001656 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001657 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001658 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001659 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001660 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001662 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663 }
1664 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001665
1666 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001667 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001668 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1669 if (opts != null) {
1670 resumeAnimOptions = opts.toBundle();
1671 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001672 next.applyOptionsLocked();
1673 } else {
1674 next.clearOptionsLocked();
1675 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001676
Craig Mautnercf910b02013-04-23 11:23:27 -07001677 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001678 if (next.app != null && next.app.thread != null) {
1679 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1680
1681 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001682 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001683
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001684 // schedule launch ticks to collect information about slow apps.
1685 next.startLaunchTickingLocked();
1686
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001687 ActivityRecord lastResumedActivity =
1688 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001689 ActivityState lastState = next.state;
1690
1691 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001692
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001693 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 next.state = ActivityState.RESUMED;
1695 mResumedActivity = next;
1696 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001697 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001698 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001700 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001701
1702 // Have the window manager re-evaluate the orientation of
1703 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001704 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001705 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001706 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001707 mService.mConfiguration,
1708 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1709 if (config != null) {
1710 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001712 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001714
Craig Mautner525f3d92013-05-07 14:01:50 -07001715 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001716 // The configuration update wasn't able to keep the existing
1717 // instance of the activity, and instead started a new one.
1718 // We should be all done, but let's just make sure our activity
1719 // is still at the top and schedule another run if something
1720 // weird happened.
1721 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001722 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 "Activity config changed during resume: " + next
1724 + ", new next: " + nextNext);
1725 if (nextNext != next) {
1726 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001727 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001729 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001730 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001731 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001732 return true;
1733 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001734 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001735 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 }
Craig Mautner58547802013-03-05 08:23:53 -08001737
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 try {
1739 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001740 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741 if (a != null) {
1742 final int N = a.size();
1743 if (!next.finishing && N > 0) {
1744 if (DEBUG_RESULTS) Slog.v(
1745 TAG, "Delivering results to " + next
1746 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001747 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 }
1749 }
1750
1751 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001752 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 }
1754
1755 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001756 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001758
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001759 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001760 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001761 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001762 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001763 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001764 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001765 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001766
Craig Mautner0eea92c2013-05-16 13:35:39 -07001767 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768
Craig Mautnerac6f8432013-07-17 13:24:59 -07001769 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 } catch (Exception e) {
1771 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001772 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1773 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001775 if (lastStack != null) {
1776 lastStack.mResumedActivity = lastResumedActivity;
1777 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 Slog.i(TAG, "Restarting because process died: " + next);
1779 if (!next.hasBeenLaunched) {
1780 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001781 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1782 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001783 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001784 next.appToken, next.packageName, next.theme,
1785 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001786 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1787 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 }
George Mount2c92c972014-03-20 09:38:23 -07001789 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001790 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 return true;
1792 }
1793
1794 // From this point on, if something goes wrong there is no way
1795 // to recover the activity.
1796 try {
1797 next.visible = true;
1798 completeResumeLocked(next);
1799 } catch (Exception e) {
1800 // If any exception gets thrown, toss away this
1801 // activity and try the next one.
1802 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001803 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001804 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001805 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806 return true;
1807 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 next.stopped = false;
1809
1810 } else {
1811 // Whoops, need to restart this activity!
1812 if (!next.hasBeenLaunched) {
1813 next.hasBeenLaunched = true;
1814 } else {
1815 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001816 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001817 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001818 mService.compatibilityInfoForPackageLocked(
1819 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001821 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001822 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 }
1824 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1825 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001826 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001827 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 }
1829
Craig Mautnercf910b02013-04-23 11:23:27 -07001830 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001831 return true;
1832 }
1833
Craig Mautnerac6f8432013-07-17 13:24:59 -07001834 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001835 // If this is being moved to the top by another activity or being launched from the home
1836 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001837 if (isOnHomeDisplay()) {
1838 ActivityStack lastStack = mStackSupervisor.getLastStack();
1839 final boolean fromHome = lastStack.isHomeStack();
1840 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001841 task.setTaskToReturnTo(fromHome
1842 ? lastStack.topTask() == null
1843 ? HOME_ACTIVITY_TYPE
1844 : lastStack.topTask().taskType
1845 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001846 }
1847 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001848 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001849 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001850
Craig Mautnerac6f8432013-07-17 13:24:59 -07001851 mTaskHistory.remove(task);
1852 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001853 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001854 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001855 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001856 while (--taskNdx >= 0) {
1857 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001858 break;
1859 }
1860 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001861 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001862 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001863 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001864 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001865 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001866
Craig Mautner8849a5e2013-04-02 16:41:03 -07001867 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001868 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001869 TaskRecord rTask = r.task;
1870 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001871 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1872 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001873 // Last activity in task had been removed or ActivityManagerService is reusing task.
1874 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001875 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001876 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001877 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001878 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001879 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 if (!newTask) {
1881 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001882 boolean startIt = true;
1883 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1884 task = mTaskHistory.get(taskNdx);
1885 if (task == r.task) {
1886 // Here it is! Now, if this is not yet visible to the
1887 // user, then just add it without starting; it will
1888 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001889 if (!startIt) {
1890 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1891 + task, new RuntimeException("here").fillInStackTrace());
1892 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001893 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001894 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1895 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001896 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001897 r.userId, r.info.configChanges, task.voiceSession != null,
1898 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001899 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001900 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001902 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903 return;
1904 }
1905 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001906 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001907 startIt = false;
1908 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 }
1910 }
1911
1912 // Place a new activity at top of stack, so it is next to interact
1913 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001914
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915 // If we are not placing the new activity frontmost, we do not want
1916 // to deliver the onUserLeaving callback to the actual frontmost
1917 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001918 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001919 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001920 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1921 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001922 }
Craig Mautner70a86932013-02-28 22:37:44 -08001923
1924 task = r.task;
1925
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001927 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001928 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001929 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001930 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001931
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001932 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001933 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001934 // We want to show the starting preview window if we are
1935 // switching to a new task, or the next activity's process is
1936 // not currently running.
1937 boolean showStartingIcon = newTask;
1938 ProcessRecord proc = r.app;
1939 if (proc == null) {
1940 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1941 }
1942 if (proc == null || proc.thread == null) {
1943 showStartingIcon = true;
1944 }
1945 if (DEBUG_TRANSITION) Slog.v(TAG,
1946 "Prepare open transition: starting " + r);
1947 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001948 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001950 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001951 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07001952 ? r.mLaunchTaskBehind
1953 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1954 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08001955 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001956 mNoAnimActivities.remove(r);
1957 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001958 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001959 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001960 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07001961 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001962 boolean doShow = true;
1963 if (newTask) {
1964 // Even though this activity is starting fresh, we still need
1965 // to reset it to make sure we apply affinities to move any
1966 // existing activities from other tasks in to it.
1967 // If the caller has requested that the target task be
1968 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001969 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 resetTaskIfNeededLocked(r, r);
1971 doShow = topRunningNonDelayedActivityLocked(null) == r;
1972 }
George Mount70778d72014-07-01 16:33:45 -07001973 } else if (options != null && new ActivityOptions(options).getAnimationType()
1974 == ActivityOptions.ANIM_SCENE_TRANSITION) {
1975 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001976 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001977 if (r.mLaunchTaskBehind) {
1978 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
1979 // tell WindowManager that r is visible even though it is at the back of the stack.
1980 mWindowManager.setAppVisibility(r.appToken, true);
1981 ensureActivitiesVisibleLocked(null, 0);
1982 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001983 // Figure out if we are transitioning from another activity that is
1984 // "has the same starting icon" as the next one. This allows the
1985 // window manager to keep the previous window it had previously
1986 // created, if it still had one.
1987 ActivityRecord prev = mResumedActivity;
1988 if (prev != null) {
1989 // We don't want to reuse the previous starting preview if:
1990 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001991 if (prev.task != r.task) {
1992 prev = null;
1993 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001994 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001995 else if (prev.nowVisible) {
1996 prev = null;
1997 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001999 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002000 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002001 mService.compatibilityInfoForPackageLocked(
2002 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002003 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002004 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002005 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 }
2007 } else {
2008 // If this is the first activity, don't do any fancy animations,
2009 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002010 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002011 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002012 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002013 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002014 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002015 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 }
2017 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002018 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002019 }
2020
2021 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002022 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 }
2024 }
2025
Dianne Hackbornbe707852011-11-11 14:32:10 -08002026 final void validateAppTokensLocked() {
2027 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002028 mValidateAppTokens.ensureCapacity(numActivities());
2029 final int numTasks = mTaskHistory.size();
2030 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2031 TaskRecord task = mTaskHistory.get(taskNdx);
2032 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002033 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002034 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002035 }
Craig Mautner000f0022013-02-26 15:04:29 -08002036 TaskGroup group = new TaskGroup();
2037 group.taskId = task.taskId;
2038 mValidateAppTokens.add(group);
2039 final int numActivities = activities.size();
2040 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2041 final ActivityRecord r = activities.get(activityNdx);
2042 group.tokens.add(r.appToken);
2043 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002044 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002045 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002046 }
2047
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 /**
2049 * Perform a reset of the given task, if needed as part of launching it.
2050 * Returns the new HistoryRecord at the top of the task.
2051 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002052 /**
2053 * Helper method for #resetTaskIfNeededLocked.
2054 * We are inside of the task being reset... we'll either finish this activity, push it out
2055 * for another task, or leave it as-is.
2056 * @param task The task containing the Activity (taskTop) that might be reset.
2057 * @param forceReset
2058 * @return An ActivityOptions that needs to be processed.
2059 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002060 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002061 ActivityOptions topOptions = null;
2062
2063 int replyChainEnd = -1;
2064 boolean canMoveOptions = true;
2065
2066 // We only do this for activities that are not the root of the task (since if we finish
2067 // the root, we may no longer have the task!).
2068 final ArrayList<ActivityRecord> activities = task.mActivities;
2069 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002070 final int rootActivityNdx = task.findEffectiveRootIndex();
2071 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002072 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002073 if (target.frontOfTask)
2074 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002075
2076 final int flags = target.info.flags;
2077 final boolean finishOnTaskLaunch =
2078 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2079 final boolean allowTaskReparenting =
2080 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2081 final boolean clearWhenTaskReset =
2082 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2083
2084 if (!finishOnTaskLaunch
2085 && !clearWhenTaskReset
2086 && target.resultTo != null) {
2087 // If this activity is sending a reply to a previous
2088 // activity, we can't do anything with it now until
2089 // we reach the start of the reply chain.
2090 // XXX note that we are assuming the result is always
2091 // to the previous activity, which is almost always
2092 // the case but we really shouldn't count on.
2093 if (replyChainEnd < 0) {
2094 replyChainEnd = i;
2095 }
2096 } else if (!finishOnTaskLaunch
2097 && !clearWhenTaskReset
2098 && allowTaskReparenting
2099 && target.taskAffinity != null
2100 && !target.taskAffinity.equals(task.affinity)) {
2101 // If this activity has an affinity for another
2102 // task, then we need to move it out of here. We will
2103 // move it as far out of the way as possible, to the
2104 // bottom of the activity stack. This also keeps it
2105 // correctly ordered with any activities we previously
2106 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002107 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002108 final ActivityRecord bottom =
2109 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002110 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002111 if (bottom != null && target.taskAffinity != null
2112 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002113 // If the activity currently at the bottom has the
2114 // same task affinity as the one we are moving,
2115 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002116 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002117 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002118 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002119 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002120 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002121 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002122 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002123 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2124 + " out to new task " + target.task);
2125 }
2126
Craig Mautnere3a74d52013-02-22 14:14:58 -08002127 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002128 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002129
Craig Mautner525f3d92013-05-07 14:01:50 -07002130 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002131 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2132 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002133 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002134 if (p.finishing) {
2135 continue;
2136 }
2137
Craig Mautnere3a74d52013-02-22 14:14:58 -08002138 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002139 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140 topOptions = p.takeOptionsLocked();
2141 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002142 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002143 }
2144 }
2145 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002146 + task + " adding to task=" + targetTask
2147 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002148 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2149 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002150 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002151 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002152
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002153 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002154 }
2155
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002156 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002157 if (VALIDATE_TOKENS) {
2158 validateAppTokensLocked();
2159 }
2160
2161 replyChainEnd = -1;
2162 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2163 // If the activity should just be removed -- either
2164 // because it asks for it, or the task should be
2165 // cleared -- then finish it and anything that is
2166 // part of its reply chain.
2167 int end;
2168 if (clearWhenTaskReset) {
2169 // In this case, we want to finish this activity
2170 // and everything above it, so be sneaky and pretend
2171 // like these are all in the reply chain.
2172 end = numActivities - 1;
2173 } else if (replyChainEnd < 0) {
2174 end = i;
2175 } else {
2176 end = replyChainEnd;
2177 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002178 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002179 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002180 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002181 if (p.finishing) {
2182 continue;
2183 }
2184 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002185 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002186 topOptions = p.takeOptionsLocked();
2187 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002188 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002189 }
2190 }
Craig Mautner58547802013-03-05 08:23:53 -08002191 if (DEBUG_TASKS) Slog.w(TAG,
2192 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002193 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002194 end--;
2195 srcPos--;
2196 }
2197 }
2198 replyChainEnd = -1;
2199 } else {
2200 // If we were in the middle of a chain, well the
2201 // activity that started it all doesn't want anything
2202 // special, so leave it all as-is.
2203 replyChainEnd = -1;
2204 }
2205 }
2206
2207 return topOptions;
2208 }
2209
2210 /**
2211 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2212 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2213 * @param affinityTask The task we are looking for an affinity to.
2214 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2215 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2216 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2217 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002218 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002219 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002220 int replyChainEnd = -1;
2221 final int taskId = task.taskId;
2222 final String taskAffinity = task.affinity;
2223
2224 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2225 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002226 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2227
2228 // Do not operate on or below the effective root Activity.
2229 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002231 if (target.frontOfTask)
2232 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002233
2234 final int flags = target.info.flags;
2235 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2236 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2237
2238 if (target.resultTo != null) {
2239 // If this activity is sending a reply to a previous
2240 // activity, we can't do anything with it now until
2241 // we reach the start of the reply chain.
2242 // XXX note that we are assuming the result is always
2243 // to the previous activity, which is almost always
2244 // the case but we really shouldn't count on.
2245 if (replyChainEnd < 0) {
2246 replyChainEnd = i;
2247 }
2248 } else if (topTaskIsHigher
2249 && allowTaskReparenting
2250 && taskAffinity != null
2251 && taskAffinity.equals(target.taskAffinity)) {
2252 // This activity has an affinity for our task. Either remove it if we are
2253 // clearing or move it over to our task. Note that
2254 // we currently punt on the case where we are resetting a
2255 // task that is not at the top but who has activities above
2256 // with an affinity to it... this is really not a normal
2257 // case, and we will need to later pull that task to the front
2258 // and usually at that point we will do the reset and pick
2259 // up those remaining activities. (This only happens if
2260 // someone starts an activity in a new task from an activity
2261 // in a task that is not currently on top.)
2262 if (forceReset || finishOnTaskLaunch) {
2263 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2264 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2265 for (int srcPos = start; srcPos >= i; --srcPos) {
2266 final ActivityRecord p = activities.get(srcPos);
2267 if (p.finishing) {
2268 continue;
2269 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002270 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002271 }
2272 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002273 if (taskInsertionPoint < 0) {
2274 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002275
Craig Mautner77878772013-03-04 19:46:24 -08002276 }
Craig Mautner77878772013-03-04 19:46:24 -08002277
2278 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2279 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2280 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2281 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002283 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002284 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002285
Craig Mautnere3a74d52013-02-22 14:14:58 -08002286 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2287 + " to stack at " + task,
2288 new RuntimeException("here").fillInStackTrace());
2289 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2290 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002291 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002292 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002293 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002294 if (VALIDATE_TOKENS) {
2295 validateAppTokensLocked();
2296 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002297
2298 // Now we've moved it in to place... but what if this is
2299 // a singleTop activity and we have put it on top of another
2300 // instance of the same activity? Then we drop the instance
2301 // below so it remains singleTop.
2302 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2303 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002304 int targetNdx = taskActivities.indexOf(target);
2305 if (targetNdx > 0) {
2306 ActivityRecord p = taskActivities.get(targetNdx - 1);
2307 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002308 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2309 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002310 }
2311 }
2312 }
2313 }
2314
2315 replyChainEnd = -1;
2316 }
2317 }
Craig Mautner77878772013-03-04 19:46:24 -08002318 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002319 }
2320
Craig Mautner8849a5e2013-04-02 16:41:03 -07002321 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002322 ActivityRecord newActivity) {
2323 boolean forceReset =
2324 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2325 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2326 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2327 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2328 forceReset = true;
2329 }
2330 }
2331
2332 final TaskRecord task = taskTop.task;
2333
2334 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2335 * for remaining tasks. Used for later tasks to reparent to task. */
2336 boolean taskFound = false;
2337
2338 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2339 ActivityOptions topOptions = null;
2340
Craig Mautner77878772013-03-04 19:46:24 -08002341 // Preserve the location for reparenting in the new task.
2342 int reparentInsertionPoint = -1;
2343
Craig Mautnere3a74d52013-02-22 14:14:58 -08002344 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2345 final TaskRecord targetTask = mTaskHistory.get(i);
2346
2347 if (targetTask == task) {
2348 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2349 taskFound = true;
2350 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002351 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2352 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002353 }
2354 }
2355
Craig Mautner70a86932013-02-28 22:37:44 -08002356 int taskNdx = mTaskHistory.indexOf(task);
2357 do {
2358 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2359 } while (taskTop == null && taskNdx >= 0);
2360
Craig Mautnere3a74d52013-02-22 14:14:58 -08002361 if (topOptions != null) {
2362 // If we got some ActivityOptions from an activity on top that
2363 // was removed from the task, propagate them to the new real top.
2364 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002365 taskTop.updateOptionsLocked(topOptions);
2366 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002367 topOptions.abort();
2368 }
2369 }
2370
2371 return taskTop;
2372 }
2373
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2375 String resultWho, int requestCode, int resultCode, Intent data) {
2376
2377 if (callingUid > 0) {
2378 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002379 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002380 }
2381
2382 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2383 + " : who=" + resultWho + " req=" + requestCode
2384 + " res=" + resultCode + " data=" + data);
2385 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2386 try {
2387 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2388 list.add(new ResultInfo(resultWho, requestCode,
2389 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002390 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 return;
2392 } catch (Exception e) {
2393 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2394 }
2395 }
2396
2397 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2398 }
2399
Craig Mautner04f0b702013-10-22 12:31:01 -07002400 private void adjustFocusedActivityLocked(ActivityRecord r) {
2401 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2402 ActivityRecord next = topRunningActivityLocked(null);
2403 if (next != r) {
2404 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002405 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2406 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002407 }
2408 }
Winson Chung648c83b2014-04-28 15:11:56 -07002409 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2410 if (top != null) {
2411 mService.setFocusedActivityLocked(top);
2412 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002413 }
2414 }
2415
Craig Mautnerf3333272013-04-22 10:55:53 -07002416 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2418 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2419 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2420 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002421 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002422 if (DEBUG_STATES) {
2423 Slog.d(TAG, "no-history finish of " + r);
2424 }
2425 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002426 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002427 } else {
2428 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2429 + " on stop because we're just sleeping");
2430 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002432 }
2433
2434 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002435 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002436 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002437 try {
2438 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002439 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2440 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 r.state = ActivityState.STOPPING;
2442 if (DEBUG_VISBILITY) Slog.v(
2443 TAG, "Stopping visible=" + r.visible + " for " + r);
2444 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002445 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002446 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002447 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002448 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002449 r.setSleeping(true);
2450 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002451 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002452 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002453 } catch (Exception e) {
2454 // Maybe just ignore exceptions here... if the process
2455 // has crashed, our death notification will clean things
2456 // up.
2457 Slog.w(TAG, "Exception thrown during pause", e);
2458 // Just in case, assume it to be stopped.
2459 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002460 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002461 r.state = ActivityState.STOPPED;
2462 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002463 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002464 }
2465 }
2466 }
2467 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002468
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 /**
2470 * @return Returns true if the activity is being finished, false if for
2471 * some reason it is being left as-is.
2472 */
2473 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002474 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002475 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002476 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002477 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002478 + ", result=" + resultCode + ", data=" + resultData
2479 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002480 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 return false;
2482 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483
Craig Mautnerd44711d2013-02-23 11:24:36 -08002484 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002485 return true;
2486 }
2487
Craig Mautnerd2328952013-03-05 12:46:26 -08002488 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002489 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2490 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2491 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2492 ActivityRecord r = activities.get(activityNdx);
2493 if (r.resultTo == self && r.requestCode == requestCode) {
2494 if ((r.resultWho == null && resultWho == null) ||
2495 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2496 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2497 false);
2498 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002499 }
2500 }
2501 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002502 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002503 }
2504
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002505 final void finishTopRunningActivityLocked(ProcessRecord app) {
2506 ActivityRecord r = topRunningActivityLocked(null);
2507 if (r != null && r.app == app) {
2508 // If the top running activity is from this crashing
2509 // process, then terminate it to avoid getting in a loop.
2510 Slog.w(TAG, " Force finishing activity "
2511 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002512 int taskNdx = mTaskHistory.indexOf(r.task);
2513 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002514 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002515 // Also terminate any activities below it that aren't yet
2516 // stopped, to avoid a situation where one will get
2517 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002518 --activityNdx;
2519 if (activityNdx < 0) {
2520 do {
2521 --taskNdx;
2522 if (taskNdx < 0) {
2523 break;
2524 }
2525 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2526 } while (activityNdx < 0);
2527 }
2528 if (activityNdx >= 0) {
2529 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002530 if (r.state == ActivityState.RESUMED
2531 || r.state == ActivityState.PAUSING
2532 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002533 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002534 Slog.w(TAG, " Force finishing activity "
2535 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002536 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002537 }
2538 }
2539 }
2540 }
2541 }
2542
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002543 final void finishVoiceTask(IVoiceInteractionSession session) {
2544 IBinder sessionBinder = session.asBinder();
2545 boolean didOne = false;
2546 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2547 TaskRecord tr = mTaskHistory.get(taskNdx);
2548 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2549 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2550 ActivityRecord r = tr.mActivities.get(activityNdx);
2551 if (!r.finishing) {
2552 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2553 false);
2554 didOne = true;
2555 }
2556 }
2557 }
2558 }
2559 if (didOne) {
2560 mService.updateOomAdjLocked();
2561 }
2562 }
2563
Craig Mautnerd2328952013-03-05 12:46:26 -08002564 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002565 ArrayList<ActivityRecord> activities = r.task.mActivities;
2566 for (int index = activities.indexOf(r); index >= 0; --index) {
2567 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002568 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002569 break;
2570 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002571 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002572 }
2573 return true;
2574 }
2575
Dianne Hackborn5c607432012-02-28 14:44:19 -08002576 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2577 // send the result
2578 ActivityRecord resultTo = r.resultTo;
2579 if (resultTo != null) {
2580 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2581 + " who=" + r.resultWho + " req=" + r.requestCode
2582 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002583 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002584 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002585 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002586 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002587 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002588 if (r.info.applicationInfo.uid > 0) {
2589 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2590 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002591 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002592 }
2593 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2594 resultData);
2595 r.resultTo = null;
2596 }
2597 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2598
2599 // Make sure this HistoryRecord is not holding on to other resources,
2600 // because clients have remote IPC references to this object so we
2601 // can't assume that will go away and want to avoid circular IPC refs.
2602 r.results = null;
2603 r.pendingResults = null;
2604 r.newIntents = null;
2605 r.icicle = null;
2606 }
2607
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002608 /**
2609 * @return Returns true if this activity has been removed from the history
2610 * list, or false if it is still in the list and will be removed later.
2611 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002612 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2613 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 if (r.finishing) {
2615 Slog.w(TAG, "Duplicate finish request for " + r);
2616 return false;
2617 }
2618
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002619 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002620 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002621 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002622 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002623 task.taskId, r.shortComponentName, reason);
2624 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002625 final int index = activities.indexOf(r);
2626 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002627 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002628 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002629 // If the caller asked that this activity (and all above it)
2630 // be cleared when the task is reset, don't lose that information,
2631 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002632 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002633 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002634 }
2635 }
2636
2637 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002638
2639 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640
Dianne Hackborn5c607432012-02-28 14:44:19 -08002641 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002642
Craig Mautnerde4ef022013-04-07 19:01:33 -07002643 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002644 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002645 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002647 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002648 ? AppTransition.TRANSIT_TASK_CLOSE
2649 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002650
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002651 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002652 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002653
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002654 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2656 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2657 startPausingLocked(false, false);
2658 }
2659
Craig Mautneraea74a52014-03-08 14:23:10 -08002660 if (endTask) {
2661 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2662 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 } else if (r.state != ActivityState.PAUSING) {
2664 // If the activity is PAUSING, we will complete the finish once
2665 // it is done pausing; else we can just directly finish it here.
2666 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002667 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002668 } else {
2669 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2670 }
2671
2672 return false;
2673 }
2674
Craig Mautnerf3333272013-04-22 10:55:53 -07002675 static final int FINISH_IMMEDIATELY = 0;
2676 static final int FINISH_AFTER_PAUSE = 1;
2677 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002678
Craig Mautnerf3333272013-04-22 10:55:53 -07002679 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002680 // First things first: if this activity is currently visible,
2681 // and the resumed activity is not yet visible, then hold off on
2682 // finishing until the resumed one becomes visible.
2683 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002684 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2685 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002686 if (mStackSupervisor.mStoppingActivities.size() > 3
2687 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688 // If we already have a few activities waiting to stop,
2689 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002690 // them out. Or if r is the last of activity of the last task the stack
2691 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002692 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002693 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002694 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 }
2696 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002697 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2698 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002700 if (oomAdj) {
2701 mService.updateOomAdjLocked();
2702 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002703 return r;
2704 }
2705
2706 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002707 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002708 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002709 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002710 if (mResumedActivity == r) {
2711 mResumedActivity = null;
2712 }
2713 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002714 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002715 r.state = ActivityState.FINISHING;
2716
2717 if (mode == FINISH_IMMEDIATELY
2718 || prevState == ActivityState.STOPPED
2719 || prevState == ActivityState.INITIALIZING) {
2720 // If this activity is already stopped, we can just finish
2721 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002722 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002723 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002724 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002725 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002726 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002727 if (DEBUG_CONTAINERS) Slog.d(TAG,
2728 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2729 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002730 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002732
2733 // Need to go through the full pause cycle to get this
2734 // activity into the stopped state and then finish it.
2735 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002736 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002737 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002738 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002739 return r;
2740 }
2741
Craig Mautneree36c772014-07-16 14:56:05 -07002742 void finishAllActivitiesLocked(boolean immediately) {
2743 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002744 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2745 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2746 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2747 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002748 noActivitiesInStack = false;
2749 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002750 continue;
2751 }
Craig Mautneree36c772014-07-16 14:56:05 -07002752 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002753 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2754 }
2755 }
Craig Mautneree36c772014-07-16 14:56:05 -07002756 if (noActivitiesInStack) {
2757 mActivityContainer.onTaskListEmptyLocked();
2758 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002759 }
2760
Craig Mautnerd2328952013-03-05 12:46:26 -08002761 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002762 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002763 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002764 final TaskRecord task = srec.task;
2765 final ArrayList<ActivityRecord> activities = task.mActivities;
2766 final int start = activities.indexOf(srec);
2767 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002768 return false;
2769 }
2770 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002771 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002772 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002773 final ComponentName dest = destIntent.getComponent();
2774 if (start > 0 && dest != null) {
2775 for (int i = finishTo; i >= 0; i--) {
2776 ActivityRecord r = activities.get(i);
2777 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002778 r.info.name.equals(dest.getClassName())) {
2779 finishTo = i;
2780 parent = r;
2781 foundParentInTask = true;
2782 break;
2783 }
2784 }
2785 }
2786
2787 IActivityController controller = mService.mController;
2788 if (controller != null) {
2789 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2790 if (next != null) {
2791 // ask watcher if this is allowed
2792 boolean resumeOK = true;
2793 try {
2794 resumeOK = controller.activityResuming(next.packageName);
2795 } catch (RemoteException e) {
2796 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002797 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002798 }
2799
2800 if (!resumeOK) {
2801 return false;
2802 }
2803 }
2804 }
2805 final long origId = Binder.clearCallingIdentity();
2806 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002807 ActivityRecord r = activities.get(i);
2808 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002809 // Only return the supplied result for the first activity finished
2810 resultCode = Activity.RESULT_CANCELED;
2811 resultData = null;
2812 }
2813
2814 if (parent != null && foundParentInTask) {
2815 final int parentLaunchMode = parent.info.launchMode;
2816 final int destIntentFlags = destIntent.getFlags();
2817 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2818 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2819 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2820 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2821 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2822 } else {
2823 try {
2824 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2825 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002826 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002827 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002828 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002829 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002830 foundParentInTask = res == ActivityManager.START_SUCCESS;
2831 } catch (RemoteException e) {
2832 foundParentInTask = false;
2833 }
2834 requestFinishActivityLocked(parent.appToken, resultCode,
2835 resultData, "navigate-up", true);
2836 }
2837 }
2838 Binder.restoreCallingIdentity(origId);
2839 return foundParentInTask;
2840 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002841 /**
2842 * Perform the common clean-up of an activity record. This is called both
2843 * as part of destroyActivityLocked() (when destroying the client-side
2844 * representation) and cleaning things up as a result of its hosting
2845 * processing going away, in which case there is no remaining client-side
2846 * state to destroy so only the cleanup here is needed.
2847 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002848 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2849 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002850 if (mResumedActivity == r) {
2851 mResumedActivity = null;
2852 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002853 if (mPausingActivity == r) {
2854 mPausingActivity = null;
2855 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002856 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857
2858 r.configDestroy = false;
2859 r.frozenBeforeDestroy = false;
2860
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002861 if (setState) {
2862 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2863 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002864 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002865 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002866 }
2867
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 // Make sure this record is no longer in the pending finishes list.
2869 // This could happen, for example, if we are trimming activities
2870 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002871 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002872 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautneree36c772014-07-16 14:56:05 -07002873 mStackSupervisor.removePendingActivityLaunchesLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002874
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 // Remove any pending results.
2876 if (r.finishing && r.pendingResults != null) {
2877 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2878 PendingIntentRecord rec = apr.get();
2879 if (rec != null) {
2880 mService.cancelIntentSenderLocked(rec, false);
2881 }
2882 }
2883 r.pendingResults = null;
2884 }
2885
2886 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002887 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002888 }
2889
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002891 removeTimeoutsForActivityLocked(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002892 if (getMediaPlayer() == r) {
2893 mStackSupervisor.setMediaPlayingLocked(r, false);
2894 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002895 }
2896
2897 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002898 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002900 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002901 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002902 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002903 }
2904
Craig Mautner04a0ea62014-01-13 12:51:26 -08002905 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002906 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002907 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2908 r.makeFinishing();
2909 if (DEBUG_ADD_REMOVE) {
2910 RuntimeException here = new RuntimeException("here");
2911 here.fillInStackTrace();
2912 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002914 r.takeFromHistory();
2915 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002916 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002917 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002918 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002919 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002920 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002921 if (VALIDATE_TOKENS) {
2922 validateAppTokensLocked();
2923 }
Craig Mautner312ba862014-02-10 17:55:01 -08002924 final TaskRecord task = r.task;
2925 if (task != null && task.removeActivity(r)) {
2926 if (DEBUG_STACK) Slog.i(TAG,
2927 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07002928 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
2929 task.isOverHomeStack()) {
2930 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08002931 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002932 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08002933 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002934 cleanUpActivityServicesLocked(r);
2935 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002937
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 /**
2939 * Perform clean-up of service connections in an activity record.
2940 */
2941 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2942 // Throw away any services that have been bound by this activity.
2943 if (r.connections != null) {
2944 Iterator<ConnectionRecord> it = r.connections.iterator();
2945 while (it.hasNext()) {
2946 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002947 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 }
2949 r.connections = null;
2950 }
2951 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002952
Craig Mautneree2e45a2014-06-27 12:10:03 -07002953 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002954 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002955 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002956 mHandler.sendMessage(msg);
2957 }
2958
Craig Mautneree2e45a2014-06-27 12:10:03 -07002959 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002960 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002961 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002962 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2963 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2964 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2965 final ActivityRecord r = activities.get(activityNdx);
2966 if (r.finishing) {
2967 continue;
2968 }
2969 if (r.fullscreen) {
2970 lastIsOpaque = true;
2971 }
2972 if (owner != null && r.app != owner) {
2973 continue;
2974 }
2975 if (!lastIsOpaque) {
2976 continue;
2977 }
2978 // We can destroy this one if we have its icicle saved and
2979 // it is not in the process of pausing/stopping/finishing.
2980 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2981 && r.haveState && !r.visible && r.stopped
2982 && r.state != ActivityState.DESTROYING
2983 && r.state != ActivityState.DESTROYED) {
2984 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2985 + " resumed=" + mResumedActivity
2986 + " pausing=" + mPausingActivity);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002987 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002988 activityRemoved = true;
2989 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002990 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002991 }
2992 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002993 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002994 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002995 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002996 }
2997
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 /**
2999 * Destroy the current CLIENT SIDE instance of an activity. This may be
3000 * called both when actually finishing an activity, or when performing
3001 * a configuration switch where we destroy the current client-side object
3002 * but then create a new client-side object for this same HistoryRecord.
3003 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003004 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003005 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003006 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3008 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003009 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003010 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011
3012 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003013
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003014 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015
3016 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003017
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003018 if (hadApp) {
3019 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003020 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3022 mService.mHeavyWeightProcess = null;
3023 mService.mHandler.sendEmptyMessage(
3024 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3025 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003026 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003027 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003028 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003029 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003030 }
3031 }
3032
3033 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003034
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 try {
3036 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003037 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 r.configChangeFlags);
3039 } catch (Exception e) {
3040 // We can just ignore exceptions here... if the process
3041 // has crashed, our death notification will clean things
3042 // up.
3043 //Slog.w(TAG, "Exception thrown during finish", e);
3044 if (r.finishing) {
3045 removeActivityFromHistoryLocked(r);
3046 removedFromHistory = true;
3047 skipDestroy = true;
3048 }
3049 }
3050
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003051 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003052
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003053 // If the activity is finishing, we need to wait on removing it
3054 // from the list to give it a chance to do its cleanup. During
3055 // that time it may make calls back with its token so we need to
3056 // be able to find it on the list and so we don't want to remove
3057 // it from the list yet. Otherwise, we can just immediately put
3058 // it in the destroyed state since we are not removing it from the
3059 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003061 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3062 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003063 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003064 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003065 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3066 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003067 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003068 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003069 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003070 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 }
3072 } else {
3073 // remove this record from the history.
3074 if (r.finishing) {
3075 removeActivityFromHistoryLocked(r);
3076 removedFromHistory = true;
3077 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003078 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003080 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003081 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 }
3083 }
3084
3085 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 if (!mLRUActivities.remove(r) && hadApp) {
3088 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3089 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003090
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003091 return removedFromHistory;
3092 }
3093
Craig Mautnerd2328952013-03-05 12:46:26 -08003094 final void activityDestroyedLocked(IBinder token) {
3095 final long origId = Binder.clearCallingIdentity();
3096 try {
3097 ActivityRecord r = ActivityRecord.forToken(token);
3098 if (r != null) {
3099 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003101 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003102
3103 if (isInStackLocked(token) != null) {
3104 if (r.state == ActivityState.DESTROYING) {
3105 cleanUpActivityLocked(r, true, false);
3106 removeActivityFromHistoryLocked(r);
3107 }
3108 }
Craig Mautner05d29032013-05-03 13:40:13 -07003109 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003110 } finally {
3111 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112 }
3113 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003114
Craig Mautneree2e45a2014-06-27 12:10:03 -07003115 void releaseMediaResources() {
3116 if (isMediaPlaying() && !mHandler.hasMessages(STOP_MEDIA_PLAYING_TIMEOUT_MSG)) {
3117 final ActivityRecord r = getMediaPlayer();
3118 if (DEBUG_STATES) Slog.d(TAG, "releaseMediaResources activtyDisplay=" +
3119 mActivityContainer.mActivityDisplay + " mediaPlayer=" + r + " app=" + r.app +
3120 " thread=" + r.app.thread);
3121 if (r != null && r.app != null && r.app.thread != null) {
3122 try {
3123 r.app.thread.scheduleStopMediaPlaying(r.appToken);
3124 } catch (RemoteException e) {
3125 }
3126 mHandler.sendEmptyMessageDelayed(STOP_MEDIA_PLAYING_TIMEOUT_MSG, 500);
3127 } else {
3128 Slog.e(TAG, "releaseMediaResources: activity " + r + " no longer running");
3129 mediaResourcesReleased(r.appToken);
3130 }
3131 }
3132 }
3133
3134 final void mediaResourcesReleased(IBinder token) {
3135 mHandler.removeMessages(STOP_MEDIA_PLAYING_TIMEOUT_MSG);
3136 final ActivityRecord r = getMediaPlayer();
3137 if (r != null) {
3138 mStackSupervisor.mStoppingActivities.add(r);
3139 setMediaPlayer(null);
3140 }
3141 mStackSupervisor.resumeTopActivitiesLocked();
3142 }
3143
3144 boolean isMediaPlaying() {
3145 return isAttached() && mActivityContainer.mActivityDisplay.isMediaPlaying();
3146 }
3147
3148 void setMediaPlayer(ActivityRecord r) {
3149 if (isAttached()) {
3150 mActivityContainer.mActivityDisplay.setMediaPlaying(r);
3151 }
3152 }
3153
3154 ActivityRecord getMediaPlayer() {
3155 return isAttached() ? mActivityContainer.mActivityDisplay.mMediaPlayingActivity : null;
3156 }
3157
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003158 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3159 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003161 if (DEBUG_CLEANUP) Slog.v(
3162 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 + " with " + i + " entries");
3164 while (i > 0) {
3165 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003166 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003167 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003169 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003170 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003171 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 }
3173 }
3174 }
3175
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003176 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3177 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003178 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3179 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003180 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3181 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003182 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003183 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003184 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3185 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003186
3187 boolean hasVisibleActivities = false;
3188
3189 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003190 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003191 if (DEBUG_CLEANUP) Slog.v(
3192 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003193 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3194 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3195 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3196 final ActivityRecord r = activities.get(activityNdx);
3197 --i;
3198 if (DEBUG_CLEANUP) Slog.v(
3199 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3200 if (r.app == app) {
3201 boolean remove;
3202 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3203 // Don't currently have state for the activity, or
3204 // it is finishing -- always remove it.
3205 remove = true;
3206 } else if (r.launchCount > 2 &&
3207 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3208 // We have launched this activity too many times since it was
3209 // able to run, so give up and remove it.
3210 remove = true;
3211 } else {
3212 // The process may be gone, but the activity lives on!
3213 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003214 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003215 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003216 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003217 RuntimeException here = new RuntimeException("here");
3218 here.fillInStackTrace();
3219 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3220 + ": haveState=" + r.haveState
3221 + " stateNotNeeded=" + r.stateNotNeeded
3222 + " finishing=" + r.finishing
3223 + " state=" + r.state, here);
3224 }
3225 if (!r.finishing) {
3226 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3227 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3228 r.userId, System.identityHashCode(r),
3229 r.task.taskId, r.shortComponentName,
3230 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003231 if (r.state == ActivityState.RESUMED) {
3232 mService.updateUsageStats(r, false);
3233 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003234 }
3235 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003236
Craig Mautner0247fc82013-02-28 14:32:06 -08003237 } else {
3238 // We have the current state for this activity, so
3239 // it can be restarted later when needed.
3240 if (localLOGV) Slog.v(
3241 TAG, "Keeping entry, setting app to null");
3242 if (r.visible) {
3243 hasVisibleActivities = true;
3244 }
3245 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3246 + r);
3247 r.app = null;
3248 r.nowVisible = false;
3249 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003250 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003251 "App died, clearing saved state of " + r);
3252 r.icicle = null;
3253 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003254 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003255
Craig Mautnerd2328952013-03-05 12:46:26 -08003256 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003257 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003258 }
3259 }
3260
3261 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003262 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003263
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003264 final void updateTransitLocked(int transit, Bundle options) {
3265 if (options != null) {
3266 ActivityRecord r = topRunningActivityLocked(null);
3267 if (r != null && r.state != ActivityState.RESUMED) {
3268 r.updateOptionsLocked(options);
3269 } else {
3270 ActivityOptions.abort(options);
3271 }
3272 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003273 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003274 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003275
Craig Mautner21d24a22014-04-23 11:45:37 -07003276 void updateTaskMovement(TaskRecord task, boolean toFront) {
3277 if (task.isPersistable) {
3278 task.mLastTimeMoved = System.currentTimeMillis();
3279 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3280 // recently will be most negative, tasks sent to the bottom before that will be less
3281 // negative. Similarly for recent tasks moved to the top which will be most positive.
3282 if (!toFront) {
3283 task.mLastTimeMoved *= -1;
3284 }
3285 }
3286 }
3287
Craig Mautner84984fa2014-06-19 11:19:20 -07003288 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003289 final int top = mTaskHistory.size() - 1;
3290 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3291 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003292 if (task.taskType == homeStackTaskType) {
3293 if (DEBUG_TASKS || DEBUG_STACK)
3294 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003295 mTaskHistory.remove(taskNdx);
3296 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003297 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003298 mWindowManager.moveTaskToTop(task.taskId);
3299 return;
3300 }
3301 }
3302 }
3303
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003304 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003305 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003306
Craig Mautner11bf9a52013-02-19 14:08:51 -08003307 final int numTasks = mTaskHistory.size();
3308 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003309 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003310 // nothing to do!
3311 if (reason != null &&
3312 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3313 ActivityOptions.abort(options);
3314 } else {
3315 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3316 }
3317 return;
3318 }
3319
Craig Mautnere0a38842013-12-16 16:14:02 -08003320 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003321
Craig Mautner11bf9a52013-02-19 14:08:51 -08003322 // Shift all activities with this task up to the top
3323 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003324 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003325
3326 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003327 if (reason != null &&
3328 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003329 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 ActivityRecord r = topRunningActivityLocked(null);
3331 if (r != null) {
3332 mNoAnimActivities.add(r);
3333 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003334 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003336 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003337 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003338
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003339 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003340
Craig Mautner05d29032013-05-03 13:40:13 -07003341 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003342 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003343
3344 if (VALIDATE_TOKENS) {
3345 validateAppTokensLocked();
3346 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 }
3348
3349 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003350 * Worker method for rearranging history stack. Implements the function of moving all
3351 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003353 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003354 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3355 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003356 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003357 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 * @return Returns true if the move completed, false if not.
3359 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003360 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003361 final TaskRecord tr = taskForIdLocked(taskId);
3362 if (tr == null) {
3363 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3364 return false;
3365 }
3366
3367 Slog.i(TAG, "moveTaskToBack: " + tr);
3368
3369 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003371 // If we have a watcher, preflight the move before committing to it. First check
3372 // for *other* available tasks, but if none are available, then try again allowing the
3373 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003374 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003375 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003376 if (next == null) {
3377 next = topRunningActivityLocked(null, 0);
3378 }
3379 if (next != null) {
3380 // ask watcher if this is allowed
3381 boolean moveOK = true;
3382 try {
3383 moveOK = mService.mController.activityResuming(next.packageName);
3384 } catch (RemoteException e) {
3385 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003386 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003387 }
3388 if (!moveOK) {
3389 return false;
3390 }
3391 }
3392 }
3393
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003394 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003395 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396
Craig Mautner11bf9a52013-02-19 14:08:51 -08003397 mTaskHistory.remove(tr);
3398 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003399 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003400
Craig Mautnerc8143c62013-09-03 12:15:57 -07003401 // There is an assumption that moving a task to the back moves it behind the home activity.
3402 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003403 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003404 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3405 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003406 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003407 break;
3408 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003409 if (taskNdx == 1) {
3410 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003411 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003412 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003413 }
3414
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003415 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003416 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3417 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003418 ActivityRecord r = topRunningActivityLocked(null);
3419 if (r != null) {
3420 mNoAnimActivities.add(r);
3421 }
3422 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003423 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003424 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003425 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003426
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003428 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003429 }
3430
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003431 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003432 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
3433 final int taskToReturnTo = tr.getTaskToReturnTo();
3434 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3435 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003436 }
3437
Craig Mautner05d29032013-05-03 13:40:13 -07003438 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003439 return true;
3440 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003441
Craig Mautner8849a5e2013-04-02 16:41:03 -07003442 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003443 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003444 final Uri data = r.intent.getData();
3445 final String strData = data != null ? data.toSafeString() : null;
3446
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003447 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003448 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003449 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003450 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 }
3452
3453 /**
3454 * Make sure the given activity matches the current configuration. Returns
3455 * false if the activity had to be destroyed. Returns true if the
3456 * configuration is the same, or the activity will remain running as-is
3457 * for whatever reason. Ensures the HistoryRecord is updated with the
3458 * correct configuration and all other bookkeeping is handled.
3459 */
3460 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3461 int globalChanges) {
3462 if (mConfigWillChange) {
3463 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3464 "Skipping config check (will change): " + r);
3465 return true;
3466 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003467
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003468 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3469 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003470
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 // Short circuit: if the two configurations are the exact same
3472 // object (the common case), then there is nothing to do.
3473 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003474 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003475 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3476 "Configuration unchanged in " + r);
3477 return true;
3478 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003479
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480 // We don't worry about activities that are finishing.
3481 if (r.finishing) {
3482 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3483 "Configuration doesn't matter in finishing " + r);
3484 r.stopFreezingScreenLocked(false);
3485 return true;
3486 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003487
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003488 // Okay we now are going to make this activity have the new config.
3489 // But then we need to figure out how it needs to deal with that.
3490 Configuration oldConfig = r.configuration;
3491 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003492
3493 // Determine what has changed. May be nothing, if this is a config
3494 // that has come back from the app after going idle. In that case
3495 // we just want to leave the official config object now in the
3496 // activity and do nothing else.
3497 final int changes = oldConfig.diff(newConfig);
3498 if (changes == 0 && !r.forceNewConfig) {
3499 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3500 "Configuration no differences in " + r);
3501 return true;
3502 }
3503
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003504 // If the activity isn't currently running, just leave the new
3505 // configuration and it will pick that up next time it starts.
3506 if (r.app == null || r.app.thread == null) {
3507 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3508 "Configuration doesn't matter not running " + r);
3509 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003510 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003511 return true;
3512 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003513
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003514 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003515 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3516 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3517 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003518 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003519 + ", newConfig=" + newConfig);
3520 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003521 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003522 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3523 r.configChangeFlags |= changes;
3524 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003525 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003526 if (r.app == null || r.app.thread == null) {
3527 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003528 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003529 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003530 } else if (r.state == ActivityState.PAUSING) {
3531 // A little annoying: we are waiting for this activity to
3532 // finish pausing. Let's not do anything now, but just
3533 // flag that it needs to be restarted when done pausing.
3534 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003535 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003536 r.configDestroy = true;
3537 return true;
3538 } else if (r.state == ActivityState.RESUMED) {
3539 // Try to optimize this case: the configuration is changing
3540 // and we need to restart the top, resumed activity.
3541 // Instead of doing the normal handshaking, just say
3542 // "restart!".
3543 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003544 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003545 relaunchActivityLocked(r, r.configChangeFlags, true);
3546 r.configChangeFlags = 0;
3547 } else {
3548 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003549 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003550 relaunchActivityLocked(r, r.configChangeFlags, false);
3551 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003552 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003553
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003554 // All done... tell the caller we weren't able to keep this
3555 // activity around.
3556 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003558
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 // Default case: the activity can handle this new configuration, so
3560 // hand it over. Note that we don't need to give it the new
3561 // configuration, since we always send configuration changes to all
3562 // process when they happen so it can just use whatever configuration
3563 // it last got.
3564 if (r.app != null && r.app.thread != null) {
3565 try {
3566 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003567 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 } catch (RemoteException e) {
3569 // If process died, whatever.
3570 }
3571 }
3572 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003573
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 return true;
3575 }
3576
Craig Mautnerc8143c62013-09-03 12:15:57 -07003577 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 int changes, boolean andResume) {
3579 List<ResultInfo> results = null;
3580 List<Intent> newIntents = null;
3581 if (andResume) {
3582 results = r.results;
3583 newIntents = r.newIntents;
3584 }
3585 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3586 + " with results=" + results + " newIntents=" + newIntents
3587 + " andResume=" + andResume);
3588 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003589 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003590 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003591
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003592 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003593
Craig Mautner34b73df2014-01-12 21:11:08 -08003594 mStackSupervisor.removeChildActivityContainers(r);
3595
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003596 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003597 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3598 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3599 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003600 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003601 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003602 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003603 // Note: don't need to call pauseIfSleepingLocked() here, because
3604 // the caller will only pass in 'andResume' if this activity is
3605 // currently resumed, which implies we aren't sleeping.
3606 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003607 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 }
3609
3610 if (andResume) {
3611 r.results = null;
3612 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003613 r.state = ActivityState.RESUMED;
3614 } else {
3615 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3616 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 }
3618
3619 return true;
3620 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003621
3622 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003623 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3624 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3625 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3626 final ActivityRecord r = activities.get(activityNdx);
3627 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003628 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003629 }
3630 if (r.fullscreen && !r.finishing) {
3631 return false;
3632 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003633 }
3634 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003635 final ActivityRecord r = ActivityRecord.forToken(token);
3636 if (r == null) {
3637 return false;
3638 }
3639 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3640 + " would have returned true for r=" + r);
3641 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003642 }
3643
3644 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003645 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3646 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3647 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3648 final ActivityRecord r = activities.get(activityNdx);
3649 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003650 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003651 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003652 }
3653 }
3654 }
3655
3656 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3657 boolean didSomething = false;
3658 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003659 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003660 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3661 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3662 int numActivities = activities.size();
3663 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3664 ActivityRecord r = activities.get(activityNdx);
3665 final boolean samePackage = r.packageName.equals(name)
3666 || (name == null && r.userId == userId);
3667 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3668 && (samePackage || r.task == lastTask)
3669 && (r.app == null || evenPersistent || !r.app.persistent)) {
3670 if (!doit) {
3671 if (r.finishing) {
3672 // If this activity is just finishing, then it is not
3673 // interesting as far as something to stop.
3674 continue;
3675 }
3676 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003677 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003678 if (r.isHomeActivity()) {
3679 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3680 Slog.i(TAG, "Skip force-stop again " + r);
3681 continue;
3682 } else {
3683 homeActivity = r.realActivity;
3684 }
3685 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003686 didSomething = true;
3687 Slog.i(TAG, " Force finishing activity " + r);
3688 if (samePackage) {
3689 if (r.app != null) {
3690 r.app.removed = true;
3691 }
3692 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003693 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003694 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003695 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3696 true)) {
3697 // r has been deleted from mActivities, accommodate.
3698 --numActivities;
3699 --activityNdx;
3700 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003701 }
3702 }
3703 }
3704 return didSomething;
3705 }
3706
Dianne Hackborn09233282014-04-30 11:33:59 -07003707 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003708 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003709 final TaskRecord task = mTaskHistory.get(taskNdx);
3710 ActivityRecord r = null;
3711 ActivityRecord top = null;
3712 int numActivities = 0;
3713 int numRunning = 0;
3714 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003715 if (activities.isEmpty()) {
3716 continue;
3717 }
Dianne Hackborn09233282014-04-30 11:33:59 -07003718 if (!allowed && !task.isHomeTask() && task.creatorUid != callingUid) {
3719 continue;
3720 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003721 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3722 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003723
Craig Mautneraab647e2013-02-28 16:31:36 -08003724 // Initialize state for next task if needed.
3725 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3726 top = r;
3727 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003728 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003729
3730 // Add 'r' into the current task.
3731 numActivities++;
3732 if (r.app != null && r.app.thread != null) {
3733 numRunning++;
3734 }
3735
3736 if (localLOGV) Slog.v(
3737 TAG, r.intent.getComponent().flattenToShortString()
3738 + ": task=" + r.task);
3739 }
3740
3741 RunningTaskInfo ci = new RunningTaskInfo();
3742 ci.id = task.taskId;
3743 ci.baseActivity = r.intent.getComponent();
3744 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003745 ci.lastActiveTime = task.lastActiveTime;
3746
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003747 if (top.task != null) {
3748 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003749 }
3750 ci.numActivities = numActivities;
3751 ci.numRunning = numRunning;
3752 //System.out.println(
3753 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003754 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003755 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003756 }
3757
3758 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003759 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003760 if (DEBUG_SWITCH) Slog.d(
3761 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003762 if (top >= 0) {
3763 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3764 int activityTop = activities.size() - 1;
3765 if (activityTop > 0) {
3766 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3767 "unhandled-back", true);
3768 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003769 }
3770 }
3771
Craig Mautner6b74cb52013-09-27 17:02:21 -07003772 /**
3773 * Reset local parameters because an app's activity died.
3774 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003775 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003776 */
3777 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003778 if (mPausingActivity != null && mPausingActivity.app == app) {
3779 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3780 "App died while pausing: " + mPausingActivity);
3781 mPausingActivity = null;
3782 }
3783 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3784 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003785 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003786 }
3787
Craig Mautner19091252013-10-05 00:03:53 -07003788 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003789 }
3790
Craig Mautnercae015f2013-02-08 14:31:27 -08003791 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003792 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3793 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3794 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3795 final ActivityRecord r = activities.get(activityNdx);
3796 if (r.app == app) {
3797 Slog.w(TAG, " Force finishing activity "
3798 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07003799 // Force the destroy to skip right to removal.
3800 r.app = null;
3801 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003802 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003803 }
3804 }
3805 }
3806
Dianne Hackborn390517b2013-05-30 15:03:32 -07003807 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003808 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003809 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003810 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3811 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003812 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3813 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003814 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003815 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003816 if (printed) {
3817 header = null;
3818 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003819 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003820 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003821 }
3822
3823 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3824 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3825
3826 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003827 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3828 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003829 }
3830 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003831 final int top = mTaskHistory.size() - 1;
3832 if (top >= 0) {
3833 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3834 int listTop = list.size() - 1;
3835 if (listTop >= 0) {
3836 activities.add(list.get(listTop));
3837 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003838 }
3839 } else {
3840 ItemMatcher matcher = new ItemMatcher();
3841 matcher.build(name);
3842
Craig Mautneraab647e2013-02-28 16:31:36 -08003843 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3844 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3845 if (matcher.match(r1, r1.intent.getComponent())) {
3846 activities.add(r1);
3847 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003848 }
3849 }
3850 }
3851
3852 return activities;
3853 }
3854
3855 ActivityRecord restartPackage(String packageName) {
3856 ActivityRecord starting = topRunningActivityLocked(null);
3857
3858 // All activities that came from the package must be
3859 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003860 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3861 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3862 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3863 final ActivityRecord a = activities.get(activityNdx);
3864 if (a.info.packageName.equals(packageName)) {
3865 a.forceNewConfig = true;
3866 if (starting != null && a == starting && a.visible) {
3867 a.startFreezingScreenLocked(starting.app,
3868 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3869 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003870 }
3871 }
3872 }
3873
3874 return starting;
3875 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003876
Craig Mautner41db4a72014-05-07 17:20:56 -07003877 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003878 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003879 mWindowManager.removeTask(task.taskId);
3880 final ActivityRecord r = mResumedActivity;
3881 if (r != null && r.task == task) {
3882 mResumedActivity = null;
3883 }
3884
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003885 final int taskNdx = mTaskHistory.indexOf(task);
3886 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07003887 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
3888 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
3889 if (!nextTask.isOverHomeStack()) {
3890 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
3891 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003892 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003893 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003894 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07003895
3896 if (task.mActivities.isEmpty()) {
3897 final boolean isVoiceSession = task.voiceSession != null;
3898 if (isVoiceSession) {
3899 try {
3900 task.voiceSession.taskFinished(task.intent, task.taskId);
3901 } catch (RemoteException e) {
3902 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003903 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003904 if (task.autoRemoveFromRecents() || isVoiceSession) {
3905 // Task creator asked to remove this when done, or this task was a voice
3906 // interaction, so it should not remain on the recent tasks list.
3907 mService.mRecentTasks.remove(task);
Craig Mautnera228ae92014-07-09 05:44:55 -07003908 task.closeRecentsChain();
Craig Mautner41db4a72014-05-07 17:20:56 -07003909 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003910 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003911
3912 if (mTaskHistory.isEmpty()) {
3913 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3914 if (isOnHomeDisplay()) {
3915 mStackSupervisor.moveHomeStack(!isHomeStack());
3916 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003917 if (mStacks != null) {
3918 mStacks.remove(this);
3919 mStacks.add(0, this);
3920 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003921 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08003922 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003923 }
3924
Dianne Hackborn91097de2014-04-04 18:02:06 -07003925 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
3926 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
3927 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07003928 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
3929 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003930 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003931 return task;
3932 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003933
3934 ArrayList<TaskRecord> getAllTasks() {
3935 return new ArrayList<TaskRecord>(mTaskHistory);
3936 }
3937
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003938 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003939 task.stack = this;
3940 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003941 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003942 } else {
3943 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003944 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003945 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003946 if (!moving && task.voiceSession != null) {
3947 try {
3948 task.voiceSession.taskStarted(task.intent, task.taskId);
3949 } catch (RemoteException e) {
3950 }
3951 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003952 }
3953
3954 public int getStackId() {
3955 return mStackId;
3956 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003957
3958 @Override
3959 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003960 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3961 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003962 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003963}