blob: 62c18a8d2885f77cd5af305695317f10ad45a1ae [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 Ogunwaled80c2632015-03-13 10:26:26 -0700510 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
511 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800512 if (isOnHomeDisplay()) {
513 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
514 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800515 final TaskRecord task = topTask();
516 if (task != null) {
517 mWindowManager.moveTaskToTop(task.taskId);
518 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800519 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800520 }
521
522 final boolean isAttached() {
523 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800524 }
525
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700526 /**
527 * Returns the top activity in any existing task matching the given
528 * Intent. Returns null if no such task is found.
529 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700530 ActivityRecord findTaskLocked(ActivityRecord target) {
531 Intent intent = target.intent;
532 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700533 ComponentName cls = intent.getComponent();
534 if (info.targetActivity != null) {
535 cls = new ComponentName(info.packageName, info.targetActivity);
536 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700537 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700538 boolean isDocument = intent != null & intent.isDocument();
539 // If documentData is non-null then it must match the existing task data.
540 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800541
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700542 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800543 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
544 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700545 if (task.voiceSession != null) {
546 // We never match voice sessions; those always run independently.
547 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
548 continue;
549 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700550 if (task.userId != userId) {
551 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700552 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700553 continue;
554 }
Craig Mautner000f0022013-02-26 15:04:29 -0800555 final ActivityRecord r = task.getTopActivity();
556 if (r == null || r.finishing || r.userId != userId ||
557 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700558 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800559 continue;
560 }
561
Craig Mautnerd00f4742014-03-12 14:17:26 -0700562 final Intent taskIntent = task.intent;
563 final Intent affinityIntent = task.affinityIntent;
564 final boolean taskIsDocument;
565 final Uri taskDocumentData;
566 if (taskIntent != null && taskIntent.isDocument()) {
567 taskIsDocument = true;
568 taskDocumentData = taskIntent.getData();
569 } else if (affinityIntent != null && affinityIntent.isDocument()) {
570 taskIsDocument = true;
571 taskDocumentData = affinityIntent.getData();
572 } else {
573 taskIsDocument = false;
574 taskDocumentData = null;
575 }
576
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700577 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700578 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700579 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700580 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700581 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
582 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700583 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700584 return r;
585 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700586 } else if (taskIntent != null && taskIntent.getComponent() != null &&
587 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700588 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700589 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800590 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700591 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
592 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800593 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700594 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
595 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700596 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700597 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800598 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700599 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
600 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800601 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700602 } else if (DEBUG_TASKS) {
603 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700604 }
605 }
606
607 return null;
608 }
609
610 /**
611 * Returns the first activity (starting from the top of the stack) that
612 * is the same as the given activity. Returns null if no such activity
613 * is found.
614 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700615 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700616 ComponentName cls = intent.getComponent();
617 if (info.targetActivity != null) {
618 cls = new ComponentName(info.packageName, info.targetActivity);
619 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700620 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700621
Craig Mautner000f0022013-02-26 15:04:29 -0800622 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700623 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100624 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700625 return null;
626 }
627 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800628 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
629 ActivityRecord r = activities.get(activityNdx);
630 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 //Slog.i(TAG, "Found matching class!");
632 //dump();
633 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
634 return r;
635 }
636 }
637 }
638
639 return null;
640 }
641
Amith Yamasani742a6712011-05-04 14:49:28 -0700642 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700643 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700644 */
Craig Mautner93529a42013-10-04 15:03:13 -0700645 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800646 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700647 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800648 }
649 mCurrentUser = userId;
650
651 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800652 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800653 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700654 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100655 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700656 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
657 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800658 mTaskHistory.remove(i);
659 mTaskHistory.add(task);
660 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800661 // Use same value for i.
662 } else {
663 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800664 }
665 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700666 if (VALIDATE_TOKENS) {
667 validateAppTokensLocked();
668 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700669 }
670
Craig Mautner2420ead2013-04-01 17:13:20 -0700671 void minimalResumeActivityLocked(ActivityRecord r) {
672 r.state = ActivityState.RESUMED;
673 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
674 + " (starting new instance)");
675 r.stopped = false;
676 mResumedActivity = r;
677 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800678 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700679 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700680 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700681 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700682 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700683 }
684
Dianne Hackborncee04b52013-07-03 17:01:28 -0700685 private void startLaunchTraces() {
686 if (mFullyDrawnStartTime != 0) {
687 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
688 }
689 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
690 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
691 }
692
693 private void stopFullyDrawnTraceIfNeeded() {
694 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
695 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
696 mFullyDrawnStartTime = 0;
697 }
698 }
699
Craig Mautnere79d42682013-04-01 19:01:53 -0700700 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700701 if (r.displayStartTime == 0) {
702 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
703 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700704 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700705 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700706 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700707 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700708 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700709 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700710 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700711 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800712
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700713 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700714 // Make sure that there is no activity waiting for this to launch.
715 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
716 r.displayStartTime = r.fullyDrawnStartTime = 0;
717 } else {
718 mStackSupervisor.removeTimeoutsForActivityLocked(r);
719 mStackSupervisor.scheduleIdleTimeoutLocked(r);
720 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700721 }
722
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800723 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800724 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800725 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
726 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
727 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
728 activities.get(activityNdx).setSleeping(false);
729 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800730 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800731 if (mPausingActivity != null) {
732 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
733 activityPausedLocked(mPausingActivity.appToken, true);
734 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800735 }
736
Craig Mautner0eea92c2013-05-16 13:35:39 -0700737 /**
738 * @return true if something must be done before going to sleep.
739 */
740 boolean checkReadyForSleepLocked() {
741 if (mResumedActivity != null) {
742 // Still have something resumed; can't sleep until it is paused.
743 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
744 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700745 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700746 return true;
747 }
748 if (mPausingActivity != null) {
749 // Still waiting for something to pause; can't sleep yet.
750 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
751 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800752 }
753
Craig Mautner0eea92c2013-05-16 13:35:39 -0700754 return false;
755 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800756
Craig Mautner0eea92c2013-05-16 13:35:39 -0700757 void goToSleep() {
758 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800759
Craig Mautner0eea92c2013-05-16 13:35:39 -0700760 // Make sure any stopped but visible activities are now sleeping.
761 // This ensures that the activity's onStop() is called.
762 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
763 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
764 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
765 final ActivityRecord r = activities.get(activityNdx);
766 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
767 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800768 }
769 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800770 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700771 }
Craig Mautner59c00972012-07-30 12:10:24 -0700772
Dianne Hackbornd2835932010-12-13 16:28:46 -0800773 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700774 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800775 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700776 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800777 return null;
778 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800779
Winson Chung083baf92014-07-11 10:32:42 -0700780 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700781 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800782 // we can just go ahead and skip taking the screenshot if this is the home stack.
783 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700784 return null;
785 }
786
Winson Chung48a10a52014-08-27 14:36:51 -0700787 int w = mService.mThumbnailWidth;
788 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800789 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800790 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
791 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
792 w, h, SCREENSHOT_FORCE_565);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800793 }
Winson Chung376543b2014-05-21 17:43:28 -0700794 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800795 return null;
796 }
797
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700798 /**
799 * Start pausing the currently resumed activity. It is an error to call this if there
800 * is already an activity being paused or there is no resumed activity.
801 *
802 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
803 * @param uiSleeping True if this is happening with the user interface going to sleep (the
804 * screen turning off).
805 * @param resuming True if this is being called as part of resuming the top activity, so
806 * we shouldn't try to instigate a resume here.
807 * @param dontWait True if the caller does not want to wait for the pause to complete. If
808 * set to true, we will immediately complete the pause here before returning.
809 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
810 * it to tell us when it is done.
811 */
812 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
813 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800814 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700815 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
816 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800817 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 ActivityRecord prev = mResumedActivity;
819 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700820 if (!resuming) {
821 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
822 mStackSupervisor.resumeTopActivitiesLocked();
823 }
824 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700825 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800826
827 if (mActivityContainer.mParentActivity == null) {
828 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700829 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800830 }
831
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700832 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
833 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800835 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700837 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
838 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 prev.state = ActivityState.PAUSING;
840 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700841 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700842 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800843 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800844 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700845 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700846 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847
848 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800849
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 if (prev.app != null && prev.app.thread != null) {
851 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
852 try {
853 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700854 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700855 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700856 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800857 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700858 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700859 } catch (Exception e) {
860 // Ignore exception, if process died other code will cleanup.
861 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800862 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700863 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700864 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700865 }
866 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800867 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700869 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 }
871
872 // If we are not going to sleep, we want to ensure the device is
873 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700874 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700875 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700876 }
877
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800878 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 // Have the window manager pause its key dispatching until the new
880 // activity has started. If we're pausing the activity just because
881 // the screen is being turned off and the UI is sleeping, don't interrupt
882 // key dispatch; the same activity will pick it up again on wakeup.
883 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800884 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885 } else {
886 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
887 }
888
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700889 if (dontWait) {
890 // If the caller said they don't want to wait for the pause, then complete
891 // the pause now.
892 completePauseLocked(false);
893 return false;
894
895 } else {
896 // Schedule a pause timeout in case the app doesn't respond.
897 // We don't give it much time because this directly impacts the
898 // responsiveness seen by the user.
899 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
900 msg.obj = prev;
901 prev.pauseTime = SystemClock.uptimeMillis();
902 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
903 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
904 return true;
905 }
906
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700907 } else {
908 // This activity failed to schedule the
909 // pause, so just treat it as being paused now.
910 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700911 if (!resuming) {
912 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
913 }
914 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915 }
916 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700917
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700918 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800920 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921
Craig Mautnerd2328952013-03-05 12:46:26 -0800922 final ActivityRecord r = isInStackLocked(token);
923 if (r != null) {
924 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
925 if (mPausingActivity == r) {
926 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
927 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700928 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800929 } else {
930 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
931 r.userId, System.identityHashCode(r), r.shortComponentName,
932 mPausingActivity != null
933 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700934 }
935 }
936 }
937
Craig Mautnera0026042014-04-23 11:45:37 -0700938 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
939 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700940 if (r.state != ActivityState.STOPPING) {
941 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
942 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
943 return;
944 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700945 if (persistentState != null) {
946 r.persistentState = persistentState;
947 mService.notifyTaskPersisterLocked(r.task, false);
948 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700949 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700950 if (icicle != null) {
951 // If icicle is null, this is happening due to a timeout, so we
952 // haven't really saved the state.
953 r.icicle = icicle;
954 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800955 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800956 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700957 }
958 if (!r.stopped) {
959 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
960 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
961 r.stopped = true;
962 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700963 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
964 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700965 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700966 if (r.finishing) {
967 r.clearOptionsLocked();
968 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700969 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700970 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700971 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700972 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700973 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800974 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700975 }
976 }
977 }
978
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700979 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800980 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700981 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800982
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800983 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700984 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800985 if (prev.finishing) {
986 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700987 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 } else if (prev.app != null) {
989 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800990 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800991 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
992 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800993 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800994 if (prev.configDestroy) {
995 // The previous is being paused because the configuration
996 // is changing, which means it is actually stopping...
997 // To juggle the fact that we are also starting a new
998 // instance right now, we need to first completely stop
999 // the current instance before starting the new one.
1000 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001001 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -07001002 } else if (!hasVisibleBehindActivity()) {
1003 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001004 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001005 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001006 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1007 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001008 // If we already have a few activities waiting to stop,
1009 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001010 // them out. Or if r is the last of activity of the last task the stack
1011 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001012 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001013 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001014 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001015 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001016 }
1017 }
1018 } else {
1019 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1020 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001021 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001022 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001024
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001025 if (resumeNext) {
1026 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1027 if (!mService.isSleepingOrShuttingDown()) {
1028 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1029 } else {
1030 mStackSupervisor.checkReadyForSleepLocked();
1031 ActivityRecord top = topStack.topRunningActivityLocked(null);
1032 if (top == null || (prev != null && top != prev)) {
1033 // If there are no more activities available to run,
1034 // do resume anyway to start something. Also if the top
1035 // activity on the stack is not the just paused activity,
1036 // we need to go ahead and resume it to ensure we complete
1037 // an in-flight app switch.
1038 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1039 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001040 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001042
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001043 if (prev != null) {
1044 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045
Craig Mautner525f3d92013-05-07 14:01:50 -07001046 if (prev.app != null && prev.cpuTimeAtResume > 0
1047 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001048 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1049 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001050 if (diff > 0) {
1051 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1052 synchronized (bsi) {
1053 BatteryStatsImpl.Uid.Proc ps =
1054 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001055 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001056 if (ps != null) {
1057 ps.addForegroundTimeLocked(diff);
1058 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059 }
1060 }
1061 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001062 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001063 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001064
1065 // Notfiy when the task stack has changed
1066 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067 }
1068
1069 /**
1070 * Once we know that we have asked an application to put an activity in
1071 * the resumed state (either by launching it or explicitly telling it),
1072 * this function updates the rest of our state to match that fact.
1073 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001074 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075 next.idle = false;
1076 next.results = null;
1077 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001078
1079 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1080 ProcessRecord app = next.task.mActivities.get(0).app;
1081 if (app != null && app != mService.mHomeProcess) {
1082 mService.mHomeProcess = app;
1083 }
1084 }
1085
Craig Mautner07566322013-09-26 16:42:55 -07001086 if (next.nowVisible) {
1087 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001088 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001089 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090
1091 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001092 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001093
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 mStackSupervisor.reportResumedActivityLocked(next);
1095
1096 next.resumeKeyDispatchingLocked();
1097 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001098
1099 // Mark the point when the activity is resuming
1100 // TODO: To be more accurate, the mark should be before the onCreate,
1101 // not after the onResume. But for subsequent starts, onResume is fine.
1102 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001103 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001104 } else {
1105 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1106 }
Winson Chung376543b2014-05-21 17:43:28 -07001107
George Mount6ba042b2014-07-28 11:12:28 -07001108 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001109
1110 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1111 // When resuming an activity, require it to call requestVisibleBehind() again.
1112 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1113 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001114 }
1115
Craig Mautnere3a00d72014-04-16 08:31:19 -07001116 private void setVisibile(ActivityRecord r, boolean visible) {
1117 r.visible = visible;
1118 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001119 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001120 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001121 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001122 container.setVisible(visible);
1123 }
1124 }
1125
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001126 // Find the first visible activity above the passed activity and if it is translucent return it
1127 // otherwise return null;
1128 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1129 TaskRecord task = r.task;
1130 if (task == null) {
1131 return null;
1132 }
1133
1134 ActivityStack stack = task.stack;
1135 if (stack == null) {
1136 return null;
1137 }
1138
1139 int stackNdx = mStacks.indexOf(stack);
1140
1141 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1142 int taskNdx = tasks.indexOf(task);
1143
1144 ArrayList<ActivityRecord> activities = task.mActivities;
1145 int activityNdx = activities.indexOf(r) + 1;
1146
1147 final int numStacks = mStacks.size();
1148 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001149 ActivityStack historyStack = mStacks.get(stackNdx);
1150 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001151 final int numTasks = tasks.size();
1152 while (taskNdx < numTasks) {
1153 activities = tasks.get(taskNdx).mActivities;
1154 final int numActivities = activities.size();
1155 while (activityNdx < numActivities) {
1156 final ActivityRecord activity = activities.get(activityNdx);
1157 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001158 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001159 }
1160 ++activityNdx;
1161 }
1162 activityNdx = 0;
1163 ++taskNdx;
1164 }
1165 taskNdx = 0;
1166 ++stackNdx;
1167 }
1168
1169 return null;
1170 }
1171
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001172 private ActivityStack getNextVisibleStackLocked() {
1173 ArrayList<ActivityStack> stacks = mStacks;
1174 final ActivityRecord parent = mActivityContainer.mParentActivity;
1175 if (parent != null) {
1176 stacks = parent.task.stack.mStacks;
1177 }
1178 if (stacks != null) {
1179 for (int i = stacks.size() - 1; i >= 0; --i) {
1180 ActivityStack stack = stacks.get(i);
1181 if (stack != this && stack.isStackVisibleLocked()) {
1182 return stack;
1183 }
1184 }
1185 }
1186 return null;
1187 }
1188
Jose Lima7ba71252014-04-30 12:59:27 -07001189 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1190 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001191 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001192 if (!isAttached()) {
1193 return false;
1194 }
1195
1196 if (mStackSupervisor.isFrontStack(this)) {
1197 return true;
1198 }
1199
Jose Lima729cb232014-05-05 15:59:40 -07001200 /**
1201 * Start at the task above this one and go up, looking for a visible
1202 * fullscreen activity, or a translucent activity that requested the
1203 * wallpaper to be shown behind it.
1204 */
Jose Lima7ba71252014-04-30 12:59:27 -07001205 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001206 ActivityStack stack = mStacks.get(i);
1207 // stack above isn't full screen, so, we assume we're still visible. at some point
1208 // we should look at the stack bounds to see if we're occluded even if the stack
1209 // isn't fullscreen
1210 if (!stack.mFullscreen) {
1211 continue;
1212 }
1213 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1214 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001215 final TaskRecord task = tasks.get(taskNdx);
1216 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001217 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001218 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001219
1220 // Conditions for an activity to obscure the stack we're
1221 // examining:
1222 // 1. Not Finishing AND Visible AND:
1223 // 2. Either:
1224 // - Full Screen Activity OR
1225 // - On top of Home and our stack is NOT home
1226 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001227 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001228 return false;
1229 }
1230 }
1231 }
1232 }
1233
1234 return true;
1235 }
1236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237 /**
1238 * Make sure that all activities that need to be visible (that is, they
1239 * currently can be seen by the user) actually are.
1240 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001241 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1242 ActivityRecord top = topRunningActivityLocked(null);
1243 if (top == null) {
1244 return;
1245 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001246 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001247 TAG, "ensureActivitiesVisible behind " + top
1248 + " configChanges=0x" + Integer.toHexString(configChanges));
1249
Craig Mautner5eda9b32013-07-02 11:58:16 -07001250 if (mTranslucentActivityWaiting != top) {
1251 mUndrawnActivitiesBelowTopTranslucent.clear();
1252 if (mTranslucentActivityWaiting != null) {
1253 // Call the callback with a timeout indication.
1254 notifyActivityDrawnLocked(null);
1255 mTranslucentActivityWaiting = null;
1256 }
1257 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1258 }
1259
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001260 // If the top activity is not fullscreen, then we need to
1261 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001262 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001263 boolean behindFullscreen = !isStackVisibleLocked();
Jose Lima7ba71252014-04-30 12:59:27 -07001264
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001265 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001266 final TaskRecord task = mTaskHistory.get(taskNdx);
1267 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001268 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1269 final ActivityRecord r = activities.get(activityNdx);
1270 if (r.finishing) {
1271 continue;
1272 }
1273 if (aboveTop && r != top) {
1274 continue;
1275 }
1276 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001277 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1278 // but must be drawn initially for the animation as though they were visible.
1279 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001280 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001281 TAG, "Make visible? " + r + " finishing=" + r.finishing
1282 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001283
Craig Mautnerd44711d2013-02-23 11:24:36 -08001284 // First: if this is not the current activity being started, make
1285 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001286 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001287 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001288 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001289
1290 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001291 // This activity needs to be visible, but isn't even
1292 // running... get it started, but don't resume it
1293 // at this point.
1294 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1295 if (r != starting) {
1296 r.startFreezingScreenLocked(r.app, configChanges);
1297 }
1298 if (!r.visible || r.mLaunchTaskBehind) {
1299 if (DEBUG_VISBILITY) Slog.v(
1300 TAG, "Starting and making visible: " + r);
1301 setVisibile(r, true);
1302 }
1303 if (r != starting) {
1304 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001305 }
1306
1307 } else if (r.visible) {
1308 // If this activity is already visible, then there is nothing
1309 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001310 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001312 try {
George Mount6ba042b2014-07-28 11:12:28 -07001313 if (r.returningOptions != null) {
1314 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1315 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001316 }
1317 } catch(RemoteException e) {
1318 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001319 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001320 // This activity is not currently visible, but is running.
1321 // Tell it to become visible.
1322 r.visible = true;
1323 if (r.state != ActivityState.RESUMED && r != starting) {
1324 // If this activity is paused, tell it
1325 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001326 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001327 TAG, "Making visible and scheduling visibility: " + r);
1328 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001329 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001330 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001331 mUndrawnActivitiesBelowTopTranslucent.add(r);
1332 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001333 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001334 r.sleeping = false;
1335 r.app.pendingUiClean = true;
1336 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1337 r.stopFreezingScreenLocked(false);
1338 } catch (Exception e) {
1339 // Just skip on any failure; we'll make it
1340 // visible when it next restarts.
1341 Slog.w(TAG, "Exception thrown making visibile: "
1342 + r.intent.getComponent(), e);
1343 }
1344 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001346
Craig Mautnerd44711d2013-02-23 11:24:36 -08001347 // Aggregate current change flags.
1348 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001349
Craig Mautnerd44711d2013-02-23 11:24:36 -08001350 if (r.fullscreen) {
1351 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001352 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1353 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001354 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001355 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001356 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001357 }
1358 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001359 if (DEBUG_VISBILITY) Slog.v(
1360 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1361 + " state=" + r.state
1362 + " behindFullscreen=" + behindFullscreen);
1363 // Now for any activities that aren't visible to the user, make
1364 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001365 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001366 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001367 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001368 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001369 switch (r.state) {
1370 case STOPPING:
1371 case STOPPED:
1372 if (r.app != null && r.app.thread != null) {
1373 if (DEBUG_VISBILITY) Slog.v(
1374 TAG, "Scheduling invisibility: " + r);
1375 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1376 }
1377 break;
1378
1379 case INITIALIZING:
1380 case RESUMED:
1381 case PAUSING:
1382 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001383 // This case created for transitioning activities from
1384 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001385 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001386 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001387 } else {
1388 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1389 mStackSupervisor.mStoppingActivities.add(r);
1390 }
1391 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001392 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001393 break;
1394
1395 default:
1396 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001397 }
1398 } catch (Exception e) {
1399 // Just skip on any failure; we'll make it
1400 // visible when it next restarts.
1401 Slog.w(TAG, "Exception thrown making hidden: "
1402 + r.intent.getComponent(), e);
1403 }
1404 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001405 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001406 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 }
1408 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001410
1411 if (mTranslucentActivityWaiting != null &&
1412 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1413 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1414 notifyActivityDrawnLocked(null);
1415 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 }
Craig Mautner58547802013-03-05 08:23:53 -08001417
Todd Kennedyaab56db2015-01-30 09:39:53 -08001418 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001419 mTranslucentActivityWaiting = r;
1420 mUndrawnActivitiesBelowTopTranslucent.clear();
1421 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1422 }
1423
1424 /**
1425 * Called as activities below the top translucent activity are redrawn. When the last one is
1426 * redrawn notify the top activity by calling
1427 * {@link Activity#onTranslucentConversionComplete}.
1428 *
1429 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1430 * occurred and the activity will be notified immediately.
1431 */
1432 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001433 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001434 if ((r == null)
1435 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1436 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1437 // The last undrawn activity below the top has just been drawn. If there is an
1438 // opaque activity at the top, notify it that it can become translucent safely now.
1439 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1440 mTranslucentActivityWaiting = null;
1441 mUndrawnActivitiesBelowTopTranslucent.clear();
1442 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1443
Craig Mautner71dd1b62014-02-18 15:48:52 -08001444 if (waitingActivity != null) {
1445 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1446 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1447 try {
1448 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1449 waitingActivity.appToken, r != null);
1450 } catch (RemoteException e) {
1451 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001452 }
1453 }
1454 }
1455 }
1456
Craig Mautnera61bc652013-10-28 15:43:18 -07001457 /** If any activities below the top running one are in the INITIALIZING state and they have a
1458 * starting window displayed then remove that starting window. It is possible that the activity
1459 * in this state will never resumed in which case that starting window will be orphaned. */
1460 void cancelInitializingActivities() {
1461 final ActivityRecord topActivity = topRunningActivityLocked(null);
1462 boolean aboveTop = true;
1463 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1464 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1465 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1466 final ActivityRecord r = activities.get(activityNdx);
1467 if (aboveTop) {
1468 if (r == topActivity) {
1469 aboveTop = false;
1470 }
1471 continue;
1472 }
1473
1474 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1475 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1476 r.mStartingWindowShown = false;
1477 mWindowManager.removeAppStartingWindow(r.appToken);
1478 }
1479 }
1480 }
1481 }
1482
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 /**
1484 * Ensure that the top activity in the stack is resumed.
1485 *
1486 * @param prev The previously resumed activity, for when in the process
1487 * of pausing; can be null to call from elsewhere.
1488 *
1489 * @return Returns true if something is being resumed, or false if
1490 * nothing happened.
1491 */
1492 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001493 return resumeTopActivityLocked(prev, null);
1494 }
1495
1496 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001497 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001498 // Don't even start recursing.
1499 return false;
1500 }
1501
1502 boolean result = false;
1503 try {
1504 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001505 mStackSupervisor.inResumeTopActivity = true;
1506 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1507 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001508 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001509 }
Craig Mautner544efa72014-09-04 16:41:20 -07001510 result = resumeTopActivityInnerLocked(prev, options);
1511 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001512 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001513 }
1514 return result;
1515 }
1516
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001517 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001518 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1519
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001520 if (!mService.mBooting && !mService.mBooted) {
1521 // Not ready yet!
1522 return false;
1523 }
1524
Craig Mautnerdf88d732014-01-27 09:21:32 -08001525 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001526 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001527 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001528 // Do not resume this stack if its parent is not resumed.
1529 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1530 return false;
1531 }
1532
Craig Mautnera61bc652013-10-28 15:43:18 -07001533 cancelInitializingActivities();
1534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001536 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537
1538 // Remember how we'll process this pause/resume situation, and ensure
1539 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001540 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1541 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542
Craig Mautner84984fa2014-06-19 11:19:20 -07001543 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001545 // There are no more activities!
1546 final String reason = "noMoreActivities";
1547 if (!mFullscreen) {
1548 // Try to move focus to the next visible stack with a running activity if this
1549 // stack is not covering the entire screen.
1550 final ActivityStack stack = getNextVisibleStackLocked();
1551 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1552 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1553 }
1554 }
1555 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001556 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001557 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001558 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001559 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001560 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1561 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1562 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001563 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001564 }
1565
1566 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001569 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1570 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 // Make sure we have executed any pending transitions, since there
1572 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001573 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001575 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001576 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001577 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 return false;
1579 }
1580
Craig Mautner525f3d92013-05-07 14:01:50 -07001581 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001582 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001583 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001584 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001585 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001586 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001587 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001588 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001589 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001590 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001591 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001592 } else if (!isOnHomeDisplay()) {
1593 return false;
1594 } else if (!isHomeStack()){
1595 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001596 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001597 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1598 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
Craig Mautner299f9602015-01-26 09:47:33 -08001599 return mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001600 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001601 }
1602
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 // If we are sleeping, and there is no resumed activity, and the top
1604 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001605 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001606 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001607 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 // Make sure we have executed any pending transitions, since there
1609 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001610 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001612 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001613 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001614 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 return false;
1616 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001617
1618 // Make sure that the user who owns this activity is started. If not,
1619 // we will just leave it as is because someone should be bringing
1620 // another user's activities to the top of the stack.
1621 if (mService.mStartedUsers.get(next.userId) == null) {
1622 Slog.w(TAG, "Skipping resume of top activity " + next
1623 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001624 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001625 return false;
1626 }
1627
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001628 // The activity may be waiting for stop, but that is no longer
1629 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001630 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001631 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001632 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001633 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634
1635 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1636
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001637 // If we are currently pausing an activity, then don't do anything
1638 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001639 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001640 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1641 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001642 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001643 return false;
1644 }
1645
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001646 // Okay we are now going to start a switch, to 'next'. We may first
1647 // have to pause the current activity, but this is an important point
1648 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001649 // XXX "App Redirected" dialog is getting too many false positives
1650 // at this point, so turn off for now.
1651 if (false) {
1652 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1653 long now = SystemClock.uptimeMillis();
1654 final boolean inTime = mLastStartedActivity.startTime != 0
1655 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1656 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1657 final int nextUid = next.info.applicationInfo.uid;
1658 if (inTime && lastUid != nextUid
1659 && lastUid != next.launchedFromUid
1660 && mService.checkPermission(
1661 android.Manifest.permission.STOP_APP_SWITCHES,
1662 -1, next.launchedFromUid)
1663 != PackageManager.PERMISSION_GRANTED) {
1664 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1665 } else {
1666 next.startTime = now;
1667 mLastStartedActivity = next;
1668 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001669 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001670 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001671 mLastStartedActivity = next;
1672 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001673 }
Craig Mautner58547802013-03-05 08:23:53 -08001674
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 // We need to start pausing the current activity so the top one
1676 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001677 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1678 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001679 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001680 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001681 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001682 }
1683 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001684 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1685 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001686 // At this point we want to put the upcoming activity's process
1687 // at the top of the LRU list, since we know we will be needing it
1688 // very soon and it would be a waste to let it get killed if it
1689 // happens to be sitting towards the end.
1690 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001691 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001692 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001693 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 return true;
1695 }
1696
Christopher Tated3f175c2012-06-14 14:16:54 -07001697 // If the most recent activity was noHistory but was only stopped rather
1698 // than stopped+finished because the device went to sleep, we need to make
1699 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001700 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001701 !mLastNoHistoryActivity.finishing) {
1702 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1703 " on new resume");
1704 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001705 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001706 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001707 }
1708
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001709 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001710 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1711 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001712 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 if (DEBUG_SWITCH) Slog.v(
1714 TAG, "Resuming top, waiting visible to hide: " + prev);
1715 } else {
1716 // The next activity is already visible, so hide the previous
1717 // activity's windows right now so we can show the new one ASAP.
1718 // We only do this if the previous is finishing, which should mean
1719 // it is on top of the one being resumed so hiding it quickly
1720 // is good. Otherwise, we want to do the normal route of allowing
1721 // the resumed activity to be shown so we can decide if the
1722 // previous should actually be hidden depending on whether the
1723 // new one is found to be full-screen or not.
1724 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001725 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1727 + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001728 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001729 + ", nowVisible=" + next.nowVisible);
1730 } else {
Craig Mautner8c14c152015-01-15 17:32:07 -08001731 if (DEBUG_SWITCH) Slog.v(TAG,
1732 "Previous already visible but still waiting to hide: " + prev
1733 + ", waitingVisible="
1734 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1735 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 }
1737 }
1738 }
1739
Dianne Hackborne7f97212011-02-24 14:40:20 -08001740 // Launching this app's activity, make sure the app is no longer
1741 // considered stopped.
1742 try {
1743 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001744 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001745 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001746 } catch (IllegalArgumentException e) {
1747 Slog.w(TAG, "Failed trying to unstop package "
1748 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001749 }
1750
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751 // We are starting up the next activity, so tell the window manager
1752 // that the previous one will be hidden soon. This way it can know
1753 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001754 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 if (prev != null) {
1756 if (prev.finishing) {
1757 if (DEBUG_TRANSITION) Slog.v(TAG,
1758 "Prepare close transition: prev=" + prev);
1759 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001760 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001761 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001763 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001764 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1765 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001766 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001767 mWindowManager.setAppWillBeHidden(prev.appToken);
1768 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001770 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001772 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001773 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001775 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001776 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001777 : next.mLaunchTaskBehind
1778 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1779 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 }
1781 }
1782 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001783 mWindowManager.setAppWillBeHidden(prev.appToken);
1784 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 }
Craig Mautner967212c2013-04-13 21:10:58 -07001786 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001787 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001789 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001790 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001792 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 }
1794 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001795
1796 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001797 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001798 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1799 if (opts != null) {
1800 resumeAnimOptions = opts.toBundle();
1801 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001802 next.applyOptionsLocked();
1803 } else {
1804 next.clearOptionsLocked();
1805 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806
Craig Mautnercf910b02013-04-23 11:23:27 -07001807 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 if (next.app != null && next.app.thread != null) {
1809 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1810
1811 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001812 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001813
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001814 // schedule launch ticks to collect information about slow apps.
1815 next.startLaunchTickingLocked();
1816
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001817 ActivityRecord lastResumedActivity =
1818 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819 ActivityState lastState = next.state;
1820
1821 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001822
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001823 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 next.state = ActivityState.RESUMED;
1825 mResumedActivity = next;
1826 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001827 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001828 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001830 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001831
1832 // Have the window manager re-evaluate the orientation of
1833 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001834 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001835 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001836 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001837 mService.mConfiguration,
1838 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1839 if (config != null) {
1840 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001841 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001842 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001844
Craig Mautner525f3d92013-05-07 14:01:50 -07001845 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001846 // The configuration update wasn't able to keep the existing
1847 // instance of the activity, and instead started a new one.
1848 // We should be all done, but let's just make sure our activity
1849 // is still at the top and schedule another run if something
1850 // weird happened.
1851 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001852 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001853 "Activity config changed during resume: " + next
1854 + ", new next: " + nextNext);
1855 if (nextNext != next) {
1856 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001857 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001858 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001859 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001860 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001861 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001862 return true;
1863 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001864 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001865 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001866 }
Craig Mautner58547802013-03-05 08:23:53 -08001867
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 try {
1869 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001870 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 if (a != null) {
1872 final int N = a.size();
1873 if (!next.finishing && N > 0) {
1874 if (DEBUG_RESULTS) Slog.v(
1875 TAG, "Delivering results to " + next
1876 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001877 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001878 }
1879 }
1880
1881 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001882 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883 }
1884
Craig Mautner299f9602015-01-26 09:47:33 -08001885 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1886 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001887
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001888 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001889 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001890 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001891 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001892 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001893 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001894 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001895
Craig Mautner0eea92c2013-05-16 13:35:39 -07001896 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897
Craig Mautnerac6f8432013-07-17 13:24:59 -07001898 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001899 } catch (Exception e) {
1900 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001901 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1902 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001904 if (lastStack != null) {
1905 lastStack.mResumedActivity = lastResumedActivity;
1906 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001907 Slog.i(TAG, "Restarting because process died: " + next);
1908 if (!next.hasBeenLaunched) {
1909 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001910 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1911 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001912 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001913 next.appToken, next.packageName, next.theme,
1914 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001915 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1916 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 }
George Mount2c92c972014-03-20 09:38:23 -07001918 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001919 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001920 return true;
1921 }
1922
1923 // From this point on, if something goes wrong there is no way
1924 // to recover the activity.
1925 try {
1926 next.visible = true;
1927 completeResumeLocked(next);
1928 } catch (Exception e) {
1929 // If any exception gets thrown, toss away this
1930 // activity and try the next one.
1931 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001932 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001933 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001934 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001935 return true;
1936 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937 next.stopped = false;
1938
1939 } else {
1940 // Whoops, need to restart this activity!
1941 if (!next.hasBeenLaunched) {
1942 next.hasBeenLaunched = true;
1943 } else {
1944 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001945 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001946 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001947 mService.compatibilityInfoForPackageLocked(
1948 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001950 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001951 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001952 }
1953 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1954 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001955 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001956 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 }
1958
Craig Mautnercf910b02013-04-23 11:23:27 -07001959 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001960 return true;
1961 }
1962
riddle_hsuc215a4f2014-12-27 12:10:45 +08001963 private TaskRecord getNextTask(TaskRecord targetTask) {
1964 final int index = mTaskHistory.indexOf(targetTask);
1965 if (index >= 0) {
1966 final int numTasks = mTaskHistory.size();
1967 for (int i = index + 1; i < numTasks; ++i) {
1968 TaskRecord task = mTaskHistory.get(i);
1969 if (task.userId == targetTask.userId) {
1970 return task;
1971 }
1972 }
1973 }
1974 return null;
1975 }
1976
Craig Mautnerac6f8432013-07-17 13:24:59 -07001977 private void insertTaskAtTop(TaskRecord task) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001978 // If the moving task is over home stack, transfer its return type to next task
1979 if (task.isOverHomeStack()) {
1980 final TaskRecord nextTask = getNextTask(task);
1981 if (nextTask != null) {
1982 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1983 }
1984 }
1985
Craig Mautner9c85c202013-10-04 20:11:26 -07001986 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08001987 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001988 if (isOnHomeDisplay()) {
1989 ActivityStack lastStack = mStackSupervisor.getLastStack();
1990 final boolean fromHome = lastStack.isHomeStack();
1991 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001992 task.setTaskToReturnTo(fromHome
1993 ? lastStack.topTask() == null
1994 ? HOME_ACTIVITY_TYPE
1995 : lastStack.topTask().taskType
1996 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001997 }
1998 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001999 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002000 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002001
Craig Mautnerac6f8432013-07-17 13:24:59 -07002002 mTaskHistory.remove(task);
2003 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002004 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01002005 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002006 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002007 while (--taskNdx >= 0) {
2008 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002009 break;
2010 }
2011 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002012 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002013 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002014 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002015 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002016 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002017
Craig Mautner8849a5e2013-04-02 16:41:03 -07002018 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002019 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002020 TaskRecord rTask = r.task;
2021 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002022 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2023 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002024 // Last activity in task had been removed or ActivityManagerService is reusing task.
2025 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002026 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002027 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002028 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002029 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002030 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002031 if (!newTask) {
2032 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002033 boolean startIt = true;
2034 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2035 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002036 if (task.getTopActivity() == null) {
2037 // All activities in task are finishing.
2038 continue;
2039 }
Craig Mautner70a86932013-02-28 22:37:44 -08002040 if (task == r.task) {
2041 // Here it is! Now, if this is not yet visible to the
2042 // user, then just add it without starting; it will
2043 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002044 if (!startIt) {
2045 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2046 + task, new RuntimeException("here").fillInStackTrace());
2047 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002048 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002049 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2050 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002051 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002052 r.userId, r.info.configChanges, task.voiceSession != null,
2053 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002055 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002057 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 return;
2059 }
2060 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002061 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 startIt = false;
2063 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 }
2065 }
2066
2067 // Place a new activity at top of stack, so it is next to interact
2068 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002069
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 // If we are not placing the new activity frontmost, we do not want
2071 // to deliver the onUserLeaving callback to the actual frontmost
2072 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002073 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002074 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002075 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2076 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 }
Craig Mautner70a86932013-02-28 22:37:44 -08002078
2079 task = r.task;
2080
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002082 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002083 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002084 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002085 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002086
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002087 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002088 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 // We want to show the starting preview window if we are
2090 // switching to a new task, or the next activity's process is
2091 // not currently running.
2092 boolean showStartingIcon = newTask;
2093 ProcessRecord proc = r.app;
2094 if (proc == null) {
2095 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2096 }
2097 if (proc == null || proc.thread == null) {
2098 showStartingIcon = true;
2099 }
2100 if (DEBUG_TRANSITION) Slog.v(TAG,
2101 "Prepare open transition: starting " + r);
2102 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002103 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002105 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002106 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002107 ? r.mLaunchTaskBehind
2108 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2109 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002110 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 mNoAnimActivities.remove(r);
2112 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002113 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002114 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002115 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002116 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 boolean doShow = true;
2118 if (newTask) {
2119 // Even though this activity is starting fresh, we still need
2120 // to reset it to make sure we apply affinities to move any
2121 // existing activities from other tasks in to it.
2122 // If the caller has requested that the target task be
2123 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002124 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002125 resetTaskIfNeededLocked(r, r);
2126 doShow = topRunningNonDelayedActivityLocked(null) == r;
2127 }
George Mount70778d72014-07-01 16:33:45 -07002128 } else if (options != null && new ActivityOptions(options).getAnimationType()
2129 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2130 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002132 if (r.mLaunchTaskBehind) {
2133 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2134 // tell WindowManager that r is visible even though it is at the back of the stack.
2135 mWindowManager.setAppVisibility(r.appToken, true);
2136 ensureActivitiesVisibleLocked(null, 0);
2137 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 // Figure out if we are transitioning from another activity that is
2139 // "has the same starting icon" as the next one. This allows the
2140 // window manager to keep the previous window it had previously
2141 // created, if it still had one.
2142 ActivityRecord prev = mResumedActivity;
2143 if (prev != null) {
2144 // We don't want to reuse the previous starting preview if:
2145 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002146 if (prev.task != r.task) {
2147 prev = null;
2148 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002149 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002150 else if (prev.nowVisible) {
2151 prev = null;
2152 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002154 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002155 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002156 mService.compatibilityInfoForPackageLocked(
2157 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002158 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002159 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002160 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 }
2162 } else {
2163 // If this is the first activity, don't do any fancy animations,
2164 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002165 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002166 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002167 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002168 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002169 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002170 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 }
2172 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002173 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 }
2175
2176 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002177 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002178 }
2179 }
2180
Dianne Hackbornbe707852011-11-11 14:32:10 -08002181 final void validateAppTokensLocked() {
2182 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002183 mValidateAppTokens.ensureCapacity(numActivities());
2184 final int numTasks = mTaskHistory.size();
2185 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2186 TaskRecord task = mTaskHistory.get(taskNdx);
2187 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002188 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002189 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002190 }
Craig Mautner000f0022013-02-26 15:04:29 -08002191 TaskGroup group = new TaskGroup();
2192 group.taskId = task.taskId;
2193 mValidateAppTokens.add(group);
2194 final int numActivities = activities.size();
2195 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2196 final ActivityRecord r = activities.get(activityNdx);
2197 group.tokens.add(r.appToken);
2198 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002199 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002200 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002201 }
2202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002203 /**
2204 * Perform a reset of the given task, if needed as part of launching it.
2205 * Returns the new HistoryRecord at the top of the task.
2206 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002207 /**
2208 * Helper method for #resetTaskIfNeededLocked.
2209 * We are inside of the task being reset... we'll either finish this activity, push it out
2210 * for another task, or leave it as-is.
2211 * @param task The task containing the Activity (taskTop) that might be reset.
2212 * @param forceReset
2213 * @return An ActivityOptions that needs to be processed.
2214 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002215 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002216 ActivityOptions topOptions = null;
2217
2218 int replyChainEnd = -1;
2219 boolean canMoveOptions = true;
2220
2221 // We only do this for activities that are not the root of the task (since if we finish
2222 // the root, we may no longer have the task!).
2223 final ArrayList<ActivityRecord> activities = task.mActivities;
2224 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002225 final int rootActivityNdx = task.findEffectiveRootIndex();
2226 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002227 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002228 if (target.frontOfTask)
2229 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230
2231 final int flags = target.info.flags;
2232 final boolean finishOnTaskLaunch =
2233 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2234 final boolean allowTaskReparenting =
2235 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2236 final boolean clearWhenTaskReset =
2237 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2238
2239 if (!finishOnTaskLaunch
2240 && !clearWhenTaskReset
2241 && target.resultTo != null) {
2242 // If this activity is sending a reply to a previous
2243 // activity, we can't do anything with it now until
2244 // we reach the start of the reply chain.
2245 // XXX note that we are assuming the result is always
2246 // to the previous activity, which is almost always
2247 // the case but we really shouldn't count on.
2248 if (replyChainEnd < 0) {
2249 replyChainEnd = i;
2250 }
2251 } else if (!finishOnTaskLaunch
2252 && !clearWhenTaskReset
2253 && allowTaskReparenting
2254 && target.taskAffinity != null
2255 && !target.taskAffinity.equals(task.affinity)) {
2256 // If this activity has an affinity for another
2257 // task, then we need to move it out of here. We will
2258 // move it as far out of the way as possible, to the
2259 // bottom of the activity stack. This also keeps it
2260 // correctly ordered with any activities we previously
2261 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002262 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002263 final ActivityRecord bottom =
2264 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002265 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002266 if (bottom != null && target.taskAffinity != null
2267 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002268 // If the activity currently at the bottom has the
2269 // same task affinity as the one we are moving,
2270 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002271 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002272 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002273 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002274 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002275 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002276 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002277 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002278 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2279 + " out to new task " + target.task);
2280 }
2281
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002283 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002284
Craig Mautner525f3d92013-05-07 14:01:50 -07002285 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002286 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2287 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002288 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002289 if (p.finishing) {
2290 continue;
2291 }
2292
Craig Mautnere3a74d52013-02-22 14:14:58 -08002293 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002294 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002295 topOptions = p.takeOptionsLocked();
2296 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002297 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002298 }
2299 }
2300 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002301 + task + " adding to task=" + targetTask
2302 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002303 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2304 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002305 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002306 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002307
Craig Mautner83162a92015-01-26 14:43:30 -08002308 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002309 }
2310
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002311 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002312 if (VALIDATE_TOKENS) {
2313 validateAppTokensLocked();
2314 }
2315
2316 replyChainEnd = -1;
2317 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2318 // If the activity should just be removed -- either
2319 // because it asks for it, or the task should be
2320 // cleared -- then finish it and anything that is
2321 // part of its reply chain.
2322 int end;
2323 if (clearWhenTaskReset) {
2324 // In this case, we want to finish this activity
2325 // and everything above it, so be sneaky and pretend
2326 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002327 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002328 } else if (replyChainEnd < 0) {
2329 end = i;
2330 } else {
2331 end = replyChainEnd;
2332 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002333 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002334 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002335 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002336 if (p.finishing) {
2337 continue;
2338 }
2339 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002340 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002341 topOptions = p.takeOptionsLocked();
2342 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002343 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002344 }
2345 }
Craig Mautner58547802013-03-05 08:23:53 -08002346 if (DEBUG_TASKS) Slog.w(TAG,
2347 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002348 if (finishActivityLocked(
2349 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002350 end--;
2351 srcPos--;
2352 }
2353 }
2354 replyChainEnd = -1;
2355 } else {
2356 // If we were in the middle of a chain, well the
2357 // activity that started it all doesn't want anything
2358 // special, so leave it all as-is.
2359 replyChainEnd = -1;
2360 }
2361 }
2362
2363 return topOptions;
2364 }
2365
2366 /**
2367 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2368 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2369 * @param affinityTask The task we are looking for an affinity to.
2370 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2371 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2372 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2373 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002374 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002375 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002376 int replyChainEnd = -1;
2377 final int taskId = task.taskId;
2378 final String taskAffinity = task.affinity;
2379
2380 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2381 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002382 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2383
2384 // Do not operate on or below the effective root Activity.
2385 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002386 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002387 if (target.frontOfTask)
2388 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002389
2390 final int flags = target.info.flags;
2391 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2392 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2393
2394 if (target.resultTo != null) {
2395 // If this activity is sending a reply to a previous
2396 // activity, we can't do anything with it now until
2397 // we reach the start of the reply chain.
2398 // XXX note that we are assuming the result is always
2399 // to the previous activity, which is almost always
2400 // the case but we really shouldn't count on.
2401 if (replyChainEnd < 0) {
2402 replyChainEnd = i;
2403 }
2404 } else if (topTaskIsHigher
2405 && allowTaskReparenting
2406 && taskAffinity != null
2407 && taskAffinity.equals(target.taskAffinity)) {
2408 // This activity has an affinity for our task. Either remove it if we are
2409 // clearing or move it over to our task. Note that
2410 // we currently punt on the case where we are resetting a
2411 // task that is not at the top but who has activities above
2412 // with an affinity to it... this is really not a normal
2413 // case, and we will need to later pull that task to the front
2414 // and usually at that point we will do the reset and pick
2415 // up those remaining activities. (This only happens if
2416 // someone starts an activity in a new task from an activity
2417 // in a task that is not currently on top.)
2418 if (forceReset || finishOnTaskLaunch) {
2419 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2420 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2421 for (int srcPos = start; srcPos >= i; --srcPos) {
2422 final ActivityRecord p = activities.get(srcPos);
2423 if (p.finishing) {
2424 continue;
2425 }
Todd Kennedy539db512014-12-15 09:57:55 -08002426 finishActivityLocked(
2427 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002428 }
2429 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002430 if (taskInsertionPoint < 0) {
2431 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002432
Craig Mautner77878772013-03-04 19:46:24 -08002433 }
Craig Mautner77878772013-03-04 19:46:24 -08002434
2435 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2436 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2437 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2438 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002439 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002440 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002441 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002442
Craig Mautnere3a74d52013-02-22 14:14:58 -08002443 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2444 + " to stack at " + task,
2445 new RuntimeException("here").fillInStackTrace());
2446 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2447 + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002448 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002449 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002450 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002451 if (VALIDATE_TOKENS) {
2452 validateAppTokensLocked();
2453 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002454
2455 // Now we've moved it in to place... but what if this is
2456 // a singleTop activity and we have put it on top of another
2457 // instance of the same activity? Then we drop the instance
2458 // below so it remains singleTop.
2459 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2460 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002461 int targetNdx = taskActivities.indexOf(target);
2462 if (targetNdx > 0) {
2463 ActivityRecord p = taskActivities.get(targetNdx - 1);
2464 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002465 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2466 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002467 }
2468 }
2469 }
2470 }
2471
2472 replyChainEnd = -1;
2473 }
2474 }
Craig Mautner77878772013-03-04 19:46:24 -08002475 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002476 }
2477
Craig Mautner8849a5e2013-04-02 16:41:03 -07002478 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002479 ActivityRecord newActivity) {
2480 boolean forceReset =
2481 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2482 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2483 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2484 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2485 forceReset = true;
2486 }
2487 }
2488
2489 final TaskRecord task = taskTop.task;
2490
2491 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2492 * for remaining tasks. Used for later tasks to reparent to task. */
2493 boolean taskFound = false;
2494
2495 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2496 ActivityOptions topOptions = null;
2497
Craig Mautner77878772013-03-04 19:46:24 -08002498 // Preserve the location for reparenting in the new task.
2499 int reparentInsertionPoint = -1;
2500
Craig Mautnere3a74d52013-02-22 14:14:58 -08002501 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2502 final TaskRecord targetTask = mTaskHistory.get(i);
2503
2504 if (targetTask == task) {
2505 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2506 taskFound = true;
2507 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002508 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2509 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002510 }
2511 }
2512
Craig Mautner70a86932013-02-28 22:37:44 -08002513 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002514 if (taskNdx >= 0) {
2515 do {
2516 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2517 } while (taskTop == null && taskNdx >= 0);
2518 }
Craig Mautner70a86932013-02-28 22:37:44 -08002519
Craig Mautnere3a74d52013-02-22 14:14:58 -08002520 if (topOptions != null) {
2521 // If we got some ActivityOptions from an activity on top that
2522 // was removed from the task, propagate them to the new real top.
2523 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002524 taskTop.updateOptionsLocked(topOptions);
2525 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 topOptions.abort();
2527 }
2528 }
2529
2530 return taskTop;
2531 }
2532
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2534 String resultWho, int requestCode, int resultCode, Intent data) {
2535
2536 if (callingUid > 0) {
2537 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002538 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002539 }
2540
2541 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2542 + " : who=" + resultWho + " req=" + requestCode
2543 + " res=" + resultCode + " data=" + data);
2544 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2545 try {
2546 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2547 list.add(new ResultInfo(resultWho, requestCode,
2548 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002549 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 return;
2551 } catch (Exception e) {
2552 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2553 }
2554 }
2555
2556 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2557 }
2558
Craig Mautner299f9602015-01-26 09:47:33 -08002559 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002560 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2561 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002562 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002563 if (next != r) {
2564 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002565 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002566 // For non-fullscreen stack, we want to move the focus to the next visible
2567 // stack to prevent the home screen from moving to the top and obscuring
2568 // other visible stacks.
2569 if (!mFullscreen
2570 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2571 return;
2572 }
2573 // Move the home stack to the top if this stack is fullscreen or there is no
2574 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002575 if (mStackSupervisor.moveHomeStackTaskToTop(
2576 task.getTaskToReturnTo(), myReason)) {
2577 // Activity focus was already adjusted. Nothing else to do...
2578 return;
2579 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002580 }
2581 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002582
2583 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002584 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002585 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002586 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002587 }
2588 }
2589
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002590 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2591 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2592 final String myReason = reason + " adjustFocusToNextVisibleStack";
2593 if (stack == null) {
2594 return false;
2595 }
2596 final ActivityRecord top = stack.topRunningActivityLocked(null);
2597 if (top == null) {
2598 return false;
2599 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002600 mService.setFocusedActivityLocked(top, myReason);
2601 return true;
2602 }
2603
Craig Mautnerf3333272013-04-22 10:55:53 -07002604 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2606 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2607 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2608 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002609 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002610 if (DEBUG_STATES) {
2611 Slog.d(TAG, "no-history finish of " + r);
2612 }
2613 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002614 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002615 } else {
2616 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2617 + " on stop because we're just sleeping");
2618 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002619 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002620 }
2621
2622 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002623 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002624 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 try {
2626 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002627 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2628 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002629 r.state = ActivityState.STOPPING;
2630 if (DEBUG_VISBILITY) Slog.v(
2631 TAG, "Stopping visible=" + r.visible + " for " + r);
2632 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002633 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002634 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002635 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002636 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002637 r.setSleeping(true);
2638 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002639 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002640 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 } catch (Exception e) {
2642 // Maybe just ignore exceptions here... if the process
2643 // has crashed, our death notification will clean things
2644 // up.
2645 Slog.w(TAG, "Exception thrown during pause", e);
2646 // Just in case, assume it to be stopped.
2647 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002648 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002649 r.state = ActivityState.STOPPED;
2650 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002651 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 }
2653 }
2654 }
2655 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002656
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002657 /**
2658 * @return Returns true if the activity is being finished, false if for
2659 * some reason it is being left as-is.
2660 */
2661 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002662 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002663 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002664 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002665 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002666 + ", result=" + resultCode + ", data=" + resultData
2667 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002668 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002669 return false;
2670 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671
Craig Mautnerd44711d2013-02-23 11:24:36 -08002672 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 return true;
2674 }
2675
Craig Mautnerd2328952013-03-05 12:46:26 -08002676 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002677 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2678 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2679 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2680 ActivityRecord r = activities.get(activityNdx);
2681 if (r.resultTo == self && r.requestCode == requestCode) {
2682 if ((r.resultWho == null && resultWho == null) ||
2683 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2684 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2685 false);
2686 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002687 }
2688 }
2689 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002690 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002691 }
2692
Todd Kennedy539db512014-12-15 09:57:55 -08002693 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002694 ActivityRecord r = topRunningActivityLocked(null);
2695 if (r != null && r.app == app) {
2696 // If the top running activity is from this crashing
2697 // process, then terminate it to avoid getting in a loop.
2698 Slog.w(TAG, " Force finishing activity "
2699 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002700 int taskNdx = mTaskHistory.indexOf(r.task);
2701 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002702 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002703 // Also terminate any activities below it that aren't yet
2704 // stopped, to avoid a situation where one will get
2705 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002706 --activityNdx;
2707 if (activityNdx < 0) {
2708 do {
2709 --taskNdx;
2710 if (taskNdx < 0) {
2711 break;
2712 }
2713 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2714 } while (activityNdx < 0);
2715 }
2716 if (activityNdx >= 0) {
2717 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002718 if (r.state == ActivityState.RESUMED
2719 || r.state == ActivityState.PAUSING
2720 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002721 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002722 Slog.w(TAG, " Force finishing activity "
2723 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002724 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002725 }
2726 }
2727 }
2728 }
2729 }
2730
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002731 final void finishVoiceTask(IVoiceInteractionSession session) {
2732 IBinder sessionBinder = session.asBinder();
2733 boolean didOne = false;
2734 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2735 TaskRecord tr = mTaskHistory.get(taskNdx);
2736 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2737 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2738 ActivityRecord r = tr.mActivities.get(activityNdx);
2739 if (!r.finishing) {
2740 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2741 false);
2742 didOne = true;
2743 }
2744 }
2745 }
2746 }
2747 if (didOne) {
2748 mService.updateOomAdjLocked();
2749 }
2750 }
2751
Craig Mautnerd2328952013-03-05 12:46:26 -08002752 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002753 ArrayList<ActivityRecord> activities = r.task.mActivities;
2754 for (int index = activities.indexOf(r); index >= 0; --index) {
2755 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002756 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002757 break;
2758 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002759 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002760 }
2761 return true;
2762 }
2763
Dianne Hackborn5c607432012-02-28 14:44:19 -08002764 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2765 // send the result
2766 ActivityRecord resultTo = r.resultTo;
2767 if (resultTo != null) {
2768 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2769 + " who=" + r.resultWho + " req=" + r.requestCode
2770 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002771 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002772 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002773 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002774 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002775 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002776 if (r.info.applicationInfo.uid > 0) {
2777 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2778 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002779 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002780 }
2781 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2782 resultData);
2783 r.resultTo = null;
2784 }
2785 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2786
2787 // Make sure this HistoryRecord is not holding on to other resources,
2788 // because clients have remote IPC references to this object so we
2789 // can't assume that will go away and want to avoid circular IPC refs.
2790 r.results = null;
2791 r.pendingResults = null;
2792 r.newIntents = null;
2793 r.icicle = null;
2794 }
2795
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 /**
2797 * @return Returns true if this activity has been removed from the history
2798 * list, or false if it is still in the list and will be removed later.
2799 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002800 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2801 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002802 if (r.finishing) {
2803 Slog.w(TAG, "Duplicate finish request for " + r);
2804 return false;
2805 }
2806
Wale Ogunwale7d701172015-03-11 15:36:30 -07002807 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002808 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002810 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002811 task.taskId, r.shortComponentName, reason);
2812 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002813 final int index = activities.indexOf(r);
2814 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002815 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002816 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002817 // If the caller asked that this activity (and all above it)
2818 // be cleared when the task is reset, don't lose that information,
2819 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002820 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002821 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002822 }
2823 }
2824
2825 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002826
Craig Mautner299f9602015-01-26 09:47:33 -08002827 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828
Dianne Hackborn5c607432012-02-28 14:44:19 -08002829 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002830
Craig Mautnerde4ef022013-04-07 19:01:33 -07002831 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002832 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002833 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002835 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002836 ? AppTransition.TRANSIT_TASK_CLOSE
2837 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002838
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002840 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002841
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002842 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2844 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002845 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 }
2847
Craig Mautneraea74a52014-03-08 14:23:10 -08002848 if (endTask) {
2849 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2850 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002851 } else if (r.state != ActivityState.PAUSING) {
2852 // If the activity is PAUSING, we will complete the finish once
2853 // it is done pausing; else we can just directly finish it here.
2854 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002855 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002856 } else {
2857 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2858 }
2859
2860 return false;
2861 }
2862
Craig Mautnerf3333272013-04-22 10:55:53 -07002863 static final int FINISH_IMMEDIATELY = 0;
2864 static final int FINISH_AFTER_PAUSE = 1;
2865 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002866
Craig Mautnerf3333272013-04-22 10:55:53 -07002867 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 // First things first: if this activity is currently visible,
2869 // and the resumed activity is not yet visible, then hold off on
2870 // finishing until the resumed one becomes visible.
2871 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002872 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2873 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002874 if (mStackSupervisor.mStoppingActivities.size() > 3
2875 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002876 // If we already have a few activities waiting to stop,
2877 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002878 // them out. Or if r is the last of activity of the last task the stack
2879 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002880 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002881 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002882 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 }
2884 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002885 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2886 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002888 if (oomAdj) {
2889 mService.updateOomAdjLocked();
2890 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002891 return r;
2892 }
2893
2894 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002895 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002896 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002897 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 if (mResumedActivity == r) {
2899 mResumedActivity = null;
2900 }
2901 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002902 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002903 r.state = ActivityState.FINISHING;
2904
2905 if (mode == FINISH_IMMEDIATELY
2906 || prevState == ActivityState.STOPPED
2907 || prevState == ActivityState.INITIALIZING) {
2908 // If this activity is already stopped, we can just finish
2909 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002910 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002911 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002912 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002913 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002914 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002915 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002916 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2917 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002918 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002920
2921 // Need to go through the full pause cycle to get this
2922 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002923 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002924 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002925 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002926 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 return r;
2928 }
2929
Craig Mautneree36c772014-07-16 14:56:05 -07002930 void finishAllActivitiesLocked(boolean immediately) {
2931 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002932 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2933 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2934 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2935 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002936 noActivitiesInStack = false;
2937 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002938 continue;
2939 }
Craig Mautneree36c772014-07-16 14:56:05 -07002940 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002941 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2942 }
2943 }
Craig Mautneree36c772014-07-16 14:56:05 -07002944 if (noActivitiesInStack) {
2945 mActivityContainer.onTaskListEmptyLocked();
2946 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002947 }
2948
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002949 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2950 // Basic case: for simple app-centric recents, we need to recreate
2951 // the task if the affinity has changed.
2952 if (srec == null || srec.task.affinity == null ||
2953 !srec.task.affinity.equals(destAffinity)) {
2954 return true;
2955 }
2956 // Document-centric case: an app may be split in to multiple documents;
2957 // they need to re-create their task if this current activity is the root
2958 // of a document, unless simply finishing it will return them to the the
2959 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002960 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2961 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002962 // Okay, this activity is at the root of its task. What to do, what to do...
2963 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2964 // Finishing won't return to an application, so we need to recreate.
2965 return true;
2966 }
2967 // We now need to get the task below it to determine what to do.
2968 int taskIdx = mTaskHistory.indexOf(srec.task);
2969 if (taskIdx <= 0) {
2970 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2971 return false;
2972 }
2973 if (taskIdx == 0) {
2974 // At the bottom of the stack, nothing to go back to.
2975 return true;
2976 }
2977 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2978 if (!srec.task.affinity.equals(prevTask.affinity)) {
2979 // These are different apps, so need to recreate.
2980 return true;
2981 }
2982 }
2983 return false;
2984 }
2985
Wale Ogunwale7d701172015-03-11 15:36:30 -07002986 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002987 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002988 final TaskRecord task = srec.task;
2989 final ArrayList<ActivityRecord> activities = task.mActivities;
2990 final int start = activities.indexOf(srec);
2991 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002992 return false;
2993 }
2994 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002995 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002996 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002997 final ComponentName dest = destIntent.getComponent();
2998 if (start > 0 && dest != null) {
2999 for (int i = finishTo; i >= 0; i--) {
3000 ActivityRecord r = activities.get(i);
3001 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003002 r.info.name.equals(dest.getClassName())) {
3003 finishTo = i;
3004 parent = r;
3005 foundParentInTask = true;
3006 break;
3007 }
3008 }
3009 }
3010
3011 IActivityController controller = mService.mController;
3012 if (controller != null) {
3013 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3014 if (next != null) {
3015 // ask watcher if this is allowed
3016 boolean resumeOK = true;
3017 try {
3018 resumeOK = controller.activityResuming(next.packageName);
3019 } catch (RemoteException e) {
3020 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003021 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003022 }
3023
3024 if (!resumeOK) {
3025 return false;
3026 }
3027 }
3028 }
3029 final long origId = Binder.clearCallingIdentity();
3030 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003031 ActivityRecord r = activities.get(i);
3032 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003033 // Only return the supplied result for the first activity finished
3034 resultCode = Activity.RESULT_CANCELED;
3035 resultData = null;
3036 }
3037
3038 if (parent != null && foundParentInTask) {
3039 final int parentLaunchMode = parent.info.launchMode;
3040 final int destIntentFlags = destIntent.getFlags();
3041 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3042 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3043 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3044 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003045 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3046 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003047 } else {
3048 try {
3049 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3050 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003051 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003052 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003053 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003054 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003055 foundParentInTask = res == ActivityManager.START_SUCCESS;
3056 } catch (RemoteException e) {
3057 foundParentInTask = false;
3058 }
3059 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003060 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003061 }
3062 }
3063 Binder.restoreCallingIdentity(origId);
3064 return foundParentInTask;
3065 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 /**
3067 * Perform the common clean-up of an activity record. This is called both
3068 * as part of destroyActivityLocked() (when destroying the client-side
3069 * representation) and cleaning things up as a result of its hosting
3070 * processing going away, in which case there is no remaining client-side
3071 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003072 *
3073 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003075 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3076 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077 if (mResumedActivity == r) {
3078 mResumedActivity = null;
3079 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003080 if (mPausingActivity == r) {
3081 mPausingActivity = null;
3082 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003083 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003084
3085 r.configDestroy = false;
3086 r.frozenBeforeDestroy = false;
3087
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003088 if (setState) {
3089 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3090 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003091 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003092 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003093 }
3094
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 // Make sure this record is no longer in the pending finishes list.
3096 // This could happen, for example, if we are trimming activities
3097 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003098 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003099 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003101 // Remove any pending results.
3102 if (r.finishing && r.pendingResults != null) {
3103 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3104 PendingIntentRecord rec = apr.get();
3105 if (rec != null) {
3106 mService.cancelIntentSenderLocked(rec, false);
3107 }
3108 }
3109 r.pendingResults = null;
3110 }
3111
3112 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003113 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114 }
3115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003116 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003117 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003118 if (getVisibleBehindActivity() == r) {
3119 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003120 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003121 }
3122
3123 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003124 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003126 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003127 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003128 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 }
3130
Craig Mautner299f9602015-01-26 09:47:33 -08003131 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003132 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003133 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003134 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003135 if (DEBUG_ADD_REMOVE) {
3136 RuntimeException here = new RuntimeException("here");
3137 here.fillInStackTrace();
3138 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003139 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003140 r.takeFromHistory();
3141 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003142 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003143 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003144 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003145 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003146 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003147 if (VALIDATE_TOKENS) {
3148 validateAppTokensLocked();
3149 }
Craig Mautner312ba862014-02-10 17:55:01 -08003150 final TaskRecord task = r.task;
3151 if (task != null && task.removeActivity(r)) {
3152 if (DEBUG_STACK) Slog.i(TAG,
3153 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003154 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3155 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003156 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003157 }
Craig Mautner299f9602015-01-26 09:47:33 -08003158 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003159 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003160 cleanUpActivityServicesLocked(r);
3161 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003164 /**
3165 * Perform clean-up of service connections in an activity record.
3166 */
3167 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3168 // Throw away any services that have been bound by this activity.
3169 if (r.connections != null) {
3170 Iterator<ConnectionRecord> it = r.connections.iterator();
3171 while (it.hasNext()) {
3172 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003173 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 }
3175 r.connections = null;
3176 }
3177 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003178
Craig Mautneree2e45a2014-06-27 12:10:03 -07003179 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003180 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003181 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003182 mHandler.sendMessage(msg);
3183 }
3184
Craig Mautneree2e45a2014-06-27 12:10:03 -07003185 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003186 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003187 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003188 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3189 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3190 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3191 final ActivityRecord r = activities.get(activityNdx);
3192 if (r.finishing) {
3193 continue;
3194 }
3195 if (r.fullscreen) {
3196 lastIsOpaque = true;
3197 }
3198 if (owner != null && r.app != owner) {
3199 continue;
3200 }
3201 if (!lastIsOpaque) {
3202 continue;
3203 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003204 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003205 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3206 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003207 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003208 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003209 activityRemoved = true;
3210 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003211 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003212 }
3213 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003214 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003215 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003216 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003217 }
3218
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003219 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3220 if (r.isDestroyable()) {
3221 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3222 + " resumed=" + mResumedActivity
3223 + " pausing=" + mPausingActivity + " for reason " + reason);
3224 return destroyActivityLocked(r, true, reason);
3225 }
3226 return false;
3227 }
3228
3229 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3230 String reason) {
3231 // Iterate over tasks starting at the back (oldest) first.
3232 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3233 int maxTasks = tasks.size() / 4;
3234 if (maxTasks < 1) {
3235 maxTasks = 1;
3236 }
3237 int numReleased = 0;
3238 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3239 final TaskRecord task = mTaskHistory.get(taskNdx);
3240 if (!tasks.contains(task)) {
3241 continue;
3242 }
3243 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3244 int curNum = 0;
3245 final ArrayList<ActivityRecord> activities = task.mActivities;
3246 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3247 final ActivityRecord activity = activities.get(actNdx);
3248 if (activity.app == app && activity.isDestroyable()) {
3249 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3250 + " in state " + activity.state + " resumed=" + mResumedActivity
3251 + " pausing=" + mPausingActivity + " for reason " + reason);
3252 destroyActivityLocked(activity, true, reason);
3253 if (activities.get(actNdx) != activity) {
3254 // Was removed from list, back up so we don't miss the next one.
3255 actNdx--;
3256 }
3257 curNum++;
3258 }
3259 }
3260 if (curNum > 0) {
3261 numReleased += curNum;
3262 maxTasks--;
3263 if (mTaskHistory.get(taskNdx) != task) {
3264 // The entire task got removed, back up so we don't miss the next one.
3265 taskNdx--;
3266 }
3267 }
3268 }
3269 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3270 return numReleased;
3271 }
3272
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003273 /**
3274 * Destroy the current CLIENT SIDE instance of an activity. This may be
3275 * called both when actually finishing an activity, or when performing
3276 * a configuration switch where we destroy the current client-side object
3277 * but then create a new client-side object for this same HistoryRecord.
3278 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003279 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003280 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003281 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3283 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003284 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003285 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286
3287 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003288
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003289 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003290
3291 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003292
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003293 if (hadApp) {
3294 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003295 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003296 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3297 mService.mHeavyWeightProcess = null;
3298 mService.mHandler.sendEmptyMessage(
3299 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3300 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003301 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003302 // Update any services we are bound to that might care about whether
3303 // their client may have activities.
3304 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003305 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003306 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003307 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 }
3309 }
3310
3311 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003312
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003313 try {
3314 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003315 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003316 r.configChangeFlags);
3317 } catch (Exception e) {
3318 // We can just ignore exceptions here... if the process
3319 // has crashed, our death notification will clean things
3320 // up.
3321 //Slog.w(TAG, "Exception thrown during finish", e);
3322 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003323 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 removedFromHistory = true;
3325 skipDestroy = true;
3326 }
3327 }
3328
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003329 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003330
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003331 // If the activity is finishing, we need to wait on removing it
3332 // from the list to give it a chance to do its cleanup. During
3333 // that time it may make calls back with its token so we need to
3334 // be able to find it on the list and so we don't want to remove
3335 // it from the list yet. Otherwise, we can just immediately put
3336 // it in the destroyed state since we are not removing it from the
3337 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003339 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3340 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003342 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3344 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003345 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003347 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003348 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 }
3350 } else {
3351 // remove this record from the history.
3352 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003353 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003354 removedFromHistory = true;
3355 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003356 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003358 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003359 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 }
3361 }
3362
3363 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 if (!mLRUActivities.remove(r) && hadApp) {
3366 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3367 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 return removedFromHistory;
3370 }
3371
Craig Mautner299f9602015-01-26 09:47:33 -08003372 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003373 final long origId = Binder.clearCallingIdentity();
3374 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003375 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003376 if (r != null) {
3377 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003378 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003379 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003380
Wale Ogunwale60454db2015-01-23 16:05:07 -08003381 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003382 if (r.state == ActivityState.DESTROYING) {
3383 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003384 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003385 }
3386 }
Craig Mautner05d29032013-05-03 13:40:13 -07003387 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003388 } finally {
3389 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003390 }
3391 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003392
Todd Kennedyaab56db2015-01-30 09:39:53 -08003393 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003394 if (hasVisibleBehindActivity() &&
3395 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003396 if (r == topRunningActivityLocked(null)) {
3397 // Don't release the top activity if it has requested to run behind the next
3398 // activity.
3399 return;
3400 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003401 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3402 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003403 " thread=" + r.app.thread);
3404 if (r != null && r.app != null && r.app.thread != null) {
3405 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003406 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003407 } catch (RemoteException e) {
3408 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003409 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003410 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003411 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003412 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003413 }
3414 }
3415 }
3416
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003417 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003418 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3419 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003420 if (r != null) {
3421 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003422 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003423 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003424 }
3425 mStackSupervisor.resumeTopActivitiesLocked();
3426 }
3427
Jose Lima4b6c6692014-08-12 17:41:12 -07003428 boolean hasVisibleBehindActivity() {
3429 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003430 }
3431
Jose Lima4b6c6692014-08-12 17:41:12 -07003432 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003433 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003434 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003435 }
3436 }
3437
Jose Lima4b6c6692014-08-12 17:41:12 -07003438 ActivityRecord getVisibleBehindActivity() {
3439 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003440 }
3441
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003442 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3443 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003444 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003445 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3446 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003447 while (i > 0) {
3448 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003449 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003450 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003452 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003453 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003454 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003455 }
3456 }
3457 }
3458
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003459 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3460 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003461 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3462 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003463 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3464 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003465 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003466 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003467 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3468 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003469
3470 boolean hasVisibleActivities = false;
3471
3472 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003473 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003474 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3475 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003476 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3477 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3478 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3479 final ActivityRecord r = activities.get(activityNdx);
3480 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003481 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3482 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003483 if (r.app == app) {
Craig Mautneracebdc82015-02-24 10:53:03 -08003484 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003485 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3486 // Don't currently have state for the activity, or
3487 // it is finishing -- always remove it.
3488 remove = true;
3489 } else if (r.launchCount > 2 &&
3490 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3491 // We have launched this activity too many times since it was
3492 // able to run, so give up and remove it.
3493 remove = true;
3494 } else {
3495 // The process may be gone, but the activity lives on!
3496 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003497 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003498 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003499 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003500 RuntimeException here = new RuntimeException("here");
3501 here.fillInStackTrace();
3502 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3503 + ": haveState=" + r.haveState
3504 + " stateNotNeeded=" + r.stateNotNeeded
3505 + " finishing=" + r.finishing
3506 + " state=" + r.state, here);
3507 }
3508 if (!r.finishing) {
3509 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3510 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3511 r.userId, System.identityHashCode(r),
3512 r.task.taskId, r.shortComponentName,
3513 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003514 if (r.state == ActivityState.RESUMED) {
3515 mService.updateUsageStats(r, false);
3516 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003517 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003518 } else {
3519 // We have the current state for this activity, so
3520 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003521 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003522 TAG, "Keeping entry, setting app to null");
3523 if (r.visible) {
3524 hasVisibleActivities = true;
3525 }
3526 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3527 + r);
3528 r.app = null;
3529 r.nowVisible = false;
3530 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003531 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003532 "App died, clearing saved state of " + r);
3533 r.icicle = null;
3534 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003535 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003536 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003537 if (remove) {
3538 removeActivityFromHistoryLocked(r, "appDied");
3539 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003540 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003541 }
3542 }
3543
3544 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003545 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003546
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003547 final void updateTransitLocked(int transit, Bundle options) {
3548 if (options != null) {
3549 ActivityRecord r = topRunningActivityLocked(null);
3550 if (r != null && r.state != ActivityState.RESUMED) {
3551 r.updateOptionsLocked(options);
3552 } else {
3553 ActivityOptions.abort(options);
3554 }
3555 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003556 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003557 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003558
Craig Mautner21d24a22014-04-23 11:45:37 -07003559 void updateTaskMovement(TaskRecord task, boolean toFront) {
3560 if (task.isPersistable) {
3561 task.mLastTimeMoved = System.currentTimeMillis();
3562 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3563 // recently will be most negative, tasks sent to the bottom before that will be less
3564 // negative. Similarly for recent tasks moved to the top which will be most positive.
3565 if (!toFront) {
3566 task.mLastTimeMoved *= -1;
3567 }
3568 }
3569 }
3570
Craig Mautner84984fa2014-06-19 11:19:20 -07003571 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003572 final int top = mTaskHistory.size() - 1;
3573 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3574 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003575 if (task.taskType == homeStackTaskType) {
3576 if (DEBUG_TASKS || DEBUG_STACK)
3577 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003578 mTaskHistory.remove(taskNdx);
3579 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003580 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003581 return;
3582 }
3583 }
3584 }
3585
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003586 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003587 String reason) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003588 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003589
Craig Mautner11bf9a52013-02-19 14:08:51 -08003590 final int numTasks = mTaskHistory.size();
3591 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003592 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003593 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003594 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003595 ActivityOptions.abort(options);
3596 } else {
3597 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3598 }
3599 return;
3600 }
3601
3602 // Shift all activities with this task up to the top
3603 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003604 insertTaskAtTop(tr);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003605
3606 // Set focus to the top running activity of this stack.
3607 ActivityRecord r = topRunningActivityLocked(null);
3608 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003609
3610 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003611 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003612 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003613 if (r != null) {
3614 mNoAnimActivities.add(r);
3615 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003616 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003618 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003620
Craig Mautner05d29032013-05-03 13:40:13 -07003621 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003622 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003623
3624 if (VALIDATE_TOKENS) {
3625 validateAppTokensLocked();
3626 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003627 }
3628
3629 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003630 * Worker method for rearranging history stack. Implements the function of moving all
3631 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003632 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003633 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003634 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3635 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003636 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003637 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 * @return Returns true if the move completed, false if not.
3639 */
Craig Mautner299f9602015-01-26 09:47:33 -08003640 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003641 final TaskRecord tr = taskForIdLocked(taskId);
3642 if (tr == null) {
3643 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3644 return false;
3645 }
3646
3647 Slog.i(TAG, "moveTaskToBack: " + tr);
3648
3649 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003650
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003651 // If we have a watcher, preflight the move before committing to it. First check
3652 // for *other* available tasks, but if none are available, then try again allowing the
3653 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003654 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003655 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003656 if (next == null) {
3657 next = topRunningActivityLocked(null, 0);
3658 }
3659 if (next != null) {
3660 // ask watcher if this is allowed
3661 boolean moveOK = true;
3662 try {
3663 moveOK = mService.mController.activityResuming(next.packageName);
3664 } catch (RemoteException e) {
3665 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003666 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003667 }
3668 if (!moveOK) {
3669 return false;
3670 }
3671 }
3672 }
3673
Wale Ogunwalecb82f302015-02-25 07:53:40 -08003674 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003675
riddle_hsuc215a4f2014-12-27 12:10:45 +08003676 boolean prevIsHome = false;
3677 if (tr.isOverHomeStack()) {
3678 final TaskRecord nextTask = getNextTask(tr);
3679 if (nextTask != null) {
3680 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3681 } else {
3682 prevIsHome = true;
3683 }
3684 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003685 mTaskHistory.remove(tr);
3686 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003687 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003688
Craig Mautnerc8143c62013-09-03 12:15:57 -07003689 // There is an assumption that moving a task to the back moves it behind the home activity.
3690 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003691 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003692 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3693 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003694 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003695 break;
3696 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003697 if (taskNdx == 1) {
3698 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003699 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003700 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003701 }
3702
Craig Mautner299f9602015-01-26 09:47:33 -08003703 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003704 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003705
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003706 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003707 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003708 }
3709
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003710 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003711 if (prevIsHome || task == tr && tr.isOverHomeStack()
3712 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003713 if (!mService.mBooting && !mService.mBooted) {
3714 // Not ready yet!
3715 return false;
3716 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003717 final int taskToReturnTo = tr.getTaskToReturnTo();
3718 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003719 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003720 }
3721
Craig Mautner05d29032013-05-03 13:40:13 -07003722 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003723 return true;
3724 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003725
Craig Mautner8849a5e2013-04-02 16:41:03 -07003726 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003727 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003728 final Uri data = r.intent.getData();
3729 final String strData = data != null ? data.toSafeString() : null;
3730
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003732 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003734 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003735 }
3736
3737 /**
3738 * Make sure the given activity matches the current configuration. Returns
3739 * false if the activity had to be destroyed. Returns true if the
3740 * configuration is the same, or the activity will remain running as-is
3741 * for whatever reason. Ensures the HistoryRecord is updated with the
3742 * correct configuration and all other bookkeeping is handled.
3743 */
3744 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3745 int globalChanges) {
3746 if (mConfigWillChange) {
3747 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3748 "Skipping config check (will change): " + r);
3749 return true;
3750 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003751
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3753 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003754
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003755 // Make sure the current stack override configuration is supported by the top task
3756 // before continuing.
3757 final TaskRecord topTask = topTask();
3758 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3759 || (!topTask.mResizeable && !mFullscreen))) {
3760 final boolean prevFullscreen = mFullscreen;
3761 final Configuration newOverrideConfig =
3762 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3763 updateOverrideConfiguration(newOverrideConfig);
3764 mForcedFullscreen = !prevFullscreen && mFullscreen;
3765 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3766 "Updated stack config to support task=" + topTask
3767 + " resizeable=" + topTask.mResizeable
3768 + " mForcedFullscreen=" + mForcedFullscreen
3769 + " prevFullscreen=" + prevFullscreen
3770 + " mFullscreen=" + mFullscreen);
3771 }
3772
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 // Short circuit: if the two configurations are the exact same
3774 // object (the common case), then there is nothing to do.
3775 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003776 if (r.configuration == newConfig
3777 && r.stackConfigOverride == mOverrideConfig
3778 && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003779 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3780 "Configuration unchanged in " + r);
3781 return true;
3782 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003783
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 // We don't worry about activities that are finishing.
3785 if (r.finishing) {
3786 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3787 "Configuration doesn't matter in finishing " + r);
3788 r.stopFreezingScreenLocked(false);
3789 return true;
3790 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003791
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003792 // Okay we now are going to make this activity have the new config.
3793 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003794 final Configuration oldConfig = r.configuration;
3795 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003797 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003798
3799 // Determine what has changed. May be nothing, if this is a config
3800 // that has come back from the app after going idle. In that case
3801 // we just want to leave the official config object now in the
3802 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003803 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003804 if (stackChanges == 0) {
3805 // {@link Configuration#diff} doesn't catch changes from unset values.
3806 // Check for changes we care about.
3807 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3808 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3809 }
3810 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3811 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3812 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3813 }
3814 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3815 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3816 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003817 }
3818 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003819 if (changes == 0 && !r.forceNewConfig) {
3820 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3821 "Configuration no differences in " + r);
3822 return true;
3823 }
3824
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003825 // If the activity isn't currently running, just leave the new
3826 // configuration and it will pick that up next time it starts.
3827 if (r.app == null || r.app.thread == null) {
3828 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3829 "Configuration doesn't matter not running " + r);
3830 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003831 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003832 return true;
3833 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003834
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003835 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003836 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3837 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3838 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003839 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003840 + ", newConfig=" + newConfig);
3841 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003842 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003843 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3844 r.configChangeFlags |= changes;
3845 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003846 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003847 if (r.app == null || r.app.thread == null) {
3848 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003849 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003850 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003851 } else if (r.state == ActivityState.PAUSING) {
3852 // A little annoying: we are waiting for this activity to
3853 // finish pausing. Let's not do anything now, but just
3854 // flag that it needs to be restarted when done pausing.
3855 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003856 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003857 r.configDestroy = true;
3858 return true;
3859 } else if (r.state == ActivityState.RESUMED) {
3860 // Try to optimize this case: the configuration is changing
3861 // and we need to restart the top, resumed activity.
3862 // Instead of doing the normal handshaking, just say
3863 // "restart!".
3864 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003865 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003866 relaunchActivityLocked(r, r.configChangeFlags, true);
3867 r.configChangeFlags = 0;
3868 } else {
3869 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003870 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003871 relaunchActivityLocked(r, r.configChangeFlags, false);
3872 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003873 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003874
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003875 // All done... tell the caller we weren't able to keep this
3876 // activity around.
3877 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003878 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003879
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003880 // Default case: the activity can handle this new configuration, so hand it over.
3881 // NOTE: We only forward the stack override configuration as the system level configuration
3882 // changes is always sent to all processes when they happen so it can just use whatever
3883 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003884 if (r.app != null && r.app.thread != null) {
3885 try {
3886 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003887 r.app.thread.scheduleActivityConfigurationChanged(
3888 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003889 } catch (RemoteException e) {
3890 // If process died, whatever.
3891 }
3892 }
3893 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003894
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003895 return true;
3896 }
3897
Craig Mautnerc8143c62013-09-03 12:15:57 -07003898 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003899 int changes, boolean andResume) {
3900 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003901 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 if (andResume) {
3903 results = r.results;
3904 newIntents = r.newIntents;
3905 }
3906 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3907 + " with results=" + results + " newIntents=" + newIntents
3908 + " andResume=" + andResume);
3909 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003910 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003911 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003912
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003914
Craig Mautner34b73df2014-01-12 21:11:08 -08003915 mStackSupervisor.removeChildActivityContainers(r);
3916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003917 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003918 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3919 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3920 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003921 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003922 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3923 !andResume, new Configuration(mService.mConfiguration),
3924 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003925 // Note: don't need to call pauseIfSleepingLocked() here, because
3926 // the caller will only pass in 'andResume' if this activity is
3927 // currently resumed, which implies we aren't sleeping.
3928 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003929 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 }
3931
3932 if (andResume) {
3933 r.results = null;
3934 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003935 r.state = ActivityState.RESUMED;
3936 } else {
3937 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3938 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003939 }
3940
3941 return true;
3942 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003943
3944 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003945 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3946 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3947 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3948 final ActivityRecord r = activities.get(activityNdx);
3949 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003950 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003951 }
3952 if (r.fullscreen && !r.finishing) {
3953 return false;
3954 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003955 }
3956 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003957 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003958 if (r == null) {
3959 return false;
3960 }
3961 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3962 + " would have returned true for r=" + r);
3963 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003964 }
3965
3966 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003967 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3968 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3969 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3970 final ActivityRecord r = activities.get(activityNdx);
3971 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003972 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003973 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003974 }
3975 }
3976 }
3977
3978 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3979 boolean didSomething = false;
3980 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003981 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003982 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3983 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3984 int numActivities = activities.size();
3985 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3986 ActivityRecord r = activities.get(activityNdx);
3987 final boolean samePackage = r.packageName.equals(name)
3988 || (name == null && r.userId == userId);
3989 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3990 && (samePackage || r.task == lastTask)
3991 && (r.app == null || evenPersistent || !r.app.persistent)) {
3992 if (!doit) {
3993 if (r.finishing) {
3994 // If this activity is just finishing, then it is not
3995 // interesting as far as something to stop.
3996 continue;
3997 }
3998 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003999 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004000 if (r.isHomeActivity()) {
4001 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4002 Slog.i(TAG, "Skip force-stop again " + r);
4003 continue;
4004 } else {
4005 homeActivity = r.realActivity;
4006 }
4007 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004008 didSomething = true;
4009 Slog.i(TAG, " Force finishing activity " + r);
4010 if (samePackage) {
4011 if (r.app != null) {
4012 r.app.removed = true;
4013 }
4014 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004015 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004016 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004017 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4018 true)) {
4019 // r has been deleted from mActivities, accommodate.
4020 --numActivities;
4021 --activityNdx;
4022 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004023 }
4024 }
4025 }
4026 return didSomething;
4027 }
4028
Dianne Hackborn09233282014-04-30 11:33:59 -07004029 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004030 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004031 final TaskRecord task = mTaskHistory.get(taskNdx);
4032 ActivityRecord r = null;
4033 ActivityRecord top = null;
4034 int numActivities = 0;
4035 int numRunning = 0;
4036 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07004037 if (activities.isEmpty()) {
4038 continue;
4039 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004040 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004041 continue;
4042 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004043 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4044 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004045
Craig Mautneraab647e2013-02-28 16:31:36 -08004046 // Initialize state for next task if needed.
4047 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4048 top = r;
4049 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004050 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004051
4052 // Add 'r' into the current task.
4053 numActivities++;
4054 if (r.app != null && r.app.thread != null) {
4055 numRunning++;
4056 }
4057
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004058 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004059 TAG, r.intent.getComponent().flattenToShortString()
4060 + ": task=" + r.task);
4061 }
4062
4063 RunningTaskInfo ci = new RunningTaskInfo();
4064 ci.id = task.taskId;
4065 ci.baseActivity = r.intent.getComponent();
4066 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004067 ci.lastActiveTime = task.lastActiveTime;
4068
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004069 if (top.task != null) {
4070 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004071 }
4072 ci.numActivities = numActivities;
4073 ci.numRunning = numRunning;
4074 //System.out.println(
4075 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08004076 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004077 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004078 }
4079
4080 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004081 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004082 if (DEBUG_SWITCH) Slog.d(
4083 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004084 if (top >= 0) {
4085 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4086 int activityTop = activities.size() - 1;
4087 if (activityTop > 0) {
4088 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4089 "unhandled-back", true);
4090 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004091 }
4092 }
4093
Craig Mautner6b74cb52013-09-27 17:02:21 -07004094 /**
4095 * Reset local parameters because an app's activity died.
4096 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004097 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004098 */
4099 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004100 if (mPausingActivity != null && mPausingActivity.app == app) {
4101 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
4102 "App died while pausing: " + mPausingActivity);
4103 mPausingActivity = null;
4104 }
4105 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4106 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004107 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004108 }
4109
Craig Mautner19091252013-10-05 00:03:53 -07004110 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004111 }
4112
Craig Mautnercae015f2013-02-08 14:31:27 -08004113 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004114 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4115 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4116 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4117 final ActivityRecord r = activities.get(activityNdx);
4118 if (r.app == app) {
4119 Slog.w(TAG, " Force finishing activity "
4120 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004121 // Force the destroy to skip right to removal.
4122 r.app = null;
4123 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004124 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004125 }
4126 }
4127 }
4128
Dianne Hackborn390517b2013-05-30 15:03:32 -07004129 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004130 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004131 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004132 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4133 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004134 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4135 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004136 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004137 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004138 if (printed) {
4139 header = null;
4140 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004141 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004142 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004143 }
4144
4145 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4146 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4147
4148 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004149 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4150 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004151 }
4152 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004153 final int top = mTaskHistory.size() - 1;
4154 if (top >= 0) {
4155 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4156 int listTop = list.size() - 1;
4157 if (listTop >= 0) {
4158 activities.add(list.get(listTop));
4159 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004160 }
4161 } else {
4162 ItemMatcher matcher = new ItemMatcher();
4163 matcher.build(name);
4164
Craig Mautneraab647e2013-02-28 16:31:36 -08004165 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4166 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4167 if (matcher.match(r1, r1.intent.getComponent())) {
4168 activities.add(r1);
4169 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004170 }
4171 }
4172 }
4173
4174 return activities;
4175 }
4176
4177 ActivityRecord restartPackage(String packageName) {
4178 ActivityRecord starting = topRunningActivityLocked(null);
4179
4180 // All activities that came from the package must be
4181 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004182 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4183 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4184 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4185 final ActivityRecord a = activities.get(activityNdx);
4186 if (a.info.packageName.equals(packageName)) {
4187 a.forceNewConfig = true;
4188 if (starting != null && a == starting && a.visible) {
4189 a.startFreezingScreenLocked(starting.app,
4190 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4191 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004192 }
4193 }
4194 }
4195
4196 return starting;
4197 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004198
Craig Mautner299f9602015-01-26 09:47:33 -08004199 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004200 removeTask(task, reason, true);
4201 }
4202
4203 void removeTask(TaskRecord task, String reason, boolean removeFromWindowManager) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004204 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004205 if (removeFromWindowManager) {
4206 mWindowManager.removeTask(task.taskId);
4207 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004208 final ActivityRecord r = mResumedActivity;
4209 if (r != null && r.task == task) {
4210 mResumedActivity = null;
4211 }
4212
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004213 final int taskNdx = mTaskHistory.indexOf(task);
4214 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004215 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4216 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4217 if (!nextTask.isOverHomeStack()) {
4218 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4219 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004220 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004221 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004222 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004223
4224 if (task.mActivities.isEmpty()) {
4225 final boolean isVoiceSession = task.voiceSession != null;
4226 if (isVoiceSession) {
4227 try {
4228 task.voiceSession.taskFinished(task.intent, task.taskId);
4229 } catch (RemoteException e) {
4230 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004231 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004232 if (task.autoRemoveFromRecents() || isVoiceSession) {
4233 // Task creator asked to remove this when done, or this task was a voice
4234 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004235 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004236 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004237 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004238 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004239
4240 if (mTaskHistory.isEmpty()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004241 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004242 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004243 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004244 String myReason = reason + " leftTaskHistoryEmpty";
4245 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4246 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4247 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004248 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004249 if (mStacks != null) {
4250 mStacks.remove(this);
4251 mStacks.add(0, this);
4252 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004253 if (notHomeStack) {
4254 mActivityContainer.onTaskListEmptyLocked();
4255 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004256 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004257
4258 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004259 }
4260
Dianne Hackborn91097de2014-04-04 18:02:06 -07004261 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4262 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4263 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004264 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4265 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004266 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004267 return task;
4268 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004269
4270 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004271 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004272 }
4273
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004274 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004275 task.stack = this;
4276 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004277 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004278 } else {
4279 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004280 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004281 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004282 if (!moving && task.voiceSession != null) {
4283 try {
4284 task.voiceSession.taskStarted(task.intent, task.taskId);
4285 } catch (RemoteException e) {
4286 }
4287 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004288 }
4289
4290 public int getStackId() {
4291 return mStackId;
4292 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004293
4294 @Override
4295 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004296 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4297 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004298 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004299
4300 boolean updateOverrideConfiguration(Configuration newConfig) {
4301 Configuration oldConfig = mOverrideConfig;
4302 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004303 // We override the configuration only when the stack's dimensions are different from
4304 // the display. In this manner, we know that if the override configuration is empty,
4305 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004306 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004307 return !mOverrideConfig.equals(oldConfig);
4308 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004309}