blob: b8e59ac3e15c34827e7e29e82fd0571021b68499 [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
Wale Ogunwalee23149f2015-03-06 15:39:44 -080019import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070020import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
21import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
22import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
23import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
24import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
25import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
26import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
27import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
28import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
29import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
30
Craig Mautner84984fa2014-06-19 11:19:20 -070031import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070033
Craig Mautner0eea92c2013-05-16 13:35:39 -070034import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
35import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070037import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070041import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
42
Dianne Hackborn89ad4562014-08-24 16:45:38 -070043import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070044import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080045import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070047import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080048import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080049import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080050import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070052import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053
54import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070055import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070056import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080058import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080060import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080066import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080067import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070069import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080070import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Handler;
72import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090073import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070075import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.RemoteException;
77import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080078import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070079import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070080import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070083import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084
Craig Mautnercae015f2013-02-08 14:31:27 -080085import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080086import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087import java.lang.ref.WeakReference;
88import java.util.ArrayList;
89import java.util.Iterator;
90import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080091import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092
93/**
94 * State and management of a single stack of activities.
95 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070096final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080097
Wale Ogunwalee23149f2015-03-06 15:39:44 -080098 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
99 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
100
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700101 // Ticks during which we check progress while waiting for an app to launch.
102 static final int LAUNCH_TICK = 500;
103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104 // How long we wait until giving up on the last activity to pause. This
105 // is short because it directly impacts the responsiveness of starting the
106 // next activity.
107 static final int PAUSE_TIMEOUT = 500;
108
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700109 // How long we wait for the activity to tell us it has stopped before
110 // giving up. This is a good amount of time because we really need this
111 // from the application in order to get its saved state.
112 static final int STOP_TIMEOUT = 10*1000;
113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700114 // How long we wait until giving up on an activity telling us it has
115 // finished destroying itself.
116 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800119 // disabled.
120 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800121
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700122 // How long between activity launches that we consider safe to not warn
123 // the user about an unexpected activity being launched on top.
124 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700126 // Set to false to disable the preview that is shown while a new activity
127 // is being started.
128 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800129
Craig Mautner5eda9b32013-07-02 11:58:16 -0700130 // How long to wait for all background Activities to redraw following a call to
131 // convertToTranslucent().
132 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
133
Craig Mautnera61bc652013-10-28 15:43:18 -0700134 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700136 enum ActivityState {
137 INITIALIZING,
138 RESUMED,
139 PAUSING,
140 PAUSED,
141 STOPPING,
142 STOPPED,
143 FINISHING,
144 DESTROYING,
145 DESTROYED
146 }
147
148 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700149 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800150 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700152 /**
153 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800154 * running) activities. It contains #TaskRecord objects.
155 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800156 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800157
158 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159 * Used for validating app tokens with window manager.
160 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800161 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800162
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700163 /**
164 * List of running activities, sorted by recent usage.
165 * The first entry in the list is the least recently used.
166 * It contains HistoryRecord objects.
167 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800168 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169
170 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171 * Animations that for the current transition have requested not to
172 * be considered for the transition animation.
173 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800174 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175
176 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177 * When we are in the process of pausing an activity, before starting the
178 * next one, this variable holds the activity that is currently being paused.
179 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800180 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181
182 /**
183 * This is the last activity that we put into the paused state. This is
184 * used to determine if we need to do an activity transition while sleeping,
185 * when we normally hold the top activity paused.
186 */
187 ActivityRecord mLastPausedActivity = null;
188
189 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700190 * Activities that specify No History must be removed once the user navigates away from them.
191 * If the device goes to sleep with such an activity in the paused state then we save it here
192 * and finish it later if another activity replaces it on wakeup.
193 */
194 ActivityRecord mLastNoHistoryActivity = null;
195
196 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700197 * Current activity that is resumed, or null if there is none.
198 */
199 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700201 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700202 * This is the last activity that has been started. It is only used to
203 * identify when multiple activities are started at once so that the user
204 * can be warned they may not be in the activity they think they are.
205 */
206 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800207
Craig Mautner5eda9b32013-07-02 11:58:16 -0700208 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
209 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
210 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
211 // Activity in mTranslucentActivityWaiting is notified via
212 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
213 // background activity being drawn then the same call will be made with a true value.
214 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700215 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700216 new ArrayList<ActivityRecord>();
217
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700218 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700219 * Set when we know we are going to be calling updateConfiguration()
220 * soon, so want to skip intermediate config checks.
221 */
222 boolean mConfigWillChange;
223
Todd Kennedyaab56db2015-01-30 09:39:53 -0800224 // Whether or not this stack covers the entire screen; by default stacks are full screen
225 boolean mFullscreen = true;
226
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700227 long mLaunchStartTime = 0;
228 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800229
Craig Mautner858d8a62013-04-23 17:08:34 -0700230 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700231
Craig Mautnerc00204b2013-03-05 15:02:14 -0800232 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800233 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800234 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
235 ArrayList<ActivityStack> mStacks;
236 /** The attached Display's unique identifier, or -1 if detached */
237 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800238
Craig Mautner27084302013-03-25 08:05:25 -0700239 /** Run all ActivityStacks through this */
240 final ActivityStackSupervisor mStackSupervisor;
241
Wale Ogunwale60454db2015-01-23 16:05:07 -0800242 Configuration mOverrideConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800243 /** True if the stack was forced to full screen because {@link TaskRecord#mResizeable} is false
244 * and the stack was previously resized. */
245 private boolean mForcedFullscreen = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800246
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700247 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700248 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
249 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
250 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
251 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700252 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700253 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700254 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700255
256 static class ScheduleDestroyArgs {
257 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700258 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700259 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700261 mReason = reason;
262 }
263 }
264
Zoran Marcetaf958b322012-08-09 20:27:12 +0900265 final Handler mHandler;
266
267 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800268
Craig Mautnerc8143c62013-09-03 12:15:57 -0700269 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900270 super(looper);
271 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272
Zoran Marcetaf958b322012-08-09 20:27:12 +0900273 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274 public void handleMessage(Message msg) {
275 switch (msg.what) {
276 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800277 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 // We don't at this point know if the activity is fullscreen,
279 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800280 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700281 synchronized (mService) {
282 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700283 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700285 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800286 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700288 case LAUNCH_TICK_MSG: {
289 ActivityRecord r = (ActivityRecord)msg.obj;
290 synchronized (mService) {
291 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700292 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700293 }
294 }
295 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800297 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 // We don't at this point know if the activity is fullscreen,
299 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800300 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800301 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800302 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800303 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700305 case STOP_TIMEOUT_MSG: {
306 ActivityRecord r = (ActivityRecord)msg.obj;
307 // We don't at this point know if the activity is fullscreen,
308 // so we need to be conservative and assume it isn't.
309 Slog.w(TAG, "Activity stop timeout for " + r);
310 synchronized (mService) {
311 if (r.isInHistory()) {
312 activityStoppedLocked(r, null, null, null);
313 }
314 }
315 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700316 case DESTROY_ACTIVITIES_MSG: {
317 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
318 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700319 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700320 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700321 } break;
322 case TRANSLUCENT_TIMEOUT_MSG: {
323 synchronized (mService) {
324 notifyActivityDrawnLocked(null);
325 }
326 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700327 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700328 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700329 final ActivityRecord r = getVisibleBehindActivity();
330 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700331 if (r != null) {
332 mService.killAppAtUsersRequest(r.app, null);
333 }
334 }
335 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 }
337 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800338 }
339
Craig Mautner34b73df2014-01-12 21:11:08 -0800340 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800341 int count = 0;
342 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
343 count += mTaskHistory.get(taskNdx).mActivities.size();
344 }
345 return count;
346 }
347
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800348 int numTasks() {
349 return mTaskHistory.size();
350 }
351
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800352 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
353 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800354 mActivityContainer = activityContainer;
355 mStackSupervisor = activityContainer.getOuter();
356 mService = mStackSupervisor.mService;
357 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
358 mWindowManager = mService.mWindowManager;
359 mStackId = activityContainer.mStackId;
360 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800361 mRecentTasks = recentTasks;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800362 mOverrideConfig = Configuration.EMPTY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700363 }
Craig Mautner5962b122012-10-05 14:45:52 -0700364
Amith Yamasani734983f2014-03-04 16:48:05 -0800365 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100366 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800367 */
Kenny Guy2a764942014-04-02 13:29:20 +0100368 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100369 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100370 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
371 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800372 }
373 return false;
374 }
375
376 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100377 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700378 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
379 }
380
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700381 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800382 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700383 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
384 if (r != null) {
385 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800386 }
387 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 return null;
389 }
390
391 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800392 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
393 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800394 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800395 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
396 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800397 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800398 return r;
399 }
400 }
401 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700402 return null;
403 }
404
405 /**
406 * This is a simplified version of topRunningActivityLocked that provides a number of
407 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800408 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700409 * @param token If non-null, any history records matching this token will be skipped.
410 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800411 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 * @return Returns the HistoryRecord of the next activity on the stack.
413 */
414 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800415 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
416 TaskRecord task = mTaskHistory.get(taskNdx);
417 if (task.taskId == taskId) {
418 continue;
419 }
420 ArrayList<ActivityRecord> activities = task.mActivities;
421 for (int i = activities.size() - 1; i >= 0; --i) {
422 final ActivityRecord r = activities.get(i);
423 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800424 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800425 return r;
426 }
427 }
428 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700429 return null;
430 }
431
Craig Mautner8849a5e2013-04-02 16:41:03 -0700432 final ActivityRecord topActivity() {
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 Mautner0175b882014-09-07 18:05:31 -0700435 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
436 final ActivityRecord r = activities.get(activityNdx);
437 if (!r.finishing) {
438 return r;
439 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700440 }
441 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700442 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700443 }
444
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700445 final TaskRecord topTask() {
446 final int size = mTaskHistory.size();
447 if (size > 0) {
448 return mTaskHistory.get(size - 1);
449 }
450 return null;
451 }
452
Craig Mautnerd2328952013-03-05 12:46:26 -0800453 TaskRecord taskForIdLocked(int id) {
454 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
455 final TaskRecord task = mTaskHistory.get(taskNdx);
456 if (task.taskId == id) {
457 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800458 }
459 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700460 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700461 }
462
Craig Mautnerd2328952013-03-05 12:46:26 -0800463 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700464 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800465 return isInStackLocked(r);
466 }
467
468 ActivityRecord isInStackLocked(ActivityRecord r) {
469 if (r == null) {
470 return null;
471 }
472 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700473 if (task != null && task.stack != null
474 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800475 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800476 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800477 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800478 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800479 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800480 }
481
Craig Mautner2420ead2013-04-01 17:13:20 -0700482 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700483 final boolean hadit = mLRUActivities.remove(r);
484 mLRUActivities.add(r);
485 return hadit;
486 }
487
Craig Mautnerde4ef022013-04-07 19:01:33 -0700488 final boolean isHomeStack() {
489 return mStackId == HOME_STACK_ID;
490 }
491
Craig Mautnere0a38842013-12-16 16:14:02 -0800492 final boolean isOnHomeDisplay() {
493 return isAttached() &&
494 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
495 }
496
Craig Mautner299f9602015-01-26 09:47:33 -0800497 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800498 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800499 final boolean homeStack = isHomeStack()
500 || (mActivityContainer.mParentActivity != null
501 && mActivityContainer.mParentActivity.isHomeActivity());
502 ActivityStack lastFocusStack = null;
503 if (!homeStack) {
504 // Need to move this stack to the front before calling
505 // {@link ActivityStackSupervisor#moveHomeStack} below.
506 lastFocusStack = mStacks.get(mStacks.size() - 1);
507 mStacks.remove(this);
508 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800509 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800510 if (isOnHomeDisplay()) {
511 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
512 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800513 final TaskRecord task = topTask();
514 if (task != null) {
515 mWindowManager.moveTaskToTop(task.taskId);
516 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800517 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 }
519
520 final boolean isAttached() {
521 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 }
523
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700524 /**
525 * Returns the top activity in any existing task matching the given
526 * Intent. Returns null if no such task is found.
527 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700528 ActivityRecord findTaskLocked(ActivityRecord target) {
529 Intent intent = target.intent;
530 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700531 ComponentName cls = intent.getComponent();
532 if (info.targetActivity != null) {
533 cls = new ComponentName(info.packageName, info.targetActivity);
534 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700535 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700536 boolean isDocument = intent != null & intent.isDocument();
537 // If documentData is non-null then it must match the existing task data.
538 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800539
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700540 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800541 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
542 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700543 if (task.voiceSession != null) {
544 // We never match voice sessions; those always run independently.
545 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
546 continue;
547 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700548 if (task.userId != userId) {
549 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700550 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700551 continue;
552 }
Craig Mautner000f0022013-02-26 15:04:29 -0800553 final ActivityRecord r = task.getTopActivity();
554 if (r == null || r.finishing || r.userId != userId ||
555 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700556 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800557 continue;
558 }
559
Craig Mautnerd00f4742014-03-12 14:17:26 -0700560 final Intent taskIntent = task.intent;
561 final Intent affinityIntent = task.affinityIntent;
562 final boolean taskIsDocument;
563 final Uri taskDocumentData;
564 if (taskIntent != null && taskIntent.isDocument()) {
565 taskIsDocument = true;
566 taskDocumentData = taskIntent.getData();
567 } else if (affinityIntent != null && affinityIntent.isDocument()) {
568 taskIsDocument = true;
569 taskDocumentData = affinityIntent.getData();
570 } else {
571 taskIsDocument = false;
572 taskDocumentData = null;
573 }
574
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700575 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700576 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700577 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700578 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700579 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
580 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700581 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700582 return r;
583 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700584 } else if (taskIntent != null && taskIntent.getComponent() != null &&
585 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700586 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700587 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800588 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700589 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
590 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800591 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700592 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
593 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700594 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700595 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800596 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700597 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
598 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800599 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700600 } else if (DEBUG_TASKS) {
601 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602 }
603 }
604
605 return null;
606 }
607
608 /**
609 * Returns the first activity (starting from the top of the stack) that
610 * is the same as the given activity. Returns null if no such activity
611 * is found.
612 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700613 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700614 ComponentName cls = intent.getComponent();
615 if (info.targetActivity != null) {
616 cls = new ComponentName(info.packageName, info.targetActivity);
617 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700618 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619
Craig Mautner000f0022013-02-26 15:04:29 -0800620 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700621 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100622 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700623 return null;
624 }
625 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800626 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
627 ActivityRecord r = activities.get(activityNdx);
628 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 //Slog.i(TAG, "Found matching class!");
630 //dump();
631 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
632 return r;
633 }
634 }
635 }
636
637 return null;
638 }
639
Amith Yamasani742a6712011-05-04 14:49:28 -0700640 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700641 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700642 */
Craig Mautner93529a42013-10-04 15:03:13 -0700643 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800644 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700645 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800646 }
647 mCurrentUser = userId;
648
649 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800650 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800651 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700652 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100653 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700654 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
655 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800656 mTaskHistory.remove(i);
657 mTaskHistory.add(task);
658 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800659 // Use same value for i.
660 } else {
661 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800662 }
663 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700664 if (VALIDATE_TOKENS) {
665 validateAppTokensLocked();
666 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700667 }
668
Craig Mautner2420ead2013-04-01 17:13:20 -0700669 void minimalResumeActivityLocked(ActivityRecord r) {
670 r.state = ActivityState.RESUMED;
671 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
672 + " (starting new instance)");
673 r.stopped = false;
674 mResumedActivity = r;
675 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800676 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700677 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700678 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700679 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700680 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 }
682
Dianne Hackborncee04b52013-07-03 17:01:28 -0700683 private void startLaunchTraces() {
684 if (mFullyDrawnStartTime != 0) {
685 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
686 }
687 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
688 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
689 }
690
691 private void stopFullyDrawnTraceIfNeeded() {
692 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
693 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
694 mFullyDrawnStartTime = 0;
695 }
696 }
697
Craig Mautnere79d42682013-04-01 19:01:53 -0700698 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700699 if (r.displayStartTime == 0) {
700 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
701 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700702 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700703 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700704 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700705 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700706 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700707 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700708 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800710
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700711 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700712 // Make sure that there is no activity waiting for this to launch.
713 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
714 r.displayStartTime = r.fullyDrawnStartTime = 0;
715 } else {
716 mStackSupervisor.removeTimeoutsForActivityLocked(r);
717 mStackSupervisor.scheduleIdleTimeoutLocked(r);
718 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700719 }
720
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800721 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800722 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800723 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
724 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
725 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
726 activities.get(activityNdx).setSleeping(false);
727 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800728 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800729 if (mPausingActivity != null) {
730 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
731 activityPausedLocked(mPausingActivity.appToken, true);
732 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800733 }
734
Craig Mautner0eea92c2013-05-16 13:35:39 -0700735 /**
736 * @return true if something must be done before going to sleep.
737 */
738 boolean checkReadyForSleepLocked() {
739 if (mResumedActivity != null) {
740 // Still have something resumed; can't sleep until it is paused.
741 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
742 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700743 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700744 return true;
745 }
746 if (mPausingActivity != null) {
747 // Still waiting for something to pause; can't sleep yet.
748 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
749 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800750 }
751
Craig Mautner0eea92c2013-05-16 13:35:39 -0700752 return false;
753 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800754
Craig Mautner0eea92c2013-05-16 13:35:39 -0700755 void goToSleep() {
756 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800757
Craig Mautner0eea92c2013-05-16 13:35:39 -0700758 // Make sure any stopped but visible activities are now sleeping.
759 // This ensures that the activity's onStop() is called.
760 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
761 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
762 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
763 final ActivityRecord r = activities.get(activityNdx);
764 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
765 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800766 }
767 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800768 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700769 }
Craig Mautner59c00972012-07-30 12:10:24 -0700770
Dianne Hackbornd2835932010-12-13 16:28:46 -0800771 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700772 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800773 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700774 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800775 return null;
776 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800777
Winson Chung083baf92014-07-11 10:32:42 -0700778 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700779 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800780 // we can just go ahead and skip taking the screenshot if this is the home stack.
781 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700782 return null;
783 }
784
Winson Chung48a10a52014-08-27 14:36:51 -0700785 int w = mService.mThumbnailWidth;
786 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800787 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800788 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
789 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
790 w, h, SCREENSHOT_FORCE_565);
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
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700796 /**
797 * Start pausing the currently resumed activity. It is an error to call this if there
798 * is already an activity being paused or there is no resumed activity.
799 *
800 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
801 * @param uiSleeping True if this is happening with the user interface going to sleep (the
802 * screen turning off).
803 * @param resuming True if this is being called as part of resuming the top activity, so
804 * we shouldn't try to instigate a resume here.
805 * @param dontWait True if the caller does not want to wait for the pause to complete. If
806 * set to true, we will immediately complete the pause here before returning.
807 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
808 * it to tell us when it is done.
809 */
810 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
811 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800812 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700813 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
814 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800815 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 ActivityRecord prev = mResumedActivity;
817 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700818 if (!resuming) {
819 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
820 mStackSupervisor.resumeTopActivitiesLocked();
821 }
822 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800824
825 if (mActivityContainer.mParentActivity == null) {
826 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700827 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800828 }
829
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700830 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
831 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800833 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700835 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
836 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 prev.state = ActivityState.PAUSING;
838 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700839 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700840 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800841 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800842 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700843 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700844 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845
846 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800847
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848 if (prev.app != null && prev.app.thread != null) {
849 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
850 try {
851 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700852 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700854 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800855 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700856 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 } catch (Exception e) {
858 // Ignore exception, if process died other code will cleanup.
859 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700861 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700862 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700863 }
864 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700867 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 }
869
870 // If we are not going to sleep, we want to ensure the device is
871 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700872 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700873 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700874 }
875
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800876 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700877 // Have the window manager pause its key dispatching until the new
878 // activity has started. If we're pausing the activity just because
879 // the screen is being turned off and the UI is sleeping, don't interrupt
880 // key dispatch; the same activity will pick it up again on wakeup.
881 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800882 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 } else {
884 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
885 }
886
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700887 if (dontWait) {
888 // If the caller said they don't want to wait for the pause, then complete
889 // the pause now.
890 completePauseLocked(false);
891 return false;
892
893 } else {
894 // Schedule a pause timeout in case the app doesn't respond.
895 // We don't give it much time because this directly impacts the
896 // responsiveness seen by the user.
897 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
898 msg.obj = prev;
899 prev.pauseTime = SystemClock.uptimeMillis();
900 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
901 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
902 return true;
903 }
904
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 } else {
906 // This activity failed to schedule the
907 // pause, so just treat it as being paused now.
908 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700909 if (!resuming) {
910 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
911 }
912 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 }
914 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700915
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700916 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800918 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919
Craig Mautnerd2328952013-03-05 12:46:26 -0800920 final ActivityRecord r = isInStackLocked(token);
921 if (r != null) {
922 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
923 if (mPausingActivity == r) {
924 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
925 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700926 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800927 } else {
928 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
929 r.userId, System.identityHashCode(r), r.shortComponentName,
930 mPausingActivity != null
931 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
933 }
934 }
935
Craig Mautnera0026042014-04-23 11:45:37 -0700936 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
937 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700938 if (r.state != ActivityState.STOPPING) {
939 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
940 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
941 return;
942 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700943 if (persistentState != null) {
944 r.persistentState = persistentState;
945 mService.notifyTaskPersisterLocked(r.task, false);
946 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700947 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700948 if (icicle != null) {
949 // If icicle is null, this is happening due to a timeout, so we
950 // haven't really saved the state.
951 r.icicle = icicle;
952 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800953 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800954 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700955 }
956 if (!r.stopped) {
957 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
958 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
959 r.stopped = true;
960 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700961 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
962 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700963 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700964 if (r.finishing) {
965 r.clearOptionsLocked();
966 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700967 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700968 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700969 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700970 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700971 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800972 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700973 }
974 }
975 }
976
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700977 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700979 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800980
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700982 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800983 if (prev.finishing) {
984 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700985 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800986 } else if (prev.app != null) {
987 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800988 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
990 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800991 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800992 if (prev.configDestroy) {
993 // The previous is being paused because the configuration
994 // is changing, which means it is actually stopping...
995 // To juggle the fact that we are also starting a new
996 // instance right now, we need to first completely stop
997 // the current instance before starting the new one.
998 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700999 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -07001000 } else if (!hasVisibleBehindActivity()) {
1001 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001002 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001003 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001004 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1005 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001006 // If we already have a few activities waiting to stop,
1007 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001008 // them out. Or if r is the last of activity of the last task the stack
1009 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001010 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001011 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001012 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001013 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001014 }
1015 }
1016 } else {
1017 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1018 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001019 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001020 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001022
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001023 if (resumeNext) {
1024 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1025 if (!mService.isSleepingOrShuttingDown()) {
1026 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1027 } else {
1028 mStackSupervisor.checkReadyForSleepLocked();
1029 ActivityRecord top = topStack.topRunningActivityLocked(null);
1030 if (top == null || (prev != null && top != prev)) {
1031 // If there are no more activities available to run,
1032 // do resume anyway to start something. Also if the top
1033 // activity on the stack is not the just paused activity,
1034 // we need to go ahead and resume it to ensure we complete
1035 // an in-flight app switch.
1036 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1037 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001038 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001039 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001040
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001041 if (prev != null) {
1042 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001043
Craig Mautner525f3d92013-05-07 14:01:50 -07001044 if (prev.app != null && prev.cpuTimeAtResume > 0
1045 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001046 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1047 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001048 if (diff > 0) {
1049 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1050 synchronized (bsi) {
1051 BatteryStatsImpl.Uid.Proc ps =
1052 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001053 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001054 if (ps != null) {
1055 ps.addForegroundTimeLocked(diff);
1056 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057 }
1058 }
1059 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001060 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001062
1063 // Notfiy when the task stack has changed
1064 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065 }
1066
1067 /**
1068 * Once we know that we have asked an application to put an activity in
1069 * the resumed state (either by launching it or explicitly telling it),
1070 * this function updates the rest of our state to match that fact.
1071 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001072 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001073 next.idle = false;
1074 next.results = null;
1075 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001076
1077 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1078 ProcessRecord app = next.task.mActivities.get(0).app;
1079 if (app != null && app != mService.mHomeProcess) {
1080 mService.mHomeProcess = app;
1081 }
1082 }
1083
Craig Mautner07566322013-09-26 16:42:55 -07001084 if (next.nowVisible) {
1085 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001086 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001087 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001088
1089 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001090 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001091
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001092 mStackSupervisor.reportResumedActivityLocked(next);
1093
1094 next.resumeKeyDispatchingLocked();
1095 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096
1097 // Mark the point when the activity is resuming
1098 // TODO: To be more accurate, the mark should be before the onCreate,
1099 // not after the onResume. But for subsequent starts, onResume is fine.
1100 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001101 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001102 } else {
1103 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1104 }
Winson Chung376543b2014-05-21 17:43:28 -07001105
George Mount6ba042b2014-07-28 11:12:28 -07001106 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001107
1108 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1109 // When resuming an activity, require it to call requestVisibleBehind() again.
1110 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1111 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001112 }
1113
Craig Mautnere3a00d72014-04-16 08:31:19 -07001114 private void setVisibile(ActivityRecord r, boolean visible) {
1115 r.visible = visible;
1116 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001117 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001118 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001119 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001120 container.setVisible(visible);
1121 }
1122 }
1123
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001124 // Find the first visible activity above the passed activity and if it is translucent return it
1125 // otherwise return null;
1126 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1127 TaskRecord task = r.task;
1128 if (task == null) {
1129 return null;
1130 }
1131
1132 ActivityStack stack = task.stack;
1133 if (stack == null) {
1134 return null;
1135 }
1136
1137 int stackNdx = mStacks.indexOf(stack);
1138
1139 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1140 int taskNdx = tasks.indexOf(task);
1141
1142 ArrayList<ActivityRecord> activities = task.mActivities;
1143 int activityNdx = activities.indexOf(r) + 1;
1144
1145 final int numStacks = mStacks.size();
1146 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001147 ActivityStack historyStack = mStacks.get(stackNdx);
1148 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001149 final int numTasks = tasks.size();
1150 while (taskNdx < numTasks) {
1151 activities = tasks.get(taskNdx).mActivities;
1152 final int numActivities = activities.size();
1153 while (activityNdx < numActivities) {
1154 final ActivityRecord activity = activities.get(activityNdx);
1155 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001156 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001157 }
1158 ++activityNdx;
1159 }
1160 activityNdx = 0;
1161 ++taskNdx;
1162 }
1163 taskNdx = 0;
1164 ++stackNdx;
1165 }
1166
1167 return null;
1168 }
1169
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001170 private ActivityStack getNextVisibleStackLocked() {
1171 ArrayList<ActivityStack> stacks = mStacks;
1172 final ActivityRecord parent = mActivityContainer.mParentActivity;
1173 if (parent != null) {
1174 stacks = parent.task.stack.mStacks;
1175 }
1176 if (stacks != null) {
1177 for (int i = stacks.size() - 1; i >= 0; --i) {
1178 ActivityStack stack = stacks.get(i);
1179 if (stack != this && stack.isStackVisibleLocked()) {
1180 return stack;
1181 }
1182 }
1183 }
1184 return null;
1185 }
1186
Jose Lima7ba71252014-04-30 12:59:27 -07001187 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1188 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001189 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001190 if (!isAttached()) {
1191 return false;
1192 }
1193
1194 if (mStackSupervisor.isFrontStack(this)) {
1195 return true;
1196 }
1197
Jose Lima729cb232014-05-05 15:59:40 -07001198 /**
1199 * Start at the task above this one and go up, looking for a visible
1200 * fullscreen activity, or a translucent activity that requested the
1201 * wallpaper to be shown behind it.
1202 */
Jose Lima7ba71252014-04-30 12:59:27 -07001203 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001204 ActivityStack stack = mStacks.get(i);
1205 // stack above isn't full screen, so, we assume we're still visible. at some point
1206 // we should look at the stack bounds to see if we're occluded even if the stack
1207 // isn't fullscreen
1208 if (!stack.mFullscreen) {
1209 continue;
1210 }
1211 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1212 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001213 final TaskRecord task = tasks.get(taskNdx);
1214 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001215 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001216 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001217
1218 // Conditions for an activity to obscure the stack we're
1219 // examining:
1220 // 1. Not Finishing AND Visible AND:
1221 // 2. Either:
1222 // - Full Screen Activity OR
1223 // - On top of Home and our stack is NOT home
1224 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001225 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001226 return false;
1227 }
1228 }
1229 }
1230 }
1231
1232 return true;
1233 }
1234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001235 /**
1236 * Make sure that all activities that need to be visible (that is, they
1237 * currently can be seen by the user) actually are.
1238 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001239 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1240 ActivityRecord top = topRunningActivityLocked(null);
1241 if (top == null) {
1242 return;
1243 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001244 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 TAG, "ensureActivitiesVisible behind " + top
1246 + " configChanges=0x" + Integer.toHexString(configChanges));
1247
Craig Mautner5eda9b32013-07-02 11:58:16 -07001248 if (mTranslucentActivityWaiting != top) {
1249 mUndrawnActivitiesBelowTopTranslucent.clear();
1250 if (mTranslucentActivityWaiting != null) {
1251 // Call the callback with a timeout indication.
1252 notifyActivityDrawnLocked(null);
1253 mTranslucentActivityWaiting = null;
1254 }
1255 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1256 }
1257
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258 // If the top activity is not fullscreen, then we need to
1259 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001260 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001261 boolean behindFullscreen = !isStackVisibleLocked();
Jose Lima7ba71252014-04-30 12:59:27 -07001262
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001263 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001264 final TaskRecord task = mTaskHistory.get(taskNdx);
1265 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001266 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1267 final ActivityRecord r = activities.get(activityNdx);
1268 if (r.finishing) {
1269 continue;
1270 }
1271 if (aboveTop && r != top) {
1272 continue;
1273 }
1274 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001275 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1276 // but must be drawn initially for the animation as though they were visible.
1277 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001279 TAG, "Make visible? " + r + " finishing=" + r.finishing
1280 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001281
Craig Mautnerd44711d2013-02-23 11:24:36 -08001282 // First: if this is not the current activity being started, make
1283 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001284 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001285 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001287
1288 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001289 // This activity needs to be visible, but isn't even
1290 // running... get it started, but don't resume it
1291 // at this point.
1292 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1293 if (r != starting) {
1294 r.startFreezingScreenLocked(r.app, configChanges);
1295 }
1296 if (!r.visible || r.mLaunchTaskBehind) {
1297 if (DEBUG_VISBILITY) Slog.v(
1298 TAG, "Starting and making visible: " + r);
1299 setVisibile(r, true);
1300 }
1301 if (r != starting) {
1302 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001303 }
1304
1305 } else if (r.visible) {
1306 // If this activity is already visible, then there is nothing
1307 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001308 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001310 try {
George Mount6ba042b2014-07-28 11:12:28 -07001311 if (r.returningOptions != null) {
1312 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1313 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001314 }
1315 } catch(RemoteException e) {
1316 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001317 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001318 // This activity is not currently visible, but is running.
1319 // Tell it to become visible.
1320 r.visible = true;
1321 if (r.state != ActivityState.RESUMED && r != starting) {
1322 // If this activity is paused, tell it
1323 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001324 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001325 TAG, "Making visible and scheduling visibility: " + r);
1326 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001327 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001328 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001329 mUndrawnActivitiesBelowTopTranslucent.add(r);
1330 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001331 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001332 r.sleeping = false;
1333 r.app.pendingUiClean = true;
1334 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1335 r.stopFreezingScreenLocked(false);
1336 } catch (Exception e) {
1337 // Just skip on any failure; we'll make it
1338 // visible when it next restarts.
1339 Slog.w(TAG, "Exception thrown making visibile: "
1340 + r.intent.getComponent(), e);
1341 }
1342 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001343 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344
Craig Mautnerd44711d2013-02-23 11:24:36 -08001345 // Aggregate current change flags.
1346 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001347
Craig Mautnerd44711d2013-02-23 11:24:36 -08001348 if (r.fullscreen) {
1349 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001350 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1351 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001352 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001353 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001354 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001355 }
1356 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001357 if (DEBUG_VISBILITY) Slog.v(
1358 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1359 + " state=" + r.state
1360 + " behindFullscreen=" + behindFullscreen);
1361 // Now for any activities that aren't visible to the user, make
1362 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001363 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001364 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001365 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001366 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001367 switch (r.state) {
1368 case STOPPING:
1369 case STOPPED:
1370 if (r.app != null && r.app.thread != null) {
1371 if (DEBUG_VISBILITY) Slog.v(
1372 TAG, "Scheduling invisibility: " + r);
1373 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1374 }
1375 break;
1376
1377 case INITIALIZING:
1378 case RESUMED:
1379 case PAUSING:
1380 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001381 // This case created for transitioning activities from
1382 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001383 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001384 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001385 } else {
1386 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1387 mStackSupervisor.mStoppingActivities.add(r);
1388 }
1389 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001390 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001391 break;
1392
1393 default:
1394 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395 }
1396 } catch (Exception e) {
1397 // Just skip on any failure; we'll make it
1398 // visible when it next restarts.
1399 Slog.w(TAG, "Exception thrown making hidden: "
1400 + r.intent.getComponent(), e);
1401 }
1402 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001403 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001405 }
1406 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001408
1409 if (mTranslucentActivityWaiting != null &&
1410 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1411 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1412 notifyActivityDrawnLocked(null);
1413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001414 }
Craig Mautner58547802013-03-05 08:23:53 -08001415
Todd Kennedyaab56db2015-01-30 09:39:53 -08001416 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001417 mTranslucentActivityWaiting = r;
1418 mUndrawnActivitiesBelowTopTranslucent.clear();
1419 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1420 }
1421
1422 /**
1423 * Called as activities below the top translucent activity are redrawn. When the last one is
1424 * redrawn notify the top activity by calling
1425 * {@link Activity#onTranslucentConversionComplete}.
1426 *
1427 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1428 * occurred and the activity will be notified immediately.
1429 */
1430 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001431 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001432 if ((r == null)
1433 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1434 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1435 // The last undrawn activity below the top has just been drawn. If there is an
1436 // opaque activity at the top, notify it that it can become translucent safely now.
1437 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1438 mTranslucentActivityWaiting = null;
1439 mUndrawnActivitiesBelowTopTranslucent.clear();
1440 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1441
Craig Mautner71dd1b62014-02-18 15:48:52 -08001442 if (waitingActivity != null) {
1443 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1444 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1445 try {
1446 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1447 waitingActivity.appToken, r != null);
1448 } catch (RemoteException e) {
1449 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001450 }
1451 }
1452 }
1453 }
1454
Craig Mautnera61bc652013-10-28 15:43:18 -07001455 /** If any activities below the top running one are in the INITIALIZING state and they have a
1456 * starting window displayed then remove that starting window. It is possible that the activity
1457 * in this state will never resumed in which case that starting window will be orphaned. */
1458 void cancelInitializingActivities() {
1459 final ActivityRecord topActivity = topRunningActivityLocked(null);
1460 boolean aboveTop = true;
1461 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1462 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1463 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1464 final ActivityRecord r = activities.get(activityNdx);
1465 if (aboveTop) {
1466 if (r == topActivity) {
1467 aboveTop = false;
1468 }
1469 continue;
1470 }
1471
1472 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1473 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1474 r.mStartingWindowShown = false;
1475 mWindowManager.removeAppStartingWindow(r.appToken);
1476 }
1477 }
1478 }
1479 }
1480
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 /**
1482 * Ensure that the top activity in the stack is resumed.
1483 *
1484 * @param prev The previously resumed activity, for when in the process
1485 * of pausing; can be null to call from elsewhere.
1486 *
1487 * @return Returns true if something is being resumed, or false if
1488 * nothing happened.
1489 */
1490 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001491 return resumeTopActivityLocked(prev, null);
1492 }
1493
1494 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001495 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001496 // Don't even start recursing.
1497 return false;
1498 }
1499
1500 boolean result = false;
1501 try {
1502 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001503 mStackSupervisor.inResumeTopActivity = true;
1504 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1505 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001506 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001507 }
Craig Mautner544efa72014-09-04 16:41:20 -07001508 result = resumeTopActivityInnerLocked(prev, options);
1509 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001510 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001511 }
1512 return result;
1513 }
1514
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001515 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001516 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1517
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001518 if (!mService.mBooting && !mService.mBooted) {
1519 // Not ready yet!
1520 return false;
1521 }
1522
Craig Mautnerdf88d732014-01-27 09:21:32 -08001523 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001524 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001525 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001526 // Do not resume this stack if its parent is not resumed.
1527 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1528 return false;
1529 }
1530
Craig Mautnera61bc652013-10-28 15:43:18 -07001531 cancelInitializingActivities();
1532
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001534 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535
1536 // Remember how we'll process this pause/resume situation, and ensure
1537 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001538 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1539 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540
Craig Mautner84984fa2014-06-19 11:19:20 -07001541 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001543 // There are no more activities!
1544 final String reason = "noMoreActivities";
1545 if (!mFullscreen) {
1546 // Try to move focus to the next visible stack with a running activity if this
1547 // stack is not covering the entire screen.
1548 final ActivityStack stack = getNextVisibleStackLocked();
1549 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1550 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1551 }
1552 }
1553 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001554 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001555 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001556 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001557 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001558 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1559 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1560 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001561 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001562 }
1563
1564 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001565
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001567 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1568 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 // Make sure we have executed any pending transitions, since there
1570 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001571 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001573 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001574 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001575 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 return false;
1577 }
1578
Craig Mautner525f3d92013-05-07 14:01:50 -07001579 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001580 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001581 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001582 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001583 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001584 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001585 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001586 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001587 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001588 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001589 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001590 } else if (!isOnHomeDisplay()) {
1591 return false;
1592 } else if (!isHomeStack()){
1593 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001594 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001595 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1596 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
Craig Mautner299f9602015-01-26 09:47:33 -08001597 return mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001598 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001599 }
1600
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601 // If we are sleeping, and there is no resumed activity, and the top
1602 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001603 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001604 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001605 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001606 // Make sure we have executed any pending transitions, since there
1607 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001608 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001609 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001610 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001611 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001612 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 return false;
1614 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001615
1616 // Make sure that the user who owns this activity is started. If not,
1617 // we will just leave it as is because someone should be bringing
1618 // another user's activities to the top of the stack.
1619 if (mService.mStartedUsers.get(next.userId) == null) {
1620 Slog.w(TAG, "Skipping resume of top activity " + next
1621 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001622 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001623 return false;
1624 }
1625
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 // The activity may be waiting for stop, but that is no longer
1627 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001628 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001629 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001630 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001631 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632
1633 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1634
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001635 // If we are currently pausing an activity, then don't do anything
1636 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001637 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001638 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1639 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001640 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001641 return false;
1642 }
1643
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001644 // Okay we are now going to start a switch, to 'next'. We may first
1645 // have to pause the current activity, but this is an important point
1646 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001647 // XXX "App Redirected" dialog is getting too many false positives
1648 // at this point, so turn off for now.
1649 if (false) {
1650 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1651 long now = SystemClock.uptimeMillis();
1652 final boolean inTime = mLastStartedActivity.startTime != 0
1653 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1654 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1655 final int nextUid = next.info.applicationInfo.uid;
1656 if (inTime && lastUid != nextUid
1657 && lastUid != next.launchedFromUid
1658 && mService.checkPermission(
1659 android.Manifest.permission.STOP_APP_SWITCHES,
1660 -1, next.launchedFromUid)
1661 != PackageManager.PERMISSION_GRANTED) {
1662 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1663 } else {
1664 next.startTime = now;
1665 mLastStartedActivity = next;
1666 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001667 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001668 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001669 mLastStartedActivity = next;
1670 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001671 }
Craig Mautner58547802013-03-05 08:23:53 -08001672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 // We need to start pausing the current activity so the top one
1674 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001675 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1676 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001677 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001678 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001679 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001680 }
1681 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001682 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1683 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001684 // At this point we want to put the upcoming activity's process
1685 // at the top of the LRU list, since we know we will be needing it
1686 // very soon and it would be a waste to let it get killed if it
1687 // happens to be sitting towards the end.
1688 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001689 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001690 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001691 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 return true;
1693 }
1694
Christopher Tated3f175c2012-06-14 14:16:54 -07001695 // If the most recent activity was noHistory but was only stopped rather
1696 // than stopped+finished because the device went to sleep, we need to make
1697 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001698 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001699 !mLastNoHistoryActivity.finishing) {
1700 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1701 " on new resume");
1702 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001703 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001704 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001705 }
1706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001708 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1709 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001710 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 if (DEBUG_SWITCH) Slog.v(
1712 TAG, "Resuming top, waiting visible to hide: " + prev);
1713 } else {
1714 // The next activity is already visible, so hide the previous
1715 // activity's windows right now so we can show the new one ASAP.
1716 // We only do this if the previous is finishing, which should mean
1717 // it is on top of the one being resumed so hiding it quickly
1718 // is good. Otherwise, we want to do the normal route of allowing
1719 // the resumed activity to be shown so we can decide if the
1720 // previous should actually be hidden depending on whether the
1721 // new one is found to be full-screen or not.
1722 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001723 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1725 + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001726 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 + ", nowVisible=" + next.nowVisible);
1728 } else {
Craig Mautner8c14c152015-01-15 17:32:07 -08001729 if (DEBUG_SWITCH) Slog.v(TAG,
1730 "Previous already visible but still waiting to hide: " + prev
1731 + ", waitingVisible="
1732 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1733 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734 }
1735 }
1736 }
1737
Dianne Hackborne7f97212011-02-24 14:40:20 -08001738 // Launching this app's activity, make sure the app is no longer
1739 // considered stopped.
1740 try {
1741 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001742 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001743 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001744 } catch (IllegalArgumentException e) {
1745 Slog.w(TAG, "Failed trying to unstop package "
1746 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001747 }
1748
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 // We are starting up the next activity, so tell the window manager
1750 // that the previous one will be hidden soon. This way it can know
1751 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001752 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 if (prev != null) {
1754 if (prev.finishing) {
1755 if (DEBUG_TRANSITION) Slog.v(TAG,
1756 "Prepare close transition: prev=" + prev);
1757 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001758 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001759 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001760 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001761 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001762 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1763 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001764 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001765 mWindowManager.setAppWillBeHidden(prev.appToken);
1766 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001768 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001770 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001771 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001773 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001774 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001775 : next.mLaunchTaskBehind
1776 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1777 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 }
1779 }
1780 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001781 mWindowManager.setAppWillBeHidden(prev.appToken);
1782 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 }
Craig Mautner967212c2013-04-13 21:10:58 -07001784 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001785 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001787 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001788 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001790 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 }
1792 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001793
1794 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001795 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001796 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1797 if (opts != null) {
1798 resumeAnimOptions = opts.toBundle();
1799 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001800 next.applyOptionsLocked();
1801 } else {
1802 next.clearOptionsLocked();
1803 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804
Craig Mautnercf910b02013-04-23 11:23:27 -07001805 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806 if (next.app != null && next.app.thread != null) {
1807 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1808
1809 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001810 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001812 // schedule launch ticks to collect information about slow apps.
1813 next.startLaunchTickingLocked();
1814
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001815 ActivityRecord lastResumedActivity =
1816 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 ActivityState lastState = next.state;
1818
1819 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001820
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001821 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 next.state = ActivityState.RESUMED;
1823 mResumedActivity = next;
1824 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001825 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001826 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001827 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001828 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829
1830 // Have the window manager re-evaluate the orientation of
1831 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001832 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001833 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001834 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001835 mService.mConfiguration,
1836 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1837 if (config != null) {
1838 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001840 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001841 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001842
Craig Mautner525f3d92013-05-07 14:01:50 -07001843 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001844 // The configuration update wasn't able to keep the existing
1845 // instance of the activity, and instead started a new one.
1846 // We should be all done, but let's just make sure our activity
1847 // is still at the top and schedule another run if something
1848 // weird happened.
1849 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001850 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001851 "Activity config changed during resume: " + next
1852 + ", new next: " + nextNext);
1853 if (nextNext != next) {
1854 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001855 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001856 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001857 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001858 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001859 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001860 return true;
1861 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001862 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001863 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001864 }
Craig Mautner58547802013-03-05 08:23:53 -08001865
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001866 try {
1867 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001868 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 if (a != null) {
1870 final int N = a.size();
1871 if (!next.finishing && N > 0) {
1872 if (DEBUG_RESULTS) Slog.v(
1873 TAG, "Delivering results to " + next
1874 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001875 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001876 }
1877 }
1878
1879 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001880 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 }
1882
Craig Mautner299f9602015-01-26 09:47:33 -08001883 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1884 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001885
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001886 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001887 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001888 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001889 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001890 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001891 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001892 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001893
Craig Mautner0eea92c2013-05-16 13:35:39 -07001894 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001895
Craig Mautnerac6f8432013-07-17 13:24:59 -07001896 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 } catch (Exception e) {
1898 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001899 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1900 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001902 if (lastStack != null) {
1903 lastStack.mResumedActivity = lastResumedActivity;
1904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 Slog.i(TAG, "Restarting because process died: " + next);
1906 if (!next.hasBeenLaunched) {
1907 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001908 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1909 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001910 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001911 next.appToken, next.packageName, next.theme,
1912 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001913 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1914 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915 }
George Mount2c92c972014-03-20 09:38:23 -07001916 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001917 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001918 return true;
1919 }
1920
1921 // From this point on, if something goes wrong there is no way
1922 // to recover the activity.
1923 try {
1924 next.visible = true;
1925 completeResumeLocked(next);
1926 } catch (Exception e) {
1927 // If any exception gets thrown, toss away this
1928 // activity and try the next one.
1929 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001930 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001931 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001932 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001933 return true;
1934 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001935 next.stopped = false;
1936
1937 } else {
1938 // Whoops, need to restart this activity!
1939 if (!next.hasBeenLaunched) {
1940 next.hasBeenLaunched = true;
1941 } else {
1942 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001943 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001944 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001945 mService.compatibilityInfoForPackageLocked(
1946 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001948 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001949 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001950 }
1951 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1952 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001953 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001954 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 }
1956
Craig Mautnercf910b02013-04-23 11:23:27 -07001957 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 return true;
1959 }
1960
riddle_hsuc215a4f2014-12-27 12:10:45 +08001961 private TaskRecord getNextTask(TaskRecord targetTask) {
1962 final int index = mTaskHistory.indexOf(targetTask);
1963 if (index >= 0) {
1964 final int numTasks = mTaskHistory.size();
1965 for (int i = index + 1; i < numTasks; ++i) {
1966 TaskRecord task = mTaskHistory.get(i);
1967 if (task.userId == targetTask.userId) {
1968 return task;
1969 }
1970 }
1971 }
1972 return null;
1973 }
1974
Craig Mautnerac6f8432013-07-17 13:24:59 -07001975 private void insertTaskAtTop(TaskRecord task) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001976 // If the moving task is over home stack, transfer its return type to next task
1977 if (task.isOverHomeStack()) {
1978 final TaskRecord nextTask = getNextTask(task);
1979 if (nextTask != null) {
1980 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1981 }
1982 }
1983
Craig Mautner9c85c202013-10-04 20:11:26 -07001984 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08001985 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001986 if (isOnHomeDisplay()) {
1987 ActivityStack lastStack = mStackSupervisor.getLastStack();
1988 final boolean fromHome = lastStack.isHomeStack();
1989 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001990 task.setTaskToReturnTo(fromHome
1991 ? lastStack.topTask() == null
1992 ? HOME_ACTIVITY_TYPE
1993 : lastStack.topTask().taskType
1994 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001995 }
1996 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001997 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001998 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001999
Craig Mautnerac6f8432013-07-17 13:24:59 -07002000 mTaskHistory.remove(task);
2001 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002002 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01002003 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002004 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002005 while (--taskNdx >= 0) {
2006 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002007 break;
2008 }
2009 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002010 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002011 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002012 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002013 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002014 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002015
Craig Mautner8849a5e2013-04-02 16:41:03 -07002016 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002017 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002018 TaskRecord rTask = r.task;
2019 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002020 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2021 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002022 // Last activity in task had been removed or ActivityManagerService is reusing task.
2023 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002024 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002025 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002026 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002027 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002028 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 if (!newTask) {
2030 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002031 boolean startIt = true;
2032 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2033 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002034 if (task.getTopActivity() == null) {
2035 // All activities in task are finishing.
2036 continue;
2037 }
Craig Mautner70a86932013-02-28 22:37:44 -08002038 if (task == r.task) {
2039 // Here it is! Now, if this is not yet visible to the
2040 // user, then just add it without starting; it will
2041 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002042 if (!startIt) {
2043 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2044 + task, new RuntimeException("here").fillInStackTrace());
2045 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002046 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002047 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2048 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002049 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002050 r.userId, r.info.configChanges, task.voiceSession != null,
2051 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002052 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002053 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002055 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 return;
2057 }
2058 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002059 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 startIt = false;
2061 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 }
2063 }
2064
2065 // Place a new activity at top of stack, so it is next to interact
2066 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002067
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 // If we are not placing the new activity frontmost, we do not want
2069 // to deliver the onUserLeaving callback to the actual frontmost
2070 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002071 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002072 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002073 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2074 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075 }
Craig Mautner70a86932013-02-28 22:37:44 -08002076
2077 task = r.task;
2078
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002080 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002081 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002082 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002083 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002084
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002085 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002086 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 // We want to show the starting preview window if we are
2088 // switching to a new task, or the next activity's process is
2089 // not currently running.
2090 boolean showStartingIcon = newTask;
2091 ProcessRecord proc = r.app;
2092 if (proc == null) {
2093 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2094 }
2095 if (proc == null || proc.thread == null) {
2096 showStartingIcon = true;
2097 }
2098 if (DEBUG_TRANSITION) Slog.v(TAG,
2099 "Prepare open transition: starting " + r);
2100 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002101 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002103 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002104 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002105 ? r.mLaunchTaskBehind
2106 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2107 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002108 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 mNoAnimActivities.remove(r);
2110 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002111 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002112 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002113 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002114 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 boolean doShow = true;
2116 if (newTask) {
2117 // Even though this activity is starting fresh, we still need
2118 // to reset it to make sure we apply affinities to move any
2119 // existing activities from other tasks in to it.
2120 // If the caller has requested that the target task be
2121 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002122 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 resetTaskIfNeededLocked(r, r);
2124 doShow = topRunningNonDelayedActivityLocked(null) == r;
2125 }
George Mount70778d72014-07-01 16:33:45 -07002126 } else if (options != null && new ActivityOptions(options).getAnimationType()
2127 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2128 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002130 if (r.mLaunchTaskBehind) {
2131 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2132 // tell WindowManager that r is visible even though it is at the back of the stack.
2133 mWindowManager.setAppVisibility(r.appToken, true);
2134 ensureActivitiesVisibleLocked(null, 0);
2135 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 // Figure out if we are transitioning from another activity that is
2137 // "has the same starting icon" as the next one. This allows the
2138 // window manager to keep the previous window it had previously
2139 // created, if it still had one.
2140 ActivityRecord prev = mResumedActivity;
2141 if (prev != null) {
2142 // We don't want to reuse the previous starting preview if:
2143 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002144 if (prev.task != r.task) {
2145 prev = null;
2146 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002148 else if (prev.nowVisible) {
2149 prev = null;
2150 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002152 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002153 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002154 mService.compatibilityInfoForPackageLocked(
2155 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002156 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002157 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002158 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 }
2160 } else {
2161 // If this is the first activity, don't do any fancy animations,
2162 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002163 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002164 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002165 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002166 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002167 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002168 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169 }
2170 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002171 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
2173
2174 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002175 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 }
2177 }
2178
Dianne Hackbornbe707852011-11-11 14:32:10 -08002179 final void validateAppTokensLocked() {
2180 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002181 mValidateAppTokens.ensureCapacity(numActivities());
2182 final int numTasks = mTaskHistory.size();
2183 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2184 TaskRecord task = mTaskHistory.get(taskNdx);
2185 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002186 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002187 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002188 }
Craig Mautner000f0022013-02-26 15:04:29 -08002189 TaskGroup group = new TaskGroup();
2190 group.taskId = task.taskId;
2191 mValidateAppTokens.add(group);
2192 final int numActivities = activities.size();
2193 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2194 final ActivityRecord r = activities.get(activityNdx);
2195 group.tokens.add(r.appToken);
2196 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002197 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002198 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002199 }
2200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 /**
2202 * Perform a reset of the given task, if needed as part of launching it.
2203 * Returns the new HistoryRecord at the top of the task.
2204 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002205 /**
2206 * Helper method for #resetTaskIfNeededLocked.
2207 * We are inside of the task being reset... we'll either finish this activity, push it out
2208 * for another task, or leave it as-is.
2209 * @param task The task containing the Activity (taskTop) that might be reset.
2210 * @param forceReset
2211 * @return An ActivityOptions that needs to be processed.
2212 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002213 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002214 ActivityOptions topOptions = null;
2215
2216 int replyChainEnd = -1;
2217 boolean canMoveOptions = true;
2218
2219 // We only do this for activities that are not the root of the task (since if we finish
2220 // the root, we may no longer have the task!).
2221 final ArrayList<ActivityRecord> activities = task.mActivities;
2222 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002223 final int rootActivityNdx = task.findEffectiveRootIndex();
2224 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002225 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002226 if (target.frontOfTask)
2227 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002228
2229 final int flags = target.info.flags;
2230 final boolean finishOnTaskLaunch =
2231 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2232 final boolean allowTaskReparenting =
2233 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2234 final boolean clearWhenTaskReset =
2235 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2236
2237 if (!finishOnTaskLaunch
2238 && !clearWhenTaskReset
2239 && target.resultTo != null) {
2240 // If this activity is sending a reply to a previous
2241 // activity, we can't do anything with it now until
2242 // we reach the start of the reply chain.
2243 // XXX note that we are assuming the result is always
2244 // to the previous activity, which is almost always
2245 // the case but we really shouldn't count on.
2246 if (replyChainEnd < 0) {
2247 replyChainEnd = i;
2248 }
2249 } else if (!finishOnTaskLaunch
2250 && !clearWhenTaskReset
2251 && allowTaskReparenting
2252 && target.taskAffinity != null
2253 && !target.taskAffinity.equals(task.affinity)) {
2254 // If this activity has an affinity for another
2255 // task, then we need to move it out of here. We will
2256 // move it as far out of the way as possible, to the
2257 // bottom of the activity stack. This also keeps it
2258 // correctly ordered with any activities we previously
2259 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002260 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002261 final ActivityRecord bottom =
2262 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002263 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002264 if (bottom != null && target.taskAffinity != null
2265 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002266 // If the activity currently at the bottom has the
2267 // same task affinity as the one we are moving,
2268 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002269 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002270 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002271 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002272 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002273 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002274 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002275 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002276 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2277 + " out to new task " + target.task);
2278 }
2279
Craig Mautnere3a74d52013-02-22 14:14:58 -08002280 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002281 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282
Craig Mautner525f3d92013-05-07 14:01:50 -07002283 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002284 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2285 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002286 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002287 if (p.finishing) {
2288 continue;
2289 }
2290
Craig Mautnere3a74d52013-02-22 14:14:58 -08002291 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002292 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002293 topOptions = p.takeOptionsLocked();
2294 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002295 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002296 }
2297 }
2298 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002299 + task + " adding to task=" + targetTask
2300 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002301 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2302 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002303 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002304 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002305
Craig Mautner83162a92015-01-26 14:43:30 -08002306 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002307 }
2308
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002309 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002310 if (VALIDATE_TOKENS) {
2311 validateAppTokensLocked();
2312 }
2313
2314 replyChainEnd = -1;
2315 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2316 // If the activity should just be removed -- either
2317 // because it asks for it, or the task should be
2318 // cleared -- then finish it and anything that is
2319 // part of its reply chain.
2320 int end;
2321 if (clearWhenTaskReset) {
2322 // In this case, we want to finish this activity
2323 // and everything above it, so be sneaky and pretend
2324 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002325 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002326 } else if (replyChainEnd < 0) {
2327 end = i;
2328 } else {
2329 end = replyChainEnd;
2330 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002331 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002332 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002333 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002334 if (p.finishing) {
2335 continue;
2336 }
2337 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002338 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002339 topOptions = p.takeOptionsLocked();
2340 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002341 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002342 }
2343 }
Craig Mautner58547802013-03-05 08:23:53 -08002344 if (DEBUG_TASKS) Slog.w(TAG,
2345 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002346 if (finishActivityLocked(
2347 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002348 end--;
2349 srcPos--;
2350 }
2351 }
2352 replyChainEnd = -1;
2353 } else {
2354 // If we were in the middle of a chain, well the
2355 // activity that started it all doesn't want anything
2356 // special, so leave it all as-is.
2357 replyChainEnd = -1;
2358 }
2359 }
2360
2361 return topOptions;
2362 }
2363
2364 /**
2365 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2366 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2367 * @param affinityTask The task we are looking for an affinity to.
2368 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2369 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2370 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2371 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002372 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002373 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002374 int replyChainEnd = -1;
2375 final int taskId = task.taskId;
2376 final String taskAffinity = task.affinity;
2377
2378 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2379 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002380 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2381
2382 // Do not operate on or below the effective root Activity.
2383 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002384 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002385 if (target.frontOfTask)
2386 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002387
2388 final int flags = target.info.flags;
2389 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2390 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2391
2392 if (target.resultTo != null) {
2393 // If this activity is sending a reply to a previous
2394 // activity, we can't do anything with it now until
2395 // we reach the start of the reply chain.
2396 // XXX note that we are assuming the result is always
2397 // to the previous activity, which is almost always
2398 // the case but we really shouldn't count on.
2399 if (replyChainEnd < 0) {
2400 replyChainEnd = i;
2401 }
2402 } else if (topTaskIsHigher
2403 && allowTaskReparenting
2404 && taskAffinity != null
2405 && taskAffinity.equals(target.taskAffinity)) {
2406 // This activity has an affinity for our task. Either remove it if we are
2407 // clearing or move it over to our task. Note that
2408 // we currently punt on the case where we are resetting a
2409 // task that is not at the top but who has activities above
2410 // with an affinity to it... this is really not a normal
2411 // case, and we will need to later pull that task to the front
2412 // and usually at that point we will do the reset and pick
2413 // up those remaining activities. (This only happens if
2414 // someone starts an activity in a new task from an activity
2415 // in a task that is not currently on top.)
2416 if (forceReset || finishOnTaskLaunch) {
2417 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2418 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2419 for (int srcPos = start; srcPos >= i; --srcPos) {
2420 final ActivityRecord p = activities.get(srcPos);
2421 if (p.finishing) {
2422 continue;
2423 }
Todd Kennedy539db512014-12-15 09:57:55 -08002424 finishActivityLocked(
2425 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002426 }
2427 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002428 if (taskInsertionPoint < 0) {
2429 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002430
Craig Mautner77878772013-03-04 19:46:24 -08002431 }
Craig Mautner77878772013-03-04 19:46:24 -08002432
2433 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2434 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2435 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2436 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002437 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002438 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002439 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002440
Craig Mautnere3a74d52013-02-22 14:14:58 -08002441 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2442 + " to stack at " + task,
2443 new RuntimeException("here").fillInStackTrace());
2444 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2445 + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002446 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002447 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002448 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002449 if (VALIDATE_TOKENS) {
2450 validateAppTokensLocked();
2451 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002452
2453 // Now we've moved it in to place... but what if this is
2454 // a singleTop activity and we have put it on top of another
2455 // instance of the same activity? Then we drop the instance
2456 // below so it remains singleTop.
2457 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2458 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002459 int targetNdx = taskActivities.indexOf(target);
2460 if (targetNdx > 0) {
2461 ActivityRecord p = taskActivities.get(targetNdx - 1);
2462 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002463 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2464 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002465 }
2466 }
2467 }
2468 }
2469
2470 replyChainEnd = -1;
2471 }
2472 }
Craig Mautner77878772013-03-04 19:46:24 -08002473 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002474 }
2475
Craig Mautner8849a5e2013-04-02 16:41:03 -07002476 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002477 ActivityRecord newActivity) {
2478 boolean forceReset =
2479 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2480 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2481 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2482 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2483 forceReset = true;
2484 }
2485 }
2486
2487 final TaskRecord task = taskTop.task;
2488
2489 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2490 * for remaining tasks. Used for later tasks to reparent to task. */
2491 boolean taskFound = false;
2492
2493 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2494 ActivityOptions topOptions = null;
2495
Craig Mautner77878772013-03-04 19:46:24 -08002496 // Preserve the location for reparenting in the new task.
2497 int reparentInsertionPoint = -1;
2498
Craig Mautnere3a74d52013-02-22 14:14:58 -08002499 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2500 final TaskRecord targetTask = mTaskHistory.get(i);
2501
2502 if (targetTask == task) {
2503 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2504 taskFound = true;
2505 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002506 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2507 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002508 }
2509 }
2510
Craig Mautner70a86932013-02-28 22:37:44 -08002511 int taskNdx = mTaskHistory.indexOf(task);
2512 do {
2513 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2514 } while (taskTop == null && taskNdx >= 0);
2515
Craig Mautnere3a74d52013-02-22 14:14:58 -08002516 if (topOptions != null) {
2517 // If we got some ActivityOptions from an activity on top that
2518 // was removed from the task, propagate them to the new real top.
2519 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002520 taskTop.updateOptionsLocked(topOptions);
2521 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002522 topOptions.abort();
2523 }
2524 }
2525
2526 return taskTop;
2527 }
2528
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002529 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2530 String resultWho, int requestCode, int resultCode, Intent data) {
2531
2532 if (callingUid > 0) {
2533 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002534 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 }
2536
2537 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2538 + " : who=" + resultWho + " req=" + requestCode
2539 + " res=" + resultCode + " data=" + data);
2540 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2541 try {
2542 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2543 list.add(new ResultInfo(resultWho, requestCode,
2544 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002545 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 return;
2547 } catch (Exception e) {
2548 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2549 }
2550 }
2551
2552 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2553 }
2554
Craig Mautner299f9602015-01-26 09:47:33 -08002555 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002556 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2557 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002558 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002559 if (next != r) {
2560 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002561 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002562 // For non-fullscreen stack, we want to move the focus to the next visible
2563 // stack to prevent the home screen from moving to the top and obscuring
2564 // other visible stacks.
2565 if (!mFullscreen
2566 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2567 return;
2568 }
2569 // Move the home stack to the top if this stack is fullscreen or there is no
2570 // other visible stack.
2571 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002572 }
2573 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002574
2575 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002576 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002577 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002578 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002579 }
2580 }
2581
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002582 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2583 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2584 final String myReason = reason + " adjustFocusToNextVisibleStack";
2585 if (stack == null) {
2586 return false;
2587 }
2588 final ActivityRecord top = stack.topRunningActivityLocked(null);
2589 if (top == null) {
2590 return false;
2591 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002592 mService.setFocusedActivityLocked(top, myReason);
2593 return true;
2594 }
2595
Craig Mautnerf3333272013-04-22 10:55:53 -07002596 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2598 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2599 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2600 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002601 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002602 if (DEBUG_STATES) {
2603 Slog.d(TAG, "no-history finish of " + r);
2604 }
2605 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002606 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002607 } else {
2608 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2609 + " on stop because we're just sleeping");
2610 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002612 }
2613
2614 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002615 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002616 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002617 try {
2618 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002619 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2620 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002621 r.state = ActivityState.STOPPING;
2622 if (DEBUG_VISBILITY) Slog.v(
2623 TAG, "Stopping visible=" + r.visible + " for " + r);
2624 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002625 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002626 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002627 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002628 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002629 r.setSleeping(true);
2630 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002631 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002632 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 } catch (Exception e) {
2634 // Maybe just ignore exceptions here... if the process
2635 // has crashed, our death notification will clean things
2636 // up.
2637 Slog.w(TAG, "Exception thrown during pause", e);
2638 // Just in case, assume it to be stopped.
2639 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002640 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 r.state = ActivityState.STOPPED;
2642 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002643 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002644 }
2645 }
2646 }
2647 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002648
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002649 /**
2650 * @return Returns true if the activity is being finished, false if for
2651 * some reason it is being left as-is.
2652 */
2653 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002654 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002655 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002656 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002657 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002658 + ", result=" + resultCode + ", data=" + resultData
2659 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002660 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 return false;
2662 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663
Craig Mautnerd44711d2013-02-23 11:24:36 -08002664 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002665 return true;
2666 }
2667
Craig Mautnerd2328952013-03-05 12:46:26 -08002668 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002669 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2670 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2671 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2672 ActivityRecord r = activities.get(activityNdx);
2673 if (r.resultTo == self && r.requestCode == requestCode) {
2674 if ((r.resultWho == null && resultWho == null) ||
2675 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2676 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2677 false);
2678 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002679 }
2680 }
2681 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002682 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002683 }
2684
Todd Kennedy539db512014-12-15 09:57:55 -08002685 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002686 ActivityRecord r = topRunningActivityLocked(null);
2687 if (r != null && r.app == app) {
2688 // If the top running activity is from this crashing
2689 // process, then terminate it to avoid getting in a loop.
2690 Slog.w(TAG, " Force finishing activity "
2691 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002692 int taskNdx = mTaskHistory.indexOf(r.task);
2693 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002694 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002695 // Also terminate any activities below it that aren't yet
2696 // stopped, to avoid a situation where one will get
2697 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002698 --activityNdx;
2699 if (activityNdx < 0) {
2700 do {
2701 --taskNdx;
2702 if (taskNdx < 0) {
2703 break;
2704 }
2705 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2706 } while (activityNdx < 0);
2707 }
2708 if (activityNdx >= 0) {
2709 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002710 if (r.state == ActivityState.RESUMED
2711 || r.state == ActivityState.PAUSING
2712 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002713 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002714 Slog.w(TAG, " Force finishing activity "
2715 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002716 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002717 }
2718 }
2719 }
2720 }
2721 }
2722
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002723 final void finishVoiceTask(IVoiceInteractionSession session) {
2724 IBinder sessionBinder = session.asBinder();
2725 boolean didOne = false;
2726 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2727 TaskRecord tr = mTaskHistory.get(taskNdx);
2728 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2729 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2730 ActivityRecord r = tr.mActivities.get(activityNdx);
2731 if (!r.finishing) {
2732 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2733 false);
2734 didOne = true;
2735 }
2736 }
2737 }
2738 }
2739 if (didOne) {
2740 mService.updateOomAdjLocked();
2741 }
2742 }
2743
Craig Mautnerd2328952013-03-05 12:46:26 -08002744 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002745 ArrayList<ActivityRecord> activities = r.task.mActivities;
2746 for (int index = activities.indexOf(r); index >= 0; --index) {
2747 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002748 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002749 break;
2750 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002751 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002752 }
2753 return true;
2754 }
2755
Dianne Hackborn5c607432012-02-28 14:44:19 -08002756 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2757 // send the result
2758 ActivityRecord resultTo = r.resultTo;
2759 if (resultTo != null) {
2760 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2761 + " who=" + r.resultWho + " req=" + r.requestCode
2762 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002763 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002764 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002765 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002766 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002767 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002768 if (r.info.applicationInfo.uid > 0) {
2769 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2770 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002771 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002772 }
2773 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2774 resultData);
2775 r.resultTo = null;
2776 }
2777 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2778
2779 // Make sure this HistoryRecord is not holding on to other resources,
2780 // because clients have remote IPC references to this object so we
2781 // can't assume that will go away and want to avoid circular IPC refs.
2782 r.results = null;
2783 r.pendingResults = null;
2784 r.newIntents = null;
2785 r.icicle = null;
2786 }
2787
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002788 /**
2789 * @return Returns true if this activity has been removed from the history
2790 * list, or false if it is still in the list and will be removed later.
2791 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002792 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2793 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002794 if (r.finishing) {
2795 Slog.w(TAG, "Duplicate finish request for " + r);
2796 return false;
2797 }
2798
Wale Ogunwale7d701172015-03-11 15:36:30 -07002799 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002800 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002802 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002803 task.taskId, r.shortComponentName, reason);
2804 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002805 final int index = activities.indexOf(r);
2806 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002807 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002808 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002809 // If the caller asked that this activity (and all above it)
2810 // be cleared when the task is reset, don't lose that information,
2811 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002812 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002813 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 }
2815 }
2816
2817 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002818
Craig Mautner299f9602015-01-26 09:47:33 -08002819 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002820
Dianne Hackborn5c607432012-02-28 14:44:19 -08002821 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002822
Craig Mautnerde4ef022013-04-07 19:01:33 -07002823 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002824 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002825 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002827 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002828 ? AppTransition.TRANSIT_TASK_CLOSE
2829 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002830
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002832 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002833
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002834 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2836 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002837 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 }
2839
Craig Mautneraea74a52014-03-08 14:23:10 -08002840 if (endTask) {
2841 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2842 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 } else if (r.state != ActivityState.PAUSING) {
2844 // If the activity is PAUSING, we will complete the finish once
2845 // it is done pausing; else we can just directly finish it here.
2846 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002847 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 } else {
2849 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2850 }
2851
2852 return false;
2853 }
2854
Craig Mautnerf3333272013-04-22 10:55:53 -07002855 static final int FINISH_IMMEDIATELY = 0;
2856 static final int FINISH_AFTER_PAUSE = 1;
2857 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858
Craig Mautnerf3333272013-04-22 10:55:53 -07002859 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 // First things first: if this activity is currently visible,
2861 // and the resumed activity is not yet visible, then hold off on
2862 // finishing until the resumed one becomes visible.
2863 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002864 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2865 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002866 if (mStackSupervisor.mStoppingActivities.size() > 3
2867 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 // If we already have a few activities waiting to stop,
2869 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002870 // them out. Or if r is the last of activity of the last task the stack
2871 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002872 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002873 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002874 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 }
2876 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002877 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2878 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002879 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002880 if (oomAdj) {
2881 mService.updateOomAdjLocked();
2882 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 return r;
2884 }
2885
2886 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002887 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002888 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002889 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890 if (mResumedActivity == r) {
2891 mResumedActivity = null;
2892 }
2893 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002894 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895 r.state = ActivityState.FINISHING;
2896
2897 if (mode == FINISH_IMMEDIATELY
2898 || prevState == ActivityState.STOPPED
2899 || prevState == ActivityState.INITIALIZING) {
2900 // If this activity is already stopped, we can just finish
2901 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002902 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002903 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002904 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002905 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002906 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002907 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002908 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2909 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002910 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002912
2913 // Need to go through the full pause cycle to get this
2914 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002915 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002916 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002917 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002918 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 return r;
2920 }
2921
Craig Mautneree36c772014-07-16 14:56:05 -07002922 void finishAllActivitiesLocked(boolean immediately) {
2923 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002924 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2925 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2926 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2927 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002928 noActivitiesInStack = false;
2929 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002930 continue;
2931 }
Craig Mautneree36c772014-07-16 14:56:05 -07002932 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002933 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2934 }
2935 }
Craig Mautneree36c772014-07-16 14:56:05 -07002936 if (noActivitiesInStack) {
2937 mActivityContainer.onTaskListEmptyLocked();
2938 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002939 }
2940
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002941 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2942 // Basic case: for simple app-centric recents, we need to recreate
2943 // the task if the affinity has changed.
2944 if (srec == null || srec.task.affinity == null ||
2945 !srec.task.affinity.equals(destAffinity)) {
2946 return true;
2947 }
2948 // Document-centric case: an app may be split in to multiple documents;
2949 // they need to re-create their task if this current activity is the root
2950 // of a document, unless simply finishing it will return them to the the
2951 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002952 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2953 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002954 // Okay, this activity is at the root of its task. What to do, what to do...
2955 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2956 // Finishing won't return to an application, so we need to recreate.
2957 return true;
2958 }
2959 // We now need to get the task below it to determine what to do.
2960 int taskIdx = mTaskHistory.indexOf(srec.task);
2961 if (taskIdx <= 0) {
2962 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2963 return false;
2964 }
2965 if (taskIdx == 0) {
2966 // At the bottom of the stack, nothing to go back to.
2967 return true;
2968 }
2969 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2970 if (!srec.task.affinity.equals(prevTask.affinity)) {
2971 // These are different apps, so need to recreate.
2972 return true;
2973 }
2974 }
2975 return false;
2976 }
2977
Wale Ogunwale7d701172015-03-11 15:36:30 -07002978 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002979 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002980 final TaskRecord task = srec.task;
2981 final ArrayList<ActivityRecord> activities = task.mActivities;
2982 final int start = activities.indexOf(srec);
2983 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002984 return false;
2985 }
2986 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002987 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002988 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002989 final ComponentName dest = destIntent.getComponent();
2990 if (start > 0 && dest != null) {
2991 for (int i = finishTo; i >= 0; i--) {
2992 ActivityRecord r = activities.get(i);
2993 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002994 r.info.name.equals(dest.getClassName())) {
2995 finishTo = i;
2996 parent = r;
2997 foundParentInTask = true;
2998 break;
2999 }
3000 }
3001 }
3002
3003 IActivityController controller = mService.mController;
3004 if (controller != null) {
3005 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3006 if (next != null) {
3007 // ask watcher if this is allowed
3008 boolean resumeOK = true;
3009 try {
3010 resumeOK = controller.activityResuming(next.packageName);
3011 } catch (RemoteException e) {
3012 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003013 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003014 }
3015
3016 if (!resumeOK) {
3017 return false;
3018 }
3019 }
3020 }
3021 final long origId = Binder.clearCallingIdentity();
3022 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003023 ActivityRecord r = activities.get(i);
3024 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003025 // Only return the supplied result for the first activity finished
3026 resultCode = Activity.RESULT_CANCELED;
3027 resultData = null;
3028 }
3029
3030 if (parent != null && foundParentInTask) {
3031 final int parentLaunchMode = parent.info.launchMode;
3032 final int destIntentFlags = destIntent.getFlags();
3033 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3034 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3035 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3036 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003037 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3038 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003039 } else {
3040 try {
3041 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3042 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003043 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003044 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003045 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003046 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003047 foundParentInTask = res == ActivityManager.START_SUCCESS;
3048 } catch (RemoteException e) {
3049 foundParentInTask = false;
3050 }
3051 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003052 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003053 }
3054 }
3055 Binder.restoreCallingIdentity(origId);
3056 return foundParentInTask;
3057 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058 /**
3059 * Perform the common clean-up of an activity record. This is called both
3060 * as part of destroyActivityLocked() (when destroying the client-side
3061 * representation) and cleaning things up as a result of its hosting
3062 * processing going away, in which case there is no remaining client-side
3063 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003064 *
3065 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003067 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3068 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 if (mResumedActivity == r) {
3070 mResumedActivity = null;
3071 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003072 if (mPausingActivity == r) {
3073 mPausingActivity = null;
3074 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003075 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076
3077 r.configDestroy = false;
3078 r.frozenBeforeDestroy = false;
3079
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003080 if (setState) {
3081 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3082 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003083 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003084 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003085 }
3086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 // Make sure this record is no longer in the pending finishes list.
3088 // This could happen, for example, if we are trimming activities
3089 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003090 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003091 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003092
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003093 // Remove any pending results.
3094 if (r.finishing && r.pendingResults != null) {
3095 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3096 PendingIntentRecord rec = apr.get();
3097 if (rec != null) {
3098 mService.cancelIntentSenderLocked(rec, false);
3099 }
3100 }
3101 r.pendingResults = null;
3102 }
3103
3104 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003105 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003106 }
3107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003109 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003110 if (getVisibleBehindActivity() == r) {
3111 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003112 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003113 }
3114
3115 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003116 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003117 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003118 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003119 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003120 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121 }
3122
Craig Mautner299f9602015-01-26 09:47:33 -08003123 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003124 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003125 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003126 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003127 if (DEBUG_ADD_REMOVE) {
3128 RuntimeException here = new RuntimeException("here");
3129 here.fillInStackTrace();
3130 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003131 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003132 r.takeFromHistory();
3133 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003134 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003135 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003136 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003137 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003138 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003139 if (VALIDATE_TOKENS) {
3140 validateAppTokensLocked();
3141 }
Craig Mautner312ba862014-02-10 17:55:01 -08003142 final TaskRecord task = r.task;
3143 if (task != null && task.removeActivity(r)) {
3144 if (DEBUG_STACK) Slog.i(TAG,
3145 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003146 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3147 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003148 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003149 }
Craig Mautner299f9602015-01-26 09:47:33 -08003150 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003151 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003152 cleanUpActivityServicesLocked(r);
3153 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003156 /**
3157 * Perform clean-up of service connections in an activity record.
3158 */
3159 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3160 // Throw away any services that have been bound by this activity.
3161 if (r.connections != null) {
3162 Iterator<ConnectionRecord> it = r.connections.iterator();
3163 while (it.hasNext()) {
3164 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003165 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 }
3167 r.connections = null;
3168 }
3169 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003170
Craig Mautneree2e45a2014-06-27 12:10:03 -07003171 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003172 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003173 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003174 mHandler.sendMessage(msg);
3175 }
3176
Craig Mautneree2e45a2014-06-27 12:10:03 -07003177 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003178 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003179 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003180 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3181 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3182 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3183 final ActivityRecord r = activities.get(activityNdx);
3184 if (r.finishing) {
3185 continue;
3186 }
3187 if (r.fullscreen) {
3188 lastIsOpaque = true;
3189 }
3190 if (owner != null && r.app != owner) {
3191 continue;
3192 }
3193 if (!lastIsOpaque) {
3194 continue;
3195 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003196 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003197 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3198 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003199 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003200 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003201 activityRemoved = true;
3202 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003203 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003204 }
3205 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003206 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003207 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003208 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003209 }
3210
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003211 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3212 if (r.isDestroyable()) {
3213 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3214 + " resumed=" + mResumedActivity
3215 + " pausing=" + mPausingActivity + " for reason " + reason);
3216 return destroyActivityLocked(r, true, reason);
3217 }
3218 return false;
3219 }
3220
3221 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3222 String reason) {
3223 // Iterate over tasks starting at the back (oldest) first.
3224 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3225 int maxTasks = tasks.size() / 4;
3226 if (maxTasks < 1) {
3227 maxTasks = 1;
3228 }
3229 int numReleased = 0;
3230 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3231 final TaskRecord task = mTaskHistory.get(taskNdx);
3232 if (!tasks.contains(task)) {
3233 continue;
3234 }
3235 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3236 int curNum = 0;
3237 final ArrayList<ActivityRecord> activities = task.mActivities;
3238 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3239 final ActivityRecord activity = activities.get(actNdx);
3240 if (activity.app == app && activity.isDestroyable()) {
3241 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3242 + " in state " + activity.state + " resumed=" + mResumedActivity
3243 + " pausing=" + mPausingActivity + " for reason " + reason);
3244 destroyActivityLocked(activity, true, reason);
3245 if (activities.get(actNdx) != activity) {
3246 // Was removed from list, back up so we don't miss the next one.
3247 actNdx--;
3248 }
3249 curNum++;
3250 }
3251 }
3252 if (curNum > 0) {
3253 numReleased += curNum;
3254 maxTasks--;
3255 if (mTaskHistory.get(taskNdx) != task) {
3256 // The entire task got removed, back up so we don't miss the next one.
3257 taskNdx--;
3258 }
3259 }
3260 }
3261 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3262 return numReleased;
3263 }
3264
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003265 /**
3266 * Destroy the current CLIENT SIDE instance of an activity. This may be
3267 * called both when actually finishing an activity, or when performing
3268 * a configuration switch where we destroy the current client-side object
3269 * but then create a new client-side object for this same HistoryRecord.
3270 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003271 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003272 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003273 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003274 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3275 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003276 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003277 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278
3279 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003280
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003281 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282
3283 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003284
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003285 if (hadApp) {
3286 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003287 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3289 mService.mHeavyWeightProcess = null;
3290 mService.mHandler.sendEmptyMessage(
3291 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3292 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003293 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003294 // Update any services we are bound to that might care about whether
3295 // their client may have activities.
3296 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003297 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003298 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003299 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 }
3301 }
3302
3303 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 try {
3306 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003307 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 r.configChangeFlags);
3309 } catch (Exception e) {
3310 // We can just ignore exceptions here... if the process
3311 // has crashed, our death notification will clean things
3312 // up.
3313 //Slog.w(TAG, "Exception thrown during finish", e);
3314 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003315 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003316 removedFromHistory = true;
3317 skipDestroy = true;
3318 }
3319 }
3320
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003322
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003323 // If the activity is finishing, we need to wait on removing it
3324 // from the list to give it a chance to do its cleanup. During
3325 // that time it may make calls back with its token so we need to
3326 // be able to find it on the list and so we don't want to remove
3327 // it from the list yet. Otherwise, we can just immediately put
3328 // it in the destroyed state since we are not removing it from the
3329 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003331 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3332 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003334 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3336 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003337 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003339 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003340 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341 }
3342 } else {
3343 // remove this record from the history.
3344 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003345 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 removedFromHistory = true;
3347 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003348 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003350 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003351 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 }
3353 }
3354
3355 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003356
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 if (!mLRUActivities.remove(r) && hadApp) {
3358 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3359 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003360
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 return removedFromHistory;
3362 }
3363
Craig Mautner299f9602015-01-26 09:47:33 -08003364 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003365 final long origId = Binder.clearCallingIdentity();
3366 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003367 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003368 if (r != null) {
3369 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003370 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003371 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003372
Wale Ogunwale60454db2015-01-23 16:05:07 -08003373 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003374 if (r.state == ActivityState.DESTROYING) {
3375 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003376 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003377 }
3378 }
Craig Mautner05d29032013-05-03 13:40:13 -07003379 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003380 } finally {
3381 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 }
3383 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003384
Todd Kennedyaab56db2015-01-30 09:39:53 -08003385 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003386 if (hasVisibleBehindActivity() &&
3387 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003388 if (r == topRunningActivityLocked(null)) {
3389 // Don't release the top activity if it has requested to run behind the next
3390 // activity.
3391 return;
3392 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003393 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3394 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003395 " thread=" + r.app.thread);
3396 if (r != null && r.app != null && r.app.thread != null) {
3397 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003398 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003399 } catch (RemoteException e) {
3400 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003401 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003402 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003403 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003404 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003405 }
3406 }
3407 }
3408
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003409 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003410 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3411 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003412 if (r != null) {
3413 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003414 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003415 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003416 }
3417 mStackSupervisor.resumeTopActivitiesLocked();
3418 }
3419
Jose Lima4b6c6692014-08-12 17:41:12 -07003420 boolean hasVisibleBehindActivity() {
3421 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003422 }
3423
Jose Lima4b6c6692014-08-12 17:41:12 -07003424 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003425 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003426 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003427 }
3428 }
3429
Jose Lima4b6c6692014-08-12 17:41:12 -07003430 ActivityRecord getVisibleBehindActivity() {
3431 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003432 }
3433
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003434 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3435 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003436 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003437 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3438 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003439 while (i > 0) {
3440 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003441 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003442 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003443 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003444 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003445 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003446 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003447 }
3448 }
3449 }
3450
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003451 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3452 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003453 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3454 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003455 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3456 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003457 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003458 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003459 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3460 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003461
3462 boolean hasVisibleActivities = false;
3463
3464 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003465 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003466 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3467 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3469 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3470 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3471 final ActivityRecord r = activities.get(activityNdx);
3472 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003473 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3474 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003475 if (r.app == app) {
Craig Mautneracebdc82015-02-24 10:53:03 -08003476 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003477 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3478 // Don't currently have state for the activity, or
3479 // it is finishing -- always remove it.
3480 remove = true;
3481 } else if (r.launchCount > 2 &&
3482 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3483 // We have launched this activity too many times since it was
3484 // able to run, so give up and remove it.
3485 remove = true;
3486 } else {
3487 // The process may be gone, but the activity lives on!
3488 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003489 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003490 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003491 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003492 RuntimeException here = new RuntimeException("here");
3493 here.fillInStackTrace();
3494 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3495 + ": haveState=" + r.haveState
3496 + " stateNotNeeded=" + r.stateNotNeeded
3497 + " finishing=" + r.finishing
3498 + " state=" + r.state, here);
3499 }
3500 if (!r.finishing) {
3501 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3502 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3503 r.userId, System.identityHashCode(r),
3504 r.task.taskId, r.shortComponentName,
3505 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003506 if (r.state == ActivityState.RESUMED) {
3507 mService.updateUsageStats(r, false);
3508 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003509 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003510 } else {
3511 // We have the current state for this activity, so
3512 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003513 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003514 TAG, "Keeping entry, setting app to null");
3515 if (r.visible) {
3516 hasVisibleActivities = true;
3517 }
3518 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3519 + r);
3520 r.app = null;
3521 r.nowVisible = false;
3522 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003523 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003524 "App died, clearing saved state of " + r);
3525 r.icicle = null;
3526 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003527 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003528 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003529 if (remove) {
3530 removeActivityFromHistoryLocked(r, "appDied");
3531 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003532 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003533 }
3534 }
3535
3536 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003537 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003538
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003539 final void updateTransitLocked(int transit, Bundle options) {
3540 if (options != null) {
3541 ActivityRecord r = topRunningActivityLocked(null);
3542 if (r != null && r.state != ActivityState.RESUMED) {
3543 r.updateOptionsLocked(options);
3544 } else {
3545 ActivityOptions.abort(options);
3546 }
3547 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003548 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003549 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003550
Craig Mautner21d24a22014-04-23 11:45:37 -07003551 void updateTaskMovement(TaskRecord task, boolean toFront) {
3552 if (task.isPersistable) {
3553 task.mLastTimeMoved = System.currentTimeMillis();
3554 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3555 // recently will be most negative, tasks sent to the bottom before that will be less
3556 // negative. Similarly for recent tasks moved to the top which will be most positive.
3557 if (!toFront) {
3558 task.mLastTimeMoved *= -1;
3559 }
3560 }
3561 }
3562
Craig Mautner84984fa2014-06-19 11:19:20 -07003563 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003564 final int top = mTaskHistory.size() - 1;
3565 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3566 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003567 if (task.taskType == homeStackTaskType) {
3568 if (DEBUG_TASKS || DEBUG_STACK)
3569 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003570 mTaskHistory.remove(taskNdx);
3571 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003572 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003573 mWindowManager.moveTaskToTop(task.taskId);
3574 return;
3575 }
3576 }
3577 }
3578
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003579 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003580 String reason) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003581 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003582
Craig Mautner11bf9a52013-02-19 14:08:51 -08003583 final int numTasks = mTaskHistory.size();
3584 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003585 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003586 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003587 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003588 ActivityOptions.abort(options);
3589 } else {
3590 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3591 }
3592 return;
3593 }
3594
3595 // Shift all activities with this task up to the top
3596 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003597 insertTaskAtTop(tr);
Craig Mautner299f9602015-01-26 09:47:33 -08003598 moveToFront(reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003599
3600 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003601 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003602 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003603 ActivityRecord r = topRunningActivityLocked(null);
3604 if (r != null) {
3605 mNoAnimActivities.add(r);
3606 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003607 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003609 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003610 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003611
Craig Mautner05d29032013-05-03 13:40:13 -07003612 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003613 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003614
3615 if (VALIDATE_TOKENS) {
3616 validateAppTokensLocked();
3617 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003618 }
3619
3620 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003621 * Worker method for rearranging history stack. Implements the function of moving all
3622 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003624 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003625 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3626 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003627 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003628 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003629 * @return Returns true if the move completed, false if not.
3630 */
Craig Mautner299f9602015-01-26 09:47:33 -08003631 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003632 final TaskRecord tr = taskForIdLocked(taskId);
3633 if (tr == null) {
3634 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3635 return false;
3636 }
3637
3638 Slog.i(TAG, "moveTaskToBack: " + tr);
3639
3640 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003641
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003642 // If we have a watcher, preflight the move before committing to it. First check
3643 // for *other* available tasks, but if none are available, then try again allowing the
3644 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003645 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003646 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003647 if (next == null) {
3648 next = topRunningActivityLocked(null, 0);
3649 }
3650 if (next != null) {
3651 // ask watcher if this is allowed
3652 boolean moveOK = true;
3653 try {
3654 moveOK = mService.mController.activityResuming(next.packageName);
3655 } catch (RemoteException e) {
3656 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003657 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658 }
3659 if (!moveOK) {
3660 return false;
3661 }
3662 }
3663 }
3664
Wale Ogunwalecb82f302015-02-25 07:53:40 -08003665 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666
riddle_hsuc215a4f2014-12-27 12:10:45 +08003667 boolean prevIsHome = false;
3668 if (tr.isOverHomeStack()) {
3669 final TaskRecord nextTask = getNextTask(tr);
3670 if (nextTask != null) {
3671 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3672 } else {
3673 prevIsHome = true;
3674 }
3675 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003676 mTaskHistory.remove(tr);
3677 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003678 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003679
Craig Mautnerc8143c62013-09-03 12:15:57 -07003680 // There is an assumption that moving a task to the back moves it behind the home activity.
3681 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003682 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003683 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3684 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003685 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003686 break;
3687 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003688 if (taskNdx == 1) {
3689 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003690 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003691 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003692 }
3693
Craig Mautner299f9602015-01-26 09:47:33 -08003694 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003695 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003696
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003697 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003698 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003699 }
3700
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003701 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003702 if (prevIsHome || task == tr && tr.isOverHomeStack()
3703 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003704 if (!mService.mBooting && !mService.mBooted) {
3705 // Not ready yet!
3706 return false;
3707 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003708 final int taskToReturnTo = tr.getTaskToReturnTo();
3709 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003710 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003711 }
3712
Craig Mautner05d29032013-05-03 13:40:13 -07003713 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003714 return true;
3715 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003716
Craig Mautner8849a5e2013-04-02 16:41:03 -07003717 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003718 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003719 final Uri data = r.intent.getData();
3720 final String strData = data != null ? data.toSafeString() : null;
3721
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003722 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003723 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003724 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003725 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003726 }
3727
3728 /**
3729 * Make sure the given activity matches the current configuration. Returns
3730 * false if the activity had to be destroyed. Returns true if the
3731 * configuration is the same, or the activity will remain running as-is
3732 * for whatever reason. Ensures the HistoryRecord is updated with the
3733 * correct configuration and all other bookkeeping is handled.
3734 */
3735 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3736 int globalChanges) {
3737 if (mConfigWillChange) {
3738 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3739 "Skipping config check (will change): " + r);
3740 return true;
3741 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003742
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003743 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3744 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003745
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003746 // Make sure the current stack override configuration is supported by the top task
3747 // before continuing.
3748 final TaskRecord topTask = topTask();
3749 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3750 || (!topTask.mResizeable && !mFullscreen))) {
3751 final boolean prevFullscreen = mFullscreen;
3752 final Configuration newOverrideConfig =
3753 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3754 updateOverrideConfiguration(newOverrideConfig);
3755 mForcedFullscreen = !prevFullscreen && mFullscreen;
3756 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3757 "Updated stack config to support task=" + topTask
3758 + " resizeable=" + topTask.mResizeable
3759 + " mForcedFullscreen=" + mForcedFullscreen
3760 + " prevFullscreen=" + prevFullscreen
3761 + " mFullscreen=" + mFullscreen);
3762 }
3763
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 // Short circuit: if the two configurations are the exact same
3765 // object (the common case), then there is nothing to do.
3766 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003767 if (r.configuration == newConfig
3768 && r.stackConfigOverride == mOverrideConfig
3769 && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003770 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3771 "Configuration unchanged in " + r);
3772 return true;
3773 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003774
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003775 // We don't worry about activities that are finishing.
3776 if (r.finishing) {
3777 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3778 "Configuration doesn't matter in finishing " + r);
3779 r.stopFreezingScreenLocked(false);
3780 return true;
3781 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003782
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003783 // Okay we now are going to make this activity have the new config.
3784 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003785 final Configuration oldConfig = r.configuration;
3786 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003787 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003788 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003789
3790 // Determine what has changed. May be nothing, if this is a config
3791 // that has come back from the app after going idle. In that case
3792 // we just want to leave the official config object now in the
3793 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003794 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003795 if (stackChanges == 0) {
3796 // {@link Configuration#diff} doesn't catch changes from unset values.
3797 // Check for changes we care about.
3798 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3799 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3800 }
3801 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3802 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3803 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3804 }
3805 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3806 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3807 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003808 }
3809 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003810 if (changes == 0 && !r.forceNewConfig) {
3811 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3812 "Configuration no differences in " + r);
3813 return true;
3814 }
3815
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003816 // If the activity isn't currently running, just leave the new
3817 // configuration and it will pick that up next time it starts.
3818 if (r.app == null || r.app.thread == null) {
3819 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3820 "Configuration doesn't matter not running " + r);
3821 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003822 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003823 return true;
3824 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003825
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003826 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003827 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3828 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3829 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003830 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003831 + ", newConfig=" + newConfig);
3832 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003833 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003834 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3835 r.configChangeFlags |= changes;
3836 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003837 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003838 if (r.app == null || r.app.thread == null) {
3839 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003840 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003841 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003842 } else if (r.state == ActivityState.PAUSING) {
3843 // A little annoying: we are waiting for this activity to
3844 // finish pausing. Let's not do anything now, but just
3845 // flag that it needs to be restarted when done pausing.
3846 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003847 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003848 r.configDestroy = true;
3849 return true;
3850 } else if (r.state == ActivityState.RESUMED) {
3851 // Try to optimize this case: the configuration is changing
3852 // and we need to restart the top, resumed activity.
3853 // Instead of doing the normal handshaking, just say
3854 // "restart!".
3855 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003856 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003857 relaunchActivityLocked(r, r.configChangeFlags, true);
3858 r.configChangeFlags = 0;
3859 } else {
3860 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003861 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003862 relaunchActivityLocked(r, r.configChangeFlags, false);
3863 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003864 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003865
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003866 // All done... tell the caller we weren't able to keep this
3867 // activity around.
3868 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003869 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003870
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003871 // Default case: the activity can handle this new configuration, so hand it over.
3872 // NOTE: We only forward the stack override configuration as the system level configuration
3873 // changes is always sent to all processes when they happen so it can just use whatever
3874 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003875 if (r.app != null && r.app.thread != null) {
3876 try {
3877 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003878 r.app.thread.scheduleActivityConfigurationChanged(
3879 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 } catch (RemoteException e) {
3881 // If process died, whatever.
3882 }
3883 }
3884 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003885
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003886 return true;
3887 }
3888
Craig Mautnerc8143c62013-09-03 12:15:57 -07003889 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003890 int changes, boolean andResume) {
3891 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003892 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003893 if (andResume) {
3894 results = r.results;
3895 newIntents = r.newIntents;
3896 }
3897 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3898 + " with results=" + results + " newIntents=" + newIntents
3899 + " andResume=" + andResume);
3900 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003901 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003903
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003904 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003905
Craig Mautner34b73df2014-01-12 21:11:08 -08003906 mStackSupervisor.removeChildActivityContainers(r);
3907
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003908 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003909 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3910 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3911 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003912 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003913 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3914 !andResume, new Configuration(mService.mConfiguration),
3915 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003916 // Note: don't need to call pauseIfSleepingLocked() here, because
3917 // the caller will only pass in 'andResume' if this activity is
3918 // currently resumed, which implies we aren't sleeping.
3919 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003920 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003921 }
3922
3923 if (andResume) {
3924 r.results = null;
3925 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003926 r.state = ActivityState.RESUMED;
3927 } else {
3928 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3929 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 }
3931
3932 return true;
3933 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003934
3935 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003936 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3937 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3938 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3939 final ActivityRecord r = activities.get(activityNdx);
3940 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003941 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003942 }
3943 if (r.fullscreen && !r.finishing) {
3944 return false;
3945 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003946 }
3947 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003948 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003949 if (r == null) {
3950 return false;
3951 }
3952 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3953 + " would have returned true for r=" + r);
3954 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003955 }
3956
3957 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003958 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3959 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3960 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3961 final ActivityRecord r = activities.get(activityNdx);
3962 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003963 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003964 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003965 }
3966 }
3967 }
3968
3969 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3970 boolean didSomething = false;
3971 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003972 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003973 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3974 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3975 int numActivities = activities.size();
3976 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3977 ActivityRecord r = activities.get(activityNdx);
3978 final boolean samePackage = r.packageName.equals(name)
3979 || (name == null && r.userId == userId);
3980 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3981 && (samePackage || r.task == lastTask)
3982 && (r.app == null || evenPersistent || !r.app.persistent)) {
3983 if (!doit) {
3984 if (r.finishing) {
3985 // If this activity is just finishing, then it is not
3986 // interesting as far as something to stop.
3987 continue;
3988 }
3989 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003990 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003991 if (r.isHomeActivity()) {
3992 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3993 Slog.i(TAG, "Skip force-stop again " + r);
3994 continue;
3995 } else {
3996 homeActivity = r.realActivity;
3997 }
3998 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003999 didSomething = true;
4000 Slog.i(TAG, " Force finishing activity " + r);
4001 if (samePackage) {
4002 if (r.app != null) {
4003 r.app.removed = true;
4004 }
4005 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004006 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004007 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004008 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4009 true)) {
4010 // r has been deleted from mActivities, accommodate.
4011 --numActivities;
4012 --activityNdx;
4013 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004014 }
4015 }
4016 }
4017 return didSomething;
4018 }
4019
Dianne Hackborn09233282014-04-30 11:33:59 -07004020 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004021 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004022 final TaskRecord task = mTaskHistory.get(taskNdx);
4023 ActivityRecord r = null;
4024 ActivityRecord top = null;
4025 int numActivities = 0;
4026 int numRunning = 0;
4027 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07004028 if (activities.isEmpty()) {
4029 continue;
4030 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004031 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004032 continue;
4033 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004034 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4035 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004036
Craig Mautneraab647e2013-02-28 16:31:36 -08004037 // Initialize state for next task if needed.
4038 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4039 top = r;
4040 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004041 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004042
4043 // Add 'r' into the current task.
4044 numActivities++;
4045 if (r.app != null && r.app.thread != null) {
4046 numRunning++;
4047 }
4048
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004049 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004050 TAG, r.intent.getComponent().flattenToShortString()
4051 + ": task=" + r.task);
4052 }
4053
4054 RunningTaskInfo ci = new RunningTaskInfo();
4055 ci.id = task.taskId;
4056 ci.baseActivity = r.intent.getComponent();
4057 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004058 ci.lastActiveTime = task.lastActiveTime;
4059
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004060 if (top.task != null) {
4061 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004062 }
4063 ci.numActivities = numActivities;
4064 ci.numRunning = numRunning;
4065 //System.out.println(
4066 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08004067 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004068 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004069 }
4070
4071 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004072 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004073 if (DEBUG_SWITCH) Slog.d(
4074 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004075 if (top >= 0) {
4076 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4077 int activityTop = activities.size() - 1;
4078 if (activityTop > 0) {
4079 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4080 "unhandled-back", true);
4081 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004082 }
4083 }
4084
Craig Mautner6b74cb52013-09-27 17:02:21 -07004085 /**
4086 * Reset local parameters because an app's activity died.
4087 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004088 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004089 */
4090 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004091 if (mPausingActivity != null && mPausingActivity.app == app) {
4092 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
4093 "App died while pausing: " + mPausingActivity);
4094 mPausingActivity = null;
4095 }
4096 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4097 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004098 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004099 }
4100
Craig Mautner19091252013-10-05 00:03:53 -07004101 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004102 }
4103
Craig Mautnercae015f2013-02-08 14:31:27 -08004104 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004105 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4106 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4107 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4108 final ActivityRecord r = activities.get(activityNdx);
4109 if (r.app == app) {
4110 Slog.w(TAG, " Force finishing activity "
4111 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004112 // Force the destroy to skip right to removal.
4113 r.app = null;
4114 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004115 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004116 }
4117 }
4118 }
4119
Dianne Hackborn390517b2013-05-30 15:03:32 -07004120 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004121 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004122 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004123 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4124 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004125 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4126 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004127 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004128 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004129 if (printed) {
4130 header = null;
4131 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004132 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004133 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004134 }
4135
4136 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4137 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4138
4139 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004140 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4141 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004142 }
4143 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004144 final int top = mTaskHistory.size() - 1;
4145 if (top >= 0) {
4146 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4147 int listTop = list.size() - 1;
4148 if (listTop >= 0) {
4149 activities.add(list.get(listTop));
4150 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004151 }
4152 } else {
4153 ItemMatcher matcher = new ItemMatcher();
4154 matcher.build(name);
4155
Craig Mautneraab647e2013-02-28 16:31:36 -08004156 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4157 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4158 if (matcher.match(r1, r1.intent.getComponent())) {
4159 activities.add(r1);
4160 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004161 }
4162 }
4163 }
4164
4165 return activities;
4166 }
4167
4168 ActivityRecord restartPackage(String packageName) {
4169 ActivityRecord starting = topRunningActivityLocked(null);
4170
4171 // All activities that came from the package must be
4172 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004173 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4174 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4175 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4176 final ActivityRecord a = activities.get(activityNdx);
4177 if (a.info.packageName.equals(packageName)) {
4178 a.forceNewConfig = true;
4179 if (starting != null && a == starting && a.visible) {
4180 a.startFreezingScreenLocked(starting.app,
4181 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4182 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004183 }
4184 }
4185 }
4186
4187 return starting;
4188 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004189
Craig Mautner299f9602015-01-26 09:47:33 -08004190 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004191 removeTask(task, reason, true);
4192 }
4193
4194 void removeTask(TaskRecord task, String reason, boolean removeFromWindowManager) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004195 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004196 if (removeFromWindowManager) {
4197 mWindowManager.removeTask(task.taskId);
4198 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004199 final ActivityRecord r = mResumedActivity;
4200 if (r != null && r.task == task) {
4201 mResumedActivity = null;
4202 }
4203
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004204 final int taskNdx = mTaskHistory.indexOf(task);
4205 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004206 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4207 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4208 if (!nextTask.isOverHomeStack()) {
4209 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4210 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004211 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004212 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004213 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004214
4215 if (task.mActivities.isEmpty()) {
4216 final boolean isVoiceSession = task.voiceSession != null;
4217 if (isVoiceSession) {
4218 try {
4219 task.voiceSession.taskFinished(task.intent, task.taskId);
4220 } catch (RemoteException e) {
4221 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004222 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004223 if (task.autoRemoveFromRecents() || isVoiceSession) {
4224 // Task creator asked to remove this when done, or this task was a voice
4225 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004226 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004227 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004228 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004229 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004230
4231 if (mTaskHistory.isEmpty()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004232 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004233 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004234 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004235 String myReason = reason + " leftTaskHistoryEmpty";
4236 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4237 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4238 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004239 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004240 if (mStacks != null) {
4241 mStacks.remove(this);
4242 mStacks.add(0, this);
4243 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004244 if (notHomeStack) {
4245 mActivityContainer.onTaskListEmptyLocked();
4246 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004247 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004248
4249 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004250 }
4251
Dianne Hackborn91097de2014-04-04 18:02:06 -07004252 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4253 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4254 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004255 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4256 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004257 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004258 return task;
4259 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004260
4261 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004262 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004263 }
4264
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004265 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004266 task.stack = this;
4267 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004268 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004269 } else {
4270 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004271 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004272 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004273 if (!moving && task.voiceSession != null) {
4274 try {
4275 task.voiceSession.taskStarted(task.intent, task.taskId);
4276 } catch (RemoteException e) {
4277 }
4278 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004279 }
4280
4281 public int getStackId() {
4282 return mStackId;
4283 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004284
4285 @Override
4286 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004287 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4288 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004289 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004290
4291 boolean updateOverrideConfiguration(Configuration newConfig) {
4292 Configuration oldConfig = mOverrideConfig;
4293 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004294 // We override the configuration only when the stack's dimensions are different from
4295 // the display. In this manner, we know that if the override configuration is empty,
4296 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004297 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004298 return !mOverrideConfig.equals(oldConfig);
4299 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004300}