blob: 777d8dccd0f1a27076d8e044e16ae0ae90e1d0b7 [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_PAUSE;
21import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
22import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
23import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
24import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
25import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
26import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
27import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070031
Craig Mautner0eea92c2013-05-16 13:35:39 -070032import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070034import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070035import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070037import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070039import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
40
Dianne Hackborn89ad4562014-08-24 16:45:38 -070041import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070042import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080043import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070045import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080046import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080047import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080048import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080049import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070050import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051
52import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070053import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070054import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080056import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080058import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080064import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080065import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070067import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080068import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Handler;
70import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090071import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070073import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.os.RemoteException;
75import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080076import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070077import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070078import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070081import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082
Craig Mautnercae015f2013-02-08 14:31:27 -080083import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080084import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085import java.lang.ref.WeakReference;
86import java.util.ArrayList;
87import java.util.Iterator;
88import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080089import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070090
91/**
92 * State and management of a single stack of activities.
93 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070094final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080095
Wale Ogunwalee23149f2015-03-06 15:39:44 -080096 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
97 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070098 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
99
100 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800101
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700102 // Ticks during which we check progress while waiting for an app to launch.
103 static final int LAUNCH_TICK = 500;
104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105 // How long we wait until giving up on the last activity to pause. This
106 // is short because it directly impacts the responsiveness of starting the
107 // next activity.
108 static final int PAUSE_TIMEOUT = 500;
109
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700110 // How long we wait for the activity to tell us it has stopped before
111 // giving up. This is a good amount of time because we really need this
112 // from the application in order to get its saved state.
113 static final int STOP_TIMEOUT = 10*1000;
114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700115 // How long we wait until giving up on an activity telling us it has
116 // finished destroying itself.
117 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800120 // disabled.
121 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800122
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700123 // How long between activity launches that we consider safe to not warn
124 // the user about an unexpected activity being launched on top.
125 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 // Set to false to disable the preview that is shown while a new activity
128 // is being started.
129 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800130
Craig Mautner5eda9b32013-07-02 11:58:16 -0700131 // How long to wait for all background Activities to redraw following a call to
132 // convertToTranslucent().
133 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135 enum ActivityState {
136 INITIALIZING,
137 RESUMED,
138 PAUSING,
139 PAUSED,
140 STOPPING,
141 STOPPED,
142 FINISHING,
143 DESTROYING,
144 DESTROYED
145 }
146
147 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700148 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800149 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700151 /**
152 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800153 * running) activities. It contains #TaskRecord objects.
154 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800155 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800156
157 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800158 * Used for validating app tokens with window manager.
159 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800160 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 /**
163 * List of running activities, sorted by recent usage.
164 * The first entry in the list is the least recently used.
165 * It contains HistoryRecord objects.
166 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800167 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168
169 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 * Animations that for the current transition have requested not to
171 * be considered for the transition animation.
172 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800173 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174
175 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700176 * When we are in the process of pausing an activity, before starting the
177 * next one, this variable holds the activity that is currently being paused.
178 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800179 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180
181 /**
182 * This is the last activity that we put into the paused state. This is
183 * used to determine if we need to do an activity transition while sleeping,
184 * when we normally hold the top activity paused.
185 */
186 ActivityRecord mLastPausedActivity = null;
187
188 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700189 * Activities that specify No History must be removed once the user navigates away from them.
190 * If the device goes to sleep with such an activity in the paused state then we save it here
191 * and finish it later if another activity replaces it on wakeup.
192 */
193 ActivityRecord mLastNoHistoryActivity = null;
194
195 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196 * Current activity that is resumed, or null if there is none.
197 */
198 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800199
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700200 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700201 * This is the last activity that has been started. It is only used to
202 * identify when multiple activities are started at once so that the user
203 * can be warned they may not be in the activity they think they are.
204 */
205 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800206
Craig Mautner5eda9b32013-07-02 11:58:16 -0700207 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
208 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
209 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
210 // Activity in mTranslucentActivityWaiting is notified via
211 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
212 // background activity being drawn then the same call will be made with a true value.
213 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700214 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700215 new ArrayList<ActivityRecord>();
216
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700217 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700218 * Set when we know we are going to be calling updateConfiguration()
219 * soon, so want to skip intermediate config checks.
220 */
221 boolean mConfigWillChange;
222
Todd Kennedyaab56db2015-01-30 09:39:53 -0800223 // Whether or not this stack covers the entire screen; by default stacks are full screen
224 boolean mFullscreen = true;
225
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700226 long mLaunchStartTime = 0;
227 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800228
Craig Mautner858d8a62013-04-23 17:08:34 -0700229 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700230
Craig Mautnerc00204b2013-03-05 15:02:14 -0800231 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800232 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800233 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
234 ArrayList<ActivityStack> mStacks;
235 /** The attached Display's unique identifier, or -1 if detached */
236 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800237
Craig Mautner27084302013-03-25 08:05:25 -0700238 /** Run all ActivityStacks through this */
239 final ActivityStackSupervisor mStackSupervisor;
240
Wale Ogunwale60454db2015-01-23 16:05:07 -0800241 Configuration mOverrideConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800242 /** True if the stack was forced to full screen because {@link TaskRecord#mResizeable} is false
243 * and the stack was previously resized. */
244 private boolean mForcedFullscreen = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800245
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700246 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700247 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
248 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
249 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
250 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700251 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700252 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700253 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700254
255 static class ScheduleDestroyArgs {
256 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700258 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700259 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 mReason = reason;
261 }
262 }
263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 final Handler mHandler;
265
266 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800267
Craig Mautnerc8143c62013-09-03 12:15:57 -0700268 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900269 super(looper);
270 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700271
Zoran Marcetaf958b322012-08-09 20:27:12 +0900272 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273 public void handleMessage(Message msg) {
274 switch (msg.what) {
275 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800276 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700277 // We don't at this point know if the activity is fullscreen,
278 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700280 synchronized (mService) {
281 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700282 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700284 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700287 case LAUNCH_TICK_MSG: {
288 ActivityRecord r = (ActivityRecord)msg.obj;
289 synchronized (mService) {
290 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700291 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700292 }
293 }
294 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800296 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 // We don't at this point know if the activity is fullscreen,
298 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800299 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800300 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800301 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800302 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700303 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700304 case STOP_TIMEOUT_MSG: {
305 ActivityRecord r = (ActivityRecord)msg.obj;
306 // We don't at this point know if the activity is fullscreen,
307 // so we need to be conservative and assume it isn't.
308 Slog.w(TAG, "Activity stop timeout for " + r);
309 synchronized (mService) {
310 if (r.isInHistory()) {
311 activityStoppedLocked(r, null, null, null);
312 }
313 }
314 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700315 case DESTROY_ACTIVITIES_MSG: {
316 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
317 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700318 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700319 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700320 } break;
321 case TRANSLUCENT_TIMEOUT_MSG: {
322 synchronized (mService) {
323 notifyActivityDrawnLocked(null);
324 }
325 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700326 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700327 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700328 final ActivityRecord r = getVisibleBehindActivity();
329 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700330 if (r != null) {
331 mService.killAppAtUsersRequest(r.app, null);
332 }
333 }
334 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 }
336 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800337 }
338
Craig Mautner34b73df2014-01-12 21:11:08 -0800339 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800340 int count = 0;
341 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
342 count += mTaskHistory.get(taskNdx).mActivities.size();
343 }
344 return count;
345 }
346
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800347 int numTasks() {
348 return mTaskHistory.size();
349 }
350
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800351 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
352 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353 mActivityContainer = activityContainer;
354 mStackSupervisor = activityContainer.getOuter();
355 mService = mStackSupervisor.mService;
356 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
357 mWindowManager = mService.mWindowManager;
358 mStackId = activityContainer.mStackId;
359 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800360 mRecentTasks = recentTasks;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800361 mOverrideConfig = Configuration.EMPTY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362 }
Craig Mautner5962b122012-10-05 14:45:52 -0700363
Amith Yamasani734983f2014-03-04 16:48:05 -0800364 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100365 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800366 */
Kenny Guy2a764942014-04-02 13:29:20 +0100367 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100368 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100369 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
370 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800371 }
372 return false;
373 }
374
375 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100376 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700377 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
378 }
379
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700382 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
383 if (r != null) {
384 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800385 }
386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 return null;
388 }
389
390 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800391 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
392 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800393 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800394 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
395 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800396 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800397 return r;
398 }
399 }
400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 return null;
402 }
403
404 /**
405 * This is a simplified version of topRunningActivityLocked that provides a number of
406 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800407 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700408 * @param token If non-null, any history records matching this token will be skipped.
409 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800410 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700411 * @return Returns the HistoryRecord of the next activity on the stack.
412 */
413 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800414 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
415 TaskRecord task = mTaskHistory.get(taskNdx);
416 if (task.taskId == taskId) {
417 continue;
418 }
419 ArrayList<ActivityRecord> activities = task.mActivities;
420 for (int i = activities.size() - 1; i >= 0; --i) {
421 final ActivityRecord r = activities.get(i);
422 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800423 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 return r;
425 }
426 }
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 return null;
429 }
430
Craig Mautner8849a5e2013-04-02 16:41:03 -0700431 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
433 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700434 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
435 final ActivityRecord r = activities.get(activityNdx);
436 if (!r.finishing) {
437 return r;
438 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700439 }
440 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700441 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700442 }
443
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700444 final TaskRecord topTask() {
445 final int size = mTaskHistory.size();
446 if (size > 0) {
447 return mTaskHistory.get(size - 1);
448 }
449 return null;
450 }
451
Craig Mautnerd2328952013-03-05 12:46:26 -0800452 TaskRecord taskForIdLocked(int id) {
453 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
454 final TaskRecord task = mTaskHistory.get(taskNdx);
455 if (task.taskId == id) {
456 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800457 }
458 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700459 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700460 }
461
Craig Mautnerd2328952013-03-05 12:46:26 -0800462 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700463 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800464 return isInStackLocked(r);
465 }
466
467 ActivityRecord isInStackLocked(ActivityRecord r) {
468 if (r == null) {
469 return null;
470 }
471 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700472 if (task != null && task.stack != null
473 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800474 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800475 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800476 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800477 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800478 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800479 }
480
Craig Mautner2420ead2013-04-01 17:13:20 -0700481 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700482 final boolean hadit = mLRUActivities.remove(r);
483 mLRUActivities.add(r);
484 return hadit;
485 }
486
Craig Mautnerde4ef022013-04-07 19:01:33 -0700487 final boolean isHomeStack() {
488 return mStackId == HOME_STACK_ID;
489 }
490
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 final boolean isOnHomeDisplay() {
492 return isAttached() &&
493 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
494 }
495
Craig Mautner299f9602015-01-26 09:47:33 -0800496 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800497 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800498 final boolean homeStack = isHomeStack()
499 || (mActivityContainer.mParentActivity != null
500 && mActivityContainer.mParentActivity.isHomeActivity());
501 ActivityStack lastFocusStack = null;
502 if (!homeStack) {
503 // Need to move this stack to the front before calling
504 // {@link ActivityStackSupervisor#moveHomeStack} below.
505 lastFocusStack = mStacks.get(mStacks.size() - 1);
506 mStacks.remove(this);
507 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800508 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700509 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
510 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800511 if (isOnHomeDisplay()) {
512 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
513 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800514 final TaskRecord task = topTask();
515 if (task != null) {
516 mWindowManager.moveTaskToTop(task.taskId);
517 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800518 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800519 }
520
521 final boolean isAttached() {
522 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800523 }
524
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700525 /**
526 * Returns the top activity in any existing task matching the given
527 * Intent. Returns null if no such task is found.
528 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700529 ActivityRecord findTaskLocked(ActivityRecord target) {
530 Intent intent = target.intent;
531 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700532 ComponentName cls = intent.getComponent();
533 if (info.targetActivity != null) {
534 cls = new ComponentName(info.packageName, info.targetActivity);
535 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700536 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700537 boolean isDocument = intent != null & intent.isDocument();
538 // If documentData is non-null then it must match the existing task data.
539 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800540
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700541 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800542 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
543 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700544 if (task.voiceSession != null) {
545 // We never match voice sessions; those always run independently.
546 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
547 continue;
548 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700549 if (task.userId != userId) {
550 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700551 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700552 continue;
553 }
Craig Mautner000f0022013-02-26 15:04:29 -0800554 final ActivityRecord r = task.getTopActivity();
555 if (r == null || r.finishing || r.userId != userId ||
556 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700557 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800558 continue;
559 }
560
Craig Mautnerd00f4742014-03-12 14:17:26 -0700561 final Intent taskIntent = task.intent;
562 final Intent affinityIntent = task.affinityIntent;
563 final boolean taskIsDocument;
564 final Uri taskDocumentData;
565 if (taskIntent != null && taskIntent.isDocument()) {
566 taskIsDocument = true;
567 taskDocumentData = taskIntent.getData();
568 } else if (affinityIntent != null && affinityIntent.isDocument()) {
569 taskIsDocument = true;
570 taskDocumentData = affinityIntent.getData();
571 } else {
572 taskIsDocument = false;
573 taskDocumentData = null;
574 }
575
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700576 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700577 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700578 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700579 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700580 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
581 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700582 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700583 return r;
584 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700585 } else if (taskIntent != null && taskIntent.getComponent() != null &&
586 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700587 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700588 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800589 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700590 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
591 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800592 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700593 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
594 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700595 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700596 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800597 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700598 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
599 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800600 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700601 } else if (DEBUG_TASKS) {
602 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700603 }
604 }
605
606 return null;
607 }
608
609 /**
610 * Returns the first activity (starting from the top of the stack) that
611 * is the same as the given activity. Returns null if no such activity
612 * is found.
613 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700614 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700615 ComponentName cls = intent.getComponent();
616 if (info.targetActivity != null) {
617 cls = new ComponentName(info.packageName, info.targetActivity);
618 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700619 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620
Craig Mautner000f0022013-02-26 15:04:29 -0800621 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700622 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100623 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700624 return null;
625 }
626 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800627 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
628 ActivityRecord r = activities.get(activityNdx);
629 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700630 //Slog.i(TAG, "Found matching class!");
631 //dump();
632 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
633 return r;
634 }
635 }
636 }
637
638 return null;
639 }
640
Amith Yamasani742a6712011-05-04 14:49:28 -0700641 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700642 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700643 */
Craig Mautner93529a42013-10-04 15:03:13 -0700644 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800645 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700646 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800647 }
648 mCurrentUser = userId;
649
650 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800651 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800652 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700653 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100654 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700655 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
656 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800657 mTaskHistory.remove(i);
658 mTaskHistory.add(task);
659 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800660 // Use same value for i.
661 } else {
662 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800663 }
664 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700665 if (VALIDATE_TOKENS) {
666 validateAppTokensLocked();
667 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700668 }
669
Craig Mautner2420ead2013-04-01 17:13:20 -0700670 void minimalResumeActivityLocked(ActivityRecord r) {
671 r.state = ActivityState.RESUMED;
672 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
673 + " (starting new instance)");
674 r.stopped = false;
675 mResumedActivity = r;
676 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800677 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700678 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700679 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700680 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700681 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 }
683
Dianne Hackborncee04b52013-07-03 17:01:28 -0700684 private void startLaunchTraces() {
685 if (mFullyDrawnStartTime != 0) {
686 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
687 }
688 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
689 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
690 }
691
692 private void stopFullyDrawnTraceIfNeeded() {
693 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
694 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
695 mFullyDrawnStartTime = 0;
696 }
697 }
698
Craig Mautnere79d42682013-04-01 19:01:53 -0700699 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700700 if (r.displayStartTime == 0) {
701 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
702 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700703 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700704 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700705 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700706 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700707 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700708 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700710 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800711
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700712 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700713 // Make sure that there is no activity waiting for this to launch.
714 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
715 r.displayStartTime = r.fullyDrawnStartTime = 0;
716 } else {
717 mStackSupervisor.removeTimeoutsForActivityLocked(r);
718 mStackSupervisor.scheduleIdleTimeoutLocked(r);
719 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700720 }
721
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800722 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800723 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800724 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
725 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
726 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
727 activities.get(activityNdx).setSleeping(false);
728 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800729 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800730 if (mPausingActivity != null) {
731 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
732 activityPausedLocked(mPausingActivity.appToken, true);
733 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800734 }
735
Craig Mautner0eea92c2013-05-16 13:35:39 -0700736 /**
737 * @return true if something must be done before going to sleep.
738 */
739 boolean checkReadyForSleepLocked() {
740 if (mResumedActivity != null) {
741 // Still have something resumed; can't sleep until it is paused.
742 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
743 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700744 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700745 return true;
746 }
747 if (mPausingActivity != null) {
748 // Still waiting for something to pause; can't sleep yet.
749 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
750 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800751 }
752
Craig Mautner0eea92c2013-05-16 13:35:39 -0700753 return false;
754 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800755
Craig Mautner0eea92c2013-05-16 13:35:39 -0700756 void goToSleep() {
757 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800758
Craig Mautner0eea92c2013-05-16 13:35:39 -0700759 // Make sure any stopped but visible activities are now sleeping.
760 // This ensures that the activity's onStop() is called.
761 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
762 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
763 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
764 final ActivityRecord r = activities.get(activityNdx);
765 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
766 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800767 }
768 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700770 }
Craig Mautner59c00972012-07-30 12:10:24 -0700771
Dianne Hackbornd2835932010-12-13 16:28:46 -0800772 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700773 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800774 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700775 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800776 return null;
777 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800778
Winson Chung083baf92014-07-11 10:32:42 -0700779 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700780 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800781 // we can just go ahead and skip taking the screenshot if this is the home stack.
782 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700783 return null;
784 }
785
Winson Chung48a10a52014-08-27 14:36:51 -0700786 int w = mService.mThumbnailWidth;
787 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800788 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800789 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
790 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700791 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800792 }
Winson Chung376543b2014-05-21 17:43:28 -0700793 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800794 return null;
795 }
796
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700797 /**
798 * Start pausing the currently resumed activity. It is an error to call this if there
799 * is already an activity being paused or there is no resumed activity.
800 *
801 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
802 * @param uiSleeping True if this is happening with the user interface going to sleep (the
803 * screen turning off).
804 * @param resuming True if this is being called as part of resuming the top activity, so
805 * we shouldn't try to instigate a resume here.
806 * @param dontWait True if the caller does not want to wait for the pause to complete. If
807 * set to true, we will immediately complete the pause here before returning.
808 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
809 * it to tell us when it is done.
810 */
811 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
812 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800813 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700814 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
815 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800816 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 ActivityRecord prev = mResumedActivity;
818 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700819 if (!resuming) {
820 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
821 mStackSupervisor.resumeTopActivitiesLocked();
822 }
823 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700824 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800825
826 if (mActivityContainer.mParentActivity == null) {
827 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700828 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800829 }
830
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700831 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
832 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700833 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800834 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700836 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
837 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 prev.state = ActivityState.PAUSING;
839 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700840 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700841 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800842 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800843 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700844 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700845 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846
847 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800848
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 if (prev.app != null && prev.app.thread != null) {
850 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
851 try {
852 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700853 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700855 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800856 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700857 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 } catch (Exception e) {
859 // Ignore exception, if process died other code will cleanup.
860 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800861 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700863 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 }
865 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800866 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700867 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700868 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 }
870
871 // If we are not going to sleep, we want to ensure the device is
872 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700873 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700874 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 }
876
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700878 // Have the window manager pause its key dispatching until the new
879 // activity has started. If we're pausing the activity just because
880 // the screen is being turned off and the UI is sleeping, don't interrupt
881 // key dispatch; the same activity will pick it up again on wakeup.
882 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800883 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700884 } else {
885 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
886 }
887
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700888 if (dontWait) {
889 // If the caller said they don't want to wait for the pause, then complete
890 // the pause now.
891 completePauseLocked(false);
892 return false;
893
894 } else {
895 // Schedule a pause timeout in case the app doesn't respond.
896 // We don't give it much time because this directly impacts the
897 // responsiveness seen by the user.
898 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
899 msg.obj = prev;
900 prev.pauseTime = SystemClock.uptimeMillis();
901 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
902 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
903 return true;
904 }
905
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906 } else {
907 // This activity failed to schedule the
908 // pause, so just treat it as being paused now.
909 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700910 if (!resuming) {
911 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
912 }
913 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 }
915 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700916
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700917 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800919 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920
Craig Mautnerd2328952013-03-05 12:46:26 -0800921 final ActivityRecord r = isInStackLocked(token);
922 if (r != null) {
923 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
924 if (mPausingActivity == r) {
925 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
926 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700927 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800928 } else {
929 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
930 r.userId, System.identityHashCode(r), r.shortComponentName,
931 mPausingActivity != null
932 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 }
934 }
935 }
936
Craig Mautnera0026042014-04-23 11:45:37 -0700937 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
938 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700939 if (r.state != ActivityState.STOPPING) {
940 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
941 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
942 return;
943 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700944 if (persistentState != null) {
945 r.persistentState = persistentState;
946 mService.notifyTaskPersisterLocked(r.task, false);
947 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700948 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700949 if (icicle != null) {
950 // If icicle is null, this is happening due to a timeout, so we
951 // haven't really saved the state.
952 r.icicle = icicle;
953 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800954 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800955 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700956 }
957 if (!r.stopped) {
958 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
959 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
960 r.stopped = true;
961 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700962 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
963 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700964 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700965 if (r.finishing) {
966 r.clearOptionsLocked();
967 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700968 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700969 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700970 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700971 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700972 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800973 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700974 }
975 }
976 }
977
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700978 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800979 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700980 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800981
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800982 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700983 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 if (prev.finishing) {
985 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700986 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800987 } else if (prev.app != null) {
988 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800989 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800990 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
991 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800992 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 if (prev.configDestroy) {
994 // The previous is being paused because the configuration
995 // is changing, which means it is actually stopping...
996 // To juggle the fact that we are also starting a new
997 // instance right now, we need to first completely stop
998 // the current instance before starting the new one.
999 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001000 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -07001001 } else if (!hasVisibleBehindActivity()) {
1002 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001003 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001004 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001005 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1006 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 // If we already have a few activities waiting to stop,
1008 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001009 // them out. Or if r is the last of activity of the last task the stack
1010 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001011 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001012 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001014 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001015 }
1016 }
1017 } else {
1018 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1019 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001020 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001021 // It is possible the activity was freezing the screen before it was paused.
1022 // In that case go ahead and remove the freeze this activity has on the screen
1023 // since it is no longer visible.
1024 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001025 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001027
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001028 if (resumeNext) {
1029 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1030 if (!mService.isSleepingOrShuttingDown()) {
1031 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1032 } else {
1033 mStackSupervisor.checkReadyForSleepLocked();
1034 ActivityRecord top = topStack.topRunningActivityLocked(null);
1035 if (top == null || (prev != null && top != prev)) {
1036 // If there are no more activities available to run,
1037 // do resume anyway to start something. Also if the top
1038 // activity on the stack is not the just paused activity,
1039 // we need to go ahead and resume it to ensure we complete
1040 // an in-flight app switch.
1041 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1042 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001043 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001044 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001045
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001046 if (prev != null) {
1047 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048
Craig Mautner525f3d92013-05-07 14:01:50 -07001049 if (prev.app != null && prev.cpuTimeAtResume > 0
1050 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001051 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1052 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001053 if (diff > 0) {
1054 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1055 synchronized (bsi) {
1056 BatteryStatsImpl.Uid.Proc ps =
1057 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001058 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001059 if (ps != null) {
1060 ps.addForegroundTimeLocked(diff);
1061 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062 }
1063 }
1064 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001065 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001066 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001067
1068 // Notfiy when the task stack has changed
1069 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070 }
1071
1072 /**
1073 * Once we know that we have asked an application to put an activity in
1074 * the resumed state (either by launching it or explicitly telling it),
1075 * this function updates the rest of our state to match that fact.
1076 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001077 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 next.idle = false;
1079 next.results = null;
1080 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001081
1082 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1083 ProcessRecord app = next.task.mActivities.get(0).app;
1084 if (app != null && app != mService.mHomeProcess) {
1085 mService.mHomeProcess = app;
1086 }
1087 }
1088
Craig Mautner07566322013-09-26 16:42:55 -07001089 if (next.nowVisible) {
1090 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001091 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001092 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001093
1094 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001095 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001097 mStackSupervisor.reportResumedActivityLocked(next);
1098
1099 next.resumeKeyDispatchingLocked();
1100 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001101
1102 // Mark the point when the activity is resuming
1103 // TODO: To be more accurate, the mark should be before the onCreate,
1104 // not after the onResume. But for subsequent starts, onResume is fine.
1105 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001106 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107 } else {
1108 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1109 }
Winson Chung376543b2014-05-21 17:43:28 -07001110
George Mount6ba042b2014-07-28 11:12:28 -07001111 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001112
1113 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1114 // When resuming an activity, require it to call requestVisibleBehind() again.
1115 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1116 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001117 }
1118
Craig Mautner2568c3a2015-03-26 14:22:34 -07001119 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001120 r.visible = visible;
1121 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001122 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001123 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001124 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001125 container.setVisible(visible);
1126 }
1127 }
1128
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001129 // Find the first visible activity above the passed activity and if it is translucent return it
1130 // otherwise return null;
1131 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1132 TaskRecord task = r.task;
1133 if (task == null) {
1134 return null;
1135 }
1136
1137 ActivityStack stack = task.stack;
1138 if (stack == null) {
1139 return null;
1140 }
1141
1142 int stackNdx = mStacks.indexOf(stack);
1143
1144 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1145 int taskNdx = tasks.indexOf(task);
1146
1147 ArrayList<ActivityRecord> activities = task.mActivities;
1148 int activityNdx = activities.indexOf(r) + 1;
1149
1150 final int numStacks = mStacks.size();
1151 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001152 ActivityStack historyStack = mStacks.get(stackNdx);
1153 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001154 final int numTasks = tasks.size();
1155 while (taskNdx < numTasks) {
1156 activities = tasks.get(taskNdx).mActivities;
1157 final int numActivities = activities.size();
1158 while (activityNdx < numActivities) {
1159 final ActivityRecord activity = activities.get(activityNdx);
1160 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001161 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001162 }
1163 ++activityNdx;
1164 }
1165 activityNdx = 0;
1166 ++taskNdx;
1167 }
1168 taskNdx = 0;
1169 ++stackNdx;
1170 }
1171
1172 return null;
1173 }
1174
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001175 private ActivityStack getNextVisibleStackLocked() {
1176 ArrayList<ActivityStack> stacks = mStacks;
1177 final ActivityRecord parent = mActivityContainer.mParentActivity;
1178 if (parent != null) {
1179 stacks = parent.task.stack.mStacks;
1180 }
1181 if (stacks != null) {
1182 for (int i = stacks.size() - 1; i >= 0; --i) {
1183 ActivityStack stack = stacks.get(i);
1184 if (stack != this && stack.isStackVisibleLocked()) {
1185 return stack;
1186 }
1187 }
1188 }
1189 return null;
1190 }
1191
Jose Lima7ba71252014-04-30 12:59:27 -07001192 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1193 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001194 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001195 if (!isAttached()) {
1196 return false;
1197 }
1198
1199 if (mStackSupervisor.isFrontStack(this)) {
1200 return true;
1201 }
1202
Jose Lima729cb232014-05-05 15:59:40 -07001203 /**
1204 * Start at the task above this one and go up, looking for a visible
1205 * fullscreen activity, or a translucent activity that requested the
1206 * wallpaper to be shown behind it.
1207 */
Jose Lima7ba71252014-04-30 12:59:27 -07001208 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001209 ActivityStack stack = mStacks.get(i);
1210 // stack above isn't full screen, so, we assume we're still visible. at some point
1211 // we should look at the stack bounds to see if we're occluded even if the stack
1212 // isn't fullscreen
1213 if (!stack.mFullscreen) {
1214 continue;
1215 }
1216 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1217 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001218 final TaskRecord task = tasks.get(taskNdx);
1219 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001220 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001221 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001222
1223 // Conditions for an activity to obscure the stack we're
1224 // examining:
1225 // 1. Not Finishing AND Visible AND:
1226 // 2. Either:
1227 // - Full Screen Activity OR
1228 // - On top of Home and our stack is NOT home
1229 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001230 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001231 return false;
1232 }
1233 }
1234 }
1235 }
1236
1237 return true;
1238 }
1239
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001240 /**
1241 * Make sure that all activities that need to be visible (that is, they
1242 * currently can be seen by the user) actually are.
1243 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001244 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1245 ActivityRecord top = topRunningActivityLocked(null);
1246 if (top == null) {
1247 return;
1248 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001249 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001250 TAG, "ensureActivitiesVisible behind " + top
1251 + " configChanges=0x" + Integer.toHexString(configChanges));
1252
Craig Mautner5eda9b32013-07-02 11:58:16 -07001253 if (mTranslucentActivityWaiting != top) {
1254 mUndrawnActivitiesBelowTopTranslucent.clear();
1255 if (mTranslucentActivityWaiting != null) {
1256 // Call the callback with a timeout indication.
1257 notifyActivityDrawnLocked(null);
1258 mTranslucentActivityWaiting = null;
1259 }
1260 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1261 }
1262
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001263 // If the top activity is not fullscreen, then we need to
1264 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001265 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001266 boolean behindFullscreen = !isStackVisibleLocked();
Jose Lima7ba71252014-04-30 12:59:27 -07001267
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001268 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001269 final TaskRecord task = mTaskHistory.get(taskNdx);
1270 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001271 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1272 final ActivityRecord r = activities.get(activityNdx);
1273 if (r.finishing) {
1274 continue;
1275 }
1276 if (aboveTop && r != top) {
1277 continue;
1278 }
1279 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001280 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1281 // but must be drawn initially for the animation as though they were visible.
1282 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001284 TAG, "Make visible? " + r + " finishing=" + r.finishing
1285 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001286
Craig Mautnerd44711d2013-02-23 11:24:36 -08001287 // First: if this is not the current activity being started, make
1288 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001289 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001290 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001292
1293 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001294 // This activity needs to be visible, but isn't even
1295 // running... get it started, but don't resume it
1296 // at this point.
1297 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1298 if (r != starting) {
1299 r.startFreezingScreenLocked(r.app, configChanges);
1300 }
1301 if (!r.visible || r.mLaunchTaskBehind) {
1302 if (DEBUG_VISBILITY) Slog.v(
1303 TAG, "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001305 }
1306 if (r != starting) {
1307 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001308 }
1309
1310 } else if (r.visible) {
1311 // If this activity is already visible, then there is nothing
1312 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001313 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001314 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001315 try {
George Mount6ba042b2014-07-28 11:12:28 -07001316 if (r.returningOptions != null) {
1317 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1318 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001319 }
1320 } catch(RemoteException e) {
1321 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001322 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001323 // This activity is not currently visible, but is running.
1324 // Tell it to become visible.
1325 r.visible = true;
1326 if (r.state != ActivityState.RESUMED && r != starting) {
1327 // If this activity is paused, tell it
1328 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001329 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001330 TAG, "Making visible and scheduling visibility: " + r);
1331 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001332 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001333 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001334 mUndrawnActivitiesBelowTopTranslucent.add(r);
1335 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001336 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001337 r.sleeping = false;
1338 r.app.pendingUiClean = true;
1339 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1340 r.stopFreezingScreenLocked(false);
1341 } catch (Exception e) {
1342 // Just skip on any failure; we'll make it
1343 // visible when it next restarts.
1344 Slog.w(TAG, "Exception thrown making visibile: "
1345 + r.intent.getComponent(), e);
1346 }
1347 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001348 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001349
Craig Mautnerd44711d2013-02-23 11:24:36 -08001350 // Aggregate current change flags.
1351 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001352
Craig Mautnerd44711d2013-02-23 11:24:36 -08001353 if (r.fullscreen) {
1354 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001355 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1356 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001357 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001358 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001359 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001360 }
1361 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001362 if (DEBUG_VISBILITY) Slog.v(
1363 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1364 + " state=" + r.state
1365 + " behindFullscreen=" + behindFullscreen);
1366 // Now for any activities that aren't visible to the user, make
1367 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001368 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001369 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001370 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001371 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001372 switch (r.state) {
1373 case STOPPING:
1374 case STOPPED:
1375 if (r.app != null && r.app.thread != null) {
1376 if (DEBUG_VISBILITY) Slog.v(
1377 TAG, "Scheduling invisibility: " + r);
1378 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1379 }
1380 break;
1381
1382 case INITIALIZING:
1383 case RESUMED:
1384 case PAUSING:
1385 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001386 // This case created for transitioning activities from
1387 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001388 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001389 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001390 } else {
1391 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1392 mStackSupervisor.mStoppingActivities.add(r);
1393 }
1394 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001395 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001396 break;
1397
1398 default:
1399 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001400 }
1401 } catch (Exception e) {
1402 // Just skip on any failure; we'll make it
1403 // visible when it next restarts.
1404 Slog.w(TAG, "Exception thrown making hidden: "
1405 + r.intent.getComponent(), e);
1406 }
1407 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001408 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001410 }
1411 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001412 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001413
1414 if (mTranslucentActivityWaiting != null &&
1415 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1416 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1417 notifyActivityDrawnLocked(null);
1418 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 }
Craig Mautner58547802013-03-05 08:23:53 -08001420
Todd Kennedyaab56db2015-01-30 09:39:53 -08001421 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001422 mTranslucentActivityWaiting = r;
1423 mUndrawnActivitiesBelowTopTranslucent.clear();
1424 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1425 }
1426
1427 /**
1428 * Called as activities below the top translucent activity are redrawn. When the last one is
1429 * redrawn notify the top activity by calling
1430 * {@link Activity#onTranslucentConversionComplete}.
1431 *
1432 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1433 * occurred and the activity will be notified immediately.
1434 */
1435 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001436 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001437 if ((r == null)
1438 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1439 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1440 // The last undrawn activity below the top has just been drawn. If there is an
1441 // opaque activity at the top, notify it that it can become translucent safely now.
1442 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1443 mTranslucentActivityWaiting = null;
1444 mUndrawnActivitiesBelowTopTranslucent.clear();
1445 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1446
Craig Mautner71dd1b62014-02-18 15:48:52 -08001447 if (waitingActivity != null) {
1448 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1449 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1450 try {
1451 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1452 waitingActivity.appToken, r != null);
1453 } catch (RemoteException e) {
1454 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001455 }
1456 }
1457 }
1458 }
1459
Craig Mautnera61bc652013-10-28 15:43:18 -07001460 /** If any activities below the top running one are in the INITIALIZING state and they have a
1461 * starting window displayed then remove that starting window. It is possible that the activity
1462 * in this state will never resumed in which case that starting window will be orphaned. */
1463 void cancelInitializingActivities() {
1464 final ActivityRecord topActivity = topRunningActivityLocked(null);
1465 boolean aboveTop = true;
1466 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1467 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1468 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1469 final ActivityRecord r = activities.get(activityNdx);
1470 if (aboveTop) {
1471 if (r == topActivity) {
1472 aboveTop = false;
1473 }
1474 continue;
1475 }
1476
1477 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1478 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1479 r.mStartingWindowShown = false;
1480 mWindowManager.removeAppStartingWindow(r.appToken);
1481 }
1482 }
1483 }
1484 }
1485
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 /**
1487 * Ensure that the top activity in the stack is resumed.
1488 *
1489 * @param prev The previously resumed activity, for when in the process
1490 * of pausing; can be null to call from elsewhere.
1491 *
1492 * @return Returns true if something is being resumed, or false if
1493 * nothing happened.
1494 */
1495 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001496 return resumeTopActivityLocked(prev, null);
1497 }
1498
1499 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001500 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001501 // Don't even start recursing.
1502 return false;
1503 }
1504
1505 boolean result = false;
1506 try {
1507 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001508 mStackSupervisor.inResumeTopActivity = true;
1509 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1510 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001511 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001512 }
Craig Mautner544efa72014-09-04 16:41:20 -07001513 result = resumeTopActivityInnerLocked(prev, options);
1514 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001515 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001516 }
1517 return result;
1518 }
1519
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001520 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001521 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1522
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001523 if (!mService.mBooting && !mService.mBooted) {
1524 // Not ready yet!
1525 return false;
1526 }
1527
Craig Mautnerdf88d732014-01-27 09:21:32 -08001528 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001529 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001530 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001531 // Do not resume this stack if its parent is not resumed.
1532 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1533 return false;
1534 }
1535
Craig Mautnera61bc652013-10-28 15:43:18 -07001536 cancelInitializingActivities();
1537
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001538 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001539 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540
1541 // Remember how we'll process this pause/resume situation, and ensure
1542 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001543 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1544 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545
Craig Mautner84984fa2014-06-19 11:19:20 -07001546 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001548 // There are no more activities!
1549 final String reason = "noMoreActivities";
1550 if (!mFullscreen) {
1551 // Try to move focus to the next visible stack with a running activity if this
1552 // stack is not covering the entire screen.
1553 final ActivityStack stack = getNextVisibleStackLocked();
1554 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1555 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1556 }
1557 }
1558 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001559 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001560 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001561 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001562 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001563 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1564 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1565 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001566 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 }
1568
1569 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001570
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001572 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1573 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 // Make sure we have executed any pending transitions, since there
1575 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001576 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001577 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001578 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001579 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001580 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 return false;
1582 }
1583
Craig Mautner525f3d92013-05-07 14:01:50 -07001584 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001585 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001586 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001587 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001588 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001589 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001590 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001591 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001592 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001593 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001594 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001595 } else if (!isOnHomeDisplay()) {
1596 return false;
1597 } else if (!isHomeStack()){
1598 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001599 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001600 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1601 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1602 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001603 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001604 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001605 }
1606
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 // If we are sleeping, and there is no resumed activity, and the top
1608 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001609 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001610 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001611 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 // Make sure we have executed any pending transitions, since there
1613 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001614 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001616 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001617 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001618 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 return false;
1620 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001621
1622 // Make sure that the user who owns this activity is started. If not,
1623 // we will just leave it as is because someone should be bringing
1624 // another user's activities to the top of the stack.
1625 if (mService.mStartedUsers.get(next.userId) == null) {
1626 Slog.w(TAG, "Skipping resume of top activity " + next
1627 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001628 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001629 return false;
1630 }
1631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 // The activity may be waiting for stop, but that is no longer
1633 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001634 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001635 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001636 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001637 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638
1639 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1640
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001641 // If we are currently pausing an activity, then don't do anything
1642 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001643 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001644 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1645 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001646 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001647 return false;
1648 }
1649
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001650 // Okay we are now going to start a switch, to 'next'. We may first
1651 // have to pause the current activity, but this is an important point
1652 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001653 // XXX "App Redirected" dialog is getting too many false positives
1654 // at this point, so turn off for now.
1655 if (false) {
1656 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1657 long now = SystemClock.uptimeMillis();
1658 final boolean inTime = mLastStartedActivity.startTime != 0
1659 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1660 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1661 final int nextUid = next.info.applicationInfo.uid;
1662 if (inTime && lastUid != nextUid
1663 && lastUid != next.launchedFromUid
1664 && mService.checkPermission(
1665 android.Manifest.permission.STOP_APP_SWITCHES,
1666 -1, next.launchedFromUid)
1667 != PackageManager.PERMISSION_GRANTED) {
1668 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1669 } else {
1670 next.startTime = now;
1671 mLastStartedActivity = next;
1672 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001673 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001674 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001675 mLastStartedActivity = next;
1676 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001677 }
Craig Mautner58547802013-03-05 08:23:53 -08001678
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001679 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1680
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001681 // We need to start pausing the current activity so the top one
1682 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001683 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1684 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001685 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001686 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001687 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001688 }
1689 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001690 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1691 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001692 // At this point we want to put the upcoming activity's process
1693 // at the top of the LRU list, since we know we will be needing it
1694 // very soon and it would be a waste to let it get killed if it
1695 // happens to be sitting towards the end.
1696 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001697 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001698 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001699 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 return true;
1701 }
1702
Christopher Tated3f175c2012-06-14 14:16:54 -07001703 // If the most recent activity was noHistory but was only stopped rather
1704 // than stopped+finished because the device went to sleep, we need to make
1705 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001706 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001707 !mLastNoHistoryActivity.finishing) {
1708 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1709 " on new resume");
1710 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001711 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001712 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001713 }
1714
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001716 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1717 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001718 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 if (DEBUG_SWITCH) Slog.v(
1720 TAG, "Resuming top, waiting visible to hide: " + prev);
1721 } else {
1722 // The next activity is already visible, so hide the previous
1723 // activity's windows right now so we can show the new one ASAP.
1724 // We only do this if the previous is finishing, which should mean
1725 // it is on top of the one being resumed so hiding it quickly
1726 // is good. Otherwise, we want to do the normal route of allowing
1727 // the resumed activity to be shown so we can decide if the
1728 // previous should actually be hidden depending on whether the
1729 // new one is found to be full-screen or not.
1730 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001731 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1733 + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001734 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 + ", nowVisible=" + next.nowVisible);
1736 } else {
Craig Mautner8c14c152015-01-15 17:32:07 -08001737 if (DEBUG_SWITCH) Slog.v(TAG,
1738 "Previous already visible but still waiting to hide: " + prev
1739 + ", waitingVisible="
1740 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1741 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 }
1743 }
1744 }
1745
Dianne Hackborne7f97212011-02-24 14:40:20 -08001746 // Launching this app's activity, make sure the app is no longer
1747 // considered stopped.
1748 try {
1749 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001750 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001751 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001752 } catch (IllegalArgumentException e) {
1753 Slog.w(TAG, "Failed trying to unstop package "
1754 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001755 }
1756
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 // We are starting up the next activity, so tell the window manager
1758 // that the previous one will be hidden soon. This way it can know
1759 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001760 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001761 if (prev != null) {
1762 if (prev.finishing) {
1763 if (DEBUG_TRANSITION) Slog.v(TAG,
1764 "Prepare close transition: prev=" + prev);
1765 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001766 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001767 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001769 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001770 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1771 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001773 mWindowManager.setAppWillBeHidden(prev.appToken);
1774 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001776 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001778 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001781 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001782 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001783 : next.mLaunchTaskBehind
1784 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1785 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 }
1787 }
1788 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001789 mWindowManager.setAppWillBeHidden(prev.appToken);
1790 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 }
Craig Mautner967212c2013-04-13 21:10:58 -07001792 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001793 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001795 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001796 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001798 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 }
1800 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001801
1802 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001803 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001804 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1805 if (opts != null) {
1806 resumeAnimOptions = opts.toBundle();
1807 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001808 next.applyOptionsLocked();
1809 } else {
1810 next.clearOptionsLocked();
1811 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812
Craig Mautnercf910b02013-04-23 11:23:27 -07001813 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 if (next.app != null && next.app.thread != null) {
1815 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1816
1817 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001818 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001820 // schedule launch ticks to collect information about slow apps.
1821 next.startLaunchTickingLocked();
1822
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001823 ActivityRecord lastResumedActivity =
1824 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825 ActivityState lastState = next.state;
1826
1827 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001828
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001829 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 next.state = ActivityState.RESUMED;
1831 mResumedActivity = next;
1832 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001833 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001834 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001836 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837
1838 // Have the window manager re-evaluate the orientation of
1839 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001840 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001841 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001842 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001843 mService.mConfiguration,
1844 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1845 if (config != null) {
1846 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001847 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001848 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001850
Craig Mautner525f3d92013-05-07 14:01:50 -07001851 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001852 // The configuration update wasn't able to keep the existing
1853 // instance of the activity, and instead started a new one.
1854 // We should be all done, but let's just make sure our activity
1855 // is still at the top and schedule another run if something
1856 // weird happened.
1857 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001858 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001859 "Activity config changed during resume: " + next
1860 + ", new next: " + nextNext);
1861 if (nextNext != next) {
1862 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001863 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001864 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001865 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001866 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001867 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001868 return true;
1869 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001870 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001871 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 }
Craig Mautner58547802013-03-05 08:23:53 -08001873
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 try {
1875 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001876 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 if (a != null) {
1878 final int N = a.size();
1879 if (!next.finishing && N > 0) {
1880 if (DEBUG_RESULTS) Slog.v(
1881 TAG, "Delivering results to " + next
1882 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001883 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001884 }
1885 }
1886
1887 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001888 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001889 }
1890
Craig Mautner299f9602015-01-26 09:47:33 -08001891 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1892 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001893
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001894 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001895 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001896 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001897 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001898 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001899 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001900 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001901
Craig Mautner0eea92c2013-05-16 13:35:39 -07001902 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903
Craig Mautnerac6f8432013-07-17 13:24:59 -07001904 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 } catch (Exception e) {
1906 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001907 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1908 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001910 if (lastStack != null) {
1911 lastStack.mResumedActivity = lastResumedActivity;
1912 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001913 Slog.i(TAG, "Restarting because process died: " + next);
1914 if (!next.hasBeenLaunched) {
1915 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001916 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1917 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001918 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001919 next.appToken, next.packageName, next.theme,
1920 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001921 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1922 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 }
George Mount2c92c972014-03-20 09:38:23 -07001924 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001925 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926 return true;
1927 }
1928
1929 // From this point on, if something goes wrong there is no way
1930 // to recover the activity.
1931 try {
1932 next.visible = true;
1933 completeResumeLocked(next);
1934 } catch (Exception e) {
1935 // If any exception gets thrown, toss away this
1936 // activity and try the next one.
1937 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001938 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001939 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001940 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 return true;
1942 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001943 next.stopped = false;
1944
1945 } else {
1946 // Whoops, need to restart this activity!
1947 if (!next.hasBeenLaunched) {
1948 next.hasBeenLaunched = true;
1949 } else {
1950 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001951 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001952 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001953 mService.compatibilityInfoForPackageLocked(
1954 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001956 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001957 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 }
1959 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1960 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001961 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001962 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001963 }
1964
Craig Mautnercf910b02013-04-23 11:23:27 -07001965 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 return true;
1967 }
1968
riddle_hsuc215a4f2014-12-27 12:10:45 +08001969 private TaskRecord getNextTask(TaskRecord targetTask) {
1970 final int index = mTaskHistory.indexOf(targetTask);
1971 if (index >= 0) {
1972 final int numTasks = mTaskHistory.size();
1973 for (int i = index + 1; i < numTasks; ++i) {
1974 TaskRecord task = mTaskHistory.get(i);
1975 if (task.userId == targetTask.userId) {
1976 return task;
1977 }
1978 }
1979 }
1980 return null;
1981 }
1982
Craig Mautnerac6f8432013-07-17 13:24:59 -07001983 private void insertTaskAtTop(TaskRecord task) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001984 // If the moving task is over home stack, transfer its return type to next task
1985 if (task.isOverHomeStack()) {
1986 final TaskRecord nextTask = getNextTask(task);
1987 if (nextTask != null) {
1988 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1989 }
1990 }
1991
Craig Mautner9c85c202013-10-04 20:11:26 -07001992 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08001993 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001994 if (isOnHomeDisplay()) {
1995 ActivityStack lastStack = mStackSupervisor.getLastStack();
1996 final boolean fromHome = lastStack.isHomeStack();
1997 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001998 task.setTaskToReturnTo(fromHome
1999 ? lastStack.topTask() == null
2000 ? HOME_ACTIVITY_TYPE
2001 : lastStack.topTask().taskType
2002 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002003 }
2004 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002005 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002006 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002007
Craig Mautnerac6f8432013-07-17 13:24:59 -07002008 mTaskHistory.remove(task);
2009 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002010 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01002011 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002012 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002013 while (--taskNdx >= 0) {
2014 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002015 break;
2016 }
2017 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002018 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002019 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002020 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002021 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002022 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002023
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002025 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002026 TaskRecord rTask = r.task;
2027 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002028 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2029 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002030 // Last activity in task had been removed or ActivityManagerService is reusing task.
2031 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002032 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002033 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002034 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002035 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002036 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002037 if (!newTask) {
2038 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002039 boolean startIt = true;
2040 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2041 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002042 if (task.getTopActivity() == null) {
2043 // All activities in task are finishing.
2044 continue;
2045 }
Craig Mautner70a86932013-02-28 22:37:44 -08002046 if (task == r.task) {
2047 // Here it is! Now, if this is not yet visible to the
2048 // user, then just add it without starting; it will
2049 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002050 if (!startIt) {
2051 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2052 + task, new RuntimeException("here").fillInStackTrace());
2053 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002054 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002055 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2056 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002057 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002058 r.userId, r.info.configChanges, task.voiceSession != null,
2059 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002061 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002063 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 return;
2065 }
2066 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002067 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 startIt = false;
2069 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 }
2071 }
2072
2073 // Place a new activity at top of stack, so it is next to interact
2074 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002076 // If we are not placing the new activity frontmost, we do not want
2077 // to deliver the onUserLeaving callback to the actual frontmost
2078 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002079 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002080 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002081 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2082 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 }
Craig Mautner70a86932013-02-28 22:37:44 -08002084
2085 task = r.task;
2086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002088 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002089 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002090 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002091 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002092
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002093 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002094 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002095 // We want to show the starting preview window if we are
2096 // switching to a new task, or the next activity's process is
2097 // not currently running.
2098 boolean showStartingIcon = newTask;
2099 ProcessRecord proc = r.app;
2100 if (proc == null) {
2101 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2102 }
2103 if (proc == null || proc.thread == null) {
2104 showStartingIcon = true;
2105 }
2106 if (DEBUG_TRANSITION) Slog.v(TAG,
2107 "Prepare open transition: starting " + r);
2108 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002109 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002112 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002113 ? r.mLaunchTaskBehind
2114 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2115 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002116 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 mNoAnimActivities.remove(r);
2118 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002119 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002120 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002121 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002122 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 boolean doShow = true;
2124 if (newTask) {
2125 // Even though this activity is starting fresh, we still need
2126 // to reset it to make sure we apply affinities to move any
2127 // existing activities from other tasks in to it.
2128 // If the caller has requested that the target task be
2129 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002130 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131 resetTaskIfNeededLocked(r, r);
2132 doShow = topRunningNonDelayedActivityLocked(null) == r;
2133 }
George Mount70778d72014-07-01 16:33:45 -07002134 } else if (options != null && new ActivityOptions(options).getAnimationType()
2135 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2136 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002138 if (r.mLaunchTaskBehind) {
2139 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2140 // tell WindowManager that r is visible even though it is at the back of the stack.
2141 mWindowManager.setAppVisibility(r.appToken, true);
2142 ensureActivitiesVisibleLocked(null, 0);
2143 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 // Figure out if we are transitioning from another activity that is
2145 // "has the same starting icon" as the next one. This allows the
2146 // window manager to keep the previous window it had previously
2147 // created, if it still had one.
2148 ActivityRecord prev = mResumedActivity;
2149 if (prev != null) {
2150 // We don't want to reuse the previous starting preview if:
2151 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002152 if (prev.task != r.task) {
2153 prev = null;
2154 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002156 else if (prev.nowVisible) {
2157 prev = null;
2158 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002160 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002161 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002162 mService.compatibilityInfoForPackageLocked(
2163 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002164 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002165 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002166 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 }
2168 } else {
2169 // If this is the first activity, don't do any fancy animations,
2170 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002171 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002172 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002173 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002174 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002175 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002176 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 }
2178 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002179 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 }
2181
2182 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002183 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184 }
2185 }
2186
Dianne Hackbornbe707852011-11-11 14:32:10 -08002187 final void validateAppTokensLocked() {
2188 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002189 mValidateAppTokens.ensureCapacity(numActivities());
2190 final int numTasks = mTaskHistory.size();
2191 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2192 TaskRecord task = mTaskHistory.get(taskNdx);
2193 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002194 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002195 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002196 }
Craig Mautner000f0022013-02-26 15:04:29 -08002197 TaskGroup group = new TaskGroup();
2198 group.taskId = task.taskId;
2199 mValidateAppTokens.add(group);
2200 final int numActivities = activities.size();
2201 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2202 final ActivityRecord r = activities.get(activityNdx);
2203 group.tokens.add(r.appToken);
2204 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002205 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002206 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002207 }
2208
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 /**
2210 * Perform a reset of the given task, if needed as part of launching it.
2211 * Returns the new HistoryRecord at the top of the task.
2212 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002213 /**
2214 * Helper method for #resetTaskIfNeededLocked.
2215 * We are inside of the task being reset... we'll either finish this activity, push it out
2216 * for another task, or leave it as-is.
2217 * @param task The task containing the Activity (taskTop) that might be reset.
2218 * @param forceReset
2219 * @return An ActivityOptions that needs to be processed.
2220 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002221 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002222 ActivityOptions topOptions = null;
2223
2224 int replyChainEnd = -1;
2225 boolean canMoveOptions = true;
2226
2227 // We only do this for activities that are not the root of the task (since if we finish
2228 // the root, we may no longer have the task!).
2229 final ArrayList<ActivityRecord> activities = task.mActivities;
2230 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002231 final int rootActivityNdx = task.findEffectiveRootIndex();
2232 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002233 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002234 if (target.frontOfTask)
2235 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002236
2237 final int flags = target.info.flags;
2238 final boolean finishOnTaskLaunch =
2239 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2240 final boolean allowTaskReparenting =
2241 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2242 final boolean clearWhenTaskReset =
2243 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2244
2245 if (!finishOnTaskLaunch
2246 && !clearWhenTaskReset
2247 && target.resultTo != null) {
2248 // If this activity is sending a reply to a previous
2249 // activity, we can't do anything with it now until
2250 // we reach the start of the reply chain.
2251 // XXX note that we are assuming the result is always
2252 // to the previous activity, which is almost always
2253 // the case but we really shouldn't count on.
2254 if (replyChainEnd < 0) {
2255 replyChainEnd = i;
2256 }
2257 } else if (!finishOnTaskLaunch
2258 && !clearWhenTaskReset
2259 && allowTaskReparenting
2260 && target.taskAffinity != null
2261 && !target.taskAffinity.equals(task.affinity)) {
2262 // If this activity has an affinity for another
2263 // task, then we need to move it out of here. We will
2264 // move it as far out of the way as possible, to the
2265 // bottom of the activity stack. This also keeps it
2266 // correctly ordered with any activities we previously
2267 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002268 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002269 final ActivityRecord bottom =
2270 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002271 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002272 if (bottom != null && target.taskAffinity != null
2273 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002274 // If the activity currently at the bottom has the
2275 // same task affinity as the one we are moving,
2276 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002277 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002278 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002279 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002280 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002281 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002282 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002283 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002284 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2285 + " out to new task " + target.task);
2286 }
2287
Craig Mautnere3a74d52013-02-22 14:14:58 -08002288 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002289 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002290
Craig Mautner525f3d92013-05-07 14:01:50 -07002291 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002292 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2293 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002294 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002295 if (p.finishing) {
2296 continue;
2297 }
2298
Craig Mautnere3a74d52013-02-22 14:14:58 -08002299 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002300 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002301 topOptions = p.takeOptionsLocked();
2302 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002303 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002304 }
2305 }
2306 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002307 + task + " adding to task=" + targetTask
2308 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002309 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2310 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002311 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002312 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002313
Craig Mautner83162a92015-01-26 14:43:30 -08002314 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002315 }
2316
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002317 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002318 if (VALIDATE_TOKENS) {
2319 validateAppTokensLocked();
2320 }
2321
2322 replyChainEnd = -1;
2323 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2324 // If the activity should just be removed -- either
2325 // because it asks for it, or the task should be
2326 // cleared -- then finish it and anything that is
2327 // part of its reply chain.
2328 int end;
2329 if (clearWhenTaskReset) {
2330 // In this case, we want to finish this activity
2331 // and everything above it, so be sneaky and pretend
2332 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002333 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002334 } else if (replyChainEnd < 0) {
2335 end = i;
2336 } else {
2337 end = replyChainEnd;
2338 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002339 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002340 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002341 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002342 if (p.finishing) {
2343 continue;
2344 }
2345 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002346 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002347 topOptions = p.takeOptionsLocked();
2348 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002349 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002350 }
2351 }
Craig Mautner58547802013-03-05 08:23:53 -08002352 if (DEBUG_TASKS) Slog.w(TAG,
2353 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002354 if (finishActivityLocked(
2355 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002356 end--;
2357 srcPos--;
2358 }
2359 }
2360 replyChainEnd = -1;
2361 } else {
2362 // If we were in the middle of a chain, well the
2363 // activity that started it all doesn't want anything
2364 // special, so leave it all as-is.
2365 replyChainEnd = -1;
2366 }
2367 }
2368
2369 return topOptions;
2370 }
2371
2372 /**
2373 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2374 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2375 * @param affinityTask The task we are looking for an affinity to.
2376 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2377 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2378 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2379 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002380 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002381 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002382 int replyChainEnd = -1;
2383 final int taskId = task.taskId;
2384 final String taskAffinity = task.affinity;
2385
2386 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2387 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002388 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2389
2390 // Do not operate on or below the effective root Activity.
2391 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002392 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002393 if (target.frontOfTask)
2394 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002395
2396 final int flags = target.info.flags;
2397 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2398 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2399
2400 if (target.resultTo != null) {
2401 // If this activity is sending a reply to a previous
2402 // activity, we can't do anything with it now until
2403 // we reach the start of the reply chain.
2404 // XXX note that we are assuming the result is always
2405 // to the previous activity, which is almost always
2406 // the case but we really shouldn't count on.
2407 if (replyChainEnd < 0) {
2408 replyChainEnd = i;
2409 }
2410 } else if (topTaskIsHigher
2411 && allowTaskReparenting
2412 && taskAffinity != null
2413 && taskAffinity.equals(target.taskAffinity)) {
2414 // This activity has an affinity for our task. Either remove it if we are
2415 // clearing or move it over to our task. Note that
2416 // we currently punt on the case where we are resetting a
2417 // task that is not at the top but who has activities above
2418 // with an affinity to it... this is really not a normal
2419 // case, and we will need to later pull that task to the front
2420 // and usually at that point we will do the reset and pick
2421 // up those remaining activities. (This only happens if
2422 // someone starts an activity in a new task from an activity
2423 // in a task that is not currently on top.)
2424 if (forceReset || finishOnTaskLaunch) {
2425 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2426 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2427 for (int srcPos = start; srcPos >= i; --srcPos) {
2428 final ActivityRecord p = activities.get(srcPos);
2429 if (p.finishing) {
2430 continue;
2431 }
Todd Kennedy539db512014-12-15 09:57:55 -08002432 finishActivityLocked(
2433 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002434 }
2435 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002436 if (taskInsertionPoint < 0) {
2437 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002438
Craig Mautner77878772013-03-04 19:46:24 -08002439 }
Craig Mautner77878772013-03-04 19:46:24 -08002440
2441 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2442 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2443 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2444 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002445 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002446 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002447 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002448
Craig Mautnere3a74d52013-02-22 14:14:58 -08002449 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2450 + " to stack at " + task,
2451 new RuntimeException("here").fillInStackTrace());
2452 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2453 + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002454 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002455 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002456 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002457 if (VALIDATE_TOKENS) {
2458 validateAppTokensLocked();
2459 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002460
2461 // Now we've moved it in to place... but what if this is
2462 // a singleTop activity and we have put it on top of another
2463 // instance of the same activity? Then we drop the instance
2464 // below so it remains singleTop.
2465 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2466 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002467 int targetNdx = taskActivities.indexOf(target);
2468 if (targetNdx > 0) {
2469 ActivityRecord p = taskActivities.get(targetNdx - 1);
2470 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002471 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2472 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002473 }
2474 }
2475 }
2476 }
2477
2478 replyChainEnd = -1;
2479 }
2480 }
Craig Mautner77878772013-03-04 19:46:24 -08002481 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002482 }
2483
Craig Mautner8849a5e2013-04-02 16:41:03 -07002484 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002485 ActivityRecord newActivity) {
2486 boolean forceReset =
2487 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2488 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2489 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2490 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2491 forceReset = true;
2492 }
2493 }
2494
2495 final TaskRecord task = taskTop.task;
2496
2497 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2498 * for remaining tasks. Used for later tasks to reparent to task. */
2499 boolean taskFound = false;
2500
2501 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2502 ActivityOptions topOptions = null;
2503
Craig Mautner77878772013-03-04 19:46:24 -08002504 // Preserve the location for reparenting in the new task.
2505 int reparentInsertionPoint = -1;
2506
Craig Mautnere3a74d52013-02-22 14:14:58 -08002507 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2508 final TaskRecord targetTask = mTaskHistory.get(i);
2509
2510 if (targetTask == task) {
2511 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2512 taskFound = true;
2513 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002514 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2515 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002516 }
2517 }
2518
Craig Mautner70a86932013-02-28 22:37:44 -08002519 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002520 if (taskNdx >= 0) {
2521 do {
2522 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2523 } while (taskTop == null && taskNdx >= 0);
2524 }
Craig Mautner70a86932013-02-28 22:37:44 -08002525
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 if (topOptions != null) {
2527 // If we got some ActivityOptions from an activity on top that
2528 // was removed from the task, propagate them to the new real top.
2529 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002530 taskTop.updateOptionsLocked(topOptions);
2531 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002532 topOptions.abort();
2533 }
2534 }
2535
2536 return taskTop;
2537 }
2538
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002539 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2540 String resultWho, int requestCode, int resultCode, Intent data) {
2541
2542 if (callingUid > 0) {
2543 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002544 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002545 }
2546
2547 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2548 + " : who=" + resultWho + " req=" + requestCode
2549 + " res=" + resultCode + " data=" + data);
2550 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2551 try {
2552 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2553 list.add(new ResultInfo(resultWho, requestCode,
2554 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002555 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002556 return;
2557 } catch (Exception e) {
2558 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2559 }
2560 }
2561
2562 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2563 }
2564
Craig Mautner299f9602015-01-26 09:47:33 -08002565 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002566 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2567 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002568 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002569 if (next != r) {
2570 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002571 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002572 // For non-fullscreen stack, we want to move the focus to the next visible
2573 // stack to prevent the home screen from moving to the top and obscuring
2574 // other visible stacks.
2575 if (!mFullscreen
2576 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2577 return;
2578 }
2579 // Move the home stack to the top if this stack is fullscreen or there is no
2580 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002581 if (mStackSupervisor.moveHomeStackTaskToTop(
2582 task.getTaskToReturnTo(), myReason)) {
2583 // Activity focus was already adjusted. Nothing else to do...
2584 return;
2585 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002586 }
2587 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002588
2589 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002590 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002591 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002592 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002593 }
2594 }
2595
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002596 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2597 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2598 final String myReason = reason + " adjustFocusToNextVisibleStack";
2599 if (stack == null) {
2600 return false;
2601 }
2602 final ActivityRecord top = stack.topRunningActivityLocked(null);
2603 if (top == null) {
2604 return false;
2605 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002606 mService.setFocusedActivityLocked(top, myReason);
2607 return true;
2608 }
2609
Craig Mautnerf3333272013-04-22 10:55:53 -07002610 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2612 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2613 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2614 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002615 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002616 if (DEBUG_STATES) {
2617 Slog.d(TAG, "no-history finish of " + r);
2618 }
2619 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002620 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002621 } else {
2622 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2623 + " on stop because we're just sleeping");
2624 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002626 }
2627
2628 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002629 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002630 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002631 try {
2632 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002633 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2634 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002635 r.state = ActivityState.STOPPING;
2636 if (DEBUG_VISBILITY) Slog.v(
2637 TAG, "Stopping visible=" + r.visible + " for " + r);
2638 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002639 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002641 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002642 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002643 r.setSleeping(true);
2644 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002645 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002646 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 } catch (Exception e) {
2648 // Maybe just ignore exceptions here... if the process
2649 // has crashed, our death notification will clean things
2650 // up.
2651 Slog.w(TAG, "Exception thrown during pause", e);
2652 // Just in case, assume it to be stopped.
2653 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002654 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 r.state = ActivityState.STOPPED;
2656 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002657 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 }
2659 }
2660 }
2661 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002662
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 /**
2664 * @return Returns true if the activity is being finished, false if for
2665 * some reason it is being left as-is.
2666 */
2667 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002668 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002669 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002670 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002671 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002672 + ", result=" + resultCode + ", data=" + resultData
2673 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002674 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675 return false;
2676 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677
Craig Mautnerd44711d2013-02-23 11:24:36 -08002678 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002679 return true;
2680 }
2681
Craig Mautnerd2328952013-03-05 12:46:26 -08002682 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002683 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2684 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2685 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2686 ActivityRecord r = activities.get(activityNdx);
2687 if (r.resultTo == self && r.requestCode == requestCode) {
2688 if ((r.resultWho == null && resultWho == null) ||
2689 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2690 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2691 false);
2692 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002693 }
2694 }
2695 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002696 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002697 }
2698
Todd Kennedy539db512014-12-15 09:57:55 -08002699 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002700 ActivityRecord r = topRunningActivityLocked(null);
2701 if (r != null && r.app == app) {
2702 // If the top running activity is from this crashing
2703 // process, then terminate it to avoid getting in a loop.
2704 Slog.w(TAG, " Force finishing activity "
2705 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002706 int taskNdx = mTaskHistory.indexOf(r.task);
2707 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002708 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002709 // Also terminate any activities below it that aren't yet
2710 // stopped, to avoid a situation where one will get
2711 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002712 --activityNdx;
2713 if (activityNdx < 0) {
2714 do {
2715 --taskNdx;
2716 if (taskNdx < 0) {
2717 break;
2718 }
2719 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2720 } while (activityNdx < 0);
2721 }
2722 if (activityNdx >= 0) {
2723 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002724 if (r.state == ActivityState.RESUMED
2725 || r.state == ActivityState.PAUSING
2726 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002727 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002728 Slog.w(TAG, " Force finishing activity "
2729 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002730 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002731 }
2732 }
2733 }
2734 }
2735 }
2736
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002737 final void finishVoiceTask(IVoiceInteractionSession session) {
2738 IBinder sessionBinder = session.asBinder();
2739 boolean didOne = false;
2740 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2741 TaskRecord tr = mTaskHistory.get(taskNdx);
2742 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2743 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2744 ActivityRecord r = tr.mActivities.get(activityNdx);
2745 if (!r.finishing) {
2746 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2747 false);
2748 didOne = true;
2749 }
2750 }
2751 }
2752 }
2753 if (didOne) {
2754 mService.updateOomAdjLocked();
2755 }
2756 }
2757
Craig Mautnerd2328952013-03-05 12:46:26 -08002758 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002759 ArrayList<ActivityRecord> activities = r.task.mActivities;
2760 for (int index = activities.indexOf(r); index >= 0; --index) {
2761 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002762 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002763 break;
2764 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002765 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002766 }
2767 return true;
2768 }
2769
Dianne Hackborn5c607432012-02-28 14:44:19 -08002770 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2771 // send the result
2772 ActivityRecord resultTo = r.resultTo;
2773 if (resultTo != null) {
2774 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2775 + " who=" + r.resultWho + " req=" + r.requestCode
2776 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002777 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002778 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002779 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002780 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002781 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002782 if (r.info.applicationInfo.uid > 0) {
2783 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2784 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002785 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002786 }
2787 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2788 resultData);
2789 r.resultTo = null;
2790 }
2791 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2792
2793 // Make sure this HistoryRecord is not holding on to other resources,
2794 // because clients have remote IPC references to this object so we
2795 // can't assume that will go away and want to avoid circular IPC refs.
2796 r.results = null;
2797 r.pendingResults = null;
2798 r.newIntents = null;
2799 r.icicle = null;
2800 }
2801
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002802 /**
2803 * @return Returns true if this activity has been removed from the history
2804 * list, or false if it is still in the list and will be removed later.
2805 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002806 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2807 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002808 if (r.finishing) {
2809 Slog.w(TAG, "Duplicate finish request for " + r);
2810 return false;
2811 }
2812
Wale Ogunwale7d701172015-03-11 15:36:30 -07002813 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002814 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002815 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002816 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002817 task.taskId, r.shortComponentName, reason);
2818 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002819 final int index = activities.indexOf(r);
2820 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002821 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002822 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002823 // If the caller asked that this activity (and all above it)
2824 // be cleared when the task is reset, don't lose that information,
2825 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002826 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002827 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828 }
2829 }
2830
2831 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002832
Craig Mautner299f9602015-01-26 09:47:33 -08002833 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834
Dianne Hackborn5c607432012-02-28 14:44:19 -08002835 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002836
Craig Mautnerde4ef022013-04-07 19:01:33 -07002837 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002838 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002839 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002840 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002841 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002842 ? AppTransition.TRANSIT_TASK_CLOSE
2843 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002844
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002846 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002847
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002848 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2850 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002851 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002852 }
2853
Craig Mautneraea74a52014-03-08 14:23:10 -08002854 if (endTask) {
2855 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2856 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857 } else if (r.state != ActivityState.PAUSING) {
2858 // If the activity is PAUSING, we will complete the finish once
2859 // it is done pausing; else we can just directly finish it here.
2860 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002861 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002862 } else {
2863 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2864 }
2865
2866 return false;
2867 }
2868
Craig Mautnerf3333272013-04-22 10:55:53 -07002869 static final int FINISH_IMMEDIATELY = 0;
2870 static final int FINISH_AFTER_PAUSE = 1;
2871 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872
Craig Mautnerf3333272013-04-22 10:55:53 -07002873 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002874 // First things first: if this activity is currently visible,
2875 // and the resumed activity is not yet visible, then hold off on
2876 // finishing until the resumed one becomes visible.
2877 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002878 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2879 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002880 if (mStackSupervisor.mStoppingActivities.size() > 3
2881 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002882 // If we already have a few activities waiting to stop,
2883 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002884 // them out. Or if r is the last of activity of the last task the stack
2885 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002886 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002887 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002888 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 }
2890 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002891 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2892 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002893 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002894 if (oomAdj) {
2895 mService.updateOomAdjLocked();
2896 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 return r;
2898 }
2899
2900 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002901 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002902 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002903 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 if (mResumedActivity == r) {
2905 mResumedActivity = null;
2906 }
2907 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002908 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002909 r.state = ActivityState.FINISHING;
2910
2911 if (mode == FINISH_IMMEDIATELY
2912 || prevState == ActivityState.STOPPED
2913 || prevState == ActivityState.INITIALIZING) {
2914 // If this activity is already stopped, we can just finish
2915 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002916 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002917 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002918 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002919 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002920 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002921 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002922 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2923 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002924 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002926
2927 // Need to go through the full pause cycle to get this
2928 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002929 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002930 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002931 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002932 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002933 return r;
2934 }
2935
Craig Mautneree36c772014-07-16 14:56:05 -07002936 void finishAllActivitiesLocked(boolean immediately) {
2937 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002938 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2939 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2940 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2941 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002942 noActivitiesInStack = false;
2943 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002944 continue;
2945 }
Craig Mautneree36c772014-07-16 14:56:05 -07002946 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002947 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2948 }
2949 }
Craig Mautneree36c772014-07-16 14:56:05 -07002950 if (noActivitiesInStack) {
2951 mActivityContainer.onTaskListEmptyLocked();
2952 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002953 }
2954
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002955 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2956 // Basic case: for simple app-centric recents, we need to recreate
2957 // the task if the affinity has changed.
2958 if (srec == null || srec.task.affinity == null ||
2959 !srec.task.affinity.equals(destAffinity)) {
2960 return true;
2961 }
2962 // Document-centric case: an app may be split in to multiple documents;
2963 // they need to re-create their task if this current activity is the root
2964 // of a document, unless simply finishing it will return them to the the
2965 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002966 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2967 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002968 // Okay, this activity is at the root of its task. What to do, what to do...
2969 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2970 // Finishing won't return to an application, so we need to recreate.
2971 return true;
2972 }
2973 // We now need to get the task below it to determine what to do.
2974 int taskIdx = mTaskHistory.indexOf(srec.task);
2975 if (taskIdx <= 0) {
2976 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2977 return false;
2978 }
2979 if (taskIdx == 0) {
2980 // At the bottom of the stack, nothing to go back to.
2981 return true;
2982 }
2983 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2984 if (!srec.task.affinity.equals(prevTask.affinity)) {
2985 // These are different apps, so need to recreate.
2986 return true;
2987 }
2988 }
2989 return false;
2990 }
2991
Wale Ogunwale7d701172015-03-11 15:36:30 -07002992 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002993 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002994 final TaskRecord task = srec.task;
2995 final ArrayList<ActivityRecord> activities = task.mActivities;
2996 final int start = activities.indexOf(srec);
2997 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002998 return false;
2999 }
3000 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003001 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003002 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003003 final ComponentName dest = destIntent.getComponent();
3004 if (start > 0 && dest != null) {
3005 for (int i = finishTo; i >= 0; i--) {
3006 ActivityRecord r = activities.get(i);
3007 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003008 r.info.name.equals(dest.getClassName())) {
3009 finishTo = i;
3010 parent = r;
3011 foundParentInTask = true;
3012 break;
3013 }
3014 }
3015 }
3016
3017 IActivityController controller = mService.mController;
3018 if (controller != null) {
3019 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3020 if (next != null) {
3021 // ask watcher if this is allowed
3022 boolean resumeOK = true;
3023 try {
3024 resumeOK = controller.activityResuming(next.packageName);
3025 } catch (RemoteException e) {
3026 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003027 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003028 }
3029
3030 if (!resumeOK) {
3031 return false;
3032 }
3033 }
3034 }
3035 final long origId = Binder.clearCallingIdentity();
3036 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003037 ActivityRecord r = activities.get(i);
3038 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003039 // Only return the supplied result for the first activity finished
3040 resultCode = Activity.RESULT_CANCELED;
3041 resultData = null;
3042 }
3043
3044 if (parent != null && foundParentInTask) {
3045 final int parentLaunchMode = parent.info.launchMode;
3046 final int destIntentFlags = destIntent.getFlags();
3047 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3048 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3049 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3050 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003051 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3052 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003053 } else {
3054 try {
3055 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3056 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003057 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003058 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003059 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003060 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003061 foundParentInTask = res == ActivityManager.START_SUCCESS;
3062 } catch (RemoteException e) {
3063 foundParentInTask = false;
3064 }
3065 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003066 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003067 }
3068 }
3069 Binder.restoreCallingIdentity(origId);
3070 return foundParentInTask;
3071 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003072 /**
3073 * Perform the common clean-up of an activity record. This is called both
3074 * as part of destroyActivityLocked() (when destroying the client-side
3075 * representation) and cleaning things up as a result of its hosting
3076 * processing going away, in which case there is no remaining client-side
3077 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003078 *
3079 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003081 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3082 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 if (mResumedActivity == r) {
3084 mResumedActivity = null;
3085 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003086 if (mPausingActivity == r) {
3087 mPausingActivity = null;
3088 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003089 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003090
3091 r.configDestroy = false;
3092 r.frozenBeforeDestroy = false;
3093
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003094 if (setState) {
3095 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3096 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003097 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003098 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003099 }
3100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003101 // Make sure this record is no longer in the pending finishes list.
3102 // This could happen, for example, if we are trimming activities
3103 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003104 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003105 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 // Remove any pending results.
3108 if (r.finishing && r.pendingResults != null) {
3109 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3110 PendingIntentRecord rec = apr.get();
3111 if (rec != null) {
3112 mService.cancelIntentSenderLocked(rec, false);
3113 }
3114 }
3115 r.pendingResults = null;
3116 }
3117
3118 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003119 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 }
3121
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003122 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003123 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003124 if (getVisibleBehindActivity() == r) {
3125 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003126 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003127 }
3128
3129 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003130 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003131 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003132 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003133 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003134 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 }
3136
Craig Mautner299f9602015-01-26 09:47:33 -08003137 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003138 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003139 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003140 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003141 if (DEBUG_ADD_REMOVE) {
3142 RuntimeException here = new RuntimeException("here");
3143 here.fillInStackTrace();
3144 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003145 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003146 r.takeFromHistory();
3147 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003148 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003149 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003150 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003151 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003152 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003153 if (VALIDATE_TOKENS) {
3154 validateAppTokensLocked();
3155 }
Craig Mautner312ba862014-02-10 17:55:01 -08003156 final TaskRecord task = r.task;
3157 if (task != null && task.removeActivity(r)) {
3158 if (DEBUG_STACK) Slog.i(TAG,
3159 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003160 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3161 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003162 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003163 }
Craig Mautner299f9602015-01-26 09:47:33 -08003164 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003165 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003166 cleanUpActivityServicesLocked(r);
3167 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003170 /**
3171 * Perform clean-up of service connections in an activity record.
3172 */
3173 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3174 // Throw away any services that have been bound by this activity.
3175 if (r.connections != null) {
3176 Iterator<ConnectionRecord> it = r.connections.iterator();
3177 while (it.hasNext()) {
3178 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003179 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 }
3181 r.connections = null;
3182 }
3183 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003184
Craig Mautneree2e45a2014-06-27 12:10:03 -07003185 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003186 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003187 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003188 mHandler.sendMessage(msg);
3189 }
3190
Craig Mautneree2e45a2014-06-27 12:10:03 -07003191 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003192 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003193 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003194 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3195 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3196 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3197 final ActivityRecord r = activities.get(activityNdx);
3198 if (r.finishing) {
3199 continue;
3200 }
3201 if (r.fullscreen) {
3202 lastIsOpaque = true;
3203 }
3204 if (owner != null && r.app != owner) {
3205 continue;
3206 }
3207 if (!lastIsOpaque) {
3208 continue;
3209 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003210 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003211 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3212 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003213 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003214 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003215 activityRemoved = true;
3216 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003217 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003218 }
3219 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003220 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003221 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003222 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003223 }
3224
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003225 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3226 if (r.isDestroyable()) {
3227 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3228 + " resumed=" + mResumedActivity
3229 + " pausing=" + mPausingActivity + " for reason " + reason);
3230 return destroyActivityLocked(r, true, reason);
3231 }
3232 return false;
3233 }
3234
3235 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3236 String reason) {
3237 // Iterate over tasks starting at the back (oldest) first.
3238 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3239 int maxTasks = tasks.size() / 4;
3240 if (maxTasks < 1) {
3241 maxTasks = 1;
3242 }
3243 int numReleased = 0;
3244 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3245 final TaskRecord task = mTaskHistory.get(taskNdx);
3246 if (!tasks.contains(task)) {
3247 continue;
3248 }
3249 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3250 int curNum = 0;
3251 final ArrayList<ActivityRecord> activities = task.mActivities;
3252 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3253 final ActivityRecord activity = activities.get(actNdx);
3254 if (activity.app == app && activity.isDestroyable()) {
3255 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3256 + " in state " + activity.state + " resumed=" + mResumedActivity
3257 + " pausing=" + mPausingActivity + " for reason " + reason);
3258 destroyActivityLocked(activity, true, reason);
3259 if (activities.get(actNdx) != activity) {
3260 // Was removed from list, back up so we don't miss the next one.
3261 actNdx--;
3262 }
3263 curNum++;
3264 }
3265 }
3266 if (curNum > 0) {
3267 numReleased += curNum;
3268 maxTasks--;
3269 if (mTaskHistory.get(taskNdx) != task) {
3270 // The entire task got removed, back up so we don't miss the next one.
3271 taskNdx--;
3272 }
3273 }
3274 }
3275 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3276 return numReleased;
3277 }
3278
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 /**
3280 * Destroy the current CLIENT SIDE instance of an activity. This may be
3281 * called both when actually finishing an activity, or when performing
3282 * a configuration switch where we destroy the current client-side object
3283 * but then create a new client-side object for this same HistoryRecord.
3284 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003285 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003286 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003287 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3289 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003290 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003291 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292
3293 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003294
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003295 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003296
3297 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003298
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 if (hadApp) {
3300 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003301 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003302 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3303 mService.mHeavyWeightProcess = null;
3304 mService.mHandler.sendEmptyMessage(
3305 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3306 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003307 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003308 // Update any services we are bound to that might care about whether
3309 // their client may have activities.
3310 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003311 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003312 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003313 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003314 }
3315 }
3316
3317 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003318
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003319 try {
3320 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003321 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 r.configChangeFlags);
3323 } catch (Exception e) {
3324 // We can just ignore exceptions here... if the process
3325 // has crashed, our death notification will clean things
3326 // up.
3327 //Slog.w(TAG, "Exception thrown during finish", e);
3328 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003329 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 removedFromHistory = true;
3331 skipDestroy = true;
3332 }
3333 }
3334
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003336
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003337 // If the activity is finishing, we need to wait on removing it
3338 // from the list to give it a chance to do its cleanup. During
3339 // that time it may make calls back with its token so we need to
3340 // be able to find it on the list and so we don't want to remove
3341 // it from the list yet. Otherwise, we can just immediately put
3342 // it in the destroyed state since we are not removing it from the
3343 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003345 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3346 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003348 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3350 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003351 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003353 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003354 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 }
3356 } else {
3357 // remove this record from the history.
3358 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003359 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 removedFromHistory = true;
3361 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003362 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003363 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003364 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003365 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003366 }
3367 }
3368
3369 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003371 if (!mLRUActivities.remove(r) && hadApp) {
3372 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3373 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 return removedFromHistory;
3376 }
3377
Craig Mautner299f9602015-01-26 09:47:33 -08003378 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003379 final long origId = Binder.clearCallingIdentity();
3380 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003381 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003382 if (r != null) {
3383 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003384 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003385 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003386
Wale Ogunwale60454db2015-01-23 16:05:07 -08003387 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003388 if (r.state == ActivityState.DESTROYING) {
3389 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003390 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003391 }
3392 }
Craig Mautner05d29032013-05-03 13:40:13 -07003393 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003394 } finally {
3395 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396 }
3397 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003398
Todd Kennedyaab56db2015-01-30 09:39:53 -08003399 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003400 if (hasVisibleBehindActivity() &&
3401 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003402 if (r == topRunningActivityLocked(null)) {
3403 // Don't release the top activity if it has requested to run behind the next
3404 // activity.
3405 return;
3406 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003407 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3408 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003409 " thread=" + r.app.thread);
3410 if (r != null && r.app != null && r.app.thread != null) {
3411 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003412 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003413 } catch (RemoteException e) {
3414 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003415 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003416 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003417 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003418 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003419 }
3420 }
3421 }
3422
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003423 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003424 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3425 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003426 if (r != null) {
3427 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003428 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003429 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003430 }
3431 mStackSupervisor.resumeTopActivitiesLocked();
3432 }
3433
Jose Lima4b6c6692014-08-12 17:41:12 -07003434 boolean hasVisibleBehindActivity() {
3435 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003436 }
3437
Jose Lima4b6c6692014-08-12 17:41:12 -07003438 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003439 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003440 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003441 }
3442 }
3443
Jose Lima4b6c6692014-08-12 17:41:12 -07003444 ActivityRecord getVisibleBehindActivity() {
3445 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003446 }
3447
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003448 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3449 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003450 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003451 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3452 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003453 while (i > 0) {
3454 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003455 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003456 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003458 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003460 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003461 }
3462 }
3463 }
3464
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003465 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3466 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003467 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3468 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003469 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3470 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003471 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003472 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003473 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3474 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003475
3476 boolean hasVisibleActivities = false;
3477
3478 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003479 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003480 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3481 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003482 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3483 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3484 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3485 final ActivityRecord r = activities.get(activityNdx);
3486 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003487 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3488 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003489 if (r.app == app) {
Craig Mautneracebdc82015-02-24 10:53:03 -08003490 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003491 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3492 // Don't currently have state for the activity, or
3493 // it is finishing -- always remove it.
3494 remove = true;
3495 } else if (r.launchCount > 2 &&
3496 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3497 // We have launched this activity too many times since it was
3498 // able to run, so give up and remove it.
3499 remove = true;
3500 } else {
3501 // The process may be gone, but the activity lives on!
3502 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003503 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003504 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003505 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003506 RuntimeException here = new RuntimeException("here");
3507 here.fillInStackTrace();
3508 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3509 + ": haveState=" + r.haveState
3510 + " stateNotNeeded=" + r.stateNotNeeded
3511 + " finishing=" + r.finishing
3512 + " state=" + r.state, here);
3513 }
3514 if (!r.finishing) {
3515 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3516 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3517 r.userId, System.identityHashCode(r),
3518 r.task.taskId, r.shortComponentName,
3519 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003520 if (r.state == ActivityState.RESUMED) {
3521 mService.updateUsageStats(r, false);
3522 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003523 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003524 } else {
3525 // We have the current state for this activity, so
3526 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003527 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003528 TAG, "Keeping entry, setting app to null");
3529 if (r.visible) {
3530 hasVisibleActivities = true;
3531 }
3532 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3533 + r);
3534 r.app = null;
3535 r.nowVisible = false;
3536 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003537 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003538 "App died, clearing saved state of " + r);
3539 r.icicle = null;
3540 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003541 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003542 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003543 if (remove) {
3544 removeActivityFromHistoryLocked(r, "appDied");
3545 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003546 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003547 }
3548 }
3549
3550 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003551 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003552
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003553 final void updateTransitLocked(int transit, Bundle options) {
3554 if (options != null) {
3555 ActivityRecord r = topRunningActivityLocked(null);
3556 if (r != null && r.state != ActivityState.RESUMED) {
3557 r.updateOptionsLocked(options);
3558 } else {
3559 ActivityOptions.abort(options);
3560 }
3561 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003562 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003563 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003564
Craig Mautner21d24a22014-04-23 11:45:37 -07003565 void updateTaskMovement(TaskRecord task, boolean toFront) {
3566 if (task.isPersistable) {
3567 task.mLastTimeMoved = System.currentTimeMillis();
3568 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3569 // recently will be most negative, tasks sent to the bottom before that will be less
3570 // negative. Similarly for recent tasks moved to the top which will be most positive.
3571 if (!toFront) {
3572 task.mLastTimeMoved *= -1;
3573 }
3574 }
3575 }
3576
Craig Mautner84984fa2014-06-19 11:19:20 -07003577 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003578 final int top = mTaskHistory.size() - 1;
3579 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3580 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003581 if (task.taskType == homeStackTaskType) {
3582 if (DEBUG_TASKS || DEBUG_STACK)
3583 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003584 mTaskHistory.remove(taskNdx);
3585 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003586 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003587 return;
3588 }
3589 }
3590 }
3591
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003592 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003593 String reason) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003594 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003595
Craig Mautner11bf9a52013-02-19 14:08:51 -08003596 final int numTasks = mTaskHistory.size();
3597 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003598 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003599 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003600 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003601 ActivityOptions.abort(options);
3602 } else {
3603 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3604 }
3605 return;
3606 }
3607
3608 // Shift all activities with this task up to the top
3609 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003610 insertTaskAtTop(tr);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003611
3612 // Set focus to the top running activity of this stack.
3613 ActivityRecord r = topRunningActivityLocked(null);
3614 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003615
3616 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003617 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003618 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 if (r != null) {
3620 mNoAnimActivities.add(r);
3621 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003622 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003624 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003625 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003626
Craig Mautner05d29032013-05-03 13:40:13 -07003627 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003628 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003629
3630 if (VALIDATE_TOKENS) {
3631 validateAppTokensLocked();
3632 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003633 }
3634
3635 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003636 * Worker method for rearranging history stack. Implements the function of moving all
3637 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003639 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3641 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003642 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003643 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003644 * @return Returns true if the move completed, false if not.
3645 */
Craig Mautner299f9602015-01-26 09:47:33 -08003646 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003647 final TaskRecord tr = taskForIdLocked(taskId);
3648 if (tr == null) {
3649 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3650 return false;
3651 }
3652
3653 Slog.i(TAG, "moveTaskToBack: " + tr);
3654
3655 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003656
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657 // If we have a watcher, preflight the move before committing to it. First check
3658 // for *other* available tasks, but if none are available, then try again allowing the
3659 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003660 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003661 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003662 if (next == null) {
3663 next = topRunningActivityLocked(null, 0);
3664 }
3665 if (next != null) {
3666 // ask watcher if this is allowed
3667 boolean moveOK = true;
3668 try {
3669 moveOK = mService.mController.activityResuming(next.packageName);
3670 } catch (RemoteException e) {
3671 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003672 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003673 }
3674 if (!moveOK) {
3675 return false;
3676 }
3677 }
3678 }
3679
Wale Ogunwalecb82f302015-02-25 07:53:40 -08003680 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003681
riddle_hsuc215a4f2014-12-27 12:10:45 +08003682 boolean prevIsHome = false;
3683 if (tr.isOverHomeStack()) {
3684 final TaskRecord nextTask = getNextTask(tr);
3685 if (nextTask != null) {
3686 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3687 } else {
3688 prevIsHome = true;
3689 }
3690 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003691 mTaskHistory.remove(tr);
3692 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003693 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003694
Craig Mautnerc8143c62013-09-03 12:15:57 -07003695 // There is an assumption that moving a task to the back moves it behind the home activity.
3696 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003697 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003698 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3699 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003700 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003701 break;
3702 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003703 if (taskNdx == 1) {
3704 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003705 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003706 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003707 }
3708
Craig Mautner299f9602015-01-26 09:47:33 -08003709 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003710 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003711
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003712 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003713 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003714 }
3715
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003716 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003717 if (prevIsHome || task == tr && tr.isOverHomeStack()
3718 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003719 if (!mService.mBooting && !mService.mBooted) {
3720 // Not ready yet!
3721 return false;
3722 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003723 final int taskToReturnTo = tr.getTaskToReturnTo();
3724 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003725 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003726 }
3727
Craig Mautner05d29032013-05-03 13:40:13 -07003728 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003729 return true;
3730 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003731
Craig Mautner8849a5e2013-04-02 16:41:03 -07003732 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003734 final Uri data = r.intent.getData();
3735 final String strData = data != null ? data.toSafeString() : null;
3736
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003737 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003738 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003740 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003741 }
3742
3743 /**
3744 * Make sure the given activity matches the current configuration. Returns
3745 * false if the activity had to be destroyed. Returns true if the
3746 * configuration is the same, or the activity will remain running as-is
3747 * for whatever reason. Ensures the HistoryRecord is updated with the
3748 * correct configuration and all other bookkeeping is handled.
3749 */
3750 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3751 int globalChanges) {
3752 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003753 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003754 "Skipping config check (will change): " + r);
3755 return true;
3756 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003757
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003758 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003759 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003760
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003761 // Make sure the current stack override configuration is supported by the top task
3762 // before continuing.
3763 final TaskRecord topTask = topTask();
3764 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3765 || (!topTask.mResizeable && !mFullscreen))) {
3766 final boolean prevFullscreen = mFullscreen;
3767 final Configuration newOverrideConfig =
3768 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3769 updateOverrideConfiguration(newOverrideConfig);
3770 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003771 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003772 "Updated stack config to support task=" + topTask
3773 + " resizeable=" + topTask.mResizeable
3774 + " mForcedFullscreen=" + mForcedFullscreen
3775 + " prevFullscreen=" + prevFullscreen
3776 + " mFullscreen=" + mFullscreen);
3777 }
3778
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003779 // Short circuit: if the two configurations are the exact same
3780 // object (the common case), then there is nothing to do.
3781 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003782 if (r.configuration == newConfig
3783 && r.stackConfigOverride == mOverrideConfig
3784 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003785 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003786 "Configuration unchanged in " + r);
3787 return true;
3788 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003789
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003790 // We don't worry about activities that are finishing.
3791 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003792 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003793 "Configuration doesn't matter in finishing " + r);
3794 r.stopFreezingScreenLocked(false);
3795 return true;
3796 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003797
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798 // Okay we now are going to make this activity have the new config.
3799 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003800 final Configuration oldConfig = r.configuration;
3801 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003802 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003803 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003804
3805 // Determine what has changed. May be nothing, if this is a config
3806 // that has come back from the app after going idle. In that case
3807 // we just want to leave the official config object now in the
3808 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003809 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003810 if (stackChanges == 0) {
3811 // {@link Configuration#diff} doesn't catch changes from unset values.
3812 // Check for changes we care about.
3813 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3814 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3815 }
3816 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3817 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3818 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3819 }
3820 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3821 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3822 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003823 }
3824 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003825 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003826 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003827 "Configuration no differences in " + r);
3828 return true;
3829 }
3830
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003831 // If the activity isn't currently running, just leave the new
3832 // configuration and it will pick that up next time it starts.
3833 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003834 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003835 "Configuration doesn't matter not running " + r);
3836 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003837 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003838 return true;
3839 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003840
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003841 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003842 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3843 "Checking to restart " + r.info.name + ": changed=0x"
3844 + Integer.toHexString(changes) + ", handles=0x"
3845 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3846
Dianne Hackborne6676352011-06-01 16:51:20 -07003847 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003848 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3849 r.configChangeFlags |= changes;
3850 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003851 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003852 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003853 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003854 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003855 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003856 } else if (r.state == ActivityState.PAUSING) {
3857 // A little annoying: we are waiting for this activity to
3858 // finish pausing. Let's not do anything now, but just
3859 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003860 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003861 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003862 r.configDestroy = true;
3863 return true;
3864 } else if (r.state == ActivityState.RESUMED) {
3865 // Try to optimize this case: the configuration is changing
3866 // and we need to restart the top, resumed activity.
3867 // Instead of doing the normal handshaking, just say
3868 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003869 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003870 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003871 relaunchActivityLocked(r, r.configChangeFlags, true);
3872 r.configChangeFlags = 0;
3873 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003874 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003875 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003876 relaunchActivityLocked(r, r.configChangeFlags, false);
3877 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003878 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003879
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003880 // All done... tell the caller we weren't able to keep this
3881 // activity around.
3882 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003883 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003884
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003885 // Default case: the activity can handle this new configuration, so hand it over.
3886 // NOTE: We only forward the stack override configuration as the system level configuration
3887 // changes is always sent to all processes when they happen so it can just use whatever
3888 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003889 if (r.app != null && r.app.thread != null) {
3890 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003891 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003892 r.app.thread.scheduleActivityConfigurationChanged(
3893 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003894 } catch (RemoteException e) {
3895 // If process died, whatever.
3896 }
3897 }
3898 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003899
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003900 return true;
3901 }
3902
Craig Mautner2568c3a2015-03-26 14:22:34 -07003903 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003904 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003905 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003906 if (andResume) {
3907 results = r.results;
3908 newIntents = r.newIntents;
3909 }
3910 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3911 + " with results=" + results + " newIntents=" + newIntents
3912 + " andResume=" + andResume);
3913 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003914 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003915 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003917 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003918
Craig Mautner34b73df2014-01-12 21:11:08 -08003919 mStackSupervisor.removeChildActivityContainers(r);
3920
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003921 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003922 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3923 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3924 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003925 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003926 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3927 !andResume, new Configuration(mService.mConfiguration),
3928 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003929 // Note: don't need to call pauseIfSleepingLocked() here, because
3930 // the caller will only pass in 'andResume' if this activity is
3931 // currently resumed, which implies we aren't sleeping.
3932 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003933 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003934 }
3935
3936 if (andResume) {
3937 r.results = null;
3938 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003939 r.state = ActivityState.RESUMED;
3940 } else {
3941 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3942 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003943 }
3944
3945 return true;
3946 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003947
3948 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003949 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3950 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3951 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3952 final ActivityRecord r = activities.get(activityNdx);
3953 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003954 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003955 }
3956 if (r.fullscreen && !r.finishing) {
3957 return false;
3958 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003959 }
3960 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003961 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003962 if (r == null) {
3963 return false;
3964 }
3965 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3966 + " would have returned true for r=" + r);
3967 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003968 }
3969
3970 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003971 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3972 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3973 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3974 final ActivityRecord r = activities.get(activityNdx);
3975 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003976 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003977 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003978 }
3979 }
3980 }
3981
3982 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3983 boolean didSomething = false;
3984 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003985 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003986 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3987 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3988 int numActivities = activities.size();
3989 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3990 ActivityRecord r = activities.get(activityNdx);
3991 final boolean samePackage = r.packageName.equals(name)
3992 || (name == null && r.userId == userId);
3993 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3994 && (samePackage || r.task == lastTask)
3995 && (r.app == null || evenPersistent || !r.app.persistent)) {
3996 if (!doit) {
3997 if (r.finishing) {
3998 // If this activity is just finishing, then it is not
3999 // interesting as far as something to stop.
4000 continue;
4001 }
4002 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004003 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004004 if (r.isHomeActivity()) {
4005 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4006 Slog.i(TAG, "Skip force-stop again " + r);
4007 continue;
4008 } else {
4009 homeActivity = r.realActivity;
4010 }
4011 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004012 didSomething = true;
4013 Slog.i(TAG, " Force finishing activity " + r);
4014 if (samePackage) {
4015 if (r.app != null) {
4016 r.app.removed = true;
4017 }
4018 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004019 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004020 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004021 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4022 true)) {
4023 // r has been deleted from mActivities, accommodate.
4024 --numActivities;
4025 --activityNdx;
4026 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004027 }
4028 }
4029 }
4030 return didSomething;
4031 }
4032
Dianne Hackborn09233282014-04-30 11:33:59 -07004033 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004034 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004035 final TaskRecord task = mTaskHistory.get(taskNdx);
4036 ActivityRecord r = null;
4037 ActivityRecord top = null;
4038 int numActivities = 0;
4039 int numRunning = 0;
4040 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07004041 if (activities.isEmpty()) {
4042 continue;
4043 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004044 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004045 continue;
4046 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004047 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4048 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004049
Craig Mautneraab647e2013-02-28 16:31:36 -08004050 // Initialize state for next task if needed.
4051 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4052 top = r;
4053 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004054 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004055
4056 // Add 'r' into the current task.
4057 numActivities++;
4058 if (r.app != null && r.app.thread != null) {
4059 numRunning++;
4060 }
4061
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004062 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004063 TAG, r.intent.getComponent().flattenToShortString()
4064 + ": task=" + r.task);
4065 }
4066
4067 RunningTaskInfo ci = new RunningTaskInfo();
4068 ci.id = task.taskId;
4069 ci.baseActivity = r.intent.getComponent();
4070 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004071 ci.lastActiveTime = task.lastActiveTime;
4072
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004073 if (top.task != null) {
4074 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004075 }
4076 ci.numActivities = numActivities;
4077 ci.numRunning = numRunning;
4078 //System.out.println(
4079 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08004080 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004081 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004082 }
4083
4084 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004085 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004086 if (DEBUG_SWITCH) Slog.d(
4087 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004088 if (top >= 0) {
4089 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4090 int activityTop = activities.size() - 1;
4091 if (activityTop > 0) {
4092 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4093 "unhandled-back", true);
4094 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004095 }
4096 }
4097
Craig Mautner6b74cb52013-09-27 17:02:21 -07004098 /**
4099 * Reset local parameters because an app's activity died.
4100 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004101 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004102 */
4103 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004104 if (mPausingActivity != null && mPausingActivity.app == app) {
4105 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
4106 "App died while pausing: " + mPausingActivity);
4107 mPausingActivity = null;
4108 }
4109 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4110 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004111 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004112 }
4113
Craig Mautner19091252013-10-05 00:03:53 -07004114 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004115 }
4116
Craig Mautnercae015f2013-02-08 14:31:27 -08004117 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004118 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4119 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4120 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4121 final ActivityRecord r = activities.get(activityNdx);
4122 if (r.app == app) {
4123 Slog.w(TAG, " Force finishing activity "
4124 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004125 // Force the destroy to skip right to removal.
4126 r.app = null;
4127 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004128 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004129 }
4130 }
4131 }
4132
Dianne Hackborn390517b2013-05-30 15:03:32 -07004133 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004134 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004135 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004136 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4137 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004138 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4139 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004140 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004141 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004142 if (printed) {
4143 header = null;
4144 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004145 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004146 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004147 }
4148
4149 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4150 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4151
4152 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004153 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4154 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004155 }
4156 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004157 final int top = mTaskHistory.size() - 1;
4158 if (top >= 0) {
4159 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4160 int listTop = list.size() - 1;
4161 if (listTop >= 0) {
4162 activities.add(list.get(listTop));
4163 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004164 }
4165 } else {
4166 ItemMatcher matcher = new ItemMatcher();
4167 matcher.build(name);
4168
Craig Mautneraab647e2013-02-28 16:31:36 -08004169 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4170 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4171 if (matcher.match(r1, r1.intent.getComponent())) {
4172 activities.add(r1);
4173 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004174 }
4175 }
4176 }
4177
4178 return activities;
4179 }
4180
4181 ActivityRecord restartPackage(String packageName) {
4182 ActivityRecord starting = topRunningActivityLocked(null);
4183
4184 // All activities that came from the package must be
4185 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004186 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4187 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4188 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4189 final ActivityRecord a = activities.get(activityNdx);
4190 if (a.info.packageName.equals(packageName)) {
4191 a.forceNewConfig = true;
4192 if (starting != null && a == starting && a.visible) {
4193 a.startFreezingScreenLocked(starting.app,
4194 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4195 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004196 }
4197 }
4198 }
4199
4200 return starting;
4201 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004202
Craig Mautner299f9602015-01-26 09:47:33 -08004203 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004204 removeTask(task, reason, true);
4205 }
4206
4207 void removeTask(TaskRecord task, String reason, boolean removeFromWindowManager) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004208 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004209 if (removeFromWindowManager) {
4210 mWindowManager.removeTask(task.taskId);
4211 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004212 final ActivityRecord r = mResumedActivity;
4213 if (r != null && r.task == task) {
4214 mResumedActivity = null;
4215 }
4216
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004217 final int taskNdx = mTaskHistory.indexOf(task);
4218 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004219 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4220 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4221 if (!nextTask.isOverHomeStack()) {
4222 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4223 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004224 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004225 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004226 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004227
4228 if (task.mActivities.isEmpty()) {
4229 final boolean isVoiceSession = task.voiceSession != null;
4230 if (isVoiceSession) {
4231 try {
4232 task.voiceSession.taskFinished(task.intent, task.taskId);
4233 } catch (RemoteException e) {
4234 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004235 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004236 if (task.autoRemoveFromRecents() || isVoiceSession) {
4237 // Task creator asked to remove this when done, or this task was a voice
4238 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004239 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004240 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004241 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004242 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004243
4244 if (mTaskHistory.isEmpty()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004245 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004246 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004247 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004248 String myReason = reason + " leftTaskHistoryEmpty";
4249 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4250 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4251 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004252 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004253 if (mStacks != null) {
4254 mStacks.remove(this);
4255 mStacks.add(0, this);
4256 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004257 if (notHomeStack) {
4258 mActivityContainer.onTaskListEmptyLocked();
4259 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004260 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004261
4262 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004263 }
4264
Dianne Hackborn91097de2014-04-04 18:02:06 -07004265 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4266 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4267 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004268 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4269 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004270 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004271 return task;
4272 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004273
4274 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004275 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004276 }
4277
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004278 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004279 task.stack = this;
4280 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004281 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004282 } else {
4283 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004284 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004285 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004286 if (!moving && task.voiceSession != null) {
4287 try {
4288 task.voiceSession.taskStarted(task.intent, task.taskId);
4289 } catch (RemoteException e) {
4290 }
4291 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004292 }
4293
4294 public int getStackId() {
4295 return mStackId;
4296 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004297
4298 @Override
4299 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004300 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4301 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004302 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004303
4304 boolean updateOverrideConfiguration(Configuration newConfig) {
4305 Configuration oldConfig = mOverrideConfig;
4306 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004307 // We override the configuration only when the stack's dimensions are different from
4308 // the display. In this manner, we know that if the override configuration is empty,
4309 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004310 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004311 return !mOverrideConfig.equals(oldConfig);
4312 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004313}