blob: a47e7c622891c70ec2284b10c58b08b10a491357 [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 Ogunwale6dfdfd62015-04-15 12:01:38 -070019import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070020
Wale Ogunwalee23149f2015-03-06 15:39:44 -080021import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070022
Craig Mautner84984fa2014-06-19 11:19:20 -070023import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
24import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070025
Craig Mautnerde4ef022013-04-07 19:01:33 -070026import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
27
Dianne Hackborn89ad4562014-08-24 16:45:38 -070028import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070029import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080030import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070031import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070032import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080033import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080034import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080035import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080036import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070037import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038
39import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070040import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070041import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080043import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080045import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080051import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080052import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070054import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080055import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.os.Handler;
57import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090058import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070060import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.os.RemoteException;
62import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080063import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070064import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070065import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070068import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069
Craig Mautnercae015f2013-02-08 14:31:27 -080070import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080071import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import java.lang.ref.WeakReference;
73import java.util.ArrayList;
74import java.util.Iterator;
75import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080076import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070077import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078
79/**
80 * State and management of a single stack of activities.
81 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070082final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080083
Wale Ogunwalee23149f2015-03-06 15:39:44 -080084 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070085 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
86 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080087 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070088 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070089 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070090 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070091 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070092 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070093 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
94 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070095 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070096 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070097 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
98 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
99 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
100 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
101 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700102
103 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800104
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700105 // Ticks during which we check progress while waiting for an app to launch.
106 static final int LAUNCH_TICK = 500;
107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long we wait until giving up on the last activity to pause. This
109 // is short because it directly impacts the responsiveness of starting the
110 // next activity.
111 static final int PAUSE_TIMEOUT = 500;
112
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700113 // How long we wait for the activity to tell us it has stopped before
114 // giving up. This is a good amount of time because we really need this
115 // from the application in order to get its saved state.
116 static final int STOP_TIMEOUT = 10*1000;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we wait until giving up on an activity telling us it has
119 // finished destroying itself.
120 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800121
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700122 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800123 // disabled.
124 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800125
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700126 // How long between activity launches that we consider safe to not warn
127 // the user about an unexpected activity being launched on top.
128 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800129
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700130 // Set to false to disable the preview that is shown while a new activity
131 // is being started.
132 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800133
Craig Mautner5eda9b32013-07-02 11:58:16 -0700134 // How long to wait for all background Activities to redraw following a call to
135 // convertToTranslucent().
136 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138 enum ActivityState {
139 INITIALIZING,
140 RESUMED,
141 PAUSING,
142 PAUSED,
143 STOPPING,
144 STOPPED,
145 FINISHING,
146 DESTROYING,
147 DESTROYED
148 }
149
150 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700151 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800152 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800153
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700154 /**
155 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800156 * running) activities. It contains #TaskRecord objects.
157 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800158 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800159
160 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800161 * Used for validating app tokens with window manager.
162 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800163 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800164
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165 /**
166 * List of running activities, sorted by recent usage.
167 * The first entry in the list is the least recently used.
168 * It contains HistoryRecord objects.
169 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800170 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173 * Animations that for the current transition have requested not to
174 * be considered for the transition animation.
175 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800176 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177
178 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179 * When we are in the process of pausing an activity, before starting the
180 * next one, this variable holds the activity that is currently being paused.
181 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800182 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183
184 /**
185 * This is the last activity that we put into the paused state. This is
186 * used to determine if we need to do an activity transition while sleeping,
187 * when we normally hold the top activity paused.
188 */
189 ActivityRecord mLastPausedActivity = null;
190
191 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700192 * Activities that specify No History must be removed once the user navigates away from them.
193 * If the device goes to sleep with such an activity in the paused state then we save it here
194 * and finish it later if another activity replaces it on wakeup.
195 */
196 ActivityRecord mLastNoHistoryActivity = null;
197
198 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199 * Current activity that is resumed, or null if there is none.
200 */
201 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700204 * This is the last activity that has been started. It is only used to
205 * identify when multiple activities are started at once so that the user
206 * can be warned they may not be in the activity they think they are.
207 */
208 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800209
Craig Mautner5eda9b32013-07-02 11:58:16 -0700210 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
211 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
212 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
213 // Activity in mTranslucentActivityWaiting is notified via
214 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
215 // background activity being drawn then the same call will be made with a true value.
216 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700217 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700218 new ArrayList<ActivityRecord>();
219
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700220 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700221 * Set when we know we are going to be calling updateConfiguration()
222 * soon, so want to skip intermediate config checks.
223 */
224 boolean mConfigWillChange;
225
Todd Kennedyaab56db2015-01-30 09:39:53 -0800226 // Whether or not this stack covers the entire screen; by default stacks are full screen
227 boolean mFullscreen = true;
228
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700229 long mLaunchStartTime = 0;
230 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800231
Craig Mautner858d8a62013-04-23 17:08:34 -0700232 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700233
Craig Mautnerc00204b2013-03-05 15:02:14 -0800234 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800236 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
237 ArrayList<ActivityStack> mStacks;
238 /** The attached Display's unique identifier, or -1 if detached */
239 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240
Craig Mautner27084302013-03-25 08:05:25 -0700241 /** Run all ActivityStacks through this */
242 final ActivityStackSupervisor mStackSupervisor;
243
Wale Ogunwale60454db2015-01-23 16:05:07 -0800244 Configuration mOverrideConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800245 /** True if the stack was forced to full screen because {@link TaskRecord#mResizeable} is false
246 * and the stack was previously resized. */
247 private boolean mForcedFullscreen = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800248
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700249 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700250 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
251 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
252 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
253 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700254 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700255 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700256 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257
258 static class ScheduleDestroyArgs {
259 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700261 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700262 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700263 mReason = reason;
264 }
265 }
266
Zoran Marcetaf958b322012-08-09 20:27:12 +0900267 final Handler mHandler;
268
269 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800270
Craig Mautnerc8143c62013-09-03 12:15:57 -0700271 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900272 super(looper);
273 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274
Zoran Marcetaf958b322012-08-09 20:27:12 +0900275 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276 public void handleMessage(Message msg) {
277 switch (msg.what) {
278 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280 // We don't at this point know if the activity is fullscreen,
281 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800282 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 synchronized (mService) {
284 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700285 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700286 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700287 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800288 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700290 case LAUNCH_TICK_MSG: {
291 ActivityRecord r = (ActivityRecord)msg.obj;
292 synchronized (mService) {
293 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700294 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700295 }
296 }
297 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800299 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 // We don't at this point know if the activity is fullscreen,
301 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800302 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800303 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800304 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800305 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700307 case STOP_TIMEOUT_MSG: {
308 ActivityRecord r = (ActivityRecord)msg.obj;
309 // We don't at this point know if the activity is fullscreen,
310 // so we need to be conservative and assume it isn't.
311 Slog.w(TAG, "Activity stop timeout for " + r);
312 synchronized (mService) {
313 if (r.isInHistory()) {
314 activityStoppedLocked(r, null, null, null);
315 }
316 }
317 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700318 case DESTROY_ACTIVITIES_MSG: {
319 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
320 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700321 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700322 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700323 } break;
324 case TRANSLUCENT_TIMEOUT_MSG: {
325 synchronized (mService) {
326 notifyActivityDrawnLocked(null);
327 }
328 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700329 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700330 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700331 final ActivityRecord r = getVisibleBehindActivity();
332 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700333 if (r != null) {
334 mService.killAppAtUsersRequest(r.app, null);
335 }
336 }
337 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700338 }
339 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800340 }
341
Craig Mautner34b73df2014-01-12 21:11:08 -0800342 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800343 int count = 0;
344 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
345 count += mTaskHistory.get(taskNdx).mActivities.size();
346 }
347 return count;
348 }
349
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800350 int numTasks() {
351 return mTaskHistory.size();
352 }
353
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800354 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
355 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 mActivityContainer = activityContainer;
357 mStackSupervisor = activityContainer.getOuter();
358 mService = mStackSupervisor.mService;
359 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
360 mWindowManager = mService.mWindowManager;
361 mStackId = activityContainer.mStackId;
362 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800363 mRecentTasks = recentTasks;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800364 mOverrideConfig = Configuration.EMPTY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 }
Craig Mautner5962b122012-10-05 14:45:52 -0700366
Amith Yamasani734983f2014-03-04 16:48:05 -0800367 boolean okToShowLocked(ActivityRecord r) {
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700368 return mStackSupervisor.isCurrentProfileLocked(r.userId)
369 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0;
Craig Mautner5962b122012-10-05 14:45:52 -0700370 }
371
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800373 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700374 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
375 if (r != null) {
376 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800377 }
378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700379 return null;
380 }
381
382 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
384 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800385 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800386 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
387 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800388 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800389 return r;
390 }
391 }
392 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 return null;
394 }
395
396 /**
397 * This is a simplified version of topRunningActivityLocked that provides a number of
398 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800399 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 * @param token If non-null, any history records matching this token will be skipped.
401 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800402 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700403 * @return Returns the HistoryRecord of the next activity on the stack.
404 */
405 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800406 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
407 TaskRecord task = mTaskHistory.get(taskNdx);
408 if (task.taskId == taskId) {
409 continue;
410 }
411 ArrayList<ActivityRecord> activities = task.mActivities;
412 for (int i = activities.size() - 1; i >= 0; --i) {
413 final ActivityRecord r = activities.get(i);
414 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800415 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800416 return r;
417 }
418 }
419 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700420 return null;
421 }
422
Craig Mautner8849a5e2013-04-02 16:41:03 -0700423 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
425 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700426 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
427 final ActivityRecord r = activities.get(activityNdx);
428 if (!r.finishing) {
429 return r;
430 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700431 }
432 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700433 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700434 }
435
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700436 final TaskRecord topTask() {
437 final int size = mTaskHistory.size();
438 if (size > 0) {
439 return mTaskHistory.get(size - 1);
440 }
441 return null;
442 }
443
Craig Mautnerd2328952013-03-05 12:46:26 -0800444 TaskRecord taskForIdLocked(int id) {
445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 final TaskRecord task = mTaskHistory.get(taskNdx);
447 if (task.taskId == id) {
448 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800449 }
450 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700451 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700452 }
453
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700455 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800456 return isInStackLocked(r);
457 }
458
459 ActivityRecord isInStackLocked(ActivityRecord r) {
460 if (r == null) {
461 return null;
462 }
463 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700464 if (task != null && task.stack != null
465 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800466 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800467 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800468 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800469 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800470 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800471 }
472
Craig Mautner2420ead2013-04-01 17:13:20 -0700473 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700474 final boolean hadit = mLRUActivities.remove(r);
475 mLRUActivities.add(r);
476 return hadit;
477 }
478
Craig Mautnerde4ef022013-04-07 19:01:33 -0700479 final boolean isHomeStack() {
480 return mStackId == HOME_STACK_ID;
481 }
482
Craig Mautnere0a38842013-12-16 16:14:02 -0800483 final boolean isOnHomeDisplay() {
484 return isAttached() &&
485 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
486 }
487
Craig Mautner299f9602015-01-26 09:47:33 -0800488 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800489 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800490 final boolean homeStack = isHomeStack()
491 || (mActivityContainer.mParentActivity != null
492 && mActivityContainer.mParentActivity.isHomeActivity());
493 ActivityStack lastFocusStack = null;
494 if (!homeStack) {
495 // Need to move this stack to the front before calling
496 // {@link ActivityStackSupervisor#moveHomeStack} below.
497 lastFocusStack = mStacks.get(mStacks.size() - 1);
498 mStacks.remove(this);
499 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800500 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700501 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
502 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800503 if (isOnHomeDisplay()) {
504 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
505 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800506 final TaskRecord task = topTask();
507 if (task != null) {
508 mWindowManager.moveTaskToTop(task.taskId);
509 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800511 }
512
513 final boolean isAttached() {
514 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800515 }
516
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700517 /**
518 * Returns the top activity in any existing task matching the given
519 * Intent. Returns null if no such task is found.
520 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521 ActivityRecord findTaskLocked(ActivityRecord target) {
522 Intent intent = target.intent;
523 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700524 ComponentName cls = intent.getComponent();
525 if (info.targetActivity != null) {
526 cls = new ComponentName(info.packageName, info.targetActivity);
527 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700528 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700529 boolean isDocument = intent != null & intent.isDocument();
530 // If documentData is non-null then it must match the existing task data.
531 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800532
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700533 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800534 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
535 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700536 if (task.voiceSession != null) {
537 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700538 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700539 continue;
540 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700541 if (task.userId != userId) {
542 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700543 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700544 continue;
545 }
Craig Mautner000f0022013-02-26 15:04:29 -0800546 final ActivityRecord r = task.getTopActivity();
547 if (r == null || r.finishing || r.userId != userId ||
548 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700549 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800550 continue;
551 }
552
Craig Mautnerd00f4742014-03-12 14:17:26 -0700553 final Intent taskIntent = task.intent;
554 final Intent affinityIntent = task.affinityIntent;
555 final boolean taskIsDocument;
556 final Uri taskDocumentData;
557 if (taskIntent != null && taskIntent.isDocument()) {
558 taskIsDocument = true;
559 taskDocumentData = taskIntent.getData();
560 } else if (affinityIntent != null && affinityIntent.isDocument()) {
561 taskIsDocument = true;
562 taskDocumentData = affinityIntent.getData();
563 } else {
564 taskIsDocument = false;
565 taskDocumentData = null;
566 }
567
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700568 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700569 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700570 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700571 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700572 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
573 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 return r;
576 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700577 } else if (taskIntent != null && taskIntent.getComponent() != null &&
578 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700579 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700580 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800581 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700582 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
583 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800584 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700585 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
586 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700587 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700588 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800589 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
591 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800592 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700593 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700594 }
595
596 return null;
597 }
598
599 /**
600 * Returns the first activity (starting from the top of the stack) that
601 * is the same as the given activity. Returns null if no such activity
602 * is found.
603 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700604 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 ComponentName cls = intent.getComponent();
606 if (info.targetActivity != null) {
607 cls = new ComponentName(info.packageName, info.targetActivity);
608 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700609 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700610
Craig Mautner000f0022013-02-26 15:04:29 -0800611 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700612 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700613 final boolean notCurrentUserTask =
614 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700615 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700616
Craig Mautner000f0022013-02-26 15:04:29 -0800617 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
618 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700619 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700620 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700621 }
Craig Mautner000f0022013-02-26 15:04:29 -0800622 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700623 return r;
624 }
625 }
626 }
627
628 return null;
629 }
630
Amith Yamasani742a6712011-05-04 14:49:28 -0700631 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700632 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700633 */
Craig Mautner93529a42013-10-04 15:03:13 -0700634 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800635 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700636 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800637 }
638 mCurrentUser = userId;
639
640 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800641 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800642 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700643 final TaskRecord task = mTaskHistory.get(i);
644
645 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
646 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700647 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700648 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700649 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700650 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800651 mTaskHistory.remove(i);
652 mTaskHistory.add(task);
653 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800654 // Use same value for i.
655 } else {
656 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800657 }
658 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700659 if (VALIDATE_TOKENS) {
660 validateAppTokensLocked();
661 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700662 }
663
Craig Mautner2420ead2013-04-01 17:13:20 -0700664 void minimalResumeActivityLocked(ActivityRecord r) {
665 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700666 if (DEBUG_STATES) Slog.v(TAG_STATES,
667 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700668 r.stopped = false;
669 mResumedActivity = r;
670 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800671 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700672 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700673 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700674 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700675 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
676 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
678
Narayan Kamath7829c812015-06-08 17:39:43 +0100679 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700680 if (mFullyDrawnStartTime != 0) {
681 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
682 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100683 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700684 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
685 }
686
687 private void stopFullyDrawnTraceIfNeeded() {
688 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
689 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
690 mFullyDrawnStartTime = 0;
691 }
692 }
693
Craig Mautnere79d42682013-04-01 19:01:53 -0700694 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700695 if (r.displayStartTime == 0) {
696 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
697 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100698 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700699 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700700 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700701 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100702 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700703 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700704 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700705 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800706
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700707 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700708 // Make sure that there is no activity waiting for this to launch.
709 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
710 r.displayStartTime = r.fullyDrawnStartTime = 0;
711 } else {
712 mStackSupervisor.removeTimeoutsForActivityLocked(r);
713 mStackSupervisor.scheduleIdleTimeoutLocked(r);
714 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700715 }
716
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800717 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800718 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800719 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
720 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
721 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
722 activities.get(activityNdx).setSleeping(false);
723 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800724 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800725 if (mPausingActivity != null) {
726 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
727 activityPausedLocked(mPausingActivity.appToken, true);
728 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800729 }
730
Craig Mautner0eea92c2013-05-16 13:35:39 -0700731 /**
732 * @return true if something must be done before going to sleep.
733 */
734 boolean checkReadyForSleepLocked() {
735 if (mResumedActivity != null) {
736 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700737 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
738 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
739 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700740 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700741 return true;
742 }
743 if (mPausingActivity != null) {
744 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700745 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700746 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800747 }
748
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700749 if (hasVisibleBehindActivity()) {
750 // Stop visible behind activity before going to sleep.
751 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
752 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700753 if (DEBUG_STATES) Slog.v(TAG_STATES,
754 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700755 return true;
756 }
757
Craig Mautner0eea92c2013-05-16 13:35:39 -0700758 return false;
759 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800760
Craig Mautner0eea92c2013-05-16 13:35:39 -0700761 void goToSleep() {
762 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800763
Craig Mautner0eea92c2013-05-16 13:35:39 -0700764 // Make sure any stopped but visible activities are now sleeping.
765 // This ensures that the activity's onStop() is called.
766 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
767 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
768 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
769 final ActivityRecord r = activities.get(activityNdx);
770 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
771 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800772 }
773 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800774 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700775 }
Craig Mautner59c00972012-07-30 12:10:24 -0700776
Dianne Hackbornd2835932010-12-13 16:28:46 -0800777 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700778 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800779 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700780 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800781 return null;
782 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800783
Winson Chung083baf92014-07-11 10:32:42 -0700784 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700785 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800786 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700787 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700788 return null;
789 }
790
Winson Chung48a10a52014-08-27 14:36:51 -0700791 int w = mService.mThumbnailWidth;
792 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800793 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700794 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800795 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700796 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800797 }
Winson Chung376543b2014-05-21 17:43:28 -0700798 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800799 return null;
800 }
801
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700802 /**
803 * Start pausing the currently resumed activity. It is an error to call this if there
804 * is already an activity being paused or there is no resumed activity.
805 *
806 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
807 * @param uiSleeping True if this is happening with the user interface going to sleep (the
808 * screen turning off).
809 * @param resuming True if this is being called as part of resuming the top activity, so
810 * we shouldn't try to instigate a resume here.
811 * @param dontWait True if the caller does not want to wait for the pause to complete. If
812 * set to true, we will immediately complete the pause here before returning.
813 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
814 * it to tell us when it is done.
815 */
816 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
817 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800818 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800819 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
820 + " state=" + mPausingActivity.state);
821 if (!mService.isSleeping()) {
822 // Avoid recursion among check for sleep and complete pause during sleeping.
823 // Because activity will be paused immediately after resume, just let pause
824 // be completed by the order of activity paused from clients.
825 completePauseLocked(false);
826 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800827 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828 ActivityRecord prev = mResumedActivity;
829 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700830 if (!resuming) {
831 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
832 mStackSupervisor.resumeTopActivitiesLocked();
833 }
834 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800836
837 if (mActivityContainer.mParentActivity == null) {
838 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700839 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800840 }
841
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700842 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700843 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800845 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700847 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
848 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 prev.state = ActivityState.PAUSING;
850 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700851 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700852 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800853 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800854 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700855 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700856 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857
858 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700861 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 try {
863 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700864 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700865 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700866 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800867 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700868 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 } catch (Exception e) {
870 // Ignore exception, if process died other code will cleanup.
871 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800872 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700873 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700874 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 }
876 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700878 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700879 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 }
881
882 // If we are not going to sleep, we want to ensure the device is
883 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700884 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700885 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886 }
887
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700889 // Have the window manager pause its key dispatching until the new
890 // activity has started. If we're pausing the activity just because
891 // the screen is being turned off and the UI is sleeping, don't interrupt
892 // key dispatch; the same activity will pick it up again on wakeup.
893 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800894 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700895 } else if (DEBUG_PAUSE) {
896 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700897 }
898
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700899 if (dontWait) {
900 // If the caller said they don't want to wait for the pause, then complete
901 // the pause now.
902 completePauseLocked(false);
903 return false;
904
905 } else {
906 // Schedule a pause timeout in case the app doesn't respond.
907 // We don't give it much time because this directly impacts the
908 // responsiveness seen by the user.
909 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
910 msg.obj = prev;
911 prev.pauseTime = SystemClock.uptimeMillis();
912 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700914 return true;
915 }
916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 } else {
918 // This activity failed to schedule the
919 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700920 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 if (!resuming) {
922 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
923 }
924 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 }
926 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700927
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700928 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700929 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
930 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931
Craig Mautnerd2328952013-03-05 12:46:26 -0800932 final ActivityRecord r = isInStackLocked(token);
933 if (r != null) {
934 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
935 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700936 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -0800937 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700938 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800939 } else {
940 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
941 r.userId, System.identityHashCode(r), r.shortComponentName,
942 mPausingActivity != null
943 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +0800944 if (r.state == ActivityState.PAUSING) {
945 r.state = ActivityState.PAUSED;
946 if (r.finishing) {
947 if (DEBUG_PAUSE) Slog.v(TAG,
948 "Executing finish of failed to pause activity: " + r);
949 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
950 }
louis_chang047dfd42015-04-08 16:35:55 +0800951 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952 }
953 }
954 }
955
Craig Mautnera0026042014-04-23 11:45:37 -0700956 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
957 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700958 if (r.state != ActivityState.STOPPING) {
959 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
960 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
961 return;
962 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700963 if (persistentState != null) {
964 r.persistentState = persistentState;
965 mService.notifyTaskPersisterLocked(r.task, false);
966 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700967 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700968 if (icicle != null) {
969 // If icicle is null, this is happening due to a timeout, so we
970 // haven't really saved the state.
971 r.icicle = icicle;
972 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800973 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800974 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700975 }
976 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700977 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700978 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
979 r.stopped = true;
980 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700981 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
982 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700983 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700984 if (r.finishing) {
985 r.clearOptionsLocked();
986 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700987 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700988 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700989 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700990 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700991 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800992 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700993 }
994 }
995 }
996
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700997 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800998 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700999 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001000
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001002 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001003 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001004 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001005 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001006 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001007 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001008 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001009 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1010 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001011 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001012 if (prev.configDestroy) {
1013 // The previous is being paused because the configuration
1014 // is changing, which means it is actually stopping...
1015 // To juggle the fact that we are also starting a new
1016 // instance right now, we need to first completely stop
1017 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001018 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001019 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001020 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001021 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001022 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001023 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001024 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1025 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001026 // If we already have a few activities waiting to stop,
1027 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001028 // them out. Or if r is the last of activity of the last task the stack
1029 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001030 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001031 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001032 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001033 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001034 }
1035 }
1036 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001037 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001038 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001039 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001040 // It is possible the activity was freezing the screen before it was paused.
1041 // In that case go ahead and remove the freeze this activity has on the screen
1042 // since it is no longer visible.
1043 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001044 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001046
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001047 if (resumeNext) {
1048 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1049 if (!mService.isSleepingOrShuttingDown()) {
1050 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1051 } else {
1052 mStackSupervisor.checkReadyForSleepLocked();
1053 ActivityRecord top = topStack.topRunningActivityLocked(null);
1054 if (top == null || (prev != null && top != prev)) {
1055 // If there are no more activities available to run,
1056 // do resume anyway to start something. Also if the top
1057 // activity on the stack is not the just paused activity,
1058 // we need to go ahead and resume it to ensure we complete
1059 // an in-flight app switch.
1060 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1061 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001062 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001063 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001064
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001065 if (prev != null) {
1066 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067
Craig Mautner525f3d92013-05-07 14:01:50 -07001068 if (prev.app != null && prev.cpuTimeAtResume > 0
1069 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001070 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1071 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001072 if (diff > 0) {
1073 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1074 synchronized (bsi) {
1075 BatteryStatsImpl.Uid.Proc ps =
1076 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001077 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001078 if (ps != null) {
1079 ps.addForegroundTimeLocked(diff);
1080 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001081 }
1082 }
1083 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001084 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001085 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001086
1087 // Notfiy when the task stack has changed
1088 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 }
1090
1091 /**
1092 * Once we know that we have asked an application to put an activity in
1093 * the resumed state (either by launching it or explicitly telling it),
1094 * this function updates the rest of our state to match that fact.
1095 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001096 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001097 next.idle = false;
1098 next.results = null;
1099 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001100
1101 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1102 ProcessRecord app = next.task.mActivities.get(0).app;
1103 if (app != null && app != mService.mHomeProcess) {
1104 mService.mHomeProcess = app;
1105 }
1106 }
1107
Craig Mautner07566322013-09-26 16:42:55 -07001108 if (next.nowVisible) {
1109 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001110 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001111 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001112
1113 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001114 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001116 mStackSupervisor.reportResumedActivityLocked(next);
1117
1118 next.resumeKeyDispatchingLocked();
1119 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001120
1121 // Mark the point when the activity is resuming
1122 // TODO: To be more accurate, the mark should be before the onCreate,
1123 // not after the onResume. But for subsequent starts, onResume is fine.
1124 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001125 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126 } else {
1127 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1128 }
Winson Chung376543b2014-05-21 17:43:28 -07001129
George Mount6ba042b2014-07-28 11:12:28 -07001130 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001131
1132 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1133 // When resuming an activity, require it to call requestVisibleBehind() again.
1134 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1135 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001136 }
1137
Craig Mautner2568c3a2015-03-26 14:22:34 -07001138 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001139 r.visible = visible;
1140 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001141 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001142 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001143 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001144 container.setVisible(visible);
1145 }
1146 }
1147
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001148 // Find the first visible activity above the passed activity and if it is translucent return it
1149 // otherwise return null;
1150 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1151 TaskRecord task = r.task;
1152 if (task == null) {
1153 return null;
1154 }
1155
1156 ActivityStack stack = task.stack;
1157 if (stack == null) {
1158 return null;
1159 }
1160
1161 int stackNdx = mStacks.indexOf(stack);
1162
1163 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1164 int taskNdx = tasks.indexOf(task);
1165
1166 ArrayList<ActivityRecord> activities = task.mActivities;
1167 int activityNdx = activities.indexOf(r) + 1;
1168
1169 final int numStacks = mStacks.size();
1170 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001171 ActivityStack historyStack = mStacks.get(stackNdx);
1172 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001173 final int numTasks = tasks.size();
1174 while (taskNdx < numTasks) {
1175 activities = tasks.get(taskNdx).mActivities;
1176 final int numActivities = activities.size();
1177 while (activityNdx < numActivities) {
1178 final ActivityRecord activity = activities.get(activityNdx);
1179 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001180 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001181 }
1182 ++activityNdx;
1183 }
1184 activityNdx = 0;
1185 ++taskNdx;
1186 }
1187 taskNdx = 0;
1188 ++stackNdx;
1189 }
1190
1191 return null;
1192 }
1193
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001194 private ActivityStack getNextVisibleStackLocked() {
1195 ArrayList<ActivityStack> stacks = mStacks;
1196 final ActivityRecord parent = mActivityContainer.mParentActivity;
1197 if (parent != null) {
1198 stacks = parent.task.stack.mStacks;
1199 }
1200 if (stacks != null) {
1201 for (int i = stacks.size() - 1; i >= 0; --i) {
1202 ActivityStack stack = stacks.get(i);
1203 if (stack != this && stack.isStackVisibleLocked()) {
1204 return stack;
1205 }
1206 }
1207 }
1208 return null;
1209 }
1210
Jose Lima7ba71252014-04-30 12:59:27 -07001211 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1212 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001213 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001214 if (!isAttached()) {
1215 return false;
1216 }
1217
1218 if (mStackSupervisor.isFrontStack(this)) {
1219 return true;
1220 }
1221
Jose Lima729cb232014-05-05 15:59:40 -07001222 /**
1223 * Start at the task above this one and go up, looking for a visible
1224 * fullscreen activity, or a translucent activity that requested the
1225 * wallpaper to be shown behind it.
1226 */
Jose Lima7ba71252014-04-30 12:59:27 -07001227 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001228 ActivityStack stack = mStacks.get(i);
1229 // stack above isn't full screen, so, we assume we're still visible. at some point
1230 // we should look at the stack bounds to see if we're occluded even if the stack
1231 // isn't fullscreen
1232 if (!stack.mFullscreen) {
1233 continue;
1234 }
1235 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1236 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001237 final TaskRecord task = tasks.get(taskNdx);
1238 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001239 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001240 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001241
1242 // Conditions for an activity to obscure the stack we're
1243 // examining:
1244 // 1. Not Finishing AND Visible AND:
1245 // 2. Either:
1246 // - Full Screen Activity OR
1247 // - On top of Home and our stack is NOT home
1248 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001249 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001250 return false;
1251 }
1252 }
1253 }
1254 }
1255
1256 return true;
1257 }
1258
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 /**
1260 * Make sure that all activities that need to be visible (that is, they
1261 * currently can be seen by the user) actually are.
1262 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001263 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1264 ActivityRecord top = topRunningActivityLocked(null);
1265 if (top == null) {
1266 return;
1267 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001268 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1269 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001270 + " configChanges=0x" + Integer.toHexString(configChanges));
1271
Craig Mautner5eda9b32013-07-02 11:58:16 -07001272 if (mTranslucentActivityWaiting != top) {
1273 mUndrawnActivitiesBelowTopTranslucent.clear();
1274 if (mTranslucentActivityWaiting != null) {
1275 // Call the callback with a timeout indication.
1276 notifyActivityDrawnLocked(null);
1277 mTranslucentActivityWaiting = null;
1278 }
1279 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1280 }
1281
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001282 // If the top activity is not fullscreen, then we need to
1283 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001284 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001285 boolean behindFullscreen = !isStackVisibleLocked();
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001286 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001287
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001288 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001289 final TaskRecord task = mTaskHistory.get(taskNdx);
1290 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001291 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1292 final ActivityRecord r = activities.get(activityNdx);
1293 if (r.finishing) {
1294 continue;
1295 }
1296 if (aboveTop && r != top) {
1297 continue;
1298 }
1299 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001300 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1301 // but must be drawn initially for the animation as though they were visible.
1302 if (!behindFullscreen || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001303 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1304 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001305 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001306
Craig Mautnerd44711d2013-02-23 11:24:36 -08001307 // First: if this is not the current activity being started, make
1308 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001309 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001310 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001312
1313 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001314 // This activity needs to be visible, but isn't even running...
1315 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001316 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1317 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001318 if (r != starting) {
1319 r.startFreezingScreenLocked(r.app, configChanges);
1320 }
1321 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001322 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1323 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001324 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001325 }
1326 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001327 mStackSupervisor.startSpecificActivityLocked(
1328 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001329 if (activityNdx >= activities.size()) {
1330 // Record may be removed if its process needs to restart.
1331 activityNdx = activities.size() - 1;
1332 } else {
1333 noStackActivityResumed = false;
1334 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001335 }
1336
1337 } else if (r.visible) {
1338 // If this activity is already visible, then there is nothing
1339 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001340 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1341 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001342 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001343 try {
George Mount6ba042b2014-07-28 11:12:28 -07001344 if (r.returningOptions != null) {
1345 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1346 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001347 }
1348 } catch(RemoteException e) {
1349 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001350 if (r.state == ActivityState.RESUMED) {
1351 noStackActivityResumed = false;
1352 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001353 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001354 // This activity is not currently visible, but is running.
1355 // Tell it to become visible.
1356 r.visible = true;
1357 if (r.state != ActivityState.RESUMED && r != starting) {
1358 // If this activity is paused, tell it
1359 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001360 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1361 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001362 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001363 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001364 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001365 mUndrawnActivitiesBelowTopTranslucent.add(r);
1366 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001367 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001368 r.sleeping = false;
1369 r.app.pendingUiClean = true;
1370 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1371 r.stopFreezingScreenLocked(false);
1372 } catch (Exception e) {
1373 // Just skip on any failure; we'll make it
1374 // visible when it next restarts.
1375 Slog.w(TAG, "Exception thrown making visibile: "
1376 + r.intent.getComponent(), e);
1377 }
1378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380
Craig Mautnerd44711d2013-02-23 11:24:36 -08001381 // Aggregate current change flags.
1382 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001383
Craig Mautnerd44711d2013-02-23 11:24:36 -08001384 if (r.fullscreen) {
1385 // At this point, nothing else needs to be shown
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001386 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r);
Craig Mautner580ea812013-04-25 12:58:38 -07001387 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001388 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001389 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001390 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001391 }
1392 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001393 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1394 "Make invisible? " + r + " finishing=" + r.finishing
1395 + " state=" + r.state + " behindFullscreen=" + behindFullscreen);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001396 // Now for any activities that aren't visible to the user, make
1397 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001399 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001400 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001401 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001402 switch (r.state) {
1403 case STOPPING:
1404 case STOPPED:
1405 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001406 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1407 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001408 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1409 }
1410 break;
1411
1412 case INITIALIZING:
1413 case RESUMED:
1414 case PAUSING:
1415 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001416 // This case created for transitioning activities from
1417 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001418 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001419 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001420 } else {
1421 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1422 mStackSupervisor.mStoppingActivities.add(r);
1423 }
1424 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001425 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001426 break;
1427
1428 default:
1429 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 }
1431 } catch (Exception e) {
1432 // Just skip on any failure; we'll make it
1433 // visible when it next restarts.
1434 Slog.w(TAG, "Exception thrown making hidden: "
1435 + r.intent.getComponent(), e);
1436 }
1437 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001438 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001440 }
1441 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001443
1444 if (mTranslucentActivityWaiting != null &&
1445 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1446 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1447 notifyActivityDrawnLocked(null);
1448 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 }
Craig Mautner58547802013-03-05 08:23:53 -08001450
Todd Kennedyaab56db2015-01-30 09:39:53 -08001451 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001452 mTranslucentActivityWaiting = r;
1453 mUndrawnActivitiesBelowTopTranslucent.clear();
1454 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1455 }
1456
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001457 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1458 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1459 final TaskRecord task = mTaskHistory.get(taskNdx);
1460 final ArrayList<ActivityRecord> activities = task.mActivities;
1461 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1462 final ActivityRecord r = activities.get(activityNdx);
1463 if ( r.appTimeTracker != except) {
1464 r.appTimeTracker = null;
1465 }
1466 }
1467 }
1468 }
1469
Craig Mautner5eda9b32013-07-02 11:58:16 -07001470 /**
1471 * Called as activities below the top translucent activity are redrawn. When the last one is
1472 * redrawn notify the top activity by calling
1473 * {@link Activity#onTranslucentConversionComplete}.
1474 *
1475 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1476 * occurred and the activity will be notified immediately.
1477 */
1478 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001479 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001480 if ((r == null)
1481 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1482 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1483 // The last undrawn activity below the top has just been drawn. If there is an
1484 // opaque activity at the top, notify it that it can become translucent safely now.
1485 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1486 mTranslucentActivityWaiting = null;
1487 mUndrawnActivitiesBelowTopTranslucent.clear();
1488 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1489
Craig Mautner71dd1b62014-02-18 15:48:52 -08001490 if (waitingActivity != null) {
1491 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1492 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1493 try {
1494 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1495 waitingActivity.appToken, r != null);
1496 } catch (RemoteException e) {
1497 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001498 }
1499 }
1500 }
1501 }
1502
Craig Mautnera61bc652013-10-28 15:43:18 -07001503 /** If any activities below the top running one are in the INITIALIZING state and they have a
1504 * starting window displayed then remove that starting window. It is possible that the activity
1505 * in this state will never resumed in which case that starting window will be orphaned. */
1506 void cancelInitializingActivities() {
1507 final ActivityRecord topActivity = topRunningActivityLocked(null);
1508 boolean aboveTop = true;
1509 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1510 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1511 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1512 final ActivityRecord r = activities.get(activityNdx);
1513 if (aboveTop) {
1514 if (r == topActivity) {
1515 aboveTop = false;
1516 }
1517 continue;
1518 }
1519
1520 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001521 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1522 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001523 r.mStartingWindowShown = false;
1524 mWindowManager.removeAppStartingWindow(r.appToken);
1525 }
1526 }
1527 }
1528 }
1529
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 /**
1531 * Ensure that the top activity in the stack is resumed.
1532 *
1533 * @param prev The previously resumed activity, for when in the process
1534 * of pausing; can be null to call from elsewhere.
1535 *
1536 * @return Returns true if something is being resumed, or false if
1537 * nothing happened.
1538 */
1539 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001540 return resumeTopActivityLocked(prev, null);
1541 }
1542
1543 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001544 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001545 // Don't even start recursing.
1546 return false;
1547 }
1548
1549 boolean result = false;
1550 try {
1551 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001552 mStackSupervisor.inResumeTopActivity = true;
1553 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1554 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001555 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001556 }
Craig Mautner544efa72014-09-04 16:41:20 -07001557 result = resumeTopActivityInnerLocked(prev, options);
1558 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001559 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001560 }
1561 return result;
1562 }
1563
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001564 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001565 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001566
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001567 if (!mService.mBooting && !mService.mBooted) {
1568 // Not ready yet!
1569 return false;
1570 }
1571
Craig Mautnerdf88d732014-01-27 09:21:32 -08001572 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001573 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001574 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001575 // Do not resume this stack if its parent is not resumed.
1576 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1577 return false;
1578 }
1579
Craig Mautnera61bc652013-10-28 15:43:18 -07001580 cancelInitializingActivities();
1581
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001583 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584
1585 // Remember how we'll process this pause/resume situation, and ensure
1586 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001587 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1588 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589
Craig Mautner84984fa2014-06-19 11:19:20 -07001590 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001592 // There are no more activities!
1593 final String reason = "noMoreActivities";
1594 if (!mFullscreen) {
1595 // Try to move focus to the next visible stack with a running activity if this
1596 // stack is not covering the entire screen.
1597 final ActivityStack stack = getNextVisibleStackLocked();
1598 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1599 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1600 }
1601 }
1602 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001603 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001604 if (DEBUG_STATES) Slog.d(TAG_STATES,
1605 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001606 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001607 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001608 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1609 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1610 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001611 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 }
1613
1614 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001615
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001617 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1618 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 // Make sure we have executed any pending transitions, since there
1620 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001621 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001622 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001623 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001624 if (DEBUG_STATES) Slog.d(TAG_STATES,
1625 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001626 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001627 return false;
1628 }
1629
Craig Mautner525f3d92013-05-07 14:01:50 -07001630 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001631 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001632 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001633 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001634 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001635 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001636 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001637 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001638 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001639 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001640 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001641 } else if (!isOnHomeDisplay()) {
1642 return false;
1643 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001644 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001645 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001646 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1647 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1648 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001649 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001650 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001651 }
1652
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 // If we are sleeping, and there is no resumed activity, and the top
1654 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001655 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001656 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001657 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001658 // Make sure we have executed any pending transitions, since there
1659 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001660 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001662 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001663 if (DEBUG_STATES) Slog.d(TAG_STATES,
1664 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001665 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001666 return false;
1667 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001668
1669 // Make sure that the user who owns this activity is started. If not,
1670 // we will just leave it as is because someone should be bringing
1671 // another user's activities to the top of the stack.
1672 if (mService.mStartedUsers.get(next.userId) == null) {
1673 Slog.w(TAG, "Skipping resume of top activity " + next
1674 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001675 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001676 return false;
1677 }
1678
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 // The activity may be waiting for stop, but that is no longer
1680 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001681 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001682 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001683 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001684 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001686 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001688 // If we are currently pausing an activity, then don't do anything
1689 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001690 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001691 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001692 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001693 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001694 return false;
1695 }
1696
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001697 // Okay we are now going to start a switch, to 'next'. We may first
1698 // have to pause the current activity, but this is an important point
1699 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001700 // XXX "App Redirected" dialog is getting too many false positives
1701 // at this point, so turn off for now.
1702 if (false) {
1703 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1704 long now = SystemClock.uptimeMillis();
1705 final boolean inTime = mLastStartedActivity.startTime != 0
1706 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1707 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1708 final int nextUid = next.info.applicationInfo.uid;
1709 if (inTime && lastUid != nextUid
1710 && lastUid != next.launchedFromUid
1711 && mService.checkPermission(
1712 android.Manifest.permission.STOP_APP_SWITCHES,
1713 -1, next.launchedFromUid)
1714 != PackageManager.PERMISSION_GRANTED) {
1715 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1716 } else {
1717 next.startTime = now;
1718 mLastStartedActivity = next;
1719 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001720 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001721 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001722 mLastStartedActivity = next;
1723 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001724 }
Craig Mautner58547802013-03-05 08:23:53 -08001725
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001726 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1727
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 // We need to start pausing the current activity so the top one
1729 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001730 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1731 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001732 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001733 if (DEBUG_STATES) Slog.d(TAG_STATES,
1734 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001735 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001736 }
1737 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001738 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001739 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001740 // At this point we want to put the upcoming activity's process
1741 // at the top of the LRU list, since we know we will be needing it
1742 // very soon and it would be a waste to let it get killed if it
1743 // happens to be sitting towards the end.
1744 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001745 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001746 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001747 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 return true;
1749 }
1750
Christopher Tated3f175c2012-06-14 14:16:54 -07001751 // If the most recent activity was noHistory but was only stopped rather
1752 // than stopped+finished because the device went to sleep, we need to make
1753 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001754 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001755 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001756 if (DEBUG_STATES) Slog.d(TAG_STATES,
1757 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001758 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001759 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001760 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001761 }
1762
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001763 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001764 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1765 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001766 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001767 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1768 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 } else {
1770 // The next activity is already visible, so hide the previous
1771 // activity's windows right now so we can show the new one ASAP.
1772 // We only do this if the previous is finishing, which should mean
1773 // it is on top of the one being resumed so hiding it quickly
1774 // is good. Otherwise, we want to do the normal route of allowing
1775 // the resumed activity to be shown so we can decide if the
1776 // previous should actually be hidden depending on whether the
1777 // new one is found to be full-screen or not.
1778 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001780 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1781 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001782 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 + ", nowVisible=" + next.nowVisible);
1784 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001785 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001786 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001787 + ", waitingVisible="
1788 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1789 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 }
1791 }
1792 }
1793
Dianne Hackborne7f97212011-02-24 14:40:20 -08001794 // Launching this app's activity, make sure the app is no longer
1795 // considered stopped.
1796 try {
1797 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001798 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001799 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001800 } catch (IllegalArgumentException e) {
1801 Slog.w(TAG, "Failed trying to unstop package "
1802 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001803 }
1804
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001805 // We are starting up the next activity, so tell the window manager
1806 // that the previous one will be hidden soon. This way it can know
1807 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001808 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 if (prev != null) {
1810 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001811 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 "Prepare close transition: prev=" + prev);
1813 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001814 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001815 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001816 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001817 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001818 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1819 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001821 mWindowManager.setAppWillBeHidden(prev.appToken);
1822 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001824 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1825 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001826 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001827 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001828 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001830 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001831 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001832 : next.mLaunchTaskBehind
1833 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1834 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 }
1836 }
1837 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001838 mWindowManager.setAppWillBeHidden(prev.appToken);
1839 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001840 }
Craig Mautner967212c2013-04-13 21:10:58 -07001841 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001842 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001844 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001845 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001846 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001847 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001848 }
1849 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001850
1851 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001852 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001853 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1854 if (opts != null) {
1855 resumeAnimOptions = opts.toBundle();
1856 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001857 next.applyOptionsLocked();
1858 } else {
1859 next.clearOptionsLocked();
1860 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861
Craig Mautnercf910b02013-04-23 11:23:27 -07001862 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001863 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001864 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001865
1866 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001867 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001869 // schedule launch ticks to collect information about slow apps.
1870 next.startLaunchTickingLocked();
1871
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001872 ActivityRecord lastResumedActivity =
1873 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 ActivityState lastState = next.state;
1875
1876 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001877
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001878 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001879 next.state = ActivityState.RESUMED;
1880 mResumedActivity = next;
1881 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001882 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001883 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001884 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001885 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001886
1887 // Have the window manager re-evaluate the orientation of
1888 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001889 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001890 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001891 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001892 mService.mConfiguration,
1893 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1894 if (config != null) {
1895 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001896 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001897 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001899
Craig Mautner525f3d92013-05-07 14:01:50 -07001900 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 // The configuration update wasn't able to keep the existing
1902 // instance of the activity, and instead started a new one.
1903 // We should be all done, but let's just make sure our activity
1904 // is still at the top and schedule another run if something
1905 // weird happened.
1906 ActivityRecord nextNext = topRunningActivityLocked(null);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001907 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001908 "Activity config changed during resume: " + next
1909 + ", new next: " + nextNext);
1910 if (nextNext != next) {
1911 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001912 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001913 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001914 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001915 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001916 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001917 return true;
1918 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001919 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001920 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921 }
Craig Mautner58547802013-03-05 08:23:53 -08001922
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 try {
1924 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001925 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926 if (a != null) {
1927 final int N = a.size();
1928 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001929 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1930 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001931 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001932 }
1933 }
1934
1935 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001936 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937 }
1938
Craig Mautner299f9602015-01-26 09:47:33 -08001939 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1940 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001941
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001942 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001943 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001944 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001945 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07001946 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001947 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001948 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001949
Craig Mautner0eea92c2013-05-16 13:35:39 -07001950 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001952 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001953 } catch (Exception e) {
1954 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001955 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001956 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001958 if (lastStack != null) {
1959 lastStack.mResumedActivity = lastResumedActivity;
1960 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001961 Slog.i(TAG, "Restarting because process died: " + next);
1962 if (!next.hasBeenLaunched) {
1963 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001964 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1965 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001966 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001967 next.appToken, next.packageName, next.theme,
1968 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001969 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1970 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001971 }
George Mount2c92c972014-03-20 09:38:23 -07001972 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001973 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 return true;
1975 }
1976
1977 // From this point on, if something goes wrong there is no way
1978 // to recover the activity.
1979 try {
1980 next.visible = true;
1981 completeResumeLocked(next);
1982 } catch (Exception e) {
1983 // If any exception gets thrown, toss away this
1984 // activity and try the next one.
1985 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001986 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001987 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001988 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989 return true;
1990 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 next.stopped = false;
1992
1993 } else {
1994 // Whoops, need to restart this activity!
1995 if (!next.hasBeenLaunched) {
1996 next.hasBeenLaunched = true;
1997 } else {
1998 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001999 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002000 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002001 mService.compatibilityInfoForPackageLocked(
2002 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002004 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002005 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002007 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002009 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002010 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002011 }
2012
Craig Mautnercf910b02013-04-23 11:23:27 -07002013 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002014 return true;
2015 }
2016
riddle_hsuc215a4f2014-12-27 12:10:45 +08002017 private TaskRecord getNextTask(TaskRecord targetTask) {
2018 final int index = mTaskHistory.indexOf(targetTask);
2019 if (index >= 0) {
2020 final int numTasks = mTaskHistory.size();
2021 for (int i = index + 1; i < numTasks; ++i) {
2022 TaskRecord task = mTaskHistory.get(i);
2023 if (task.userId == targetTask.userId) {
2024 return task;
2025 }
2026 }
2027 }
2028 return null;
2029 }
2030
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002031 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002032 // If the moving task is over home stack, transfer its return type to next task
2033 if (task.isOverHomeStack()) {
2034 final TaskRecord nextTask = getNextTask(task);
2035 if (nextTask != null) {
2036 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2037 }
2038 }
2039
Craig Mautner9c85c202013-10-04 20:11:26 -07002040 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002041 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002042 if (isOnHomeDisplay()) {
2043 ActivityStack lastStack = mStackSupervisor.getLastStack();
2044 final boolean fromHome = lastStack.isHomeStack();
2045 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002046 task.setTaskToReturnTo(fromHome
2047 ? lastStack.topTask() == null
2048 ? HOME_ACTIVITY_TYPE
2049 : lastStack.topTask().taskType
2050 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002051 }
2052 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002053 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002054 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002055
Craig Mautnerac6f8432013-07-17 13:24:59 -07002056 mTaskHistory.remove(task);
2057 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002058 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002059 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002060 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002061 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002062 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002063 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002064 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002065 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002066 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002067 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002068 break;
2069 }
2070 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002071 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002072 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002073 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002074 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002075 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002076
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002078 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002079 TaskRecord rTask = r.task;
2080 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002081 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2082 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002083 // Last activity in task had been removed or ActivityManagerService is reusing task.
2084 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002085 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002086 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002087 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002088 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002089 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 if (!newTask) {
2091 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002092 boolean startIt = true;
2093 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2094 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002095 if (task.getTopActivity() == null) {
2096 // All activities in task are finishing.
2097 continue;
2098 }
Craig Mautner70a86932013-02-28 22:37:44 -08002099 if (task == r.task) {
2100 // Here it is! Now, if this is not yet visible to the
2101 // user, then just add it without starting; it will
2102 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002103 if (!startIt) {
2104 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2105 + task, new RuntimeException("here").fillInStackTrace());
2106 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002107 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002108 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2109 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002110 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002111 r.userId, r.info.configChanges, task.voiceSession != null,
2112 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002114 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002116 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 return;
2118 }
2119 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002120 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 startIt = false;
2122 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 }
2124 }
2125
2126 // Place a new activity at top of stack, so it is next to interact
2127 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129 // If we are not placing the new activity frontmost, we do not want
2130 // to deliver the onUserLeaving callback to the actual frontmost
2131 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002132 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002133 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002134 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002135 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 }
Craig Mautner70a86932013-02-28 22:37:44 -08002137
2138 task = r.task;
2139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002141 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002142 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002143 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002144 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002145
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002146 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002147 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 // We want to show the starting preview window if we are
2149 // switching to a new task, or the next activity's process is
2150 // not currently running.
2151 boolean showStartingIcon = newTask;
2152 ProcessRecord proc = r.app;
2153 if (proc == null) {
2154 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2155 }
2156 if (proc == null || proc.thread == null) {
2157 showStartingIcon = true;
2158 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002159 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002161 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002162 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002164 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002165 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002166 ? r.mLaunchTaskBehind
2167 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2168 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002169 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 mNoAnimActivities.remove(r);
2171 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002172 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002173 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002174 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002175 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 boolean doShow = true;
2177 if (newTask) {
2178 // Even though this activity is starting fresh, we still need
2179 // to reset it to make sure we apply affinities to move any
2180 // existing activities from other tasks in to it.
2181 // If the caller has requested that the target task be
2182 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002183 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184 resetTaskIfNeededLocked(r, r);
2185 doShow = topRunningNonDelayedActivityLocked(null) == r;
2186 }
George Mount70778d72014-07-01 16:33:45 -07002187 } else if (options != null && new ActivityOptions(options).getAnimationType()
2188 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2189 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002191 if (r.mLaunchTaskBehind) {
2192 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2193 // tell WindowManager that r is visible even though it is at the back of the stack.
2194 mWindowManager.setAppVisibility(r.appToken, true);
2195 ensureActivitiesVisibleLocked(null, 0);
2196 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 // Figure out if we are transitioning from another activity that is
2198 // "has the same starting icon" as the next one. This allows the
2199 // window manager to keep the previous window it had previously
2200 // created, if it still had one.
2201 ActivityRecord prev = mResumedActivity;
2202 if (prev != null) {
2203 // We don't want to reuse the previous starting preview if:
2204 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002205 if (prev.task != r.task) {
2206 prev = null;
2207 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002209 else if (prev.nowVisible) {
2210 prev = null;
2211 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002213 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002214 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002215 mService.compatibilityInfoForPackageLocked(
2216 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002217 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002218 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002219 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 }
2221 } else {
2222 // If this is the first activity, don't do any fancy animations,
2223 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002224 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002225 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002226 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002227 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002228 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002229 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002230 }
2231 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002232 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002233 }
2234
2235 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002236 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 }
2238 }
2239
Dianne Hackbornbe707852011-11-11 14:32:10 -08002240 final void validateAppTokensLocked() {
2241 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002242 mValidateAppTokens.ensureCapacity(numActivities());
2243 final int numTasks = mTaskHistory.size();
2244 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2245 TaskRecord task = mTaskHistory.get(taskNdx);
2246 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002247 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002248 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002249 }
Craig Mautner000f0022013-02-26 15:04:29 -08002250 TaskGroup group = new TaskGroup();
2251 group.taskId = task.taskId;
2252 mValidateAppTokens.add(group);
2253 final int numActivities = activities.size();
2254 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2255 final ActivityRecord r = activities.get(activityNdx);
2256 group.tokens.add(r.appToken);
2257 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002258 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002259 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002260 }
2261
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002262 /**
2263 * Perform a reset of the given task, if needed as part of launching it.
2264 * Returns the new HistoryRecord at the top of the task.
2265 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002266 /**
2267 * Helper method for #resetTaskIfNeededLocked.
2268 * We are inside of the task being reset... we'll either finish this activity, push it out
2269 * for another task, or leave it as-is.
2270 * @param task The task containing the Activity (taskTop) that might be reset.
2271 * @param forceReset
2272 * @return An ActivityOptions that needs to be processed.
2273 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002274 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002275 ActivityOptions topOptions = null;
2276
2277 int replyChainEnd = -1;
2278 boolean canMoveOptions = true;
2279
2280 // We only do this for activities that are not the root of the task (since if we finish
2281 // the root, we may no longer have the task!).
2282 final ArrayList<ActivityRecord> activities = task.mActivities;
2283 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002284 final int rootActivityNdx = task.findEffectiveRootIndex();
2285 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002286 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002287 if (target.frontOfTask)
2288 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002289
2290 final int flags = target.info.flags;
2291 final boolean finishOnTaskLaunch =
2292 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2293 final boolean allowTaskReparenting =
2294 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2295 final boolean clearWhenTaskReset =
2296 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2297
2298 if (!finishOnTaskLaunch
2299 && !clearWhenTaskReset
2300 && target.resultTo != null) {
2301 // If this activity is sending a reply to a previous
2302 // activity, we can't do anything with it now until
2303 // we reach the start of the reply chain.
2304 // XXX note that we are assuming the result is always
2305 // to the previous activity, which is almost always
2306 // the case but we really shouldn't count on.
2307 if (replyChainEnd < 0) {
2308 replyChainEnd = i;
2309 }
2310 } else if (!finishOnTaskLaunch
2311 && !clearWhenTaskReset
2312 && allowTaskReparenting
2313 && target.taskAffinity != null
2314 && !target.taskAffinity.equals(task.affinity)) {
2315 // If this activity has an affinity for another
2316 // task, then we need to move it out of here. We will
2317 // move it as far out of the way as possible, to the
2318 // bottom of the activity stack. This also keeps it
2319 // correctly ordered with any activities we previously
2320 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002321 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002322 final ActivityRecord bottom =
2323 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002324 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002325 if (bottom != null && target.taskAffinity != null
2326 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002327 // If the activity currently at the bottom has the
2328 // same task affinity as the one we are moving,
2329 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002330 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002331 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002332 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002333 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002334 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002335 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002336 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002337 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002338 + " out to new task " + target.task);
2339 }
2340
Craig Mautnere3a74d52013-02-22 14:14:58 -08002341 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002342 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002343
Craig Mautner525f3d92013-05-07 14:01:50 -07002344 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002345 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2346 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002347 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002348 if (p.finishing) {
2349 continue;
2350 }
2351
Craig Mautnere3a74d52013-02-22 14:14:58 -08002352 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002353 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002354 topOptions = p.takeOptionsLocked();
2355 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002356 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002357 }
2358 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002359 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2360 "Removing activity " + p + " from task=" + task + " adding to task="
2361 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002362 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2363 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002364 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002365 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002366
Craig Mautner83162a92015-01-26 14:43:30 -08002367 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002368 }
2369
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002370 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002371 if (VALIDATE_TOKENS) {
2372 validateAppTokensLocked();
2373 }
2374
2375 replyChainEnd = -1;
2376 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2377 // If the activity should just be removed -- either
2378 // because it asks for it, or the task should be
2379 // cleared -- then finish it and anything that is
2380 // part of its reply chain.
2381 int end;
2382 if (clearWhenTaskReset) {
2383 // In this case, we want to finish this activity
2384 // and everything above it, so be sneaky and pretend
2385 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002386 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002387 } else if (replyChainEnd < 0) {
2388 end = i;
2389 } else {
2390 end = replyChainEnd;
2391 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002392 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002393 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002394 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002395 if (p.finishing) {
2396 continue;
2397 }
2398 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002399 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002400 topOptions = p.takeOptionsLocked();
2401 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002402 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002403 }
2404 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002405 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002406 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002407 if (finishActivityLocked(
2408 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002409 end--;
2410 srcPos--;
2411 }
2412 }
2413 replyChainEnd = -1;
2414 } else {
2415 // If we were in the middle of a chain, well the
2416 // activity that started it all doesn't want anything
2417 // special, so leave it all as-is.
2418 replyChainEnd = -1;
2419 }
2420 }
2421
2422 return topOptions;
2423 }
2424
2425 /**
2426 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2427 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2428 * @param affinityTask The task we are looking for an affinity to.
2429 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2430 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2431 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2432 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002433 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002434 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002435 int replyChainEnd = -1;
2436 final int taskId = task.taskId;
2437 final String taskAffinity = task.affinity;
2438
2439 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2440 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002441 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2442
2443 // Do not operate on or below the effective root Activity.
2444 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002445 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002446 if (target.frontOfTask)
2447 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002448
2449 final int flags = target.info.flags;
2450 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2451 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2452
2453 if (target.resultTo != null) {
2454 // If this activity is sending a reply to a previous
2455 // activity, we can't do anything with it now until
2456 // we reach the start of the reply chain.
2457 // XXX note that we are assuming the result is always
2458 // to the previous activity, which is almost always
2459 // the case but we really shouldn't count on.
2460 if (replyChainEnd < 0) {
2461 replyChainEnd = i;
2462 }
2463 } else if (topTaskIsHigher
2464 && allowTaskReparenting
2465 && taskAffinity != null
2466 && taskAffinity.equals(target.taskAffinity)) {
2467 // This activity has an affinity for our task. Either remove it if we are
2468 // clearing or move it over to our task. Note that
2469 // we currently punt on the case where we are resetting a
2470 // task that is not at the top but who has activities above
2471 // with an affinity to it... this is really not a normal
2472 // case, and we will need to later pull that task to the front
2473 // and usually at that point we will do the reset and pick
2474 // up those remaining activities. (This only happens if
2475 // someone starts an activity in a new task from an activity
2476 // in a task that is not currently on top.)
2477 if (forceReset || finishOnTaskLaunch) {
2478 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002479 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2480 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002481 for (int srcPos = start; srcPos >= i; --srcPos) {
2482 final ActivityRecord p = activities.get(srcPos);
2483 if (p.finishing) {
2484 continue;
2485 }
Todd Kennedy539db512014-12-15 09:57:55 -08002486 finishActivityLocked(
2487 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002488 }
2489 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002490 if (taskInsertionPoint < 0) {
2491 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002492
Craig Mautner77878772013-03-04 19:46:24 -08002493 }
Craig Mautner77878772013-03-04 19:46:24 -08002494
2495 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002496 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2497 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2498 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002499 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002500 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002501 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002502 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002503
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002504 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2505 "Removing and adding activity " + p + " to stack at " + task
2506 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002507 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2508 + " from " + srcPos + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002509 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002510 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002511 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002512 if (VALIDATE_TOKENS) {
2513 validateAppTokensLocked();
2514 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002515
2516 // Now we've moved it in to place... but what if this is
2517 // a singleTop activity and we have put it on top of another
2518 // instance of the same activity? Then we drop the instance
2519 // below so it remains singleTop.
2520 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2521 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002522 int targetNdx = taskActivities.indexOf(target);
2523 if (targetNdx > 0) {
2524 ActivityRecord p = taskActivities.get(targetNdx - 1);
2525 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002526 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2527 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002528 }
2529 }
2530 }
2531 }
2532
2533 replyChainEnd = -1;
2534 }
2535 }
Craig Mautner77878772013-03-04 19:46:24 -08002536 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002537 }
2538
Craig Mautner8849a5e2013-04-02 16:41:03 -07002539 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002540 ActivityRecord newActivity) {
2541 boolean forceReset =
2542 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2543 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2544 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2545 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2546 forceReset = true;
2547 }
2548 }
2549
2550 final TaskRecord task = taskTop.task;
2551
2552 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2553 * for remaining tasks. Used for later tasks to reparent to task. */
2554 boolean taskFound = false;
2555
2556 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2557 ActivityOptions topOptions = null;
2558
Craig Mautner77878772013-03-04 19:46:24 -08002559 // Preserve the location for reparenting in the new task.
2560 int reparentInsertionPoint = -1;
2561
Craig Mautnere3a74d52013-02-22 14:14:58 -08002562 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2563 final TaskRecord targetTask = mTaskHistory.get(i);
2564
2565 if (targetTask == task) {
2566 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2567 taskFound = true;
2568 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002569 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2570 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002571 }
2572 }
2573
Craig Mautner70a86932013-02-28 22:37:44 -08002574 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002575 if (taskNdx >= 0) {
2576 do {
2577 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2578 } while (taskTop == null && taskNdx >= 0);
2579 }
Craig Mautner70a86932013-02-28 22:37:44 -08002580
Craig Mautnere3a74d52013-02-22 14:14:58 -08002581 if (topOptions != null) {
2582 // If we got some ActivityOptions from an activity on top that
2583 // was removed from the task, propagate them to the new real top.
2584 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002585 taskTop.updateOptionsLocked(topOptions);
2586 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002587 topOptions.abort();
2588 }
2589 }
2590
2591 return taskTop;
2592 }
2593
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002594 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2595 String resultWho, int requestCode, int resultCode, Intent data) {
2596
2597 if (callingUid > 0) {
2598 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002599 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 }
2601
2602 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2603 + " : who=" + resultWho + " req=" + requestCode
2604 + " res=" + resultCode + " data=" + data);
2605 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2606 try {
2607 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2608 list.add(new ResultInfo(resultWho, requestCode,
2609 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002610 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611 return;
2612 } catch (Exception e) {
2613 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2614 }
2615 }
2616
2617 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2618 }
2619
Craig Mautner299f9602015-01-26 09:47:33 -08002620 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002621 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2622 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002623 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002624 if (next != r) {
2625 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002626 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002627 // For non-fullscreen stack, we want to move the focus to the next visible
2628 // stack to prevent the home screen from moving to the top and obscuring
2629 // other visible stacks.
2630 if (!mFullscreen
2631 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2632 return;
2633 }
2634 // Move the home stack to the top if this stack is fullscreen or there is no
2635 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002636 if (mStackSupervisor.moveHomeStackTaskToTop(
2637 task.getTaskToReturnTo(), myReason)) {
2638 // Activity focus was already adjusted. Nothing else to do...
2639 return;
2640 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002641 }
2642 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002643
2644 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002645 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002646 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002647 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002648 }
2649 }
2650
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002651 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2652 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2653 final String myReason = reason + " adjustFocusToNextVisibleStack";
2654 if (stack == null) {
2655 return false;
2656 }
2657 final ActivityRecord top = stack.topRunningActivityLocked(null);
2658 if (top == null) {
2659 return false;
2660 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002661 mService.setFocusedActivityLocked(top, myReason);
2662 return true;
2663 }
2664
Craig Mautnerf3333272013-04-22 10:55:53 -07002665 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002666 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002667 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2668 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2669 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002670 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002671 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002672 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2673 "stop-no-history", false)) {
2674 // Activity was finished, no need to continue trying to schedule stop.
2675 adjustFocusedActivityLocked(r, "stopActivityFinished");
2676 r.resumeKeyDispatchingLocked();
2677 return;
2678 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002679 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002680 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002681 + " on stop because we're just sleeping");
2682 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002683 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002684 }
2685
2686 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002687 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002688 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 try {
2690 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002691 if (DEBUG_STATES) Slog.v(TAG_STATES,
2692 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002693 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002694 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2695 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002697 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002699 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002700 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002701 r.setSleeping(true);
2702 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002703 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002704 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002705 } catch (Exception e) {
2706 // Maybe just ignore exceptions here... if the process
2707 // has crashed, our death notification will clean things
2708 // up.
2709 Slog.w(TAG, "Exception thrown during pause", e);
2710 // Just in case, assume it to be stopped.
2711 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002712 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 r.state = ActivityState.STOPPED;
2714 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002715 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 }
2717 }
2718 }
2719 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002720
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721 /**
2722 * @return Returns true if the activity is being finished, false if for
2723 * some reason it is being left as-is.
2724 */
2725 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002726 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002727 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002728 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2729 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002730 + ", result=" + resultCode + ", data=" + resultData
2731 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002732 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002733 return false;
2734 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735
Craig Mautnerd44711d2013-02-23 11:24:36 -08002736 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002737 return true;
2738 }
2739
Craig Mautnerd2328952013-03-05 12:46:26 -08002740 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002741 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2742 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2743 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2744 ActivityRecord r = activities.get(activityNdx);
2745 if (r.resultTo == self && r.requestCode == requestCode) {
2746 if ((r.resultWho == null && resultWho == null) ||
2747 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2748 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2749 false);
2750 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002751 }
2752 }
2753 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002754 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002755 }
2756
Todd Kennedy539db512014-12-15 09:57:55 -08002757 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002758 ActivityRecord r = topRunningActivityLocked(null);
2759 if (r != null && r.app == app) {
2760 // If the top running activity is from this crashing
2761 // process, then terminate it to avoid getting in a loop.
2762 Slog.w(TAG, " Force finishing activity "
2763 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002764 int taskNdx = mTaskHistory.indexOf(r.task);
2765 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002766 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002767 // Also terminate any activities below it that aren't yet
2768 // stopped, to avoid a situation where one will get
2769 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002770 --activityNdx;
2771 if (activityNdx < 0) {
2772 do {
2773 --taskNdx;
2774 if (taskNdx < 0) {
2775 break;
2776 }
2777 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2778 } while (activityNdx < 0);
2779 }
2780 if (activityNdx >= 0) {
2781 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002782 if (r.state == ActivityState.RESUMED
2783 || r.state == ActivityState.PAUSING
2784 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002785 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002786 Slog.w(TAG, " Force finishing activity "
2787 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002788 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002789 }
2790 }
2791 }
2792 }
2793 }
2794
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002795 final void finishVoiceTask(IVoiceInteractionSession session) {
2796 IBinder sessionBinder = session.asBinder();
2797 boolean didOne = false;
2798 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2799 TaskRecord tr = mTaskHistory.get(taskNdx);
2800 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2801 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2802 ActivityRecord r = tr.mActivities.get(activityNdx);
2803 if (!r.finishing) {
2804 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2805 false);
2806 didOne = true;
2807 }
2808 }
2809 }
2810 }
2811 if (didOne) {
2812 mService.updateOomAdjLocked();
2813 }
2814 }
2815
Craig Mautnerd2328952013-03-05 12:46:26 -08002816 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002817 ArrayList<ActivityRecord> activities = r.task.mActivities;
2818 for (int index = activities.indexOf(r); index >= 0; --index) {
2819 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002820 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002821 break;
2822 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002823 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002824 }
2825 return true;
2826 }
2827
Dianne Hackborn5c607432012-02-28 14:44:19 -08002828 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2829 // send the result
2830 ActivityRecord resultTo = r.resultTo;
2831 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002832 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002833 + " who=" + r.resultWho + " req=" + r.requestCode
2834 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002835 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002836 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01002837 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002838 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002839 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002840 if (r.info.applicationInfo.uid > 0) {
2841 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2842 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002843 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002844 }
2845 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2846 resultData);
2847 r.resultTo = null;
2848 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002849 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002850
2851 // Make sure this HistoryRecord is not holding on to other resources,
2852 // because clients have remote IPC references to this object so we
2853 // can't assume that will go away and want to avoid circular IPC refs.
2854 r.results = null;
2855 r.pendingResults = null;
2856 r.newIntents = null;
2857 r.icicle = null;
2858 }
2859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 /**
2861 * @return Returns true if this activity has been removed from the history
2862 * list, or false if it is still in the list and will be removed later.
2863 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002864 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2865 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002866 if (r.finishing) {
2867 Slog.w(TAG, "Duplicate finish request for " + r);
2868 return false;
2869 }
2870
Wale Ogunwale7d701172015-03-11 15:36:30 -07002871 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002872 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002874 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002875 task.taskId, r.shortComponentName, reason);
2876 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002877 final int index = activities.indexOf(r);
2878 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002879 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002880 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002881 // If the caller asked that this activity (and all above it)
2882 // be cleared when the task is reset, don't lose that information,
2883 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002884 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002885 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002886 }
2887 }
2888
2889 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002890
Craig Mautner299f9602015-01-26 09:47:33 -08002891 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002892
Dianne Hackborn5c607432012-02-28 14:44:19 -08002893 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002894
Craig Mautnerde4ef022013-04-07 19:01:33 -07002895 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002896 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002897 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002899 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002900 ? AppTransition.TRANSIT_TASK_CLOSE
2901 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002902
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002903 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002904 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002905
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002906 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002907 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
2908 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2909 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002910 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 }
2912
Craig Mautneraea74a52014-03-08 14:23:10 -08002913 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07002914 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002915 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002916 } else if (r.state != ActivityState.PAUSING) {
2917 // If the activity is PAUSING, we will complete the finish once
2918 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002919 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002920 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002921 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002922 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 }
2924
2925 return false;
2926 }
2927
Craig Mautnerf3333272013-04-22 10:55:53 -07002928 static final int FINISH_IMMEDIATELY = 0;
2929 static final int FINISH_AFTER_PAUSE = 1;
2930 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931
Craig Mautnerf3333272013-04-22 10:55:53 -07002932 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002933 // First things first: if this activity is currently visible,
2934 // and the resumed activity is not yet visible, then hold off on
2935 // finishing until the resumed one becomes visible.
2936 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002937 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2938 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002939 if (mStackSupervisor.mStoppingActivities.size() > 3
2940 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941 // If we already have a few activities waiting to stop,
2942 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002943 // them out. Or if r is the last of activity of the last task the stack
2944 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002945 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002946 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002947 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 }
2949 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002950 if (DEBUG_STATES) Slog.v(TAG_STATES,
2951 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002953 if (oomAdj) {
2954 mService.updateOomAdjLocked();
2955 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 return r;
2957 }
2958
2959 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002960 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002961 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002962 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002963 if (mResumedActivity == r) {
2964 mResumedActivity = null;
2965 }
2966 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002967 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002968 r.state = ActivityState.FINISHING;
2969
2970 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002971 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 || prevState == ActivityState.STOPPED
2973 || prevState == ActivityState.INITIALIZING) {
2974 // If this activity is already stopped, we can just finish
2975 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002976 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002977 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002978 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002979 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002980 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002981 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07002982 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2983 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002984 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002986
2987 // Need to go through the full pause cycle to get this
2988 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002989 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002990 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002991 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002992 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 return r;
2994 }
2995
Craig Mautneree36c772014-07-16 14:56:05 -07002996 void finishAllActivitiesLocked(boolean immediately) {
2997 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002998 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2999 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3000 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3001 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003002 noActivitiesInStack = false;
3003 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003004 continue;
3005 }
Craig Mautneree36c772014-07-16 14:56:05 -07003006 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003007 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3008 }
3009 }
Craig Mautneree36c772014-07-16 14:56:05 -07003010 if (noActivitiesInStack) {
3011 mActivityContainer.onTaskListEmptyLocked();
3012 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003013 }
3014
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003015 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3016 // Basic case: for simple app-centric recents, we need to recreate
3017 // the task if the affinity has changed.
3018 if (srec == null || srec.task.affinity == null ||
3019 !srec.task.affinity.equals(destAffinity)) {
3020 return true;
3021 }
3022 // Document-centric case: an app may be split in to multiple documents;
3023 // they need to re-create their task if this current activity is the root
3024 // of a document, unless simply finishing it will return them to the the
3025 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003026 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3027 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003028 // Okay, this activity is at the root of its task. What to do, what to do...
3029 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3030 // Finishing won't return to an application, so we need to recreate.
3031 return true;
3032 }
3033 // We now need to get the task below it to determine what to do.
3034 int taskIdx = mTaskHistory.indexOf(srec.task);
3035 if (taskIdx <= 0) {
3036 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3037 return false;
3038 }
3039 if (taskIdx == 0) {
3040 // At the bottom of the stack, nothing to go back to.
3041 return true;
3042 }
3043 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3044 if (!srec.task.affinity.equals(prevTask.affinity)) {
3045 // These are different apps, so need to recreate.
3046 return true;
3047 }
3048 }
3049 return false;
3050 }
3051
Wale Ogunwale7d701172015-03-11 15:36:30 -07003052 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003053 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003054 final TaskRecord task = srec.task;
3055 final ArrayList<ActivityRecord> activities = task.mActivities;
3056 final int start = activities.indexOf(srec);
3057 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003058 return false;
3059 }
3060 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003061 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003062 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003063 final ComponentName dest = destIntent.getComponent();
3064 if (start > 0 && dest != null) {
3065 for (int i = finishTo; i >= 0; i--) {
3066 ActivityRecord r = activities.get(i);
3067 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003068 r.info.name.equals(dest.getClassName())) {
3069 finishTo = i;
3070 parent = r;
3071 foundParentInTask = true;
3072 break;
3073 }
3074 }
3075 }
3076
3077 IActivityController controller = mService.mController;
3078 if (controller != null) {
3079 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3080 if (next != null) {
3081 // ask watcher if this is allowed
3082 boolean resumeOK = true;
3083 try {
3084 resumeOK = controller.activityResuming(next.packageName);
3085 } catch (RemoteException e) {
3086 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003087 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003088 }
3089
3090 if (!resumeOK) {
3091 return false;
3092 }
3093 }
3094 }
3095 final long origId = Binder.clearCallingIdentity();
3096 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003097 ActivityRecord r = activities.get(i);
3098 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003099 // Only return the supplied result for the first activity finished
3100 resultCode = Activity.RESULT_CANCELED;
3101 resultData = null;
3102 }
3103
3104 if (parent != null && foundParentInTask) {
3105 final int parentLaunchMode = parent.info.launchMode;
3106 final int destIntentFlags = destIntent.getFlags();
3107 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3108 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3109 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3110 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003111 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3112 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003113 } else {
3114 try {
3115 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3116 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003117 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003118 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003119 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003120 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003121 foundParentInTask = res == ActivityManager.START_SUCCESS;
3122 } catch (RemoteException e) {
3123 foundParentInTask = false;
3124 }
3125 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003126 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003127 }
3128 }
3129 Binder.restoreCallingIdentity(origId);
3130 return foundParentInTask;
3131 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 /**
3133 * Perform the common clean-up of an activity record. This is called both
3134 * as part of destroyActivityLocked() (when destroying the client-side
3135 * representation) and cleaning things up as a result of its hosting
3136 * processing going away, in which case there is no remaining client-side
3137 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003138 *
3139 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003141 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3142 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 if (mResumedActivity == r) {
3144 mResumedActivity = null;
3145 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003146 if (mPausingActivity == r) {
3147 mPausingActivity = null;
3148 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003149 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150
3151 r.configDestroy = false;
3152 r.frozenBeforeDestroy = false;
3153
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003154 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003155 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003156 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003157 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003158 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003159 }
3160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003161 // Make sure this record is no longer in the pending finishes list.
3162 // This could happen, for example, if we are trimming activities
3163 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003164 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003165 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003166
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003167 // Remove any pending results.
3168 if (r.finishing && r.pendingResults != null) {
3169 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3170 PendingIntentRecord rec = apr.get();
3171 if (rec != null) {
3172 mService.cancelIntentSenderLocked(rec, false);
3173 }
3174 }
3175 r.pendingResults = null;
3176 }
3177
3178 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003179 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 }
3181
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003182 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003183 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003184 if (getVisibleBehindActivity() == r) {
3185 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003186 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003187 }
3188
3189 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003190 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003192 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003193 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003194 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 }
3196
Craig Mautner299f9602015-01-26 09:47:33 -08003197 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003198 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003199 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003200 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003201 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3202 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3203
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003204 r.takeFromHistory();
3205 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003206 if (DEBUG_STATES) Slog.v(TAG_STATES,
3207 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003208 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003209 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003210 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003211 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003212 if (VALIDATE_TOKENS) {
3213 validateAppTokensLocked();
3214 }
Craig Mautner312ba862014-02-10 17:55:01 -08003215 final TaskRecord task = r.task;
3216 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003217 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003218 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003219 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3220 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003221 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003222 }
Craig Mautner299f9602015-01-26 09:47:33 -08003223 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003224 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003225 cleanUpActivityServicesLocked(r);
3226 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003227 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003228
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 /**
3230 * Perform clean-up of service connections in an activity record.
3231 */
3232 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3233 // Throw away any services that have been bound by this activity.
3234 if (r.connections != null) {
3235 Iterator<ConnectionRecord> it = r.connections.iterator();
3236 while (it.hasNext()) {
3237 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003238 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003239 }
3240 r.connections = null;
3241 }
3242 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003243
Craig Mautneree2e45a2014-06-27 12:10:03 -07003244 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003245 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003246 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003247 mHandler.sendMessage(msg);
3248 }
3249
Craig Mautneree2e45a2014-06-27 12:10:03 -07003250 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003251 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003252 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003253 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3254 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3255 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3256 final ActivityRecord r = activities.get(activityNdx);
3257 if (r.finishing) {
3258 continue;
3259 }
3260 if (r.fullscreen) {
3261 lastIsOpaque = true;
3262 }
3263 if (owner != null && r.app != owner) {
3264 continue;
3265 }
3266 if (!lastIsOpaque) {
3267 continue;
3268 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003269 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003270 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003271 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003272 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003273 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003274 activityRemoved = true;
3275 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003276 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003277 }
3278 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003279 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003280 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003281 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003282 }
3283
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003284 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3285 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003286 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3287 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003288 + " pausing=" + mPausingActivity + " for reason " + reason);
3289 return destroyActivityLocked(r, true, reason);
3290 }
3291 return false;
3292 }
3293
3294 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3295 String reason) {
3296 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003297 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003298 int maxTasks = tasks.size() / 4;
3299 if (maxTasks < 1) {
3300 maxTasks = 1;
3301 }
3302 int numReleased = 0;
3303 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3304 final TaskRecord task = mTaskHistory.get(taskNdx);
3305 if (!tasks.contains(task)) {
3306 continue;
3307 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003308 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003309 int curNum = 0;
3310 final ArrayList<ActivityRecord> activities = task.mActivities;
3311 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3312 final ActivityRecord activity = activities.get(actNdx);
3313 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003314 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003315 + " in state " + activity.state + " resumed=" + mResumedActivity
3316 + " pausing=" + mPausingActivity + " for reason " + reason);
3317 destroyActivityLocked(activity, true, reason);
3318 if (activities.get(actNdx) != activity) {
3319 // Was removed from list, back up so we don't miss the next one.
3320 actNdx--;
3321 }
3322 curNum++;
3323 }
3324 }
3325 if (curNum > 0) {
3326 numReleased += curNum;
3327 maxTasks--;
3328 if (mTaskHistory.get(taskNdx) != task) {
3329 // The entire task got removed, back up so we don't miss the next one.
3330 taskNdx--;
3331 }
3332 }
3333 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003334 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3335 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003336 return numReleased;
3337 }
3338
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003339 /**
3340 * Destroy the current CLIENT SIDE instance of an activity. This may be
3341 * called both when actually finishing an activity, or when performing
3342 * a configuration switch where we destroy the current client-side object
3343 * but then create a new client-side object for this same HistoryRecord.
3344 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003345 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003346 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3347 "Removing activity from " + reason + ": token=" + r
3348 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003350 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003351 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352
3353 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003354
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003355 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003356
3357 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003358
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003359 if (hadApp) {
3360 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003361 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003362 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3363 mService.mHeavyWeightProcess = null;
3364 mService.mHandler.sendEmptyMessage(
3365 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3366 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003367 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003368 // Update any services we are bound to that might care about whether
3369 // their client may have activities.
3370 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003371 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003372 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003373 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003374 }
3375 }
3376
3377 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003378
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003379 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003380 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003381 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 r.configChangeFlags);
3383 } catch (Exception e) {
3384 // We can just ignore exceptions here... if the process
3385 // has crashed, our death notification will clean things
3386 // up.
3387 //Slog.w(TAG, "Exception thrown during finish", e);
3388 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003389 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003390 removedFromHistory = true;
3391 skipDestroy = true;
3392 }
3393 }
3394
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003395 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003396
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003397 // If the activity is finishing, we need to wait on removing it
3398 // from the list to give it a chance to do its cleanup. During
3399 // that time it may make calls back with its token so we need to
3400 // be able to find it on the list and so we don't want to remove
3401 // it from the list yet. Otherwise, we can just immediately put
3402 // it in the destroyed state since we are not removing it from the
3403 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003404 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003405 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003406 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003407 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003408 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003409 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3410 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003411 if (DEBUG_STATES) Slog.v(TAG_STATES,
3412 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003413 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003414 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003415 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003416 }
3417 } else {
3418 // remove this record from the history.
3419 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003420 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003421 removedFromHistory = true;
3422 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003423 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003424 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003425 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003426 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 }
3428 }
3429
3430 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003431
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003432 if (!mLRUActivities.remove(r) && hadApp) {
3433 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3434 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003435
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003436 return removedFromHistory;
3437 }
3438
Craig Mautner299f9602015-01-26 09:47:33 -08003439 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003440 final long origId = Binder.clearCallingIdentity();
3441 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003442 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003443 if (r != null) {
3444 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003445 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003446 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003447
Wale Ogunwale60454db2015-01-23 16:05:07 -08003448 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003449 if (r.state == ActivityState.DESTROYING) {
3450 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003451 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003452 }
3453 }
Craig Mautner05d29032013-05-03 13:40:13 -07003454 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003455 } finally {
3456 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 }
3458 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003459
Todd Kennedyaab56db2015-01-30 09:39:53 -08003460 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003461 if (hasVisibleBehindActivity() &&
3462 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003463 if (r == topRunningActivityLocked(null)) {
3464 // Don't release the top activity if it has requested to run behind the next
3465 // activity.
3466 return;
3467 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003468 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003469 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003470 " thread=" + r.app.thread);
3471 if (r != null && r.app != null && r.app.thread != null) {
3472 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003473 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003474 } catch (RemoteException e) {
3475 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003476 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003477 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003478 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003479 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003480 }
3481 }
3482 }
3483
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003484 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003485 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3486 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003487 if (r != null) {
3488 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003489 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003490 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003491 }
3492 mStackSupervisor.resumeTopActivitiesLocked();
3493 }
3494
Jose Lima4b6c6692014-08-12 17:41:12 -07003495 boolean hasVisibleBehindActivity() {
3496 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003497 }
3498
Jose Lima4b6c6692014-08-12 17:41:12 -07003499 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003500 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003501 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003502 }
3503 }
3504
Jose Lima4b6c6692014-08-12 17:41:12 -07003505 ActivityRecord getVisibleBehindActivity() {
3506 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003507 }
3508
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003509 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3510 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003511 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003512 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3513 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003514 while (i > 0) {
3515 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003516 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003517 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003518 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003519 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003521 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 }
3523 }
3524 }
3525
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003526 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3527 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003528 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3529 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003530 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3531 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003532 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003533 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003534 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3535 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003536
3537 boolean hasVisibleActivities = false;
3538
3539 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003540 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003541 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3542 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003543 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3544 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3545 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3546 final ActivityRecord r = activities.get(activityNdx);
3547 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003548 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3549 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003550 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003551 if (r.visible) {
3552 hasVisibleActivities = true;
3553 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003554 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003555 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3556 // Don't currently have state for the activity, or
3557 // it is finishing -- always remove it.
3558 remove = true;
3559 } else if (r.launchCount > 2 &&
3560 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3561 // We have launched this activity too many times since it was
3562 // able to run, so give up and remove it.
3563 remove = true;
3564 } else {
3565 // The process may be gone, but the activity lives on!
3566 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003567 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003568 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003569 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3570 "Removing activity " + r + " from stack at " + i
3571 + ": haveState=" + r.haveState
3572 + " stateNotNeeded=" + r.stateNotNeeded
3573 + " finishing=" + r.finishing
3574 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003575 if (!r.finishing) {
3576 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3577 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3578 r.userId, System.identityHashCode(r),
3579 r.task.taskId, r.shortComponentName,
3580 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003581 if (r.state == ActivityState.RESUMED) {
3582 mService.updateUsageStats(r, false);
3583 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003584 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003585 } else {
3586 // We have the current state for this activity, so
3587 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003588 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3589 if (DEBUG_APP) Slog.v(TAG_APP,
3590 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003591 r.app = null;
3592 r.nowVisible = false;
3593 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003594 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003595 "App died, clearing saved state of " + r);
3596 r.icicle = null;
3597 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003598 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003599 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003600 if (remove) {
3601 removeActivityFromHistoryLocked(r, "appDied");
3602 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003603 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003604 }
3605 }
3606
3607 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003609
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003610 final void updateTransitLocked(int transit, Bundle options) {
3611 if (options != null) {
3612 ActivityRecord r = topRunningActivityLocked(null);
3613 if (r != null && r.state != ActivityState.RESUMED) {
3614 r.updateOptionsLocked(options);
3615 } else {
3616 ActivityOptions.abort(options);
3617 }
3618 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003619 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003620 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003621
Craig Mautner21d24a22014-04-23 11:45:37 -07003622 void updateTaskMovement(TaskRecord task, boolean toFront) {
3623 if (task.isPersistable) {
3624 task.mLastTimeMoved = System.currentTimeMillis();
3625 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3626 // recently will be most negative, tasks sent to the bottom before that will be less
3627 // negative. Similarly for recent tasks moved to the top which will be most positive.
3628 if (!toFront) {
3629 task.mLastTimeMoved *= -1;
3630 }
3631 }
3632 }
3633
Craig Mautner84984fa2014-06-19 11:19:20 -07003634 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003635 final int top = mTaskHistory.size() - 1;
3636 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3637 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003638 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003639 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3640 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003641 mTaskHistory.remove(taskNdx);
3642 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003643 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003644 return;
3645 }
3646 }
3647 }
3648
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003649 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003650 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003651 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003652
Craig Mautner11bf9a52013-02-19 14:08:51 -08003653 final int numTasks = mTaskHistory.size();
3654 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003655 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003656 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003657 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003658 ActivityOptions.abort(options);
3659 } else {
3660 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3661 }
3662 return;
3663 }
3664
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003665 if (timeTracker != null) {
3666 // The caller wants a time tracker associated with this task.
3667 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3668 tr.mActivities.get(i).appTimeTracker = timeTracker;
3669 }
3670 }
3671
Craig Mautner11bf9a52013-02-19 14:08:51 -08003672 // Shift all activities with this task up to the top
3673 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003674 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003675
3676 // Set focus to the top running activity of this stack.
3677 ActivityRecord r = topRunningActivityLocked(null);
3678 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003679
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003680 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003681 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003682 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003683 if (r != null) {
3684 mNoAnimActivities.add(r);
3685 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003686 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003687 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003688 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003690
Craig Mautner05d29032013-05-03 13:40:13 -07003691 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003692 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003693
3694 if (VALIDATE_TOKENS) {
3695 validateAppTokensLocked();
3696 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003697 }
3698
3699 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003700 * Worker method for rearranging history stack. Implements the function of moving all
3701 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003703 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003704 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3705 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003706 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003707 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003708 * @return Returns true if the move completed, false if not.
3709 */
Craig Mautner299f9602015-01-26 09:47:33 -08003710 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003711 final TaskRecord tr = taskForIdLocked(taskId);
3712 if (tr == null) {
3713 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3714 return false;
3715 }
3716
3717 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003718 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003719
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003720 // If we have a watcher, preflight the move before committing to it. First check
3721 // for *other* available tasks, but if none are available, then try again allowing the
3722 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003723 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003724 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 if (next == null) {
3726 next = topRunningActivityLocked(null, 0);
3727 }
3728 if (next != null) {
3729 // ask watcher if this is allowed
3730 boolean moveOK = true;
3731 try {
3732 moveOK = mService.mController.activityResuming(next.packageName);
3733 } catch (RemoteException e) {
3734 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003735 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736 }
3737 if (!moveOK) {
3738 return false;
3739 }
3740 }
3741 }
3742
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003743 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003744
riddle_hsuc215a4f2014-12-27 12:10:45 +08003745 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003746
3747 // If true, we should resume the home activity next if the task we are moving to the
3748 // back is over the home stack. We force to false if the task we are moving to back
3749 // is the home task and we don't want it resumed after moving to the back.
3750 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3751 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003752 final TaskRecord nextTask = getNextTask(tr);
3753 if (nextTask != null) {
3754 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3755 } else {
3756 prevIsHome = true;
3757 }
3758 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003759 mTaskHistory.remove(tr);
3760 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003761 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003762
Craig Mautnerc8143c62013-09-03 12:15:57 -07003763 // There is an assumption that moving a task to the back moves it behind the home activity.
3764 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003765 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003766 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3767 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003768 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003769 break;
3770 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003771 if (taskNdx == 1) {
3772 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003773 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003774 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003775 }
3776
Craig Mautner299f9602015-01-26 09:47:33 -08003777 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003778 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003779
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003780 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003781 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003782 }
3783
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003784 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003785 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003786 if (!mService.mBooting && !mService.mBooted) {
3787 // Not ready yet!
3788 return false;
3789 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003790 final int taskToReturnTo = tr.getTaskToReturnTo();
3791 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003792 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003793 }
3794
Craig Mautner05d29032013-05-03 13:40:13 -07003795 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796 return true;
3797 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003798
Craig Mautner8849a5e2013-04-02 16:41:03 -07003799 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003800 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003801 final Uri data = r.intent.getData();
3802 final String strData = data != null ? data.toSafeString() : null;
3803
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003804 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003805 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003806 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003807 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 }
3809
3810 /**
3811 * Make sure the given activity matches the current configuration. Returns
3812 * false if the activity had to be destroyed. Returns true if the
3813 * configuration is the same, or the activity will remain running as-is
3814 * for whatever reason. Ensures the HistoryRecord is updated with the
3815 * correct configuration and all other bookkeeping is handled.
3816 */
3817 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3818 int globalChanges) {
3819 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003820 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003821 "Skipping config check (will change): " + r);
3822 return true;
3823 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003824
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003825 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003826 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003827
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003828 // Make sure the current stack override configuration is supported by the top task
3829 // before continuing.
3830 final TaskRecord topTask = topTask();
3831 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3832 || (!topTask.mResizeable && !mFullscreen))) {
3833 final boolean prevFullscreen = mFullscreen;
3834 final Configuration newOverrideConfig =
3835 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3836 updateOverrideConfiguration(newOverrideConfig);
3837 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003838 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003839 "Updated stack config to support task=" + topTask
3840 + " resizeable=" + topTask.mResizeable
3841 + " mForcedFullscreen=" + mForcedFullscreen
3842 + " prevFullscreen=" + prevFullscreen
3843 + " mFullscreen=" + mFullscreen);
3844 }
3845
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003846 // Short circuit: if the two configurations are the exact same
3847 // object (the common case), then there is nothing to do.
3848 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003849 if (r.configuration == newConfig
3850 && r.stackConfigOverride == mOverrideConfig
3851 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003852 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003853 "Configuration unchanged in " + r);
3854 return true;
3855 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003856
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003857 // We don't worry about activities that are finishing.
3858 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003859 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003860 "Configuration doesn't matter in finishing " + r);
3861 r.stopFreezingScreenLocked(false);
3862 return true;
3863 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003864
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003865 // Okay we now are going to make this activity have the new config.
3866 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003867 final Configuration oldConfig = r.configuration;
3868 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003869 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003870 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003871
3872 // Determine what has changed. May be nothing, if this is a config
3873 // that has come back from the app after going idle. In that case
3874 // we just want to leave the official config object now in the
3875 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003876 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003877 if (stackChanges == 0) {
3878 // {@link Configuration#diff} doesn't catch changes from unset values.
3879 // Check for changes we care about.
3880 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3881 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3882 }
3883 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3884 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3885 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3886 }
3887 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3888 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3889 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003890 }
3891 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003892 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003893 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003894 "Configuration no differences in " + r);
3895 return true;
3896 }
3897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003898 // If the activity isn't currently running, just leave the new
3899 // configuration and it will pick that up next time it starts.
3900 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003901 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 "Configuration doesn't matter not running " + r);
3903 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003904 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003905 return true;
3906 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003907
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003908 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003909 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3910 "Checking to restart " + r.info.name + ": changed=0x"
3911 + Integer.toHexString(changes) + ", handles=0x"
3912 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3913
Dianne Hackborne6676352011-06-01 16:51:20 -07003914 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003915 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3916 r.configChangeFlags |= changes;
3917 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003918 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003919 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003920 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003921 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003922 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003923 } else if (r.state == ActivityState.PAUSING) {
3924 // A little annoying: we are waiting for this activity to
3925 // finish pausing. Let's not do anything now, but just
3926 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003927 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003928 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003929 r.configDestroy = true;
3930 return true;
3931 } else if (r.state == ActivityState.RESUMED) {
3932 // Try to optimize this case: the configuration is changing
3933 // and we need to restart the top, resumed activity.
3934 // Instead of doing the normal handshaking, just say
3935 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003936 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003937 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003938 relaunchActivityLocked(r, r.configChangeFlags, true);
3939 r.configChangeFlags = 0;
3940 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003941 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003942 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003943 relaunchActivityLocked(r, r.configChangeFlags, false);
3944 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003946
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003947 // All done... tell the caller we weren't able to keep this
3948 // activity around.
3949 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003950 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003951
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003952 // Default case: the activity can handle this new configuration, so hand it over.
3953 // NOTE: We only forward the stack override configuration as the system level configuration
3954 // changes is always sent to all processes when they happen so it can just use whatever
3955 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 if (r.app != null && r.app.thread != null) {
3957 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003958 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003959 r.app.thread.scheduleActivityConfigurationChanged(
3960 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003961 } catch (RemoteException e) {
3962 // If process died, whatever.
3963 }
3964 }
3965 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003966
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003967 return true;
3968 }
3969
Craig Mautner2568c3a2015-03-26 14:22:34 -07003970 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003971 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003972 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003973 if (andResume) {
3974 results = r.results;
3975 newIntents = r.newIntents;
3976 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003977 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3978 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 + " andResume=" + andResume);
3980 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003981 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003982 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003983
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003984 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003985
Craig Mautner34b73df2014-01-12 21:11:08 -08003986 mStackSupervisor.removeChildActivityContainers(r);
3987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003988 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003989 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
3990 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003991 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003992 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3993 !andResume, new Configuration(mService.mConfiguration),
3994 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003995 // Note: don't need to call pauseIfSleepingLocked() here, because
3996 // the caller will only pass in 'andResume' if this activity is
3997 // currently resumed, which implies we aren't sleeping.
3998 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003999 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004000 }
4001
4002 if (andResume) {
4003 r.results = null;
4004 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004005 r.state = ActivityState.RESUMED;
4006 } else {
4007 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4008 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004009 }
4010
4011 return true;
4012 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004013
4014 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004015 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4016 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4017 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4018 final ActivityRecord r = activities.get(activityNdx);
4019 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004020 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004021 }
4022 if (r.fullscreen && !r.finishing) {
4023 return false;
4024 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004025 }
4026 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004027 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004028 if (r == null) {
4029 return false;
4030 }
4031 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4032 + " would have returned true for r=" + r);
4033 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004034 }
4035
4036 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004037 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4038 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4039 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4040 final ActivityRecord r = activities.get(activityNdx);
4041 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004042 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004043 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004044 }
4045 }
4046 }
4047
Wale Ogunwale540e1232015-05-01 15:35:39 -07004048 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4049 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004050 boolean didSomething = false;
4051 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004052 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004053 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4054 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4055 int numActivities = activities.size();
4056 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4057 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004058 final boolean sameComponent =
4059 (r.packageName.equals(packageName) && (filterByClasses == null
4060 || filterByClasses.contains(r.realActivity.getClassName())))
4061 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004062 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004063 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004064 && (r.app == null || evenPersistent || !r.app.persistent)) {
4065 if (!doit) {
4066 if (r.finishing) {
4067 // If this activity is just finishing, then it is not
4068 // interesting as far as something to stop.
4069 continue;
4070 }
4071 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004072 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004073 if (r.isHomeActivity()) {
4074 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4075 Slog.i(TAG, "Skip force-stop again " + r);
4076 continue;
4077 } else {
4078 homeActivity = r.realActivity;
4079 }
4080 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004081 didSomething = true;
4082 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004083 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004084 if (r.app != null) {
4085 r.app.removed = true;
4086 }
4087 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004088 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004089 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004090 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4091 true)) {
4092 // r has been deleted from mActivities, accommodate.
4093 --numActivities;
4094 --activityNdx;
4095 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004096 }
4097 }
4098 }
4099 return didSomething;
4100 }
4101
Dianne Hackborn09233282014-04-30 11:33:59 -07004102 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004103 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4104 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004105 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004106 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004107 if (task.getTopActivity() == null) {
4108 continue;
4109 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004110 ActivityRecord r = null;
4111 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004112 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004113 int numActivities = 0;
4114 int numRunning = 0;
4115 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004116 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004117 continue;
4118 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004119 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004120 tmp = activities.get(activityNdx);
4121 if (tmp.finishing) {
4122 continue;
4123 }
4124 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004125
Craig Mautneraab647e2013-02-28 16:31:36 -08004126 // Initialize state for next task if needed.
4127 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4128 top = r;
4129 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004130 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004131
4132 // Add 'r' into the current task.
4133 numActivities++;
4134 if (r.app != null && r.app.thread != null) {
4135 numRunning++;
4136 }
4137
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004138 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004139 TAG, r.intent.getComponent().flattenToShortString()
4140 + ": task=" + r.task);
4141 }
4142
4143 RunningTaskInfo ci = new RunningTaskInfo();
4144 ci.id = task.taskId;
4145 ci.baseActivity = r.intent.getComponent();
4146 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004147 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004148 if (focusedStack && topTask) {
4149 // Give the latest time to ensure foreground task can be sorted
4150 // at the first, because lastActiveTime of creating task is 0.
4151 ci.lastActiveTime = System.currentTimeMillis();
4152 topTask = false;
4153 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004154
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004155 if (top.task != null) {
4156 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004157 }
4158 ci.numActivities = numActivities;
4159 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004160 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004161 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004162 }
4163
4164 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004165 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004166 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004167 if (top >= 0) {
4168 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4169 int activityTop = activities.size() - 1;
4170 if (activityTop > 0) {
4171 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4172 "unhandled-back", true);
4173 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004174 }
4175 }
4176
Craig Mautner6b74cb52013-09-27 17:02:21 -07004177 /**
4178 * Reset local parameters because an app's activity died.
4179 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004180 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004181 */
4182 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004183 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004184 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004185 "App died while pausing: " + mPausingActivity);
4186 mPausingActivity = null;
4187 }
4188 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4189 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004190 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004191 }
4192
Craig Mautner19091252013-10-05 00:03:53 -07004193 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004194 }
4195
Craig Mautnercae015f2013-02-08 14:31:27 -08004196 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004197 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4198 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4199 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4200 final ActivityRecord r = activities.get(activityNdx);
4201 if (r.app == app) {
4202 Slog.w(TAG, " Force finishing activity "
4203 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004204 // Force the destroy to skip right to removal.
4205 r.app = null;
4206 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004207 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004208 }
4209 }
4210 }
4211
Dianne Hackborn390517b2013-05-30 15:03:32 -07004212 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004213 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004214 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004215 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4216 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004217 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4218 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004219 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004220 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004221 if (printed) {
4222 header = null;
4223 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004224 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004225 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004226 }
4227
4228 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4229 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4230
4231 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004232 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4233 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004234 }
4235 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004236 final int top = mTaskHistory.size() - 1;
4237 if (top >= 0) {
4238 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4239 int listTop = list.size() - 1;
4240 if (listTop >= 0) {
4241 activities.add(list.get(listTop));
4242 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004243 }
4244 } else {
4245 ItemMatcher matcher = new ItemMatcher();
4246 matcher.build(name);
4247
Craig Mautneraab647e2013-02-28 16:31:36 -08004248 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4249 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4250 if (matcher.match(r1, r1.intent.getComponent())) {
4251 activities.add(r1);
4252 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004253 }
4254 }
4255 }
4256
4257 return activities;
4258 }
4259
4260 ActivityRecord restartPackage(String packageName) {
4261 ActivityRecord starting = topRunningActivityLocked(null);
4262
4263 // All activities that came from the package must be
4264 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004265 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4266 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4267 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4268 final ActivityRecord a = activities.get(activityNdx);
4269 if (a.info.packageName.equals(packageName)) {
4270 a.forceNewConfig = true;
4271 if (starting != null && a == starting && a.visible) {
4272 a.startFreezingScreenLocked(starting.app,
4273 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4274 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004275 }
4276 }
4277 }
4278
4279 return starting;
4280 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004281
Craig Mautner299f9602015-01-26 09:47:33 -08004282 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07004283 removeTask(task, reason, true /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004284 }
4285
Wale Ogunwale000957c2015-04-03 08:19:12 -07004286 /**
4287 * Removes the input task from this stack.
4288 * @param task to remove.
4289 * @param reason for removal.
4290 * @param notMoving task to another stack. In the case we are moving we don't want to perform
4291 * some operations on the task like removing it from window manager or recents.
4292 */
4293 void removeTask(TaskRecord task, String reason, boolean notMoving) {
4294 if (notMoving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004295 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004296 mWindowManager.removeTask(task.taskId);
4297 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004298
Craig Mautner04a0ea62014-01-13 12:51:26 -08004299 final ActivityRecord r = mResumedActivity;
4300 if (r != null && r.task == task) {
4301 mResumedActivity = null;
4302 }
4303
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004304 final int taskNdx = mTaskHistory.indexOf(task);
4305 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004306 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4307 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4308 if (!nextTask.isOverHomeStack()) {
4309 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4310 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004311 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004312 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004313 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004314
Wale Ogunwale000957c2015-04-03 08:19:12 -07004315 if (notMoving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004316 final boolean isVoiceSession = task.voiceSession != null;
4317 if (isVoiceSession) {
4318 try {
4319 task.voiceSession.taskFinished(task.intent, task.taskId);
4320 } catch (RemoteException e) {
4321 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004322 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004323 if (task.autoRemoveFromRecents() || isVoiceSession) {
4324 // Task creator asked to remove this when done, or this task was a voice
4325 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004326 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004327 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004328 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004329 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004330
4331 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004332 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004333 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004334 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004335 String myReason = reason + " leftTaskHistoryEmpty";
4336 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4337 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4338 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004339 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004340 if (mStacks != null) {
4341 mStacks.remove(this);
4342 mStacks.add(0, this);
4343 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004344 if (notHomeStack) {
4345 mActivityContainer.onTaskListEmptyLocked();
4346 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004347 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004348
4349 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004350 }
4351
Dianne Hackborn91097de2014-04-04 18:02:06 -07004352 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4353 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4354 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004355 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4356 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004357 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004358 return task;
4359 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004360
4361 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004362 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004363 }
4364
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004365 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004366 task.stack = this;
4367 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004368 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004369 } else {
4370 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004371 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004372 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004373 if (!moving && task.voiceSession != null) {
4374 try {
4375 task.voiceSession.taskStarted(task.intent, task.taskId);
4376 } catch (RemoteException e) {
4377 }
4378 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004379 }
4380
4381 public int getStackId() {
4382 return mStackId;
4383 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004384
4385 @Override
4386 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004387 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4388 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004389 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004390
4391 boolean updateOverrideConfiguration(Configuration newConfig) {
4392 Configuration oldConfig = mOverrideConfig;
4393 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004394 // We override the configuration only when the stack's dimensions are different from
4395 // the display. In this manner, we know that if the override configuration is empty,
4396 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004397 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004398 return !mOverrideConfig.equals(oldConfig);
4399 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004400
4401 void onLockTaskPackagesUpdatedLocked() {
4402 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4403 mTaskHistory.get(taskNdx).setLockTaskAuth();
4404 }
4405 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004406}