blob: 066ff372522a079e0b7769d52a0f2808f9cf8c1d [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 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001021 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001022 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001023
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001024 if (resumeNext) {
1025 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1026 if (!mService.isSleepingOrShuttingDown()) {
1027 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1028 } else {
1029 mStackSupervisor.checkReadyForSleepLocked();
1030 ActivityRecord top = topStack.topRunningActivityLocked(null);
1031 if (top == null || (prev != null && top != prev)) {
1032 // If there are no more activities available to run,
1033 // do resume anyway to start something. Also if the top
1034 // activity on the stack is not the just paused activity,
1035 // we need to go ahead and resume it to ensure we complete
1036 // an in-flight app switch.
1037 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1038 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001039 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001041
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001042 if (prev != null) {
1043 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001044
Craig Mautner525f3d92013-05-07 14:01:50 -07001045 if (prev.app != null && prev.cpuTimeAtResume > 0
1046 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001047 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1048 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001049 if (diff > 0) {
1050 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1051 synchronized (bsi) {
1052 BatteryStatsImpl.Uid.Proc ps =
1053 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001054 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001055 if (ps != null) {
1056 ps.addForegroundTimeLocked(diff);
1057 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001058 }
1059 }
1060 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001061 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001063
1064 // Notfiy when the task stack has changed
1065 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001066 }
1067
1068 /**
1069 * Once we know that we have asked an application to put an activity in
1070 * the resumed state (either by launching it or explicitly telling it),
1071 * this function updates the rest of our state to match that fact.
1072 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001073 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001074 next.idle = false;
1075 next.results = null;
1076 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001077
1078 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1079 ProcessRecord app = next.task.mActivities.get(0).app;
1080 if (app != null && app != mService.mHomeProcess) {
1081 mService.mHomeProcess = app;
1082 }
1083 }
1084
Craig Mautner07566322013-09-26 16:42:55 -07001085 if (next.nowVisible) {
1086 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001087 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001088 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089
1090 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001091 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 mStackSupervisor.reportResumedActivityLocked(next);
1094
1095 next.resumeKeyDispatchingLocked();
1096 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001097
1098 // Mark the point when the activity is resuming
1099 // TODO: To be more accurate, the mark should be before the onCreate,
1100 // not after the onResume. But for subsequent starts, onResume is fine.
1101 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001102 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001103 } else {
1104 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1105 }
Winson Chung376543b2014-05-21 17:43:28 -07001106
George Mount6ba042b2014-07-28 11:12:28 -07001107 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001108
1109 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1110 // When resuming an activity, require it to call requestVisibleBehind() again.
1111 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1112 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001113 }
1114
Craig Mautner2568c3a2015-03-26 14:22:34 -07001115 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001116 r.visible = visible;
1117 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001118 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001119 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001120 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001121 container.setVisible(visible);
1122 }
1123 }
1124
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001125 // Find the first visible activity above the passed activity and if it is translucent return it
1126 // otherwise return null;
1127 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1128 TaskRecord task = r.task;
1129 if (task == null) {
1130 return null;
1131 }
1132
1133 ActivityStack stack = task.stack;
1134 if (stack == null) {
1135 return null;
1136 }
1137
1138 int stackNdx = mStacks.indexOf(stack);
1139
1140 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1141 int taskNdx = tasks.indexOf(task);
1142
1143 ArrayList<ActivityRecord> activities = task.mActivities;
1144 int activityNdx = activities.indexOf(r) + 1;
1145
1146 final int numStacks = mStacks.size();
1147 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001148 ActivityStack historyStack = mStacks.get(stackNdx);
1149 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001150 final int numTasks = tasks.size();
1151 while (taskNdx < numTasks) {
1152 activities = tasks.get(taskNdx).mActivities;
1153 final int numActivities = activities.size();
1154 while (activityNdx < numActivities) {
1155 final ActivityRecord activity = activities.get(activityNdx);
1156 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001157 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001158 }
1159 ++activityNdx;
1160 }
1161 activityNdx = 0;
1162 ++taskNdx;
1163 }
1164 taskNdx = 0;
1165 ++stackNdx;
1166 }
1167
1168 return null;
1169 }
1170
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001171 private ActivityStack getNextVisibleStackLocked() {
1172 ArrayList<ActivityStack> stacks = mStacks;
1173 final ActivityRecord parent = mActivityContainer.mParentActivity;
1174 if (parent != null) {
1175 stacks = parent.task.stack.mStacks;
1176 }
1177 if (stacks != null) {
1178 for (int i = stacks.size() - 1; i >= 0; --i) {
1179 ActivityStack stack = stacks.get(i);
1180 if (stack != this && stack.isStackVisibleLocked()) {
1181 return stack;
1182 }
1183 }
1184 }
1185 return null;
1186 }
1187
Jose Lima7ba71252014-04-30 12:59:27 -07001188 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1189 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001190 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001191 if (!isAttached()) {
1192 return false;
1193 }
1194
1195 if (mStackSupervisor.isFrontStack(this)) {
1196 return true;
1197 }
1198
Jose Lima729cb232014-05-05 15:59:40 -07001199 /**
1200 * Start at the task above this one and go up, looking for a visible
1201 * fullscreen activity, or a translucent activity that requested the
1202 * wallpaper to be shown behind it.
1203 */
Jose Lima7ba71252014-04-30 12:59:27 -07001204 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001205 ActivityStack stack = mStacks.get(i);
1206 // stack above isn't full screen, so, we assume we're still visible. at some point
1207 // we should look at the stack bounds to see if we're occluded even if the stack
1208 // isn't fullscreen
1209 if (!stack.mFullscreen) {
1210 continue;
1211 }
1212 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1213 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001214 final TaskRecord task = tasks.get(taskNdx);
1215 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001216 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001217 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001218
1219 // Conditions for an activity to obscure the stack we're
1220 // examining:
1221 // 1. Not Finishing AND Visible AND:
1222 // 2. Either:
1223 // - Full Screen Activity OR
1224 // - On top of Home and our stack is NOT home
1225 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001226 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001227 return false;
1228 }
1229 }
1230 }
1231 }
1232
1233 return true;
1234 }
1235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001236 /**
1237 * Make sure that all activities that need to be visible (that is, they
1238 * currently can be seen by the user) actually are.
1239 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001240 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1241 ActivityRecord top = topRunningActivityLocked(null);
1242 if (top == null) {
1243 return;
1244 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001245 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001246 TAG, "ensureActivitiesVisible behind " + top
1247 + " configChanges=0x" + Integer.toHexString(configChanges));
1248
Craig Mautner5eda9b32013-07-02 11:58:16 -07001249 if (mTranslucentActivityWaiting != top) {
1250 mUndrawnActivitiesBelowTopTranslucent.clear();
1251 if (mTranslucentActivityWaiting != null) {
1252 // Call the callback with a timeout indication.
1253 notifyActivityDrawnLocked(null);
1254 mTranslucentActivityWaiting = null;
1255 }
1256 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1257 }
1258
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 // If the top activity is not fullscreen, then we need to
1260 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001261 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001262 boolean behindFullscreen = !isStackVisibleLocked();
Jose Lima7ba71252014-04-30 12:59:27 -07001263
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001264 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001265 final TaskRecord task = mTaskHistory.get(taskNdx);
1266 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001267 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1268 final ActivityRecord r = activities.get(activityNdx);
1269 if (r.finishing) {
1270 continue;
1271 }
1272 if (aboveTop && r != top) {
1273 continue;
1274 }
1275 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001276 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1277 // but must be drawn initially for the animation as though they were visible.
1278 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001279 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001280 TAG, "Make visible? " + r + " finishing=" + r.finishing
1281 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001282
Craig Mautnerd44711d2013-02-23 11:24:36 -08001283 // First: if this is not the current activity being started, make
1284 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001285 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001286 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001287 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001288
1289 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001290 // This activity needs to be visible, but isn't even
1291 // running... get it started, but don't resume it
1292 // at this point.
1293 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1294 if (r != starting) {
1295 r.startFreezingScreenLocked(r.app, configChanges);
1296 }
1297 if (!r.visible || r.mLaunchTaskBehind) {
1298 if (DEBUG_VISBILITY) Slog.v(
1299 TAG, "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001300 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001301 }
1302 if (r != starting) {
1303 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001304 }
1305
1306 } else if (r.visible) {
1307 // If this activity is already visible, then there is nothing
1308 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001309 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001310 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001311 try {
George Mount6ba042b2014-07-28 11:12:28 -07001312 if (r.returningOptions != null) {
1313 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1314 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001315 }
1316 } catch(RemoteException e) {
1317 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001318 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001319 // This activity is not currently visible, but is running.
1320 // Tell it to become visible.
1321 r.visible = true;
1322 if (r.state != ActivityState.RESUMED && r != starting) {
1323 // If this activity is paused, tell it
1324 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001325 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001326 TAG, "Making visible and scheduling visibility: " + r);
1327 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001328 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001329 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001330 mUndrawnActivitiesBelowTopTranslucent.add(r);
1331 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001332 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001333 r.sleeping = false;
1334 r.app.pendingUiClean = true;
1335 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1336 r.stopFreezingScreenLocked(false);
1337 } catch (Exception e) {
1338 // Just skip on any failure; we'll make it
1339 // visible when it next restarts.
1340 Slog.w(TAG, "Exception thrown making visibile: "
1341 + r.intent.getComponent(), e);
1342 }
1343 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345
Craig Mautnerd44711d2013-02-23 11:24:36 -08001346 // Aggregate current change flags.
1347 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001348
Craig Mautnerd44711d2013-02-23 11:24:36 -08001349 if (r.fullscreen) {
1350 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001351 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1352 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001353 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001354 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001355 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001356 }
1357 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001358 if (DEBUG_VISBILITY) Slog.v(
1359 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1360 + " state=" + r.state
1361 + " behindFullscreen=" + behindFullscreen);
1362 // Now for any activities that aren't visible to the user, make
1363 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001365 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001367 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001368 switch (r.state) {
1369 case STOPPING:
1370 case STOPPED:
1371 if (r.app != null && r.app.thread != null) {
1372 if (DEBUG_VISBILITY) Slog.v(
1373 TAG, "Scheduling invisibility: " + r);
1374 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1375 }
1376 break;
1377
1378 case INITIALIZING:
1379 case RESUMED:
1380 case PAUSING:
1381 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001382 // This case created for transitioning activities from
1383 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001384 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001385 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001386 } else {
1387 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1388 mStackSupervisor.mStoppingActivities.add(r);
1389 }
1390 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001391 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001392 break;
1393
1394 default:
1395 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001396 }
1397 } catch (Exception e) {
1398 // Just skip on any failure; we'll make it
1399 // visible when it next restarts.
1400 Slog.w(TAG, "Exception thrown making hidden: "
1401 + r.intent.getComponent(), e);
1402 }
1403 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001404 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001405 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001406 }
1407 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001409
1410 if (mTranslucentActivityWaiting != null &&
1411 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1412 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1413 notifyActivityDrawnLocked(null);
1414 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001415 }
Craig Mautner58547802013-03-05 08:23:53 -08001416
Todd Kennedyaab56db2015-01-30 09:39:53 -08001417 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001418 mTranslucentActivityWaiting = r;
1419 mUndrawnActivitiesBelowTopTranslucent.clear();
1420 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1421 }
1422
1423 /**
1424 * Called as activities below the top translucent activity are redrawn. When the last one is
1425 * redrawn notify the top activity by calling
1426 * {@link Activity#onTranslucentConversionComplete}.
1427 *
1428 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1429 * occurred and the activity will be notified immediately.
1430 */
1431 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001432 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001433 if ((r == null)
1434 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1435 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1436 // The last undrawn activity below the top has just been drawn. If there is an
1437 // opaque activity at the top, notify it that it can become translucent safely now.
1438 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1439 mTranslucentActivityWaiting = null;
1440 mUndrawnActivitiesBelowTopTranslucent.clear();
1441 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1442
Craig Mautner71dd1b62014-02-18 15:48:52 -08001443 if (waitingActivity != null) {
1444 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1445 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1446 try {
1447 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1448 waitingActivity.appToken, r != null);
1449 } catch (RemoteException e) {
1450 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001451 }
1452 }
1453 }
1454 }
1455
Craig Mautnera61bc652013-10-28 15:43:18 -07001456 /** If any activities below the top running one are in the INITIALIZING state and they have a
1457 * starting window displayed then remove that starting window. It is possible that the activity
1458 * in this state will never resumed in which case that starting window will be orphaned. */
1459 void cancelInitializingActivities() {
1460 final ActivityRecord topActivity = topRunningActivityLocked(null);
1461 boolean aboveTop = true;
1462 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1463 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1464 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1465 final ActivityRecord r = activities.get(activityNdx);
1466 if (aboveTop) {
1467 if (r == topActivity) {
1468 aboveTop = false;
1469 }
1470 continue;
1471 }
1472
1473 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1474 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1475 r.mStartingWindowShown = false;
1476 mWindowManager.removeAppStartingWindow(r.appToken);
1477 }
1478 }
1479 }
1480 }
1481
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 /**
1483 * Ensure that the top activity in the stack is resumed.
1484 *
1485 * @param prev The previously resumed activity, for when in the process
1486 * of pausing; can be null to call from elsewhere.
1487 *
1488 * @return Returns true if something is being resumed, or false if
1489 * nothing happened.
1490 */
1491 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001492 return resumeTopActivityLocked(prev, null);
1493 }
1494
1495 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001496 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001497 // Don't even start recursing.
1498 return false;
1499 }
1500
1501 boolean result = false;
1502 try {
1503 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001504 mStackSupervisor.inResumeTopActivity = true;
1505 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1506 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001507 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001508 }
Craig Mautner544efa72014-09-04 16:41:20 -07001509 result = resumeTopActivityInnerLocked(prev, options);
1510 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001511 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001512 }
1513 return result;
1514 }
1515
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001516 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001517 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1518
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001519 if (!mService.mBooting && !mService.mBooted) {
1520 // Not ready yet!
1521 return false;
1522 }
1523
Craig Mautnerdf88d732014-01-27 09:21:32 -08001524 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001525 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001526 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001527 // Do not resume this stack if its parent is not resumed.
1528 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1529 return false;
1530 }
1531
Craig Mautnera61bc652013-10-28 15:43:18 -07001532 cancelInitializingActivities();
1533
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001535 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536
1537 // Remember how we'll process this pause/resume situation, and ensure
1538 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001539 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1540 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541
Craig Mautner84984fa2014-06-19 11:19:20 -07001542 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001544 // There are no more activities!
1545 final String reason = "noMoreActivities";
1546 if (!mFullscreen) {
1547 // Try to move focus to the next visible stack with a running activity if this
1548 // stack is not covering the entire screen.
1549 final ActivityStack stack = getNextVisibleStackLocked();
1550 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1551 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1552 }
1553 }
1554 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001555 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001556 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001557 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001558 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001559 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1560 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1561 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001562 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 }
1564
1565 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001568 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1569 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001570 // Make sure we have executed any pending transitions, since there
1571 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001572 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001574 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001575 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001576 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001577 return false;
1578 }
1579
Craig Mautner525f3d92013-05-07 14:01:50 -07001580 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001581 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001582 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001583 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001584 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001585 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001586 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001587 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001588 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001589 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001590 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001591 } else if (!isOnHomeDisplay()) {
1592 return false;
1593 } else if (!isHomeStack()){
1594 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001595 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001596 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1597 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1598 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001599 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001600 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001601 }
1602
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 // If we are sleeping, and there is no resumed activity, and the top
1604 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001605 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001606 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001607 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 // Make sure we have executed any pending transitions, since there
1609 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001610 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001612 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001613 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001614 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 return false;
1616 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001617
1618 // Make sure that the user who owns this activity is started. If not,
1619 // we will just leave it as is because someone should be bringing
1620 // another user's activities to the top of the stack.
1621 if (mService.mStartedUsers.get(next.userId) == null) {
1622 Slog.w(TAG, "Skipping resume of top activity " + next
1623 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001624 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001625 return false;
1626 }
1627
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001628 // The activity may be waiting for stop, but that is no longer
1629 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001630 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001631 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001632 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001633 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634
1635 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1636
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001637 // If we are currently pausing an activity, then don't do anything
1638 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001639 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001640 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1641 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001642 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001643 return false;
1644 }
1645
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001646 // Okay we are now going to start a switch, to 'next'. We may first
1647 // have to pause the current activity, but this is an important point
1648 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001649 // XXX "App Redirected" dialog is getting too many false positives
1650 // at this point, so turn off for now.
1651 if (false) {
1652 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1653 long now = SystemClock.uptimeMillis();
1654 final boolean inTime = mLastStartedActivity.startTime != 0
1655 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1656 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1657 final int nextUid = next.info.applicationInfo.uid;
1658 if (inTime && lastUid != nextUid
1659 && lastUid != next.launchedFromUid
1660 && mService.checkPermission(
1661 android.Manifest.permission.STOP_APP_SWITCHES,
1662 -1, next.launchedFromUid)
1663 != PackageManager.PERMISSION_GRANTED) {
1664 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1665 } else {
1666 next.startTime = now;
1667 mLastStartedActivity = next;
1668 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001669 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001670 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001671 mLastStartedActivity = next;
1672 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001673 }
Craig Mautner58547802013-03-05 08:23:53 -08001674
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001675 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1676
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 // We need to start pausing the current activity so the top one
1678 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001679 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1680 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001681 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001682 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001683 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001684 }
1685 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001686 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1687 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001688 // At this point we want to put the upcoming activity's process
1689 // at the top of the LRU list, since we know we will be needing it
1690 // very soon and it would be a waste to let it get killed if it
1691 // happens to be sitting towards the end.
1692 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001693 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001694 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001695 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 return true;
1697 }
1698
Christopher Tated3f175c2012-06-14 14:16:54 -07001699 // If the most recent activity was noHistory but was only stopped rather
1700 // than stopped+finished because the device went to sleep, we need to make
1701 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001702 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001703 !mLastNoHistoryActivity.finishing) {
1704 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1705 " on new resume");
1706 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001707 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001708 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001709 }
1710
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001712 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1713 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001714 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 if (DEBUG_SWITCH) Slog.v(
1716 TAG, "Resuming top, waiting visible to hide: " + prev);
1717 } else {
1718 // The next activity is already visible, so hide the previous
1719 // activity's windows right now so we can show the new one ASAP.
1720 // We only do this if the previous is finishing, which should mean
1721 // it is on top of the one being resumed so hiding it quickly
1722 // is good. Otherwise, we want to do the normal route of allowing
1723 // the resumed activity to be shown so we can decide if the
1724 // previous should actually be hidden depending on whether the
1725 // new one is found to be full-screen or not.
1726 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001727 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1729 + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001730 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001731 + ", nowVisible=" + next.nowVisible);
1732 } else {
Craig Mautner8c14c152015-01-15 17:32:07 -08001733 if (DEBUG_SWITCH) Slog.v(TAG,
1734 "Previous already visible but still waiting to hide: " + prev
1735 + ", waitingVisible="
1736 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1737 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 }
1739 }
1740 }
1741
Dianne Hackborne7f97212011-02-24 14:40:20 -08001742 // Launching this app's activity, make sure the app is no longer
1743 // considered stopped.
1744 try {
1745 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001746 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001747 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001748 } catch (IllegalArgumentException e) {
1749 Slog.w(TAG, "Failed trying to unstop package "
1750 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001751 }
1752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 // We are starting up the next activity, so tell the window manager
1754 // that the previous one will be hidden soon. This way it can know
1755 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001756 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 if (prev != null) {
1758 if (prev.finishing) {
1759 if (DEBUG_TRANSITION) Slog.v(TAG,
1760 "Prepare close transition: prev=" + prev);
1761 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001762 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001763 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001764 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001765 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001766 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1767 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001769 mWindowManager.setAppWillBeHidden(prev.appToken);
1770 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001772 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001774 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001775 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001777 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001778 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001779 : next.mLaunchTaskBehind
1780 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1781 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 }
1783 }
1784 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001785 mWindowManager.setAppWillBeHidden(prev.appToken);
1786 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 }
Craig Mautner967212c2013-04-13 21:10:58 -07001788 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001789 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001791 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001792 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001794 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795 }
1796 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001797
1798 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001799 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001800 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1801 if (opts != null) {
1802 resumeAnimOptions = opts.toBundle();
1803 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001804 next.applyOptionsLocked();
1805 } else {
1806 next.clearOptionsLocked();
1807 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808
Craig Mautnercf910b02013-04-23 11:23:27 -07001809 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 if (next.app != null && next.app.thread != null) {
1811 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1812
1813 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001814 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001816 // schedule launch ticks to collect information about slow apps.
1817 next.startLaunchTickingLocked();
1818
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001819 ActivityRecord lastResumedActivity =
1820 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821 ActivityState lastState = next.state;
1822
1823 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001824
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001825 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001826 next.state = ActivityState.RESUMED;
1827 mResumedActivity = next;
1828 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001829 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001830 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001831 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001832 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833
1834 // Have the window manager re-evaluate the orientation of
1835 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001836 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001837 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001838 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001839 mService.mConfiguration,
1840 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1841 if (config != null) {
1842 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001844 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001846
Craig Mautner525f3d92013-05-07 14:01:50 -07001847 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001848 // The configuration update wasn't able to keep the existing
1849 // instance of the activity, and instead started a new one.
1850 // We should be all done, but let's just make sure our activity
1851 // is still at the top and schedule another run if something
1852 // weird happened.
1853 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001854 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001855 "Activity config changed during resume: " + next
1856 + ", new next: " + nextNext);
1857 if (nextNext != next) {
1858 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001859 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001861 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001862 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001863 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001864 return true;
1865 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001866 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001867 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 }
Craig Mautner58547802013-03-05 08:23:53 -08001869
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001870 try {
1871 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001872 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001873 if (a != null) {
1874 final int N = a.size();
1875 if (!next.finishing && N > 0) {
1876 if (DEBUG_RESULTS) Slog.v(
1877 TAG, "Delivering results to " + next
1878 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001879 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 }
1881 }
1882
1883 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001884 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 }
1886
Craig Mautner299f9602015-01-26 09:47:33 -08001887 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1888 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001889
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001890 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001891 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001892 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001893 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001894 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001895 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001896 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001897
Craig Mautner0eea92c2013-05-16 13:35:39 -07001898 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001899
Craig Mautnerac6f8432013-07-17 13:24:59 -07001900 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 } catch (Exception e) {
1902 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001903 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1904 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001906 if (lastStack != null) {
1907 lastStack.mResumedActivity = lastResumedActivity;
1908 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 Slog.i(TAG, "Restarting because process died: " + next);
1910 if (!next.hasBeenLaunched) {
1911 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001912 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1913 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001914 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001915 next.appToken, next.packageName, next.theme,
1916 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001917 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1918 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001919 }
George Mount2c92c972014-03-20 09:38:23 -07001920 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001921 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001922 return true;
1923 }
1924
1925 // From this point on, if something goes wrong there is no way
1926 // to recover the activity.
1927 try {
1928 next.visible = true;
1929 completeResumeLocked(next);
1930 } catch (Exception e) {
1931 // If any exception gets thrown, toss away this
1932 // activity and try the next one.
1933 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001934 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001935 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001936 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937 return true;
1938 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001939 next.stopped = false;
1940
1941 } else {
1942 // Whoops, need to restart this activity!
1943 if (!next.hasBeenLaunched) {
1944 next.hasBeenLaunched = true;
1945 } else {
1946 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001947 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001948 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001949 mService.compatibilityInfoForPackageLocked(
1950 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001952 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001953 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001954 }
1955 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1956 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001957 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001958 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 }
1960
Craig Mautnercf910b02013-04-23 11:23:27 -07001961 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001962 return true;
1963 }
1964
riddle_hsuc215a4f2014-12-27 12:10:45 +08001965 private TaskRecord getNextTask(TaskRecord targetTask) {
1966 final int index = mTaskHistory.indexOf(targetTask);
1967 if (index >= 0) {
1968 final int numTasks = mTaskHistory.size();
1969 for (int i = index + 1; i < numTasks; ++i) {
1970 TaskRecord task = mTaskHistory.get(i);
1971 if (task.userId == targetTask.userId) {
1972 return task;
1973 }
1974 }
1975 }
1976 return null;
1977 }
1978
Craig Mautnerac6f8432013-07-17 13:24:59 -07001979 private void insertTaskAtTop(TaskRecord task) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001980 // If the moving task is over home stack, transfer its return type to next task
1981 if (task.isOverHomeStack()) {
1982 final TaskRecord nextTask = getNextTask(task);
1983 if (nextTask != null) {
1984 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1985 }
1986 }
1987
Craig Mautner9c85c202013-10-04 20:11:26 -07001988 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08001989 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001990 if (isOnHomeDisplay()) {
1991 ActivityStack lastStack = mStackSupervisor.getLastStack();
1992 final boolean fromHome = lastStack.isHomeStack();
1993 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001994 task.setTaskToReturnTo(fromHome
1995 ? lastStack.topTask() == null
1996 ? HOME_ACTIVITY_TYPE
1997 : lastStack.topTask().taskType
1998 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001999 }
2000 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002001 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002002 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002003
Craig Mautnerac6f8432013-07-17 13:24:59 -07002004 mTaskHistory.remove(task);
2005 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002006 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01002007 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002008 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002009 while (--taskNdx >= 0) {
2010 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002011 break;
2012 }
2013 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002014 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002015 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002016 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002017 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002018 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002019
Craig Mautner8849a5e2013-04-02 16:41:03 -07002020 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002021 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002022 TaskRecord rTask = r.task;
2023 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002024 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2025 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002026 // Last activity in task had been removed or ActivityManagerService is reusing task.
2027 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002028 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002029 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002030 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002031 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002032 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002033 if (!newTask) {
2034 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002035 boolean startIt = true;
2036 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2037 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002038 if (task.getTopActivity() == null) {
2039 // All activities in task are finishing.
2040 continue;
2041 }
Craig Mautner70a86932013-02-28 22:37:44 -08002042 if (task == r.task) {
2043 // Here it is! Now, if this is not yet visible to the
2044 // user, then just add it without starting; it will
2045 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002046 if (!startIt) {
2047 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2048 + task, new RuntimeException("here").fillInStackTrace());
2049 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002050 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002051 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2052 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002053 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002054 r.userId, r.info.configChanges, task.voiceSession != null,
2055 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002057 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002059 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 return;
2061 }
2062 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002063 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 startIt = false;
2065 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 }
2067 }
2068
2069 // Place a new activity at top of stack, so it is next to interact
2070 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002071
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002072 // If we are not placing the new activity frontmost, we do not want
2073 // to deliver the onUserLeaving callback to the actual frontmost
2074 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002075 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002076 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002077 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2078 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 }
Craig Mautner70a86932013-02-28 22:37:44 -08002080
2081 task = r.task;
2082
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002084 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002085 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002086 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002087 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002088
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002089 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002090 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002091 // We want to show the starting preview window if we are
2092 // switching to a new task, or the next activity's process is
2093 // not currently running.
2094 boolean showStartingIcon = newTask;
2095 ProcessRecord proc = r.app;
2096 if (proc == null) {
2097 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2098 }
2099 if (proc == null || proc.thread == null) {
2100 showStartingIcon = true;
2101 }
2102 if (DEBUG_TRANSITION) Slog.v(TAG,
2103 "Prepare open transition: starting " + r);
2104 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002105 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002106 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002107 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002108 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002109 ? r.mLaunchTaskBehind
2110 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2111 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002112 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 mNoAnimActivities.remove(r);
2114 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002115 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002116 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002117 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002118 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 boolean doShow = true;
2120 if (newTask) {
2121 // Even though this activity is starting fresh, we still need
2122 // to reset it to make sure we apply affinities to move any
2123 // existing activities from other tasks in to it.
2124 // If the caller has requested that the target task be
2125 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002126 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 resetTaskIfNeededLocked(r, r);
2128 doShow = topRunningNonDelayedActivityLocked(null) == r;
2129 }
George Mount70778d72014-07-01 16:33:45 -07002130 } else if (options != null && new ActivityOptions(options).getAnimationType()
2131 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2132 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002134 if (r.mLaunchTaskBehind) {
2135 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2136 // tell WindowManager that r is visible even though it is at the back of the stack.
2137 mWindowManager.setAppVisibility(r.appToken, true);
2138 ensureActivitiesVisibleLocked(null, 0);
2139 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 // Figure out if we are transitioning from another activity that is
2141 // "has the same starting icon" as the next one. This allows the
2142 // window manager to keep the previous window it had previously
2143 // created, if it still had one.
2144 ActivityRecord prev = mResumedActivity;
2145 if (prev != null) {
2146 // We don't want to reuse the previous starting preview if:
2147 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002148 if (prev.task != r.task) {
2149 prev = null;
2150 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002152 else if (prev.nowVisible) {
2153 prev = null;
2154 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002156 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002157 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002158 mService.compatibilityInfoForPackageLocked(
2159 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002160 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002161 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002162 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 }
2164 } else {
2165 // If this is the first activity, don't do any fancy animations,
2166 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002167 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002168 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002169 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002170 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002171 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002172 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002173 }
2174 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002175 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 }
2177
2178 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002179 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 }
2181 }
2182
Dianne Hackbornbe707852011-11-11 14:32:10 -08002183 final void validateAppTokensLocked() {
2184 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002185 mValidateAppTokens.ensureCapacity(numActivities());
2186 final int numTasks = mTaskHistory.size();
2187 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2188 TaskRecord task = mTaskHistory.get(taskNdx);
2189 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002190 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002191 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002192 }
Craig Mautner000f0022013-02-26 15:04:29 -08002193 TaskGroup group = new TaskGroup();
2194 group.taskId = task.taskId;
2195 mValidateAppTokens.add(group);
2196 final int numActivities = activities.size();
2197 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2198 final ActivityRecord r = activities.get(activityNdx);
2199 group.tokens.add(r.appToken);
2200 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002201 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002202 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002203 }
2204
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 /**
2206 * Perform a reset of the given task, if needed as part of launching it.
2207 * Returns the new HistoryRecord at the top of the task.
2208 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002209 /**
2210 * Helper method for #resetTaskIfNeededLocked.
2211 * We are inside of the task being reset... we'll either finish this activity, push it out
2212 * for another task, or leave it as-is.
2213 * @param task The task containing the Activity (taskTop) that might be reset.
2214 * @param forceReset
2215 * @return An ActivityOptions that needs to be processed.
2216 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002217 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002218 ActivityOptions topOptions = null;
2219
2220 int replyChainEnd = -1;
2221 boolean canMoveOptions = true;
2222
2223 // We only do this for activities that are not the root of the task (since if we finish
2224 // the root, we may no longer have the task!).
2225 final ArrayList<ActivityRecord> activities = task.mActivities;
2226 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002227 final int rootActivityNdx = task.findEffectiveRootIndex();
2228 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002229 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002230 if (target.frontOfTask)
2231 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002232
2233 final int flags = target.info.flags;
2234 final boolean finishOnTaskLaunch =
2235 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2236 final boolean allowTaskReparenting =
2237 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2238 final boolean clearWhenTaskReset =
2239 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2240
2241 if (!finishOnTaskLaunch
2242 && !clearWhenTaskReset
2243 && target.resultTo != null) {
2244 // If this activity is sending a reply to a previous
2245 // activity, we can't do anything with it now until
2246 // we reach the start of the reply chain.
2247 // XXX note that we are assuming the result is always
2248 // to the previous activity, which is almost always
2249 // the case but we really shouldn't count on.
2250 if (replyChainEnd < 0) {
2251 replyChainEnd = i;
2252 }
2253 } else if (!finishOnTaskLaunch
2254 && !clearWhenTaskReset
2255 && allowTaskReparenting
2256 && target.taskAffinity != null
2257 && !target.taskAffinity.equals(task.affinity)) {
2258 // If this activity has an affinity for another
2259 // task, then we need to move it out of here. We will
2260 // move it as far out of the way as possible, to the
2261 // bottom of the activity stack. This also keeps it
2262 // correctly ordered with any activities we previously
2263 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002264 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002265 final ActivityRecord bottom =
2266 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002267 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002268 if (bottom != null && target.taskAffinity != null
2269 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002270 // If the activity currently at the bottom has the
2271 // same task affinity as the one we are moving,
2272 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002273 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002274 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002275 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002276 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002277 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002278 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002279 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002280 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2281 + " out to new task " + target.task);
2282 }
2283
Craig Mautnere3a74d52013-02-22 14:14:58 -08002284 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002285 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002286
Craig Mautner525f3d92013-05-07 14:01:50 -07002287 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002288 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2289 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002290 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002291 if (p.finishing) {
2292 continue;
2293 }
2294
Craig Mautnere3a74d52013-02-22 14:14:58 -08002295 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002296 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002297 topOptions = p.takeOptionsLocked();
2298 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002299 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002300 }
2301 }
2302 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002303 + task + " adding to task=" + targetTask
2304 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002305 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2306 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002307 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002308 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002309
Craig Mautner83162a92015-01-26 14:43:30 -08002310 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002311 }
2312
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002313 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002314 if (VALIDATE_TOKENS) {
2315 validateAppTokensLocked();
2316 }
2317
2318 replyChainEnd = -1;
2319 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2320 // If the activity should just be removed -- either
2321 // because it asks for it, or the task should be
2322 // cleared -- then finish it and anything that is
2323 // part of its reply chain.
2324 int end;
2325 if (clearWhenTaskReset) {
2326 // In this case, we want to finish this activity
2327 // and everything above it, so be sneaky and pretend
2328 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002329 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002330 } else if (replyChainEnd < 0) {
2331 end = i;
2332 } else {
2333 end = replyChainEnd;
2334 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002335 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002336 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002337 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002338 if (p.finishing) {
2339 continue;
2340 }
2341 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002342 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002343 topOptions = p.takeOptionsLocked();
2344 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002345 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002346 }
2347 }
Craig Mautner58547802013-03-05 08:23:53 -08002348 if (DEBUG_TASKS) Slog.w(TAG,
2349 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002350 if (finishActivityLocked(
2351 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002352 end--;
2353 srcPos--;
2354 }
2355 }
2356 replyChainEnd = -1;
2357 } else {
2358 // If we were in the middle of a chain, well the
2359 // activity that started it all doesn't want anything
2360 // special, so leave it all as-is.
2361 replyChainEnd = -1;
2362 }
2363 }
2364
2365 return topOptions;
2366 }
2367
2368 /**
2369 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2370 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2371 * @param affinityTask The task we are looking for an affinity to.
2372 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2373 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2374 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2375 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002376 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002377 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002378 int replyChainEnd = -1;
2379 final int taskId = task.taskId;
2380 final String taskAffinity = task.affinity;
2381
2382 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2383 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002384 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2385
2386 // Do not operate on or below the effective root Activity.
2387 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002388 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002389 if (target.frontOfTask)
2390 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002391
2392 final int flags = target.info.flags;
2393 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2394 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2395
2396 if (target.resultTo != null) {
2397 // If this activity is sending a reply to a previous
2398 // activity, we can't do anything with it now until
2399 // we reach the start of the reply chain.
2400 // XXX note that we are assuming the result is always
2401 // to the previous activity, which is almost always
2402 // the case but we really shouldn't count on.
2403 if (replyChainEnd < 0) {
2404 replyChainEnd = i;
2405 }
2406 } else if (topTaskIsHigher
2407 && allowTaskReparenting
2408 && taskAffinity != null
2409 && taskAffinity.equals(target.taskAffinity)) {
2410 // This activity has an affinity for our task. Either remove it if we are
2411 // clearing or move it over to our task. Note that
2412 // we currently punt on the case where we are resetting a
2413 // task that is not at the top but who has activities above
2414 // with an affinity to it... this is really not a normal
2415 // case, and we will need to later pull that task to the front
2416 // and usually at that point we will do the reset and pick
2417 // up those remaining activities. (This only happens if
2418 // someone starts an activity in a new task from an activity
2419 // in a task that is not currently on top.)
2420 if (forceReset || finishOnTaskLaunch) {
2421 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2422 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2423 for (int srcPos = start; srcPos >= i; --srcPos) {
2424 final ActivityRecord p = activities.get(srcPos);
2425 if (p.finishing) {
2426 continue;
2427 }
Todd Kennedy539db512014-12-15 09:57:55 -08002428 finishActivityLocked(
2429 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002430 }
2431 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002432 if (taskInsertionPoint < 0) {
2433 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002434
Craig Mautner77878772013-03-04 19:46:24 -08002435 }
Craig Mautner77878772013-03-04 19:46:24 -08002436
2437 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2438 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2439 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2440 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002441 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002442 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002443 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002444
Craig Mautnere3a74d52013-02-22 14:14:58 -08002445 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2446 + " to stack at " + task,
2447 new RuntimeException("here").fillInStackTrace());
2448 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2449 + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002450 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002451 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002452 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002453 if (VALIDATE_TOKENS) {
2454 validateAppTokensLocked();
2455 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002456
2457 // Now we've moved it in to place... but what if this is
2458 // a singleTop activity and we have put it on top of another
2459 // instance of the same activity? Then we drop the instance
2460 // below so it remains singleTop.
2461 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2462 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002463 int targetNdx = taskActivities.indexOf(target);
2464 if (targetNdx > 0) {
2465 ActivityRecord p = taskActivities.get(targetNdx - 1);
2466 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002467 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2468 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002469 }
2470 }
2471 }
2472 }
2473
2474 replyChainEnd = -1;
2475 }
2476 }
Craig Mautner77878772013-03-04 19:46:24 -08002477 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002478 }
2479
Craig Mautner8849a5e2013-04-02 16:41:03 -07002480 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002481 ActivityRecord newActivity) {
2482 boolean forceReset =
2483 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2484 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2485 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2486 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2487 forceReset = true;
2488 }
2489 }
2490
2491 final TaskRecord task = taskTop.task;
2492
2493 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2494 * for remaining tasks. Used for later tasks to reparent to task. */
2495 boolean taskFound = false;
2496
2497 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2498 ActivityOptions topOptions = null;
2499
Craig Mautner77878772013-03-04 19:46:24 -08002500 // Preserve the location for reparenting in the new task.
2501 int reparentInsertionPoint = -1;
2502
Craig Mautnere3a74d52013-02-22 14:14:58 -08002503 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2504 final TaskRecord targetTask = mTaskHistory.get(i);
2505
2506 if (targetTask == task) {
2507 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2508 taskFound = true;
2509 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002510 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2511 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002512 }
2513 }
2514
Craig Mautner70a86932013-02-28 22:37:44 -08002515 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002516 if (taskNdx >= 0) {
2517 do {
2518 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2519 } while (taskTop == null && taskNdx >= 0);
2520 }
Craig Mautner70a86932013-02-28 22:37:44 -08002521
Craig Mautnere3a74d52013-02-22 14:14:58 -08002522 if (topOptions != null) {
2523 // If we got some ActivityOptions from an activity on top that
2524 // was removed from the task, propagate them to the new real top.
2525 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 taskTop.updateOptionsLocked(topOptions);
2527 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002528 topOptions.abort();
2529 }
2530 }
2531
2532 return taskTop;
2533 }
2534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2536 String resultWho, int requestCode, int resultCode, Intent data) {
2537
2538 if (callingUid > 0) {
2539 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002540 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002541 }
2542
2543 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2544 + " : who=" + resultWho + " req=" + requestCode
2545 + " res=" + resultCode + " data=" + data);
2546 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2547 try {
2548 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2549 list.add(new ResultInfo(resultWho, requestCode,
2550 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002551 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 return;
2553 } catch (Exception e) {
2554 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2555 }
2556 }
2557
2558 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2559 }
2560
Craig Mautner299f9602015-01-26 09:47:33 -08002561 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002562 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2563 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002564 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002565 if (next != r) {
2566 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002567 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002568 // For non-fullscreen stack, we want to move the focus to the next visible
2569 // stack to prevent the home screen from moving to the top and obscuring
2570 // other visible stacks.
2571 if (!mFullscreen
2572 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2573 return;
2574 }
2575 // Move the home stack to the top if this stack is fullscreen or there is no
2576 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002577 if (mStackSupervisor.moveHomeStackTaskToTop(
2578 task.getTaskToReturnTo(), myReason)) {
2579 // Activity focus was already adjusted. Nothing else to do...
2580 return;
2581 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002582 }
2583 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002584
2585 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002586 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002587 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002588 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002589 }
2590 }
2591
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002592 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2593 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2594 final String myReason = reason + " adjustFocusToNextVisibleStack";
2595 if (stack == null) {
2596 return false;
2597 }
2598 final ActivityRecord top = stack.topRunningActivityLocked(null);
2599 if (top == null) {
2600 return false;
2601 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002602 mService.setFocusedActivityLocked(top, myReason);
2603 return true;
2604 }
2605
Craig Mautnerf3333272013-04-22 10:55:53 -07002606 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2608 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2609 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2610 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002611 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002612 if (DEBUG_STATES) {
2613 Slog.d(TAG, "no-history finish of " + r);
2614 }
2615 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002616 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002617 } else {
2618 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2619 + " on stop because we're just sleeping");
2620 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002621 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002622 }
2623
2624 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002625 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002626 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 try {
2628 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002629 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2630 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002631 r.state = ActivityState.STOPPING;
2632 if (DEBUG_VISBILITY) Slog.v(
2633 TAG, "Stopping visible=" + r.visible + " for " + r);
2634 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002635 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002636 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002637 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002638 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002639 r.setSleeping(true);
2640 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002641 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002642 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 } catch (Exception e) {
2644 // Maybe just ignore exceptions here... if the process
2645 // has crashed, our death notification will clean things
2646 // up.
2647 Slog.w(TAG, "Exception thrown during pause", e);
2648 // Just in case, assume it to be stopped.
2649 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002650 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002651 r.state = ActivityState.STOPPED;
2652 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002653 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 }
2655 }
2656 }
2657 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002658
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002659 /**
2660 * @return Returns true if the activity is being finished, false if for
2661 * some reason it is being left as-is.
2662 */
2663 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002664 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002665 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002666 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002667 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002668 + ", result=" + resultCode + ", data=" + resultData
2669 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002670 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 return false;
2672 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673
Craig Mautnerd44711d2013-02-23 11:24:36 -08002674 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675 return true;
2676 }
2677
Craig Mautnerd2328952013-03-05 12:46:26 -08002678 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002679 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2680 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2681 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2682 ActivityRecord r = activities.get(activityNdx);
2683 if (r.resultTo == self && r.requestCode == requestCode) {
2684 if ((r.resultWho == null && resultWho == null) ||
2685 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2686 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2687 false);
2688 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002689 }
2690 }
2691 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002692 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002693 }
2694
Todd Kennedy539db512014-12-15 09:57:55 -08002695 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002696 ActivityRecord r = topRunningActivityLocked(null);
2697 if (r != null && r.app == app) {
2698 // If the top running activity is from this crashing
2699 // process, then terminate it to avoid getting in a loop.
2700 Slog.w(TAG, " Force finishing activity "
2701 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002702 int taskNdx = mTaskHistory.indexOf(r.task);
2703 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002704 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002705 // Also terminate any activities below it that aren't yet
2706 // stopped, to avoid a situation where one will get
2707 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002708 --activityNdx;
2709 if (activityNdx < 0) {
2710 do {
2711 --taskNdx;
2712 if (taskNdx < 0) {
2713 break;
2714 }
2715 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2716 } while (activityNdx < 0);
2717 }
2718 if (activityNdx >= 0) {
2719 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002720 if (r.state == ActivityState.RESUMED
2721 || r.state == ActivityState.PAUSING
2722 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002723 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002724 Slog.w(TAG, " Force finishing activity "
2725 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002726 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002727 }
2728 }
2729 }
2730 }
2731 }
2732
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002733 final void finishVoiceTask(IVoiceInteractionSession session) {
2734 IBinder sessionBinder = session.asBinder();
2735 boolean didOne = false;
2736 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2737 TaskRecord tr = mTaskHistory.get(taskNdx);
2738 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2739 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2740 ActivityRecord r = tr.mActivities.get(activityNdx);
2741 if (!r.finishing) {
2742 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2743 false);
2744 didOne = true;
2745 }
2746 }
2747 }
2748 }
2749 if (didOne) {
2750 mService.updateOomAdjLocked();
2751 }
2752 }
2753
Craig Mautnerd2328952013-03-05 12:46:26 -08002754 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002755 ArrayList<ActivityRecord> activities = r.task.mActivities;
2756 for (int index = activities.indexOf(r); index >= 0; --index) {
2757 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002758 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002759 break;
2760 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002761 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002762 }
2763 return true;
2764 }
2765
Dianne Hackborn5c607432012-02-28 14:44:19 -08002766 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2767 // send the result
2768 ActivityRecord resultTo = r.resultTo;
2769 if (resultTo != null) {
2770 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2771 + " who=" + r.resultWho + " req=" + r.requestCode
2772 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002773 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002774 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002775 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002776 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002777 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002778 if (r.info.applicationInfo.uid > 0) {
2779 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2780 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002781 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002782 }
2783 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2784 resultData);
2785 r.resultTo = null;
2786 }
2787 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2788
2789 // Make sure this HistoryRecord is not holding on to other resources,
2790 // because clients have remote IPC references to this object so we
2791 // can't assume that will go away and want to avoid circular IPC refs.
2792 r.results = null;
2793 r.pendingResults = null;
2794 r.newIntents = null;
2795 r.icicle = null;
2796 }
2797
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002798 /**
2799 * @return Returns true if this activity has been removed from the history
2800 * list, or false if it is still in the list and will be removed later.
2801 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002802 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2803 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002804 if (r.finishing) {
2805 Slog.w(TAG, "Duplicate finish request for " + r);
2806 return false;
2807 }
2808
Wale Ogunwale7d701172015-03-11 15:36:30 -07002809 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002810 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002811 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002812 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002813 task.taskId, r.shortComponentName, reason);
2814 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002815 final int index = activities.indexOf(r);
2816 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002817 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002818 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002819 // If the caller asked that this activity (and all above it)
2820 // be cleared when the task is reset, don't lose that information,
2821 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002822 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002823 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 }
2825 }
2826
2827 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002828
Craig Mautner299f9602015-01-26 09:47:33 -08002829 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002830
Dianne Hackborn5c607432012-02-28 14:44:19 -08002831 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002832
Craig Mautnerde4ef022013-04-07 19:01:33 -07002833 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002834 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002835 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002836 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002837 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002838 ? AppTransition.TRANSIT_TASK_CLOSE
2839 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002840
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002841 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002842 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002843
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002844 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2846 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002847 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 }
2849
Craig Mautneraea74a52014-03-08 14:23:10 -08002850 if (endTask) {
2851 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2852 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002853 } else if (r.state != ActivityState.PAUSING) {
2854 // If the activity is PAUSING, we will complete the finish once
2855 // it is done pausing; else we can just directly finish it here.
2856 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002857 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858 } else {
2859 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2860 }
2861
2862 return false;
2863 }
2864
Craig Mautnerf3333272013-04-22 10:55:53 -07002865 static final int FINISH_IMMEDIATELY = 0;
2866 static final int FINISH_AFTER_PAUSE = 1;
2867 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868
Craig Mautnerf3333272013-04-22 10:55:53 -07002869 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002870 // First things first: if this activity is currently visible,
2871 // and the resumed activity is not yet visible, then hold off on
2872 // finishing until the resumed one becomes visible.
2873 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002874 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2875 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002876 if (mStackSupervisor.mStoppingActivities.size() > 3
2877 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 // If we already have a few activities waiting to stop,
2879 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002880 // them out. Or if r is the last of activity of the last task the stack
2881 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002882 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002883 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002884 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 }
2886 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002887 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2888 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002890 if (oomAdj) {
2891 mService.updateOomAdjLocked();
2892 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002893 return r;
2894 }
2895
2896 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002897 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002898 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002899 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002900 if (mResumedActivity == r) {
2901 mResumedActivity = null;
2902 }
2903 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002904 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 r.state = ActivityState.FINISHING;
2906
2907 if (mode == FINISH_IMMEDIATELY
2908 || prevState == ActivityState.STOPPED
2909 || prevState == ActivityState.INITIALIZING) {
2910 // If this activity is already stopped, we can just finish
2911 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002912 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002913 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002914 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002915 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002916 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002917 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002918 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2919 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002920 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002921 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002922
2923 // Need to go through the full pause cycle to get this
2924 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002925 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002926 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002927 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002928 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 return r;
2930 }
2931
Craig Mautneree36c772014-07-16 14:56:05 -07002932 void finishAllActivitiesLocked(boolean immediately) {
2933 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002934 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2935 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2936 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2937 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002938 noActivitiesInStack = false;
2939 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002940 continue;
2941 }
Craig Mautneree36c772014-07-16 14:56:05 -07002942 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002943 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2944 }
2945 }
Craig Mautneree36c772014-07-16 14:56:05 -07002946 if (noActivitiesInStack) {
2947 mActivityContainer.onTaskListEmptyLocked();
2948 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002949 }
2950
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002951 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2952 // Basic case: for simple app-centric recents, we need to recreate
2953 // the task if the affinity has changed.
2954 if (srec == null || srec.task.affinity == null ||
2955 !srec.task.affinity.equals(destAffinity)) {
2956 return true;
2957 }
2958 // Document-centric case: an app may be split in to multiple documents;
2959 // they need to re-create their task if this current activity is the root
2960 // of a document, unless simply finishing it will return them to the the
2961 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002962 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2963 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002964 // Okay, this activity is at the root of its task. What to do, what to do...
2965 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2966 // Finishing won't return to an application, so we need to recreate.
2967 return true;
2968 }
2969 // We now need to get the task below it to determine what to do.
2970 int taskIdx = mTaskHistory.indexOf(srec.task);
2971 if (taskIdx <= 0) {
2972 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2973 return false;
2974 }
2975 if (taskIdx == 0) {
2976 // At the bottom of the stack, nothing to go back to.
2977 return true;
2978 }
2979 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2980 if (!srec.task.affinity.equals(prevTask.affinity)) {
2981 // These are different apps, so need to recreate.
2982 return true;
2983 }
2984 }
2985 return false;
2986 }
2987
Wale Ogunwale7d701172015-03-11 15:36:30 -07002988 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002989 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002990 final TaskRecord task = srec.task;
2991 final ArrayList<ActivityRecord> activities = task.mActivities;
2992 final int start = activities.indexOf(srec);
2993 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002994 return false;
2995 }
2996 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002997 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002998 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002999 final ComponentName dest = destIntent.getComponent();
3000 if (start > 0 && dest != null) {
3001 for (int i = finishTo; i >= 0; i--) {
3002 ActivityRecord r = activities.get(i);
3003 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003004 r.info.name.equals(dest.getClassName())) {
3005 finishTo = i;
3006 parent = r;
3007 foundParentInTask = true;
3008 break;
3009 }
3010 }
3011 }
3012
3013 IActivityController controller = mService.mController;
3014 if (controller != null) {
3015 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3016 if (next != null) {
3017 // ask watcher if this is allowed
3018 boolean resumeOK = true;
3019 try {
3020 resumeOK = controller.activityResuming(next.packageName);
3021 } catch (RemoteException e) {
3022 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003023 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003024 }
3025
3026 if (!resumeOK) {
3027 return false;
3028 }
3029 }
3030 }
3031 final long origId = Binder.clearCallingIdentity();
3032 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003033 ActivityRecord r = activities.get(i);
3034 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003035 // Only return the supplied result for the first activity finished
3036 resultCode = Activity.RESULT_CANCELED;
3037 resultData = null;
3038 }
3039
3040 if (parent != null && foundParentInTask) {
3041 final int parentLaunchMode = parent.info.launchMode;
3042 final int destIntentFlags = destIntent.getFlags();
3043 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3044 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3045 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3046 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003047 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3048 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003049 } else {
3050 try {
3051 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3052 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003053 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003054 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003055 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003056 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003057 foundParentInTask = res == ActivityManager.START_SUCCESS;
3058 } catch (RemoteException e) {
3059 foundParentInTask = false;
3060 }
3061 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003062 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003063 }
3064 }
3065 Binder.restoreCallingIdentity(origId);
3066 return foundParentInTask;
3067 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003068 /**
3069 * Perform the common clean-up of an activity record. This is called both
3070 * as part of destroyActivityLocked() (when destroying the client-side
3071 * representation) and cleaning things up as a result of its hosting
3072 * processing going away, in which case there is no remaining client-side
3073 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003074 *
3075 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003077 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3078 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 if (mResumedActivity == r) {
3080 mResumedActivity = null;
3081 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003082 if (mPausingActivity == r) {
3083 mPausingActivity = null;
3084 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003085 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003086
3087 r.configDestroy = false;
3088 r.frozenBeforeDestroy = false;
3089
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003090 if (setState) {
3091 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3092 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003093 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003094 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003095 }
3096
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003097 // Make sure this record is no longer in the pending finishes list.
3098 // This could happen, for example, if we are trimming activities
3099 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003100 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003101 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103 // Remove any pending results.
3104 if (r.finishing && r.pendingResults != null) {
3105 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3106 PendingIntentRecord rec = apr.get();
3107 if (rec != null) {
3108 mService.cancelIntentSenderLocked(rec, false);
3109 }
3110 }
3111 r.pendingResults = null;
3112 }
3113
3114 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003115 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003116 }
3117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003119 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003120 if (getVisibleBehindActivity() == r) {
3121 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003122 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003123 }
3124
3125 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003126 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003128 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003129 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003130 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003131 }
3132
Craig Mautner299f9602015-01-26 09:47:33 -08003133 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003134 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003135 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003136 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003137 if (DEBUG_ADD_REMOVE) {
3138 RuntimeException here = new RuntimeException("here");
3139 here.fillInStackTrace();
3140 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003142 r.takeFromHistory();
3143 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003144 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003145 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003146 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003147 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003148 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003149 if (VALIDATE_TOKENS) {
3150 validateAppTokensLocked();
3151 }
Craig Mautner312ba862014-02-10 17:55:01 -08003152 final TaskRecord task = r.task;
3153 if (task != null && task.removeActivity(r)) {
3154 if (DEBUG_STACK) Slog.i(TAG,
3155 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003156 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3157 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003158 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003159 }
Craig Mautner299f9602015-01-26 09:47:33 -08003160 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003161 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003162 cleanUpActivityServicesLocked(r);
3163 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003164 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003165
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 /**
3167 * Perform clean-up of service connections in an activity record.
3168 */
3169 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3170 // Throw away any services that have been bound by this activity.
3171 if (r.connections != null) {
3172 Iterator<ConnectionRecord> it = r.connections.iterator();
3173 while (it.hasNext()) {
3174 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003175 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 }
3177 r.connections = null;
3178 }
3179 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003180
Craig Mautneree2e45a2014-06-27 12:10:03 -07003181 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003182 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003183 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003184 mHandler.sendMessage(msg);
3185 }
3186
Craig Mautneree2e45a2014-06-27 12:10:03 -07003187 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003188 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003189 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003190 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3191 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3192 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3193 final ActivityRecord r = activities.get(activityNdx);
3194 if (r.finishing) {
3195 continue;
3196 }
3197 if (r.fullscreen) {
3198 lastIsOpaque = true;
3199 }
3200 if (owner != null && r.app != owner) {
3201 continue;
3202 }
3203 if (!lastIsOpaque) {
3204 continue;
3205 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003206 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003207 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3208 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003209 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003210 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003211 activityRemoved = true;
3212 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003213 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003214 }
3215 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003216 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003217 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003218 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003219 }
3220
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003221 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3222 if (r.isDestroyable()) {
3223 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3224 + " resumed=" + mResumedActivity
3225 + " pausing=" + mPausingActivity + " for reason " + reason);
3226 return destroyActivityLocked(r, true, reason);
3227 }
3228 return false;
3229 }
3230
3231 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3232 String reason) {
3233 // Iterate over tasks starting at the back (oldest) first.
3234 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3235 int maxTasks = tasks.size() / 4;
3236 if (maxTasks < 1) {
3237 maxTasks = 1;
3238 }
3239 int numReleased = 0;
3240 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3241 final TaskRecord task = mTaskHistory.get(taskNdx);
3242 if (!tasks.contains(task)) {
3243 continue;
3244 }
3245 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3246 int curNum = 0;
3247 final ArrayList<ActivityRecord> activities = task.mActivities;
3248 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3249 final ActivityRecord activity = activities.get(actNdx);
3250 if (activity.app == app && activity.isDestroyable()) {
3251 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3252 + " in state " + activity.state + " resumed=" + mResumedActivity
3253 + " pausing=" + mPausingActivity + " for reason " + reason);
3254 destroyActivityLocked(activity, true, reason);
3255 if (activities.get(actNdx) != activity) {
3256 // Was removed from list, back up so we don't miss the next one.
3257 actNdx--;
3258 }
3259 curNum++;
3260 }
3261 }
3262 if (curNum > 0) {
3263 numReleased += curNum;
3264 maxTasks--;
3265 if (mTaskHistory.get(taskNdx) != task) {
3266 // The entire task got removed, back up so we don't miss the next one.
3267 taskNdx--;
3268 }
3269 }
3270 }
3271 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3272 return numReleased;
3273 }
3274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003275 /**
3276 * Destroy the current CLIENT SIDE instance of an activity. This may be
3277 * called both when actually finishing an activity, or when performing
3278 * a configuration switch where we destroy the current client-side object
3279 * but then create a new client-side object for this same HistoryRecord.
3280 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003281 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003282 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003283 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003284 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3285 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003286 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003287 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288
3289 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003290
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003291 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292
3293 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003294
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 if (hadApp) {
3296 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003297 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003298 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3299 mService.mHeavyWeightProcess = null;
3300 mService.mHandler.sendEmptyMessage(
3301 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3302 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003303 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003304 // Update any services we are bound to that might care about whether
3305 // their client may have activities.
3306 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003307 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003308 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003309 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003310 }
3311 }
3312
3313 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003315 try {
3316 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003317 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 r.configChangeFlags);
3319 } catch (Exception e) {
3320 // We can just ignore exceptions here... if the process
3321 // has crashed, our death notification will clean things
3322 // up.
3323 //Slog.w(TAG, "Exception thrown during finish", e);
3324 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003325 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003326 removedFromHistory = true;
3327 skipDestroy = true;
3328 }
3329 }
3330
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003332
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003333 // If the activity is finishing, we need to wait on removing it
3334 // from the list to give it a chance to do its cleanup. During
3335 // that time it may make calls back with its token so we need to
3336 // be able to find it on the list and so we don't want to remove
3337 // it from the list yet. Otherwise, we can just immediately put
3338 // it in the destroyed state since we are not removing it from the
3339 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003340 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003341 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3342 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003344 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3346 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003347 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003348 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003349 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003350 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003351 }
3352 } else {
3353 // remove this record from the history.
3354 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003355 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003356 removedFromHistory = true;
3357 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003358 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003359 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003360 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003361 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003362 }
3363 }
3364
3365 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003366
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 if (!mLRUActivities.remove(r) && hadApp) {
3368 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3369 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003371 return removedFromHistory;
3372 }
3373
Craig Mautner299f9602015-01-26 09:47:33 -08003374 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003375 final long origId = Binder.clearCallingIdentity();
3376 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003377 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003378 if (r != null) {
3379 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003380 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003381 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003382
Wale Ogunwale60454db2015-01-23 16:05:07 -08003383 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003384 if (r.state == ActivityState.DESTROYING) {
3385 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003386 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003387 }
3388 }
Craig Mautner05d29032013-05-03 13:40:13 -07003389 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003390 } finally {
3391 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003392 }
3393 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003394
Todd Kennedyaab56db2015-01-30 09:39:53 -08003395 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003396 if (hasVisibleBehindActivity() &&
3397 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003398 if (r == topRunningActivityLocked(null)) {
3399 // Don't release the top activity if it has requested to run behind the next
3400 // activity.
3401 return;
3402 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003403 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3404 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003405 " thread=" + r.app.thread);
3406 if (r != null && r.app != null && r.app.thread != null) {
3407 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003408 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003409 } catch (RemoteException e) {
3410 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003411 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003412 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003413 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003414 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003415 }
3416 }
3417 }
3418
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003419 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003420 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3421 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003422 if (r != null) {
3423 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003424 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003425 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003426 }
3427 mStackSupervisor.resumeTopActivitiesLocked();
3428 }
3429
Jose Lima4b6c6692014-08-12 17:41:12 -07003430 boolean hasVisibleBehindActivity() {
3431 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003432 }
3433
Jose Lima4b6c6692014-08-12 17:41:12 -07003434 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003435 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003436 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003437 }
3438 }
3439
Jose Lima4b6c6692014-08-12 17:41:12 -07003440 ActivityRecord getVisibleBehindActivity() {
3441 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003442 }
3443
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003444 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3445 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003446 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003447 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3448 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003449 while (i > 0) {
3450 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003451 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003452 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003453 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003454 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003455 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003456 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 }
3458 }
3459 }
3460
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003461 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3462 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003463 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3464 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003465 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3466 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003467 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003468 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003469 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3470 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003471
3472 boolean hasVisibleActivities = false;
3473
3474 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003475 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003476 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3477 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003478 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3479 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3480 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3481 final ActivityRecord r = activities.get(activityNdx);
3482 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003483 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3484 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003485 if (r.app == app) {
Craig Mautneracebdc82015-02-24 10:53:03 -08003486 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003487 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3488 // Don't currently have state for the activity, or
3489 // it is finishing -- always remove it.
3490 remove = true;
3491 } else if (r.launchCount > 2 &&
3492 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3493 // We have launched this activity too many times since it was
3494 // able to run, so give up and remove it.
3495 remove = true;
3496 } else {
3497 // The process may be gone, but the activity lives on!
3498 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003499 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003500 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003501 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003502 RuntimeException here = new RuntimeException("here");
3503 here.fillInStackTrace();
3504 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3505 + ": haveState=" + r.haveState
3506 + " stateNotNeeded=" + r.stateNotNeeded
3507 + " finishing=" + r.finishing
3508 + " state=" + r.state, here);
3509 }
3510 if (!r.finishing) {
3511 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3512 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3513 r.userId, System.identityHashCode(r),
3514 r.task.taskId, r.shortComponentName,
3515 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003516 if (r.state == ActivityState.RESUMED) {
3517 mService.updateUsageStats(r, false);
3518 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003519 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003520 } else {
3521 // We have the current state for this activity, so
3522 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003523 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003524 TAG, "Keeping entry, setting app to null");
3525 if (r.visible) {
3526 hasVisibleActivities = true;
3527 }
3528 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3529 + r);
3530 r.app = null;
3531 r.nowVisible = false;
3532 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003533 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003534 "App died, clearing saved state of " + r);
3535 r.icicle = null;
3536 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003537 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003538 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003539 if (remove) {
3540 removeActivityFromHistoryLocked(r, "appDied");
3541 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003542 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003543 }
3544 }
3545
3546 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003547 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003548
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003549 final void updateTransitLocked(int transit, Bundle options) {
3550 if (options != null) {
3551 ActivityRecord r = topRunningActivityLocked(null);
3552 if (r != null && r.state != ActivityState.RESUMED) {
3553 r.updateOptionsLocked(options);
3554 } else {
3555 ActivityOptions.abort(options);
3556 }
3557 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003558 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003559 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003560
Craig Mautner21d24a22014-04-23 11:45:37 -07003561 void updateTaskMovement(TaskRecord task, boolean toFront) {
3562 if (task.isPersistable) {
3563 task.mLastTimeMoved = System.currentTimeMillis();
3564 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3565 // recently will be most negative, tasks sent to the bottom before that will be less
3566 // negative. Similarly for recent tasks moved to the top which will be most positive.
3567 if (!toFront) {
3568 task.mLastTimeMoved *= -1;
3569 }
3570 }
3571 }
3572
Craig Mautner84984fa2014-06-19 11:19:20 -07003573 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003574 final int top = mTaskHistory.size() - 1;
3575 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3576 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003577 if (task.taskType == homeStackTaskType) {
3578 if (DEBUG_TASKS || DEBUG_STACK)
3579 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003580 mTaskHistory.remove(taskNdx);
3581 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003582 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003583 return;
3584 }
3585 }
3586 }
3587
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003588 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003589 String reason) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003590 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003591
Craig Mautner11bf9a52013-02-19 14:08:51 -08003592 final int numTasks = mTaskHistory.size();
3593 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003594 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003595 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003596 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003597 ActivityOptions.abort(options);
3598 } else {
3599 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3600 }
3601 return;
3602 }
3603
3604 // Shift all activities with this task up to the top
3605 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003606 insertTaskAtTop(tr);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003607
3608 // Set focus to the top running activity of this stack.
3609 ActivityRecord r = topRunningActivityLocked(null);
3610 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003611
3612 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003613 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003614 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 if (r != null) {
3616 mNoAnimActivities.add(r);
3617 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003618 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003620 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003621 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003622
Craig Mautner05d29032013-05-03 13:40:13 -07003623 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003624 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003625
3626 if (VALIDATE_TOKENS) {
3627 validateAppTokensLocked();
3628 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003629 }
3630
3631 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003632 * Worker method for rearranging history stack. Implements the function of moving all
3633 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003634 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003635 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3637 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003638 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003639 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640 * @return Returns true if the move completed, false if not.
3641 */
Craig Mautner299f9602015-01-26 09:47:33 -08003642 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003643 final TaskRecord tr = taskForIdLocked(taskId);
3644 if (tr == null) {
3645 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3646 return false;
3647 }
3648
3649 Slog.i(TAG, "moveTaskToBack: " + tr);
3650
3651 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003652
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003653 // If we have a watcher, preflight the move before committing to it. First check
3654 // for *other* available tasks, but if none are available, then try again allowing the
3655 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003656 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003657 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658 if (next == null) {
3659 next = topRunningActivityLocked(null, 0);
3660 }
3661 if (next != null) {
3662 // ask watcher if this is allowed
3663 boolean moveOK = true;
3664 try {
3665 moveOK = mService.mController.activityResuming(next.packageName);
3666 } catch (RemoteException e) {
3667 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003668 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003669 }
3670 if (!moveOK) {
3671 return false;
3672 }
3673 }
3674 }
3675
Wale Ogunwalecb82f302015-02-25 07:53:40 -08003676 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003677
riddle_hsuc215a4f2014-12-27 12:10:45 +08003678 boolean prevIsHome = false;
3679 if (tr.isOverHomeStack()) {
3680 final TaskRecord nextTask = getNextTask(tr);
3681 if (nextTask != null) {
3682 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3683 } else {
3684 prevIsHome = true;
3685 }
3686 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003687 mTaskHistory.remove(tr);
3688 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003689 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003690
Craig Mautnerc8143c62013-09-03 12:15:57 -07003691 // There is an assumption that moving a task to the back moves it behind the home activity.
3692 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003693 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003694 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3695 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003696 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003697 break;
3698 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003699 if (taskNdx == 1) {
3700 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003701 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003702 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003703 }
3704
Craig Mautner299f9602015-01-26 09:47:33 -08003705 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003706 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003707
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003708 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003709 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710 }
3711
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003712 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003713 if (prevIsHome || task == tr && tr.isOverHomeStack()
3714 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003715 if (!mService.mBooting && !mService.mBooted) {
3716 // Not ready yet!
3717 return false;
3718 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003719 final int taskToReturnTo = tr.getTaskToReturnTo();
3720 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003721 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003722 }
3723
Craig Mautner05d29032013-05-03 13:40:13 -07003724 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 return true;
3726 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003727
Craig Mautner8849a5e2013-04-02 16:41:03 -07003728 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003729 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003730 final Uri data = r.intent.getData();
3731 final String strData = data != null ? data.toSafeString() : null;
3732
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003734 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003735 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003736 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003737 }
3738
3739 /**
3740 * Make sure the given activity matches the current configuration. Returns
3741 * false if the activity had to be destroyed. Returns true if the
3742 * configuration is the same, or the activity will remain running as-is
3743 * for whatever reason. Ensures the HistoryRecord is updated with the
3744 * correct configuration and all other bookkeeping is handled.
3745 */
3746 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3747 int globalChanges) {
3748 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003749 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003750 "Skipping config check (will change): " + r);
3751 return true;
3752 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003753
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003754 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003756
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003757 // Make sure the current stack override configuration is supported by the top task
3758 // before continuing.
3759 final TaskRecord topTask = topTask();
3760 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3761 || (!topTask.mResizeable && !mFullscreen))) {
3762 final boolean prevFullscreen = mFullscreen;
3763 final Configuration newOverrideConfig =
3764 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3765 updateOverrideConfiguration(newOverrideConfig);
3766 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003767 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003768 "Updated stack config to support task=" + topTask
3769 + " resizeable=" + topTask.mResizeable
3770 + " mForcedFullscreen=" + mForcedFullscreen
3771 + " prevFullscreen=" + prevFullscreen
3772 + " mFullscreen=" + mFullscreen);
3773 }
3774
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003775 // Short circuit: if the two configurations are the exact same
3776 // object (the common case), then there is nothing to do.
3777 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003778 if (r.configuration == newConfig
3779 && r.stackConfigOverride == mOverrideConfig
3780 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003781 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003782 "Configuration unchanged in " + r);
3783 return true;
3784 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003785
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003786 // We don't worry about activities that are finishing.
3787 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003788 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003789 "Configuration doesn't matter in finishing " + r);
3790 r.stopFreezingScreenLocked(false);
3791 return true;
3792 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003793
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003794 // Okay we now are going to make this activity have the new config.
3795 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003796 final Configuration oldConfig = r.configuration;
3797 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003799 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003800
3801 // Determine what has changed. May be nothing, if this is a config
3802 // that has come back from the app after going idle. In that case
3803 // we just want to leave the official config object now in the
3804 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003805 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003806 if (stackChanges == 0) {
3807 // {@link Configuration#diff} doesn't catch changes from unset values.
3808 // Check for changes we care about.
3809 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3810 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3811 }
3812 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3813 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3814 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3815 }
3816 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3817 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3818 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003819 }
3820 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003821 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003822 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003823 "Configuration no differences in " + r);
3824 return true;
3825 }
3826
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003827 // If the activity isn't currently running, just leave the new
3828 // configuration and it will pick that up next time it starts.
3829 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003830 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003831 "Configuration doesn't matter not running " + r);
3832 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003833 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003834 return true;
3835 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003836
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003837 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003838 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3839 "Checking to restart " + r.info.name + ": changed=0x"
3840 + Integer.toHexString(changes) + ", handles=0x"
3841 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3842
Dianne Hackborne6676352011-06-01 16:51:20 -07003843 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003844 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3845 r.configChangeFlags |= changes;
3846 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003847 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003848 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003849 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003850 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003851 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003852 } else if (r.state == ActivityState.PAUSING) {
3853 // A little annoying: we are waiting for this activity to
3854 // finish pausing. Let's not do anything now, but just
3855 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003856 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003857 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003858 r.configDestroy = true;
3859 return true;
3860 } else if (r.state == ActivityState.RESUMED) {
3861 // Try to optimize this case: the configuration is changing
3862 // and we need to restart the top, resumed activity.
3863 // Instead of doing the normal handshaking, just say
3864 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003865 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003866 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003867 relaunchActivityLocked(r, r.configChangeFlags, true);
3868 r.configChangeFlags = 0;
3869 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003870 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003871 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003872 relaunchActivityLocked(r, r.configChangeFlags, false);
3873 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003874 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003875
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003876 // All done... tell the caller we weren't able to keep this
3877 // activity around.
3878 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003879 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003880
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003881 // Default case: the activity can handle this new configuration, so hand it over.
3882 // NOTE: We only forward the stack override configuration as the system level configuration
3883 // changes is always sent to all processes when they happen so it can just use whatever
3884 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003885 if (r.app != null && r.app.thread != null) {
3886 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003887 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003888 r.app.thread.scheduleActivityConfigurationChanged(
3889 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003890 } catch (RemoteException e) {
3891 // If process died, whatever.
3892 }
3893 }
3894 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003895
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003896 return true;
3897 }
3898
Craig Mautner2568c3a2015-03-26 14:22:34 -07003899 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003900 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003901 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 if (andResume) {
3903 results = r.results;
3904 newIntents = r.newIntents;
3905 }
3906 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3907 + " with results=" + results + " newIntents=" + newIntents
3908 + " andResume=" + andResume);
3909 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003910 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003911 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003912
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003914
Craig Mautner34b73df2014-01-12 21:11:08 -08003915 mStackSupervisor.removeChildActivityContainers(r);
3916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003917 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003918 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3919 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3920 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003921 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003922 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3923 !andResume, new Configuration(mService.mConfiguration),
3924 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003925 // Note: don't need to call pauseIfSleepingLocked() here, because
3926 // the caller will only pass in 'andResume' if this activity is
3927 // currently resumed, which implies we aren't sleeping.
3928 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003929 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 }
3931
3932 if (andResume) {
3933 r.results = null;
3934 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003935 r.state = ActivityState.RESUMED;
3936 } else {
3937 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3938 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003939 }
3940
3941 return true;
3942 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003943
3944 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003945 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3946 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3947 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3948 final ActivityRecord r = activities.get(activityNdx);
3949 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003950 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003951 }
3952 if (r.fullscreen && !r.finishing) {
3953 return false;
3954 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003955 }
3956 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003957 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003958 if (r == null) {
3959 return false;
3960 }
3961 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3962 + " would have returned true for r=" + r);
3963 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003964 }
3965
3966 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003967 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3968 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3969 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3970 final ActivityRecord r = activities.get(activityNdx);
3971 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003972 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003973 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003974 }
3975 }
3976 }
3977
3978 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3979 boolean didSomething = false;
3980 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003981 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003982 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3983 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3984 int numActivities = activities.size();
3985 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3986 ActivityRecord r = activities.get(activityNdx);
3987 final boolean samePackage = r.packageName.equals(name)
3988 || (name == null && r.userId == userId);
3989 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3990 && (samePackage || r.task == lastTask)
3991 && (r.app == null || evenPersistent || !r.app.persistent)) {
3992 if (!doit) {
3993 if (r.finishing) {
3994 // If this activity is just finishing, then it is not
3995 // interesting as far as something to stop.
3996 continue;
3997 }
3998 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003999 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004000 if (r.isHomeActivity()) {
4001 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4002 Slog.i(TAG, "Skip force-stop again " + r);
4003 continue;
4004 } else {
4005 homeActivity = r.realActivity;
4006 }
4007 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004008 didSomething = true;
4009 Slog.i(TAG, " Force finishing activity " + r);
4010 if (samePackage) {
4011 if (r.app != null) {
4012 r.app.removed = true;
4013 }
4014 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004015 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004016 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004017 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4018 true)) {
4019 // r has been deleted from mActivities, accommodate.
4020 --numActivities;
4021 --activityNdx;
4022 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004023 }
4024 }
4025 }
4026 return didSomething;
4027 }
4028
Dianne Hackborn09233282014-04-30 11:33:59 -07004029 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004030 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004031 final TaskRecord task = mTaskHistory.get(taskNdx);
4032 ActivityRecord r = null;
4033 ActivityRecord top = null;
4034 int numActivities = 0;
4035 int numRunning = 0;
4036 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07004037 if (activities.isEmpty()) {
4038 continue;
4039 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004040 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004041 continue;
4042 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004043 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4044 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004045
Craig Mautneraab647e2013-02-28 16:31:36 -08004046 // Initialize state for next task if needed.
4047 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4048 top = r;
4049 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004050 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004051
4052 // Add 'r' into the current task.
4053 numActivities++;
4054 if (r.app != null && r.app.thread != null) {
4055 numRunning++;
4056 }
4057
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004058 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004059 TAG, r.intent.getComponent().flattenToShortString()
4060 + ": task=" + r.task);
4061 }
4062
4063 RunningTaskInfo ci = new RunningTaskInfo();
4064 ci.id = task.taskId;
4065 ci.baseActivity = r.intent.getComponent();
4066 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004067 ci.lastActiveTime = task.lastActiveTime;
4068
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004069 if (top.task != null) {
4070 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004071 }
4072 ci.numActivities = numActivities;
4073 ci.numRunning = numRunning;
4074 //System.out.println(
4075 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08004076 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004077 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004078 }
4079
4080 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004081 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004082 if (DEBUG_SWITCH) Slog.d(
4083 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004084 if (top >= 0) {
4085 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4086 int activityTop = activities.size() - 1;
4087 if (activityTop > 0) {
4088 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4089 "unhandled-back", true);
4090 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004091 }
4092 }
4093
Craig Mautner6b74cb52013-09-27 17:02:21 -07004094 /**
4095 * Reset local parameters because an app's activity died.
4096 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004097 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004098 */
4099 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004100 if (mPausingActivity != null && mPausingActivity.app == app) {
4101 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
4102 "App died while pausing: " + mPausingActivity);
4103 mPausingActivity = null;
4104 }
4105 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4106 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004107 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004108 }
4109
Craig Mautner19091252013-10-05 00:03:53 -07004110 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004111 }
4112
Craig Mautnercae015f2013-02-08 14:31:27 -08004113 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004114 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4115 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4116 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4117 final ActivityRecord r = activities.get(activityNdx);
4118 if (r.app == app) {
4119 Slog.w(TAG, " Force finishing activity "
4120 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004121 // Force the destroy to skip right to removal.
4122 r.app = null;
4123 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004124 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004125 }
4126 }
4127 }
4128
Dianne Hackborn390517b2013-05-30 15:03:32 -07004129 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004130 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004131 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004132 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4133 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004134 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4135 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004136 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004137 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004138 if (printed) {
4139 header = null;
4140 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004141 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004142 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004143 }
4144
4145 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4146 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4147
4148 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004149 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4150 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004151 }
4152 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004153 final int top = mTaskHistory.size() - 1;
4154 if (top >= 0) {
4155 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4156 int listTop = list.size() - 1;
4157 if (listTop >= 0) {
4158 activities.add(list.get(listTop));
4159 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004160 }
4161 } else {
4162 ItemMatcher matcher = new ItemMatcher();
4163 matcher.build(name);
4164
Craig Mautneraab647e2013-02-28 16:31:36 -08004165 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4166 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4167 if (matcher.match(r1, r1.intent.getComponent())) {
4168 activities.add(r1);
4169 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004170 }
4171 }
4172 }
4173
4174 return activities;
4175 }
4176
4177 ActivityRecord restartPackage(String packageName) {
4178 ActivityRecord starting = topRunningActivityLocked(null);
4179
4180 // All activities that came from the package must be
4181 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004182 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4183 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4184 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4185 final ActivityRecord a = activities.get(activityNdx);
4186 if (a.info.packageName.equals(packageName)) {
4187 a.forceNewConfig = true;
4188 if (starting != null && a == starting && a.visible) {
4189 a.startFreezingScreenLocked(starting.app,
4190 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4191 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004192 }
4193 }
4194 }
4195
4196 return starting;
4197 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004198
Craig Mautner299f9602015-01-26 09:47:33 -08004199 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004200 removeTask(task, reason, true);
4201 }
4202
4203 void removeTask(TaskRecord task, String reason, boolean removeFromWindowManager) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004204 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004205 if (removeFromWindowManager) {
4206 mWindowManager.removeTask(task.taskId);
4207 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004208 final ActivityRecord r = mResumedActivity;
4209 if (r != null && r.task == task) {
4210 mResumedActivity = null;
4211 }
4212
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004213 final int taskNdx = mTaskHistory.indexOf(task);
4214 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004215 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4216 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4217 if (!nextTask.isOverHomeStack()) {
4218 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4219 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004220 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004221 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004222 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004223
4224 if (task.mActivities.isEmpty()) {
4225 final boolean isVoiceSession = task.voiceSession != null;
4226 if (isVoiceSession) {
4227 try {
4228 task.voiceSession.taskFinished(task.intent, task.taskId);
4229 } catch (RemoteException e) {
4230 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004231 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004232 if (task.autoRemoveFromRecents() || isVoiceSession) {
4233 // Task creator asked to remove this when done, or this task was a voice
4234 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004235 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004236 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004237 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004238 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004239
4240 if (mTaskHistory.isEmpty()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004241 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004242 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004243 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004244 String myReason = reason + " leftTaskHistoryEmpty";
4245 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4246 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4247 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004248 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004249 if (mStacks != null) {
4250 mStacks.remove(this);
4251 mStacks.add(0, this);
4252 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004253 if (notHomeStack) {
4254 mActivityContainer.onTaskListEmptyLocked();
4255 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004256 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004257
4258 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004259 }
4260
Dianne Hackborn91097de2014-04-04 18:02:06 -07004261 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4262 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4263 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004264 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4265 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004266 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004267 return task;
4268 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004269
4270 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004271 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004272 }
4273
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004274 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004275 task.stack = this;
4276 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004277 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004278 } else {
4279 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004280 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004281 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004282 if (!moving && task.voiceSession != null) {
4283 try {
4284 task.voiceSession.taskStarted(task.intent, task.taskId);
4285 } catch (RemoteException e) {
4286 }
4287 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004288 }
4289
4290 public int getStackId() {
4291 return mStackId;
4292 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004293
4294 @Override
4295 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004296 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4297 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004298 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004299
4300 boolean updateOverrideConfiguration(Configuration newConfig) {
4301 Configuration oldConfig = mOverrideConfig;
4302 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004303 // We override the configuration only when the stack's dimensions are different from
4304 // the display. In this manner, we know that if the override configuration is empty,
4305 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004306 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004307 return !mOverrideConfig.equals(oldConfig);
4308 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004309}