blob: 431b4338552b8f5f12e7f8f7ab9b134cde0457be [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.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700116 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700117
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.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700120 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.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700128 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;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700217 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700218
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700219 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700220 * Set when we know we are going to be calling updateConfiguration()
221 * soon, so want to skip intermediate config checks.
222 */
223 boolean mConfigWillChange;
224
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700225 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800226 boolean mFullscreen = true;
227
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700228 long mLaunchStartTime = 0;
229 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800230
Craig Mautner858d8a62013-04-23 17:08:34 -0700231 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700232
Craig Mautnerc00204b2013-03-05 15:02:14 -0800233 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800234 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800235 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
236 ArrayList<ActivityStack> mStacks;
237 /** The attached Display's unique identifier, or -1 if detached */
238 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800239
Craig Mautner27084302013-03-25 08:05:25 -0700240 /** Run all ActivityStacks through this */
241 final ActivityStackSupervisor mStackSupervisor;
242
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700243 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700244 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
245 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
246 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
247 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700248 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700249 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700250 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700251
252 static class ScheduleDestroyArgs {
253 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700254 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700255 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700256 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257 mReason = reason;
258 }
259 }
260
Zoran Marcetaf958b322012-08-09 20:27:12 +0900261 final Handler mHandler;
262
263 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800264
Craig Mautnerc8143c62013-09-03 12:15:57 -0700265 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900266 super(looper);
267 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268
Zoran Marcetaf958b322012-08-09 20:27:12 +0900269 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 public void handleMessage(Message msg) {
271 switch (msg.what) {
272 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800273 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274 // We don't at this point know if the activity is fullscreen,
275 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800276 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700277 synchronized (mService) {
278 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700279 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700280 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700281 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800282 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700283 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 case LAUNCH_TICK_MSG: {
285 ActivityRecord r = (ActivityRecord)msg.obj;
286 synchronized (mService) {
287 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700288 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700289 }
290 }
291 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800293 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700294 // We don't at this point know if the activity is fullscreen,
295 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800296 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800297 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800298 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800299 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700301 case STOP_TIMEOUT_MSG: {
302 ActivityRecord r = (ActivityRecord)msg.obj;
303 // We don't at this point know if the activity is fullscreen,
304 // so we need to be conservative and assume it isn't.
305 Slog.w(TAG, "Activity stop timeout for " + r);
306 synchronized (mService) {
307 if (r.isInHistory()) {
308 activityStoppedLocked(r, null, null, null);
309 }
310 }
311 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700312 case DESTROY_ACTIVITIES_MSG: {
313 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
314 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700315 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700316 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700317 } break;
318 case TRANSLUCENT_TIMEOUT_MSG: {
319 synchronized (mService) {
320 notifyActivityDrawnLocked(null);
321 }
322 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700323 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700324 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700325 final ActivityRecord r = getVisibleBehindActivity();
326 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700327 if (r != null) {
328 mService.killAppAtUsersRequest(r.app, null);
329 }
330 }
331 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700332 }
333 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800334 }
335
Craig Mautner34b73df2014-01-12 21:11:08 -0800336 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800337 int count = 0;
338 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
339 count += mTaskHistory.get(taskNdx).mActivities.size();
340 }
341 return count;
342 }
343
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800344 int numTasks() {
345 return mTaskHistory.size();
346 }
347
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800348 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
349 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800350 mActivityContainer = activityContainer;
351 mStackSupervisor = activityContainer.getOuter();
352 mService = mStackSupervisor.mService;
353 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
354 mWindowManager = mService.mWindowManager;
355 mStackId = activityContainer.mStackId;
356 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800357 mRecentTasks = recentTasks;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700358 }
Craig Mautner5962b122012-10-05 14:45:52 -0700359
Amith Yamasani734983f2014-03-04 16:48:05 -0800360 boolean okToShowLocked(ActivityRecord r) {
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700361 return mStackSupervisor.isCurrentProfileLocked(r.userId)
362 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0;
Craig Mautner5962b122012-10-05 14:45:52 -0700363 }
364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800366 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700367 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
368 if (r != null) {
369 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800370 }
371 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 return null;
373 }
374
375 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800376 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
377 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800378 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800379 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
380 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800381 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800382 return r;
383 }
384 }
385 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386 return null;
387 }
388
389 /**
390 * This is a simplified version of topRunningActivityLocked that provides a number of
391 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800392 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 * @param token If non-null, any history records matching this token will be skipped.
394 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800395 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700396 * @return Returns the HistoryRecord of the next activity on the stack.
397 */
398 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800399 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
400 TaskRecord task = mTaskHistory.get(taskNdx);
401 if (task.taskId == taskId) {
402 continue;
403 }
404 ArrayList<ActivityRecord> activities = task.mActivities;
405 for (int i = activities.size() - 1; i >= 0; --i) {
406 final ActivityRecord r = activities.get(i);
407 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800408 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800409 return r;
410 }
411 }
412 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700413 return null;
414 }
415
Craig Mautner8849a5e2013-04-02 16:41:03 -0700416 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700417 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
418 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700419 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
420 final ActivityRecord r = activities.get(activityNdx);
421 if (!r.finishing) {
422 return r;
423 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700424 }
425 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700426 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700427 }
428
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700429 final TaskRecord topTask() {
430 final int size = mTaskHistory.size();
431 if (size > 0) {
432 return mTaskHistory.get(size - 1);
433 }
434 return null;
435 }
436
Craig Mautnerd2328952013-03-05 12:46:26 -0800437 TaskRecord taskForIdLocked(int id) {
438 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
439 final TaskRecord task = mTaskHistory.get(taskNdx);
440 if (task.taskId == id) {
441 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800442 }
443 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700444 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700445 }
446
Craig Mautnerd2328952013-03-05 12:46:26 -0800447 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700448 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800449 return isInStackLocked(r);
450 }
451
452 ActivityRecord isInStackLocked(ActivityRecord r) {
453 if (r == null) {
454 return null;
455 }
456 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700457 if (task != null && task.stack != null
458 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800459 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800460 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800461 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800462 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800463 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800464 }
465
Craig Mautner2420ead2013-04-01 17:13:20 -0700466 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700467 final boolean hadit = mLRUActivities.remove(r);
468 mLRUActivities.add(r);
469 return hadit;
470 }
471
Craig Mautnerde4ef022013-04-07 19:01:33 -0700472 final boolean isHomeStack() {
473 return mStackId == HOME_STACK_ID;
474 }
475
Craig Mautnere0a38842013-12-16 16:14:02 -0800476 final boolean isOnHomeDisplay() {
477 return isAttached() &&
478 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
479 }
480
Craig Mautner299f9602015-01-26 09:47:33 -0800481 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800482 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800483 final boolean homeStack = isHomeStack()
484 || (mActivityContainer.mParentActivity != null
485 && mActivityContainer.mParentActivity.isHomeActivity());
486 ActivityStack lastFocusStack = null;
487 if (!homeStack) {
488 // Need to move this stack to the front before calling
489 // {@link ActivityStackSupervisor#moveHomeStack} below.
490 lastFocusStack = mStacks.get(mStacks.size() - 1);
491 mStacks.remove(this);
492 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800493 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700494 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
495 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800496 if (isOnHomeDisplay()) {
497 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
498 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800499 final TaskRecord task = topTask();
500 if (task != null) {
501 mWindowManager.moveTaskToTop(task.taskId);
502 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800503 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800504 }
505
506 final boolean isAttached() {
507 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 }
509
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700510 /**
511 * Returns the top activity in any existing task matching the given
512 * Intent. Returns null if no such task is found.
513 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700514 ActivityRecord findTaskLocked(ActivityRecord target) {
515 Intent intent = target.intent;
516 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700517 ComponentName cls = intent.getComponent();
518 if (info.targetActivity != null) {
519 cls = new ComponentName(info.packageName, info.targetActivity);
520 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700521 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700522 boolean isDocument = intent != null & intent.isDocument();
523 // If documentData is non-null then it must match the existing task data.
524 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800525
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700526 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800527 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
528 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700529 if (task.voiceSession != null) {
530 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700531 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700532 continue;
533 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700534 if (task.userId != userId) {
535 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700536 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700537 continue;
538 }
Craig Mautner000f0022013-02-26 15:04:29 -0800539 final ActivityRecord r = task.getTopActivity();
540 if (r == null || r.finishing || r.userId != userId ||
541 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700542 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800543 continue;
544 }
545
Craig Mautnerd00f4742014-03-12 14:17:26 -0700546 final Intent taskIntent = task.intent;
547 final Intent affinityIntent = task.affinityIntent;
548 final boolean taskIsDocument;
549 final Uri taskDocumentData;
550 if (taskIntent != null && taskIntent.isDocument()) {
551 taskIsDocument = true;
552 taskDocumentData = taskIntent.getData();
553 } else if (affinityIntent != null && affinityIntent.isDocument()) {
554 taskIsDocument = true;
555 taskDocumentData = affinityIntent.getData();
556 } else {
557 taskIsDocument = false;
558 taskDocumentData = null;
559 }
560
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700561 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700562 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700563 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700564 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700565 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
566 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700567 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700568 return r;
569 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700570 } else if (taskIntent != null && taskIntent.getComponent() != null &&
571 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700572 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700573 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800574 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700575 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
576 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800577 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700578 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
579 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700580 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700581 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800582 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
584 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800585 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700586 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587 }
588
589 return null;
590 }
591
592 /**
593 * Returns the first activity (starting from the top of the stack) that
594 * is the same as the given activity. Returns null if no such activity
595 * is found.
596 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700597 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700598 ComponentName cls = intent.getComponent();
599 if (info.targetActivity != null) {
600 cls = new ComponentName(info.packageName, info.targetActivity);
601 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700602 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700603
Craig Mautner000f0022013-02-26 15:04:29 -0800604 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700605 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700606 final boolean notCurrentUserTask =
607 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700608 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700609
Craig Mautner000f0022013-02-26 15:04:29 -0800610 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
611 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700612 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700613 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700614 }
Craig Mautner000f0022013-02-26 15:04:29 -0800615 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700616 return r;
617 }
618 }
619 }
620
621 return null;
622 }
623
Amith Yamasani742a6712011-05-04 14:49:28 -0700624 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700625 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700626 */
Craig Mautner93529a42013-10-04 15:03:13 -0700627 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700629 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800630 }
631 mCurrentUser = userId;
632
633 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800634 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800635 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700636 final TaskRecord task = mTaskHistory.get(i);
637
638 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
639 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700640 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700641 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700642 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700643 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800644 mTaskHistory.remove(i);
645 mTaskHistory.add(task);
646 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800647 // Use same value for i.
648 } else {
649 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800650 }
651 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700652 if (VALIDATE_TOKENS) {
653 validateAppTokensLocked();
654 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700655 }
656
Craig Mautner2420ead2013-04-01 17:13:20 -0700657 void minimalResumeActivityLocked(ActivityRecord r) {
658 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700659 if (DEBUG_STATES) Slog.v(TAG_STATES,
660 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700661 r.stopped = false;
662 mResumedActivity = r;
663 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800664 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700665 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700666 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700667 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700668 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
669 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700670 }
671
Narayan Kamath7829c812015-06-08 17:39:43 +0100672 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700673 if (mFullyDrawnStartTime != 0) {
674 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
675 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100676 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700677 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
678 }
679
680 private void stopFullyDrawnTraceIfNeeded() {
681 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
682 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
683 mFullyDrawnStartTime = 0;
684 }
685 }
686
Craig Mautnere79d42682013-04-01 19:01:53 -0700687 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700688 if (r.displayStartTime == 0) {
689 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
690 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100691 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700692 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700693 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700694 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100695 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700696 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700697 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700698 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800699
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700700 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700701 // Make sure that there is no activity waiting for this to launch.
702 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
703 r.displayStartTime = r.fullyDrawnStartTime = 0;
704 } else {
705 mStackSupervisor.removeTimeoutsForActivityLocked(r);
706 mStackSupervisor.scheduleIdleTimeoutLocked(r);
707 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700708 }
709
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800710 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800711 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800712 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
713 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
714 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
715 activities.get(activityNdx).setSleeping(false);
716 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800717 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800718 if (mPausingActivity != null) {
719 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
720 activityPausedLocked(mPausingActivity.appToken, true);
721 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800722 }
723
Craig Mautner0eea92c2013-05-16 13:35:39 -0700724 /**
725 * @return true if something must be done before going to sleep.
726 */
727 boolean checkReadyForSleepLocked() {
728 if (mResumedActivity != null) {
729 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700730 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
731 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
732 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700733 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700734 return true;
735 }
736 if (mPausingActivity != null) {
737 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700738 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700739 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800740 }
741
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700742 if (hasVisibleBehindActivity()) {
743 // Stop visible behind activity before going to sleep.
744 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
745 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700746 if (DEBUG_STATES) Slog.v(TAG_STATES,
747 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700748 return true;
749 }
750
Craig Mautner0eea92c2013-05-16 13:35:39 -0700751 return false;
752 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800753
Craig Mautner0eea92c2013-05-16 13:35:39 -0700754 void goToSleep() {
755 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800756
Craig Mautner0eea92c2013-05-16 13:35:39 -0700757 // Make sure any stopped but visible activities are now sleeping.
758 // This ensures that the activity's onStop() is called.
759 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
760 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
761 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
762 final ActivityRecord r = activities.get(activityNdx);
763 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
764 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800765 }
766 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800767 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 }
Craig Mautner59c00972012-07-30 12:10:24 -0700769
Dianne Hackbornd2835932010-12-13 16:28:46 -0800770 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700771 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800772 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700773 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800774 return null;
775 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800776
Winson Chung083baf92014-07-11 10:32:42 -0700777 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700778 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800779 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700780 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700781 return null;
782 }
783
Winson Chung48a10a52014-08-27 14:36:51 -0700784 int w = mService.mThumbnailWidth;
785 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800786 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700787 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800788 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700789 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800790 }
Winson Chung376543b2014-05-21 17:43:28 -0700791 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800792 return null;
793 }
794
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700795 /**
796 * Start pausing the currently resumed activity. It is an error to call this if there
797 * is already an activity being paused or there is no resumed activity.
798 *
799 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
800 * @param uiSleeping True if this is happening with the user interface going to sleep (the
801 * screen turning off).
802 * @param resuming True if this is being called as part of resuming the top activity, so
803 * we shouldn't try to instigate a resume here.
804 * @param dontWait True if the caller does not want to wait for the pause to complete. If
805 * set to true, we will immediately complete the pause here before returning.
806 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
807 * it to tell us when it is done.
808 */
809 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
810 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800811 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800812 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
813 + " state=" + mPausingActivity.state);
814 if (!mService.isSleeping()) {
815 // Avoid recursion among check for sleep and complete pause during sleeping.
816 // Because activity will be paused immediately after resume, just let pause
817 // be completed by the order of activity paused from clients.
818 completePauseLocked(false);
819 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800820 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 ActivityRecord prev = mResumedActivity;
822 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700823 if (!resuming) {
824 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
825 mStackSupervisor.resumeTopActivitiesLocked();
826 }
827 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800829
830 if (mActivityContainer.mParentActivity == null) {
831 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700832 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800833 }
834
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700835 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700836 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800838 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700840 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
841 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842 prev.state = ActivityState.PAUSING;
843 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700844 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700845 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800846 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800847 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700848 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700849 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850
851 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800852
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700854 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700855 try {
856 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700857 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700859 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800860 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700861 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 } catch (Exception e) {
863 // Ignore exception, if process died other code will cleanup.
864 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700867 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 }
869 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800870 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700871 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700872 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700873 }
874
875 // If we are not going to sleep, we want to ensure the device is
876 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700877 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700878 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 }
880
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800881 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700882 // Have the window manager pause its key dispatching until the new
883 // activity has started. If we're pausing the activity just because
884 // the screen is being turned off and the UI is sleeping, don't interrupt
885 // key dispatch; the same activity will pick it up again on wakeup.
886 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800887 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700888 } else if (DEBUG_PAUSE) {
889 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700890 }
891
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700892 if (dontWait) {
893 // If the caller said they don't want to wait for the pause, then complete
894 // the pause now.
895 completePauseLocked(false);
896 return false;
897
898 } else {
899 // Schedule a pause timeout in case the app doesn't respond.
900 // We don't give it much time because this directly impacts the
901 // responsiveness seen by the user.
902 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
903 msg.obj = prev;
904 prev.pauseTime = SystemClock.uptimeMillis();
905 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700906 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700907 return true;
908 }
909
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700910 } else {
911 // This activity failed to schedule the
912 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700914 if (!resuming) {
915 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
916 }
917 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 }
919 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700920
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700922 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
923 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700924
Craig Mautnerd2328952013-03-05 12:46:26 -0800925 final ActivityRecord r = isInStackLocked(token);
926 if (r != null) {
927 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
928 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700929 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -0800930 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700931 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800932 } else {
933 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
934 r.userId, System.identityHashCode(r), r.shortComponentName,
935 mPausingActivity != null
936 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +0800937 if (r.finishing && r.state == ActivityState.PAUSING) {
938 if (DEBUG_PAUSE) Slog.v(TAG,
939 "Executing finish of failed to pause activity: " + r);
940 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
941 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700942 }
943 }
944 }
945
Craig Mautnera0026042014-04-23 11:45:37 -0700946 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
947 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700948 if (r.state != ActivityState.STOPPING) {
949 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
950 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
951 return;
952 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700953 if (persistentState != null) {
954 r.persistentState = persistentState;
955 mService.notifyTaskPersisterLocked(r.task, false);
956 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700957 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700958 if (icicle != null) {
959 // If icicle is null, this is happening due to a timeout, so we
960 // haven't really saved the state.
961 r.icicle = icicle;
962 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800963 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800964 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700965 }
966 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700967 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700968 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
969 r.stopped = true;
970 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700971 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
972 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700973 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700974 if (r.finishing) {
975 r.clearOptionsLocked();
976 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700977 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700978 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700979 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700980 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700981 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800982 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700983 }
984 }
985 }
986
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700987 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700989 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800990
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800991 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700992 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700994 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700995 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800996 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700997 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800998 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700999 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1000 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001001 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001002 if (prev.configDestroy) {
1003 // The previous is being paused because the configuration
1004 // is changing, which means it is actually stopping...
1005 // To juggle the fact that we are also starting a new
1006 // instance right now, we need to first completely stop
1007 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001008 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001009 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001010 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001011 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001012 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001013 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001014 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1015 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001016 // If we already have a few activities waiting to stop,
1017 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001018 // them out. Or if r is the last of activity of the last task the stack
1019 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001020 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001021 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001022 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001023 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001024 }
1025 }
1026 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001027 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001028 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001029 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001030 // It is possible the activity was freezing the screen before it was paused.
1031 // In that case go ahead and remove the freeze this activity has on the screen
1032 // since it is no longer visible.
1033 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001034 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001036
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001037 if (resumeNext) {
1038 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1039 if (!mService.isSleepingOrShuttingDown()) {
1040 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1041 } else {
1042 mStackSupervisor.checkReadyForSleepLocked();
1043 ActivityRecord top = topStack.topRunningActivityLocked(null);
1044 if (top == null || (prev != null && top != prev)) {
1045 // If there are no more activities available to run,
1046 // do resume anyway to start something. Also if the top
1047 // activity on the stack is not the just paused activity,
1048 // we need to go ahead and resume it to ensure we complete
1049 // an in-flight app switch.
1050 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1051 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001052 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001054
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001055 if (prev != null) {
1056 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057
Craig Mautner525f3d92013-05-07 14:01:50 -07001058 if (prev.app != null && prev.cpuTimeAtResume > 0
1059 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001060 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1061 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001062 if (diff > 0) {
1063 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1064 synchronized (bsi) {
1065 BatteryStatsImpl.Uid.Proc ps =
1066 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001067 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001068 if (ps != null) {
1069 ps.addForegroundTimeLocked(diff);
1070 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071 }
1072 }
1073 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001074 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001076
1077 // Notfiy when the task stack has changed
1078 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079 }
1080
1081 /**
1082 * Once we know that we have asked an application to put an activity in
1083 * the resumed state (either by launching it or explicitly telling it),
1084 * this function updates the rest of our state to match that fact.
1085 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001086 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 next.idle = false;
1088 next.results = null;
1089 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001090
1091 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1092 ProcessRecord app = next.task.mActivities.get(0).app;
1093 if (app != null && app != mService.mHomeProcess) {
1094 mService.mHomeProcess = app;
1095 }
1096 }
1097
Craig Mautner07566322013-09-26 16:42:55 -07001098 if (next.nowVisible) {
1099 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001100 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001101 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001102
1103 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001104 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001105
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001106 mStackSupervisor.reportResumedActivityLocked(next);
1107
1108 next.resumeKeyDispatchingLocked();
1109 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001110
1111 // Mark the point when the activity is resuming
1112 // TODO: To be more accurate, the mark should be before the onCreate,
1113 // not after the onResume. But for subsequent starts, onResume is fine.
1114 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001115 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 } else {
1117 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1118 }
Winson Chung376543b2014-05-21 17:43:28 -07001119
George Mount6ba042b2014-07-28 11:12:28 -07001120 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001121
1122 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1123 // When resuming an activity, require it to call requestVisibleBehind() again.
1124 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1125 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126 }
1127
Craig Mautner2568c3a2015-03-26 14:22:34 -07001128 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001129 r.visible = visible;
1130 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001131 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001132 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001133 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001134 container.setVisible(visible);
1135 }
1136 }
1137
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001138 // Find the first visible activity above the passed activity and if it is translucent return it
1139 // otherwise return null;
1140 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1141 TaskRecord task = r.task;
1142 if (task == null) {
1143 return null;
1144 }
1145
1146 ActivityStack stack = task.stack;
1147 if (stack == null) {
1148 return null;
1149 }
1150
1151 int stackNdx = mStacks.indexOf(stack);
1152
1153 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1154 int taskNdx = tasks.indexOf(task);
1155
1156 ArrayList<ActivityRecord> activities = task.mActivities;
1157 int activityNdx = activities.indexOf(r) + 1;
1158
1159 final int numStacks = mStacks.size();
1160 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001161 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001162 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001163 final int numTasks = tasks.size();
1164 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001165 final TaskRecord currentTask = tasks.get(taskNdx);
1166 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001167 final int numActivities = activities.size();
1168 while (activityNdx < numActivities) {
1169 final ActivityRecord activity = activities.get(activityNdx);
1170 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001171 return historyStack.mFullscreen
1172 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001173 }
1174 ++activityNdx;
1175 }
1176 activityNdx = 0;
1177 ++taskNdx;
1178 }
1179 taskNdx = 0;
1180 ++stackNdx;
1181 }
1182
1183 return null;
1184 }
1185
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001186 private ActivityStack getNextVisibleStackLocked() {
1187 ArrayList<ActivityStack> stacks = mStacks;
1188 final ActivityRecord parent = mActivityContainer.mParentActivity;
1189 if (parent != null) {
1190 stacks = parent.task.stack.mStacks;
1191 }
1192 if (stacks != null) {
1193 for (int i = stacks.size() - 1; i >= 0; --i) {
1194 ActivityStack stack = stacks.get(i);
1195 if (stack != this && stack.isStackVisibleLocked()) {
1196 return stack;
1197 }
1198 }
1199 }
1200 return null;
1201 }
1202
Jose Lima7ba71252014-04-30 12:59:27 -07001203 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1204 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001205 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001206 if (!isAttached()) {
1207 return false;
1208 }
1209
1210 if (mStackSupervisor.isFrontStack(this)) {
1211 return true;
1212 }
1213
Jose Lima729cb232014-05-05 15:59:40 -07001214 /**
1215 * Start at the task above this one and go up, looking for a visible
1216 * fullscreen activity, or a translucent activity that requested the
1217 * wallpaper to be shown behind it.
1218 */
Jose Lima7ba71252014-04-30 12:59:27 -07001219 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001220 final ActivityStack stack = mStacks.get(i);
1221 // stack above isn't fullscreen, so, we assume we're still visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001222 if (!stack.mFullscreen) {
1223 continue;
1224 }
1225 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1226 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001227 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001228 // task above isn't fullscreen, so, we assume we're still visible.
1229 if (!task.mFullscreen) {
1230 continue;
1231 }
Craig Mautner84984fa2014-06-19 11:19:20 -07001232 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001233 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001234 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001235
1236 // Conditions for an activity to obscure the stack we're
1237 // examining:
1238 // 1. Not Finishing AND Visible AND:
1239 // 2. Either:
1240 // - Full Screen Activity OR
1241 // - On top of Home and our stack is NOT home
1242 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001243 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001244 return false;
1245 }
1246 }
1247 }
1248 }
1249
1250 return true;
1251 }
1252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 /**
1254 * Make sure that all activities that need to be visible (that is, they
1255 * currently can be seen by the user) actually are.
1256 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001257 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1258 ActivityRecord top = topRunningActivityLocked(null);
1259 if (top == null) {
1260 return;
1261 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001262 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1263 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001264 + " configChanges=0x" + Integer.toHexString(configChanges));
1265
Craig Mautner5eda9b32013-07-02 11:58:16 -07001266 if (mTranslucentActivityWaiting != top) {
1267 mUndrawnActivitiesBelowTopTranslucent.clear();
1268 if (mTranslucentActivityWaiting != null) {
1269 // Call the callback with a timeout indication.
1270 notifyActivityDrawnLocked(null);
1271 mTranslucentActivityWaiting = null;
1272 }
1273 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1274 }
1275
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001276 // If the top activity is not fullscreen, then we need to
1277 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001278 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001279 boolean behindFullscreen = !isStackVisibleLocked();
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001280 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001281
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001282 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001283 final TaskRecord task = mTaskHistory.get(taskNdx);
1284 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001285 // Set to true if an activity in this task is fullscreen thereby hiding other
1286 // activities in the same task. Initialized to the same value as behindFullscreen
1287 // which represent if the entire task/stack is behind another fullscreen task/stack.
1288 boolean behindFullscreenActivity = behindFullscreen;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001289 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1290 final ActivityRecord r = activities.get(activityNdx);
1291 if (r.finishing) {
1292 continue;
1293 }
1294 if (aboveTop && r != top) {
1295 continue;
1296 }
1297 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001298 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1299 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001300 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001301 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1302 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001303 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001304
Craig Mautnerd44711d2013-02-23 11:24:36 -08001305 // First: if this is not the current activity being started, make
1306 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001307 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001308 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001310
1311 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001312 // This activity needs to be visible, but isn't even running...
1313 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001314 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1315 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001316 if (r != starting) {
1317 r.startFreezingScreenLocked(r.app, configChanges);
1318 }
1319 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001320 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1321 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001322 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001323 }
1324 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001325 mStackSupervisor.startSpecificActivityLocked(
1326 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001327 if (activityNdx >= activities.size()) {
1328 // Record may be removed if its process needs to restart.
1329 activityNdx = activities.size() - 1;
1330 } else {
1331 noStackActivityResumed = false;
1332 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001333 }
1334
1335 } else if (r.visible) {
1336 // If this activity is already visible, then there is nothing
1337 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001338 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1339 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001341 try {
George Mount6ba042b2014-07-28 11:12:28 -07001342 if (r.returningOptions != null) {
1343 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1344 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001345 }
1346 } catch(RemoteException e) {
1347 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001348 if (r.state == ActivityState.RESUMED) {
1349 noStackActivityResumed = false;
1350 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001351 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001352 // This activity is not currently visible, but is running.
1353 // Tell it to become visible.
1354 r.visible = true;
1355 if (r.state != ActivityState.RESUMED && r != starting) {
1356 // If this activity is paused, tell it
1357 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001358 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1359 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001360 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001361 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001362 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001363 mUndrawnActivitiesBelowTopTranslucent.add(r);
1364 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001365 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001366 r.sleeping = false;
1367 r.app.pendingUiClean = true;
1368 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1369 r.stopFreezingScreenLocked(false);
1370 } catch (Exception e) {
1371 // Just skip on any failure; we'll make it
1372 // visible when it next restarts.
1373 Slog.w(TAG, "Exception thrown making visibile: "
1374 + r.intent.getComponent(), e);
1375 }
1376 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001377 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378
Craig Mautnerd44711d2013-02-23 11:24:36 -08001379 // Aggregate current change flags.
1380 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381
Craig Mautnerd44711d2013-02-23 11:24:36 -08001382 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001383 // At this point, nothing else needs to be shown in this task.
1384 behindFullscreenActivity = true;
1385 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1386 + " behindFullscreen=" + behindFullscreen
1387 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001388 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001389 behindFullscreenActivity = true;
1390 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1391 + " behindFullscreen=" + behindFullscreen
1392 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001393 }
1394 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001395 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001396 "Make invisible? " + r + " finishing=" + r.finishing
1397 + " state=" + r.state + " behindFullscreen=" + behindFullscreen
1398 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001399 // Now for any activities that aren't visible to the user, make
1400 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001402 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001403 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001404 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001405 switch (r.state) {
1406 case STOPPING:
1407 case STOPPED:
1408 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001409 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1410 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001411 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1412 }
1413 break;
1414
1415 case INITIALIZING:
1416 case RESUMED:
1417 case PAUSING:
1418 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001419 // This case created for transitioning activities from
1420 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001421 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001422 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001423 } else {
1424 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1425 mStackSupervisor.mStoppingActivities.add(r);
1426 }
1427 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001428 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001429 break;
1430
1431 default:
1432 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 }
1434 } catch (Exception e) {
1435 // Just skip on any failure; we'll make it
1436 // visible when it next restarts.
1437 Slog.w(TAG, "Exception thrown making hidden: "
1438 + r.intent.getComponent(), e);
1439 }
1440 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001441 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001443 }
1444 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001445 // Factoring if the previous task is fullscreen there by affecting the visibility of
1446 // task behind it.
1447 behindFullscreen |= task.mFullscreen;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001448 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001449
1450 if (mTranslucentActivityWaiting != null &&
1451 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1452 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1453 notifyActivityDrawnLocked(null);
1454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001455 }
Craig Mautner58547802013-03-05 08:23:53 -08001456
Todd Kennedyaab56db2015-01-30 09:39:53 -08001457 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001458 mTranslucentActivityWaiting = r;
1459 mUndrawnActivitiesBelowTopTranslucent.clear();
1460 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1461 }
1462
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001463 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1464 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1465 final TaskRecord task = mTaskHistory.get(taskNdx);
1466 final ArrayList<ActivityRecord> activities = task.mActivities;
1467 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1468 final ActivityRecord r = activities.get(activityNdx);
1469 if ( r.appTimeTracker != except) {
1470 r.appTimeTracker = null;
1471 }
1472 }
1473 }
1474 }
1475
Craig Mautner5eda9b32013-07-02 11:58:16 -07001476 /**
1477 * Called as activities below the top translucent activity are redrawn. When the last one is
1478 * redrawn notify the top activity by calling
1479 * {@link Activity#onTranslucentConversionComplete}.
1480 *
1481 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1482 * occurred and the activity will be notified immediately.
1483 */
1484 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001485 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001486 if ((r == null)
1487 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1488 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1489 // The last undrawn activity below the top has just been drawn. If there is an
1490 // opaque activity at the top, notify it that it can become translucent safely now.
1491 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1492 mTranslucentActivityWaiting = null;
1493 mUndrawnActivitiesBelowTopTranslucent.clear();
1494 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1495
Craig Mautner71dd1b62014-02-18 15:48:52 -08001496 if (waitingActivity != null) {
1497 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1498 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1499 try {
1500 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1501 waitingActivity.appToken, r != null);
1502 } catch (RemoteException e) {
1503 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001504 }
1505 }
1506 }
1507 }
1508
Craig Mautnera61bc652013-10-28 15:43:18 -07001509 /** If any activities below the top running one are in the INITIALIZING state and they have a
1510 * starting window displayed then remove that starting window. It is possible that the activity
1511 * in this state will never resumed in which case that starting window will be orphaned. */
1512 void cancelInitializingActivities() {
1513 final ActivityRecord topActivity = topRunningActivityLocked(null);
1514 boolean aboveTop = true;
1515 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1516 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1517 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1518 final ActivityRecord r = activities.get(activityNdx);
1519 if (aboveTop) {
1520 if (r == topActivity) {
1521 aboveTop = false;
1522 }
1523 continue;
1524 }
1525
1526 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001527 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1528 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001529 r.mStartingWindowShown = false;
1530 mWindowManager.removeAppStartingWindow(r.appToken);
1531 }
1532 }
1533 }
1534 }
1535
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 /**
1537 * Ensure that the top activity in the stack is resumed.
1538 *
1539 * @param prev The previously resumed activity, for when in the process
1540 * of pausing; can be null to call from elsewhere.
1541 *
1542 * @return Returns true if something is being resumed, or false if
1543 * nothing happened.
1544 */
1545 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001546 return resumeTopActivityLocked(prev, null);
1547 }
1548
1549 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001550 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001551 // Don't even start recursing.
1552 return false;
1553 }
1554
1555 boolean result = false;
1556 try {
1557 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001558 mStackSupervisor.inResumeTopActivity = true;
1559 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1560 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001561 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001562 }
Craig Mautner544efa72014-09-04 16:41:20 -07001563 result = resumeTopActivityInnerLocked(prev, options);
1564 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001565 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001566 }
1567 return result;
1568 }
1569
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001570 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001571 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001572
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001573 if (!mService.mBooting && !mService.mBooted) {
1574 // Not ready yet!
1575 return false;
1576 }
1577
Craig Mautnerdf88d732014-01-27 09:21:32 -08001578 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001579 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001580 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001581 // Do not resume this stack if its parent is not resumed.
1582 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1583 return false;
1584 }
1585
Craig Mautnera61bc652013-10-28 15:43:18 -07001586 cancelInitializingActivities();
1587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001589 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001590
1591 // Remember how we'll process this pause/resume situation, and ensure
1592 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001593 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1594 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595
Craig Mautner84984fa2014-06-19 11:19:20 -07001596 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001598 // There are no more activities!
1599 final String reason = "noMoreActivities";
1600 if (!mFullscreen) {
1601 // Try to move focus to the next visible stack with a running activity if this
1602 // stack is not covering the entire screen.
1603 final ActivityStack stack = getNextVisibleStackLocked();
1604 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1605 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1606 }
1607 }
1608 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001609 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001610 if (DEBUG_STATES) Slog.d(TAG_STATES,
1611 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001612 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001613 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001614 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1615 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1616 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001617 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 }
1619
1620 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001621
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001622 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001623 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1624 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001625 // Make sure we have executed any pending transitions, since there
1626 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001627 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001628 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001629 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001630 if (DEBUG_STATES) Slog.d(TAG_STATES,
1631 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001632 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 return false;
1634 }
1635
Craig Mautner525f3d92013-05-07 14:01:50 -07001636 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001637 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001638 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001639 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001640 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001641 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001642 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001643 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001644 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001645 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001646 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001647 } else if (!isOnHomeDisplay()) {
1648 return false;
1649 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001650 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001651 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001652 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1653 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1654 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001655 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001656 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001657 }
1658
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 // If we are sleeping, and there is no resumed activity, and the top
1660 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001661 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001662 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001663 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001664 // Make sure we have executed any pending transitions, since there
1665 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001666 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001668 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001669 if (DEBUG_STATES) Slog.d(TAG_STATES,
1670 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001671 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001672 return false;
1673 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001674
1675 // Make sure that the user who owns this activity is started. If not,
1676 // we will just leave it as is because someone should be bringing
1677 // another user's activities to the top of the stack.
1678 if (mService.mStartedUsers.get(next.userId) == null) {
1679 Slog.w(TAG, "Skipping resume of top activity " + next
1680 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001681 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001682 return false;
1683 }
1684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 // The activity may be waiting for stop, but that is no longer
1686 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001687 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001688 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001689 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001690 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001692 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001694 // If we are currently pausing an activity, then don't do anything
1695 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001696 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001697 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001698 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001699 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001700 return false;
1701 }
1702
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001703 // Okay we are now going to start a switch, to 'next'. We may first
1704 // have to pause the current activity, but this is an important point
1705 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001706 // XXX "App Redirected" dialog is getting too many false positives
1707 // at this point, so turn off for now.
1708 if (false) {
1709 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1710 long now = SystemClock.uptimeMillis();
1711 final boolean inTime = mLastStartedActivity.startTime != 0
1712 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1713 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1714 final int nextUid = next.info.applicationInfo.uid;
1715 if (inTime && lastUid != nextUid
1716 && lastUid != next.launchedFromUid
1717 && mService.checkPermission(
1718 android.Manifest.permission.STOP_APP_SWITCHES,
1719 -1, next.launchedFromUid)
1720 != PackageManager.PERMISSION_GRANTED) {
1721 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1722 } else {
1723 next.startTime = now;
1724 mLastStartedActivity = next;
1725 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001726 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001727 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001728 mLastStartedActivity = next;
1729 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001730 }
Craig Mautner58547802013-03-05 08:23:53 -08001731
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001732 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1733
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734 // We need to start pausing the current activity so the top one
1735 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001736 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1737 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001738 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001739 if (DEBUG_STATES) Slog.d(TAG_STATES,
1740 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001741 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001742 }
1743 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001744 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001745 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001746 // At this point we want to put the upcoming activity's process
1747 // at the top of the LRU list, since we know we will be needing it
1748 // very soon and it would be a waste to let it get killed if it
1749 // happens to be sitting towards the end.
1750 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001751 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001752 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001753 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 return true;
1755 }
1756
Christopher Tated3f175c2012-06-14 14:16:54 -07001757 // If the most recent activity was noHistory but was only stopped rather
1758 // than stopped+finished because the device went to sleep, we need to make
1759 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001760 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001761 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001762 if (DEBUG_STATES) Slog.d(TAG_STATES,
1763 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001764 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001765 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001766 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001767 }
1768
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001770 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1771 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001772 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001773 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1774 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 } else {
1776 // The next activity is already visible, so hide the previous
1777 // activity's windows right now so we can show the new one ASAP.
1778 // We only do this if the previous is finishing, which should mean
1779 // it is on top of the one being resumed so hiding it quickly
1780 // is good. Otherwise, we want to do the normal route of allowing
1781 // the resumed activity to be shown so we can decide if the
1782 // previous should actually be hidden depending on whether the
1783 // new one is found to be full-screen or not.
1784 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001785 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001786 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1787 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001788 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 + ", nowVisible=" + next.nowVisible);
1790 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001791 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001792 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001793 + ", waitingVisible="
1794 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1795 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001796 }
1797 }
1798 }
1799
Dianne Hackborne7f97212011-02-24 14:40:20 -08001800 // Launching this app's activity, make sure the app is no longer
1801 // considered stopped.
1802 try {
1803 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001804 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001805 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001806 } catch (IllegalArgumentException e) {
1807 Slog.w(TAG, "Failed trying to unstop package "
1808 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001809 }
1810
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811 // We are starting up the next activity, so tell the window manager
1812 // that the previous one will be hidden soon. This way it can know
1813 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001814 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 if (prev != null) {
1816 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001817 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 "Prepare close transition: prev=" + prev);
1819 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001820 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001821 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001823 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001824 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1825 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001826 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001827 mWindowManager.setAppWillBeHidden(prev.appToken);
1828 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001830 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1831 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001832 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001833 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001834 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001836 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001837 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001838 : next.mLaunchTaskBehind
1839 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1840 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001841 }
1842 }
1843 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001844 mWindowManager.setAppWillBeHidden(prev.appToken);
1845 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001846 }
Craig Mautner967212c2013-04-13 21:10:58 -07001847 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001848 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001850 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001851 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001852 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001853 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 }
1855 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001856
1857 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001858 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001859 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1860 if (opts != null) {
1861 resumeAnimOptions = opts.toBundle();
1862 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001863 next.applyOptionsLocked();
1864 } else {
1865 next.clearOptionsLocked();
1866 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001867
Craig Mautnercf910b02013-04-23 11:23:27 -07001868 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001870 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871
1872 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001873 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001875 // schedule launch ticks to collect information about slow apps.
1876 next.startLaunchTickingLocked();
1877
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001878 ActivityRecord lastResumedActivity =
1879 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 ActivityState lastState = next.state;
1881
1882 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001883
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001884 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 next.state = ActivityState.RESUMED;
1886 mResumedActivity = next;
1887 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001888 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001889 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001890 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001891 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001892
1893 // Have the window manager re-evaluate the orientation of
1894 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001895 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001896 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001897 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001898 mService.mConfiguration,
1899 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1900 if (config != null) {
1901 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001902 }
Maxim Bogatov05075302015-05-19 18:33:08 -07001903 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001904 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001905
Craig Mautner525f3d92013-05-07 14:01:50 -07001906 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001907 // The configuration update wasn't able to keep the existing
1908 // instance of the activity, and instead started a new one.
1909 // We should be all done, but let's just make sure our activity
1910 // is still at the top and schedule another run if something
1911 // weird happened.
1912 ActivityRecord nextNext = topRunningActivityLocked(null);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001913 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001914 "Activity config changed during resume: " + next
1915 + ", new next: " + nextNext);
1916 if (nextNext != next) {
1917 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001918 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001919 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001920 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001921 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001922 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001923 return true;
1924 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001925 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001926 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001927 }
Craig Mautner58547802013-03-05 08:23:53 -08001928
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001929 try {
1930 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001931 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001932 if (a != null) {
1933 final int N = a.size();
1934 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001935 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1936 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001937 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001938 }
1939 }
1940
1941 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001942 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001943 }
1944
Craig Mautner299f9602015-01-26 09:47:33 -08001945 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1946 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001947
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001948 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001949 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001950 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001951 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07001952 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001953 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001954 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001955
Craig Mautner0eea92c2013-05-16 13:35:39 -07001956 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001958 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 } catch (Exception e) {
1960 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001961 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001962 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001963 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001964 if (lastStack != null) {
1965 lastStack.mResumedActivity = lastResumedActivity;
1966 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 Slog.i(TAG, "Restarting because process died: " + next);
1968 if (!next.hasBeenLaunched) {
1969 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001970 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1971 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001972 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001973 next.appToken, next.packageName, next.theme,
1974 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001975 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1976 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001977 }
George Mount2c92c972014-03-20 09:38:23 -07001978 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001979 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 return true;
1981 }
1982
1983 // From this point on, if something goes wrong there is no way
1984 // to recover the activity.
1985 try {
1986 next.visible = true;
1987 completeResumeLocked(next);
1988 } catch (Exception e) {
1989 // If any exception gets thrown, toss away this
1990 // activity and try the next one.
1991 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001992 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001993 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001994 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001995 return true;
1996 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 next.stopped = false;
1998
1999 } else {
2000 // Whoops, need to restart this activity!
2001 if (!next.hasBeenLaunched) {
2002 next.hasBeenLaunched = true;
2003 } else {
2004 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002005 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002006 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002007 mService.compatibilityInfoForPackageLocked(
2008 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002009 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002010 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002011 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002012 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002013 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002014 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002015 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002016 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002017 }
2018
Craig Mautnercf910b02013-04-23 11:23:27 -07002019 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 return true;
2021 }
2022
riddle_hsuc215a4f2014-12-27 12:10:45 +08002023 private TaskRecord getNextTask(TaskRecord targetTask) {
2024 final int index = mTaskHistory.indexOf(targetTask);
2025 if (index >= 0) {
2026 final int numTasks = mTaskHistory.size();
2027 for (int i = index + 1; i < numTasks; ++i) {
2028 TaskRecord task = mTaskHistory.get(i);
2029 if (task.userId == targetTask.userId) {
2030 return task;
2031 }
2032 }
2033 }
2034 return null;
2035 }
2036
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002037 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002038 // If the moving task is over home stack, transfer its return type to next task
2039 if (task.isOverHomeStack()) {
2040 final TaskRecord nextTask = getNextTask(task);
2041 if (nextTask != null) {
2042 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2043 }
2044 }
2045
Craig Mautner9c85c202013-10-04 20:11:26 -07002046 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002047 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002048 if (isOnHomeDisplay()) {
2049 ActivityStack lastStack = mStackSupervisor.getLastStack();
2050 final boolean fromHome = lastStack.isHomeStack();
2051 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002052 task.setTaskToReturnTo(fromHome
2053 ? lastStack.topTask() == null
2054 ? HOME_ACTIVITY_TYPE
2055 : lastStack.topTask().taskType
2056 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002057 }
2058 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002059 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002060 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002061
Craig Mautnerac6f8432013-07-17 13:24:59 -07002062 mTaskHistory.remove(task);
2063 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002064 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002065 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002066 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002067 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002068 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002069 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002070 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002071 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002072 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002073 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002074 break;
2075 }
2076 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002077 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002078 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002079 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002080 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002081 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002082
Craig Mautner8849a5e2013-04-02 16:41:03 -07002083 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002084 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002085 TaskRecord rTask = r.task;
2086 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002087 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2088 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002089 // Last activity in task had been removed or ActivityManagerService is reusing task.
2090 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002091 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002092 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002093 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002094 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002095 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 if (!newTask) {
2097 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002098 boolean startIt = true;
2099 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2100 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002101 if (task.getTopActivity() == null) {
2102 // All activities in task are finishing.
2103 continue;
2104 }
Craig Mautner70a86932013-02-28 22:37:44 -08002105 if (task == r.task) {
2106 // Here it is! Now, if this is not yet visible to the
2107 // user, then just add it without starting; it will
2108 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002109 if (!startIt) {
2110 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2111 + task, new RuntimeException("here").fillInStackTrace());
2112 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002113 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002114 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2115 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002116 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002117 r.userId, r.info.configChanges, task.voiceSession != null,
2118 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002120 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002122 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 return;
2124 }
2125 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002126 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 startIt = false;
2128 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129 }
2130 }
2131
2132 // Place a new activity at top of stack, so it is next to interact
2133 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 // If we are not placing the new activity frontmost, we do not want
2136 // to deliver the onUserLeaving callback to the actual frontmost
2137 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002138 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002139 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002140 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002141 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 }
Craig Mautner70a86932013-02-28 22:37:44 -08002143
2144 task = r.task;
2145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002147 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002148 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002149 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002150 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002151
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002152 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002153 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154 // We want to show the starting preview window if we are
2155 // switching to a new task, or the next activity's process is
2156 // not currently running.
2157 boolean showStartingIcon = newTask;
2158 ProcessRecord proc = r.app;
2159 if (proc == null) {
2160 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2161 }
2162 if (proc == null || proc.thread == null) {
2163 showStartingIcon = true;
2164 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002165 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002166 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002167 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002168 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002171 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002172 ? r.mLaunchTaskBehind
2173 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2174 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002175 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 mNoAnimActivities.remove(r);
2177 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002178 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002179 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002180 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002181 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 boolean doShow = true;
2183 if (newTask) {
2184 // Even though this activity is starting fresh, we still need
2185 // to reset it to make sure we apply affinities to move any
2186 // existing activities from other tasks in to it.
2187 // If the caller has requested that the target task be
2188 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002189 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 resetTaskIfNeededLocked(r, r);
2191 doShow = topRunningNonDelayedActivityLocked(null) == r;
2192 }
George Mount70778d72014-07-01 16:33:45 -07002193 } else if (options != null && new ActivityOptions(options).getAnimationType()
2194 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2195 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002196 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002197 if (r.mLaunchTaskBehind) {
2198 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2199 // tell WindowManager that r is visible even though it is at the back of the stack.
2200 mWindowManager.setAppVisibility(r.appToken, true);
2201 ensureActivitiesVisibleLocked(null, 0);
2202 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002203 // Figure out if we are transitioning from another activity that is
2204 // "has the same starting icon" as the next one. This allows the
2205 // window manager to keep the previous window it had previously
2206 // created, if it still had one.
2207 ActivityRecord prev = mResumedActivity;
2208 if (prev != null) {
2209 // We don't want to reuse the previous starting preview if:
2210 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002211 if (prev.task != r.task) {
2212 prev = null;
2213 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002214 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002215 else if (prev.nowVisible) {
2216 prev = null;
2217 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002218 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002219 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002220 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002221 mService.compatibilityInfoForPackageLocked(
2222 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002223 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002224 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002225 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 }
2227 } else {
2228 // If this is the first activity, don't do any fancy animations,
2229 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002230 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002231 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002232 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002233 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002234 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002235 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002236 }
2237 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002238 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 }
2240
2241 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002242 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 }
2244 }
2245
Dianne Hackbornbe707852011-11-11 14:32:10 -08002246 final void validateAppTokensLocked() {
2247 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002248 mValidateAppTokens.ensureCapacity(numActivities());
2249 final int numTasks = mTaskHistory.size();
2250 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2251 TaskRecord task = mTaskHistory.get(taskNdx);
2252 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002253 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002254 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002255 }
Craig Mautner000f0022013-02-26 15:04:29 -08002256 TaskGroup group = new TaskGroup();
2257 group.taskId = task.taskId;
2258 mValidateAppTokens.add(group);
2259 final int numActivities = activities.size();
2260 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2261 final ActivityRecord r = activities.get(activityNdx);
2262 group.tokens.add(r.appToken);
2263 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002264 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002265 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002266 }
2267
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 /**
2269 * Perform a reset of the given task, if needed as part of launching it.
2270 * Returns the new HistoryRecord at the top of the task.
2271 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002272 /**
2273 * Helper method for #resetTaskIfNeededLocked.
2274 * We are inside of the task being reset... we'll either finish this activity, push it out
2275 * for another task, or leave it as-is.
2276 * @param task The task containing the Activity (taskTop) that might be reset.
2277 * @param forceReset
2278 * @return An ActivityOptions that needs to be processed.
2279 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002280 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002281 ActivityOptions topOptions = null;
2282
2283 int replyChainEnd = -1;
2284 boolean canMoveOptions = true;
2285
2286 // We only do this for activities that are not the root of the task (since if we finish
2287 // the root, we may no longer have the task!).
2288 final ArrayList<ActivityRecord> activities = task.mActivities;
2289 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002290 final int rootActivityNdx = task.findEffectiveRootIndex();
2291 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002292 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002293 if (target.frontOfTask)
2294 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002295
2296 final int flags = target.info.flags;
2297 final boolean finishOnTaskLaunch =
2298 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2299 final boolean allowTaskReparenting =
2300 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2301 final boolean clearWhenTaskReset =
2302 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2303
2304 if (!finishOnTaskLaunch
2305 && !clearWhenTaskReset
2306 && target.resultTo != null) {
2307 // If this activity is sending a reply to a previous
2308 // activity, we can't do anything with it now until
2309 // we reach the start of the reply chain.
2310 // XXX note that we are assuming the result is always
2311 // to the previous activity, which is almost always
2312 // the case but we really shouldn't count on.
2313 if (replyChainEnd < 0) {
2314 replyChainEnd = i;
2315 }
2316 } else if (!finishOnTaskLaunch
2317 && !clearWhenTaskReset
2318 && allowTaskReparenting
2319 && target.taskAffinity != null
2320 && !target.taskAffinity.equals(task.affinity)) {
2321 // If this activity has an affinity for another
2322 // task, then we need to move it out of here. We will
2323 // move it as far out of the way as possible, to the
2324 // bottom of the activity stack. This also keeps it
2325 // correctly ordered with any activities we previously
2326 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002327 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002328 final ActivityRecord bottom =
2329 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002330 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002331 if (bottom != null && target.taskAffinity != null
2332 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002333 // If the activity currently at the bottom has the
2334 // same task affinity as the one we are moving,
2335 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002336 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002337 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002338 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002339 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002340 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002341 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002342 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002343 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002344 + " out to new task " + target.task);
2345 }
2346
Craig Mautnere3a74d52013-02-22 14:14:58 -08002347 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002348 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002349
Craig Mautner525f3d92013-05-07 14:01:50 -07002350 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002351 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2352 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002353 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002354 if (p.finishing) {
2355 continue;
2356 }
2357
Craig Mautnere3a74d52013-02-22 14:14:58 -08002358 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002359 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002360 topOptions = p.takeOptionsLocked();
2361 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002362 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002363 }
2364 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002365 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2366 "Removing activity " + p + " from task=" + task + " adding to task="
2367 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002368 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2369 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002370 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002371 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002372
Craig Mautner83162a92015-01-26 14:43:30 -08002373 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002374 }
2375
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002376 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002377 if (VALIDATE_TOKENS) {
2378 validateAppTokensLocked();
2379 }
2380
2381 replyChainEnd = -1;
2382 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2383 // If the activity should just be removed -- either
2384 // because it asks for it, or the task should be
2385 // cleared -- then finish it and anything that is
2386 // part of its reply chain.
2387 int end;
2388 if (clearWhenTaskReset) {
2389 // In this case, we want to finish this activity
2390 // and everything above it, so be sneaky and pretend
2391 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002392 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002393 } else if (replyChainEnd < 0) {
2394 end = i;
2395 } else {
2396 end = replyChainEnd;
2397 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002398 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002399 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002400 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002401 if (p.finishing) {
2402 continue;
2403 }
2404 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002405 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002406 topOptions = p.takeOptionsLocked();
2407 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002408 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002409 }
2410 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002411 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002412 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002413 if (finishActivityLocked(
2414 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002415 end--;
2416 srcPos--;
2417 }
2418 }
2419 replyChainEnd = -1;
2420 } else {
2421 // If we were in the middle of a chain, well the
2422 // activity that started it all doesn't want anything
2423 // special, so leave it all as-is.
2424 replyChainEnd = -1;
2425 }
2426 }
2427
2428 return topOptions;
2429 }
2430
2431 /**
2432 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2433 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2434 * @param affinityTask The task we are looking for an affinity to.
2435 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2436 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2437 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2438 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002439 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002440 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002441 int replyChainEnd = -1;
2442 final int taskId = task.taskId;
2443 final String taskAffinity = task.affinity;
2444
2445 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2446 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002447 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2448
2449 // Do not operate on or below the effective root Activity.
2450 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002451 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002452 if (target.frontOfTask)
2453 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002454
2455 final int flags = target.info.flags;
2456 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2457 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2458
2459 if (target.resultTo != null) {
2460 // If this activity is sending a reply to a previous
2461 // activity, we can't do anything with it now until
2462 // we reach the start of the reply chain.
2463 // XXX note that we are assuming the result is always
2464 // to the previous activity, which is almost always
2465 // the case but we really shouldn't count on.
2466 if (replyChainEnd < 0) {
2467 replyChainEnd = i;
2468 }
2469 } else if (topTaskIsHigher
2470 && allowTaskReparenting
2471 && taskAffinity != null
2472 && taskAffinity.equals(target.taskAffinity)) {
2473 // This activity has an affinity for our task. Either remove it if we are
2474 // clearing or move it over to our task. Note that
2475 // we currently punt on the case where we are resetting a
2476 // task that is not at the top but who has activities above
2477 // with an affinity to it... this is really not a normal
2478 // case, and we will need to later pull that task to the front
2479 // and usually at that point we will do the reset and pick
2480 // up those remaining activities. (This only happens if
2481 // someone starts an activity in a new task from an activity
2482 // in a task that is not currently on top.)
2483 if (forceReset || finishOnTaskLaunch) {
2484 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002485 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2486 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002487 for (int srcPos = start; srcPos >= i; --srcPos) {
2488 final ActivityRecord p = activities.get(srcPos);
2489 if (p.finishing) {
2490 continue;
2491 }
Todd Kennedy539db512014-12-15 09:57:55 -08002492 finishActivityLocked(
2493 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002494 }
2495 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002496 if (taskInsertionPoint < 0) {
2497 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002498
Craig Mautner77878772013-03-04 19:46:24 -08002499 }
Craig Mautner77878772013-03-04 19:46:24 -08002500
2501 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002502 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2503 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2504 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002505 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002506 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002507 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002508 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002510 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2511 "Removing and adding activity " + p + " to stack at " + task
2512 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002513 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2514 + " from " + srcPos + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002515 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002516 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002517 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002518 if (VALIDATE_TOKENS) {
2519 validateAppTokensLocked();
2520 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002521
2522 // Now we've moved it in to place... but what if this is
2523 // a singleTop activity and we have put it on top of another
2524 // instance of the same activity? Then we drop the instance
2525 // below so it remains singleTop.
2526 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2527 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002528 int targetNdx = taskActivities.indexOf(target);
2529 if (targetNdx > 0) {
2530 ActivityRecord p = taskActivities.get(targetNdx - 1);
2531 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002532 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2533 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002534 }
2535 }
2536 }
2537 }
2538
2539 replyChainEnd = -1;
2540 }
2541 }
Craig Mautner77878772013-03-04 19:46:24 -08002542 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002543 }
2544
Craig Mautner8849a5e2013-04-02 16:41:03 -07002545 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002546 ActivityRecord newActivity) {
2547 boolean forceReset =
2548 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2549 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2550 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2551 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2552 forceReset = true;
2553 }
2554 }
2555
2556 final TaskRecord task = taskTop.task;
2557
2558 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2559 * for remaining tasks. Used for later tasks to reparent to task. */
2560 boolean taskFound = false;
2561
2562 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2563 ActivityOptions topOptions = null;
2564
Craig Mautner77878772013-03-04 19:46:24 -08002565 // Preserve the location for reparenting in the new task.
2566 int reparentInsertionPoint = -1;
2567
Craig Mautnere3a74d52013-02-22 14:14:58 -08002568 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2569 final TaskRecord targetTask = mTaskHistory.get(i);
2570
2571 if (targetTask == task) {
2572 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2573 taskFound = true;
2574 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002575 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2576 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002577 }
2578 }
2579
Craig Mautner70a86932013-02-28 22:37:44 -08002580 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002581 if (taskNdx >= 0) {
2582 do {
2583 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2584 } while (taskTop == null && taskNdx >= 0);
2585 }
Craig Mautner70a86932013-02-28 22:37:44 -08002586
Craig Mautnere3a74d52013-02-22 14:14:58 -08002587 if (topOptions != null) {
2588 // If we got some ActivityOptions from an activity on top that
2589 // was removed from the task, propagate them to the new real top.
2590 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002591 taskTop.updateOptionsLocked(topOptions);
2592 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002593 topOptions.abort();
2594 }
2595 }
2596
2597 return taskTop;
2598 }
2599
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2601 String resultWho, int requestCode, int resultCode, Intent data) {
2602
2603 if (callingUid > 0) {
2604 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002605 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002606 }
2607
2608 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2609 + " : who=" + resultWho + " req=" + requestCode
2610 + " res=" + resultCode + " data=" + data);
2611 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2612 try {
2613 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2614 list.add(new ResultInfo(resultWho, requestCode,
2615 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002616 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002617 return;
2618 } catch (Exception e) {
2619 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2620 }
2621 }
2622
2623 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2624 }
2625
Craig Mautner299f9602015-01-26 09:47:33 -08002626 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002627 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2628 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002629 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002630 if (next != r) {
2631 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002632 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002633 // For non-fullscreen stack, we want to move the focus to the next visible
2634 // stack to prevent the home screen from moving to the top and obscuring
2635 // other visible stacks.
2636 if (!mFullscreen
2637 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2638 return;
2639 }
2640 // Move the home stack to the top if this stack is fullscreen or there is no
2641 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002642 if (mStackSupervisor.moveHomeStackTaskToTop(
2643 task.getTaskToReturnTo(), myReason)) {
2644 // Activity focus was already adjusted. Nothing else to do...
2645 return;
2646 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002647 }
2648 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002649
2650 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002651 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002652 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002653 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002654 }
2655 }
2656
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002657 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2658 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2659 final String myReason = reason + " adjustFocusToNextVisibleStack";
2660 if (stack == null) {
2661 return false;
2662 }
2663 final ActivityRecord top = stack.topRunningActivityLocked(null);
2664 if (top == null) {
2665 return false;
2666 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002667 mService.setFocusedActivityLocked(top, myReason);
2668 return true;
2669 }
2670
Craig Mautnerf3333272013-04-22 10:55:53 -07002671 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002672 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2674 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2675 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002676 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002677 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002678 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2679 "stop-no-history", false)) {
2680 // Activity was finished, no need to continue trying to schedule stop.
2681 adjustFocusedActivityLocked(r, "stopActivityFinished");
2682 r.resumeKeyDispatchingLocked();
2683 return;
2684 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002685 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002686 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002687 + " on stop because we're just sleeping");
2688 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002690 }
2691
2692 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002693 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002694 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 try {
2696 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002697 if (DEBUG_STATES) Slog.v(TAG_STATES,
2698 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002700 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2701 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002702 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002703 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002704 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002705 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002706 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002707 r.setSleeping(true);
2708 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002709 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002710 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 } catch (Exception e) {
2712 // Maybe just ignore exceptions here... if the process
2713 // has crashed, our death notification will clean things
2714 // up.
2715 Slog.w(TAG, "Exception thrown during pause", e);
2716 // Just in case, assume it to be stopped.
2717 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002718 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 r.state = ActivityState.STOPPED;
2720 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002721 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722 }
2723 }
2724 }
2725 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002726
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002727 /**
2728 * @return Returns true if the activity is being finished, false if for
2729 * some reason it is being left as-is.
2730 */
2731 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002732 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002733 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002734 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2735 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002736 + ", result=" + resultCode + ", data=" + resultData
2737 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002738 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002739 return false;
2740 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002741
Craig Mautnerd44711d2013-02-23 11:24:36 -08002742 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 return true;
2744 }
2745
Craig Mautnerd2328952013-03-05 12:46:26 -08002746 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002747 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2748 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2749 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2750 ActivityRecord r = activities.get(activityNdx);
2751 if (r.resultTo == self && r.requestCode == requestCode) {
2752 if ((r.resultWho == null && resultWho == null) ||
2753 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2754 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2755 false);
2756 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002757 }
2758 }
2759 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002760 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002761 }
2762
Todd Kennedy539db512014-12-15 09:57:55 -08002763 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002764 ActivityRecord r = topRunningActivityLocked(null);
2765 if (r != null && r.app == app) {
2766 // If the top running activity is from this crashing
2767 // process, then terminate it to avoid getting in a loop.
2768 Slog.w(TAG, " Force finishing activity "
2769 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002770 int taskNdx = mTaskHistory.indexOf(r.task);
2771 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002772 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002773 // Also terminate any activities below it that aren't yet
2774 // stopped, to avoid a situation where one will get
2775 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002776 --activityNdx;
2777 if (activityNdx < 0) {
2778 do {
2779 --taskNdx;
2780 if (taskNdx < 0) {
2781 break;
2782 }
2783 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2784 } while (activityNdx < 0);
2785 }
2786 if (activityNdx >= 0) {
2787 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002788 if (r.state == ActivityState.RESUMED
2789 || r.state == ActivityState.PAUSING
2790 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002791 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002792 Slog.w(TAG, " Force finishing activity "
2793 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002794 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002795 }
2796 }
2797 }
2798 }
2799 }
2800
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002801 final void finishVoiceTask(IVoiceInteractionSession session) {
2802 IBinder sessionBinder = session.asBinder();
2803 boolean didOne = false;
2804 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2805 TaskRecord tr = mTaskHistory.get(taskNdx);
2806 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2807 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2808 ActivityRecord r = tr.mActivities.get(activityNdx);
2809 if (!r.finishing) {
2810 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2811 false);
2812 didOne = true;
2813 }
2814 }
2815 }
2816 }
2817 if (didOne) {
2818 mService.updateOomAdjLocked();
2819 }
2820 }
2821
Craig Mautnerd2328952013-03-05 12:46:26 -08002822 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002823 ArrayList<ActivityRecord> activities = r.task.mActivities;
2824 for (int index = activities.indexOf(r); index >= 0; --index) {
2825 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002826 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002827 break;
2828 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002829 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002830 }
2831 return true;
2832 }
2833
Dianne Hackborn5c607432012-02-28 14:44:19 -08002834 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2835 // send the result
2836 ActivityRecord resultTo = r.resultTo;
2837 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002838 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002839 + " who=" + r.resultWho + " req=" + r.requestCode
2840 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002841 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002842 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01002843 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002844 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002845 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002846 if (r.info.applicationInfo.uid > 0) {
2847 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2848 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002849 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002850 }
2851 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2852 resultData);
2853 r.resultTo = null;
2854 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002855 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002856
2857 // Make sure this HistoryRecord is not holding on to other resources,
2858 // because clients have remote IPC references to this object so we
2859 // can't assume that will go away and want to avoid circular IPC refs.
2860 r.results = null;
2861 r.pendingResults = null;
2862 r.newIntents = null;
2863 r.icicle = null;
2864 }
2865
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002866 /**
2867 * @return Returns true if this activity has been removed from the history
2868 * list, or false if it is still in the list and will be removed later.
2869 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002870 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2871 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872 if (r.finishing) {
2873 Slog.w(TAG, "Duplicate finish request for " + r);
2874 return false;
2875 }
2876
Wale Ogunwale7d701172015-03-11 15:36:30 -07002877 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002878 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002879 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002880 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002881 task.taskId, r.shortComponentName, reason);
2882 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002883 final int index = activities.indexOf(r);
2884 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002885 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002886 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002887 // If the caller asked that this activity (and all above it)
2888 // be cleared when the task is reset, don't lose that information,
2889 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002890 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002891 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002892 }
2893 }
2894
2895 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002896
Craig Mautner299f9602015-01-26 09:47:33 -08002897 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898
Dianne Hackborn5c607432012-02-28 14:44:19 -08002899 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002900
Craig Mautnerde4ef022013-04-07 19:01:33 -07002901 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002902 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002903 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002905 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002906 ? AppTransition.TRANSIT_TASK_CLOSE
2907 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002908
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002909 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002910 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002911
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002912 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002913 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
2914 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2915 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002916 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 }
2918
Craig Mautneraea74a52014-03-08 14:23:10 -08002919 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07002920 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002921 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002922 } else if (r.state != ActivityState.PAUSING) {
2923 // If the activity is PAUSING, we will complete the finish once
2924 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002925 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002926 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002928 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 }
2930
2931 return false;
2932 }
2933
Craig Mautnerf3333272013-04-22 10:55:53 -07002934 static final int FINISH_IMMEDIATELY = 0;
2935 static final int FINISH_AFTER_PAUSE = 1;
2936 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937
Craig Mautnerf3333272013-04-22 10:55:53 -07002938 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 // First things first: if this activity is currently visible,
2940 // and the resumed activity is not yet visible, then hold off on
2941 // finishing until the resumed one becomes visible.
2942 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002943 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2944 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002945 if (mStackSupervisor.mStoppingActivities.size() > 3
2946 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 // If we already have a few activities waiting to stop,
2948 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002949 // them out. Or if r is the last of activity of the last task the stack
2950 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002951 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002952 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002953 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002954 }
2955 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002956 if (DEBUG_STATES) Slog.v(TAG_STATES,
2957 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002958 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002959 if (oomAdj) {
2960 mService.updateOomAdjLocked();
2961 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 return r;
2963 }
2964
2965 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002966 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002967 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002968 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 if (mResumedActivity == r) {
2970 mResumedActivity = null;
2971 }
2972 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002973 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 r.state = ActivityState.FINISHING;
2975
2976 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002977 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002978 || prevState == ActivityState.STOPPED
2979 || prevState == ActivityState.INITIALIZING) {
2980 // If this activity is already stopped, we can just finish
2981 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002982 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002983 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002984 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002985 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002986 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002987 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07002988 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2989 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002990 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002992
2993 // Need to go through the full pause cycle to get this
2994 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002995 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002996 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002997 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002998 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999 return r;
3000 }
3001
Craig Mautneree36c772014-07-16 14:56:05 -07003002 void finishAllActivitiesLocked(boolean immediately) {
3003 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003004 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3005 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3006 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3007 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003008 noActivitiesInStack = false;
3009 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003010 continue;
3011 }
Craig Mautneree36c772014-07-16 14:56:05 -07003012 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003013 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3014 }
3015 }
Craig Mautneree36c772014-07-16 14:56:05 -07003016 if (noActivitiesInStack) {
3017 mActivityContainer.onTaskListEmptyLocked();
3018 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003019 }
3020
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003021 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3022 // Basic case: for simple app-centric recents, we need to recreate
3023 // the task if the affinity has changed.
3024 if (srec == null || srec.task.affinity == null ||
3025 !srec.task.affinity.equals(destAffinity)) {
3026 return true;
3027 }
3028 // Document-centric case: an app may be split in to multiple documents;
3029 // they need to re-create their task if this current activity is the root
3030 // of a document, unless simply finishing it will return them to the the
3031 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003032 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3033 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003034 // Okay, this activity is at the root of its task. What to do, what to do...
3035 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3036 // Finishing won't return to an application, so we need to recreate.
3037 return true;
3038 }
3039 // We now need to get the task below it to determine what to do.
3040 int taskIdx = mTaskHistory.indexOf(srec.task);
3041 if (taskIdx <= 0) {
3042 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3043 return false;
3044 }
3045 if (taskIdx == 0) {
3046 // At the bottom of the stack, nothing to go back to.
3047 return true;
3048 }
3049 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3050 if (!srec.task.affinity.equals(prevTask.affinity)) {
3051 // These are different apps, so need to recreate.
3052 return true;
3053 }
3054 }
3055 return false;
3056 }
3057
Wale Ogunwale7d701172015-03-11 15:36:30 -07003058 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003059 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003060 final TaskRecord task = srec.task;
3061 final ArrayList<ActivityRecord> activities = task.mActivities;
3062 final int start = activities.indexOf(srec);
3063 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003064 return false;
3065 }
3066 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003067 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003068 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003069 final ComponentName dest = destIntent.getComponent();
3070 if (start > 0 && dest != null) {
3071 for (int i = finishTo; i >= 0; i--) {
3072 ActivityRecord r = activities.get(i);
3073 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003074 r.info.name.equals(dest.getClassName())) {
3075 finishTo = i;
3076 parent = r;
3077 foundParentInTask = true;
3078 break;
3079 }
3080 }
3081 }
3082
3083 IActivityController controller = mService.mController;
3084 if (controller != null) {
3085 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3086 if (next != null) {
3087 // ask watcher if this is allowed
3088 boolean resumeOK = true;
3089 try {
3090 resumeOK = controller.activityResuming(next.packageName);
3091 } catch (RemoteException e) {
3092 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003093 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003094 }
3095
3096 if (!resumeOK) {
3097 return false;
3098 }
3099 }
3100 }
3101 final long origId = Binder.clearCallingIdentity();
3102 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003103 ActivityRecord r = activities.get(i);
3104 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003105 // Only return the supplied result for the first activity finished
3106 resultCode = Activity.RESULT_CANCELED;
3107 resultData = null;
3108 }
3109
3110 if (parent != null && foundParentInTask) {
3111 final int parentLaunchMode = parent.info.launchMode;
3112 final int destIntentFlags = destIntent.getFlags();
3113 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3114 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3115 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3116 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003117 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3118 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003119 } else {
3120 try {
3121 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3122 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003123 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003124 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003125 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003126 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003127 foundParentInTask = res == ActivityManager.START_SUCCESS;
3128 } catch (RemoteException e) {
3129 foundParentInTask = false;
3130 }
3131 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003132 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003133 }
3134 }
3135 Binder.restoreCallingIdentity(origId);
3136 return foundParentInTask;
3137 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 /**
3139 * Perform the common clean-up of an activity record. This is called both
3140 * as part of destroyActivityLocked() (when destroying the client-side
3141 * representation) and cleaning things up as a result of its hosting
3142 * processing going away, in which case there is no remaining client-side
3143 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003144 *
3145 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003147 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3148 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 if (mResumedActivity == r) {
3150 mResumedActivity = null;
3151 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003152 if (mPausingActivity == r) {
3153 mPausingActivity = null;
3154 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003155 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003156
3157 r.configDestroy = false;
3158 r.frozenBeforeDestroy = false;
3159
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003160 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003161 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003162 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003163 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003164 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003165 }
3166
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003167 // Make sure this record is no longer in the pending finishes list.
3168 // This could happen, for example, if we are trimming activities
3169 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003170 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003171 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003172
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173 // Remove any pending results.
3174 if (r.finishing && r.pendingResults != null) {
3175 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3176 PendingIntentRecord rec = apr.get();
3177 if (rec != null) {
3178 mService.cancelIntentSenderLocked(rec, false);
3179 }
3180 }
3181 r.pendingResults = null;
3182 }
3183
3184 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003185 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003186 }
3187
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003189 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003190 if (getVisibleBehindActivity() == r) {
3191 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003192 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003193 }
3194
3195 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003196 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003198 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003199 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003200 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 }
3202
Craig Mautner299f9602015-01-26 09:47:33 -08003203 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003204 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003205 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003206 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003207 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3208 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3209
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003210 r.takeFromHistory();
3211 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003212 if (DEBUG_STATES) Slog.v(TAG_STATES,
3213 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003214 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003215 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003216 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003217 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003218 if (VALIDATE_TOKENS) {
3219 validateAppTokensLocked();
3220 }
Craig Mautner312ba862014-02-10 17:55:01 -08003221 final TaskRecord task = r.task;
3222 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003223 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003224 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003225 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3226 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003227 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003228 }
Craig Mautner299f9602015-01-26 09:47:33 -08003229 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003230 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003231 cleanUpActivityServicesLocked(r);
3232 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003235 /**
3236 * Perform clean-up of service connections in an activity record.
3237 */
3238 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3239 // Throw away any services that have been bound by this activity.
3240 if (r.connections != null) {
3241 Iterator<ConnectionRecord> it = r.connections.iterator();
3242 while (it.hasNext()) {
3243 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003244 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 }
3246 r.connections = null;
3247 }
3248 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003249
Craig Mautneree2e45a2014-06-27 12:10:03 -07003250 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003251 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003252 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003253 mHandler.sendMessage(msg);
3254 }
3255
Craig Mautneree2e45a2014-06-27 12:10:03 -07003256 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003257 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003258 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003259 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3260 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3261 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3262 final ActivityRecord r = activities.get(activityNdx);
3263 if (r.finishing) {
3264 continue;
3265 }
3266 if (r.fullscreen) {
3267 lastIsOpaque = true;
3268 }
3269 if (owner != null && r.app != owner) {
3270 continue;
3271 }
3272 if (!lastIsOpaque) {
3273 continue;
3274 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003275 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003276 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003277 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003278 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003279 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003280 activityRemoved = true;
3281 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003282 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003283 }
3284 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003285 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003286 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003287 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003288 }
3289
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003290 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3291 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003292 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3293 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003294 + " pausing=" + mPausingActivity + " for reason " + reason);
3295 return destroyActivityLocked(r, true, reason);
3296 }
3297 return false;
3298 }
3299
3300 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3301 String reason) {
3302 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003303 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003304 int maxTasks = tasks.size() / 4;
3305 if (maxTasks < 1) {
3306 maxTasks = 1;
3307 }
3308 int numReleased = 0;
3309 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3310 final TaskRecord task = mTaskHistory.get(taskNdx);
3311 if (!tasks.contains(task)) {
3312 continue;
3313 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003314 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003315 int curNum = 0;
3316 final ArrayList<ActivityRecord> activities = task.mActivities;
3317 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3318 final ActivityRecord activity = activities.get(actNdx);
3319 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003320 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003321 + " in state " + activity.state + " resumed=" + mResumedActivity
3322 + " pausing=" + mPausingActivity + " for reason " + reason);
3323 destroyActivityLocked(activity, true, reason);
3324 if (activities.get(actNdx) != activity) {
3325 // Was removed from list, back up so we don't miss the next one.
3326 actNdx--;
3327 }
3328 curNum++;
3329 }
3330 }
3331 if (curNum > 0) {
3332 numReleased += curNum;
3333 maxTasks--;
3334 if (mTaskHistory.get(taskNdx) != task) {
3335 // The entire task got removed, back up so we don't miss the next one.
3336 taskNdx--;
3337 }
3338 }
3339 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003340 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3341 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003342 return numReleased;
3343 }
3344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 /**
3346 * Destroy the current CLIENT SIDE instance of an activity. This may be
3347 * called both when actually finishing an activity, or when performing
3348 * a configuration switch where we destroy the current client-side object
3349 * but then create a new client-side object for this same HistoryRecord.
3350 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003351 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003352 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3353 "Removing activity from " + reason + ": token=" + r
3354 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003356 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003357 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358
3359 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003360
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003361 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003362
3363 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 if (hadApp) {
3366 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003367 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003368 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3369 mService.mHeavyWeightProcess = null;
3370 mService.mHandler.sendEmptyMessage(
3371 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3372 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003373 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003374 // Update any services we are bound to that might care about whether
3375 // their client may have activities.
3376 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003377 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003378 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003379 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003380 }
3381 }
3382
3383 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003385 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003386 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003387 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003388 r.configChangeFlags);
3389 } catch (Exception e) {
3390 // We can just ignore exceptions here... if the process
3391 // has crashed, our death notification will clean things
3392 // up.
3393 //Slog.w(TAG, "Exception thrown during finish", e);
3394 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003395 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396 removedFromHistory = true;
3397 skipDestroy = true;
3398 }
3399 }
3400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003401 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003402
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003403 // If the activity is finishing, we need to wait on removing it
3404 // from the list to give it a chance to do its cleanup. During
3405 // that time it may make calls back with its token so we need to
3406 // be able to find it on the list and so we don't want to remove
3407 // it from the list yet. Otherwise, we can just immediately put
3408 // it in the destroyed state since we are not removing it from the
3409 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003410 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003411 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003412 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003413 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003414 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003415 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3416 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003417 if (DEBUG_STATES) Slog.v(TAG_STATES,
3418 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003419 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003420 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003421 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003422 }
3423 } else {
3424 // remove this record from the history.
3425 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003426 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 removedFromHistory = true;
3428 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003429 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003430 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003431 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003432 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003433 }
3434 }
3435
3436 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003437
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003438 if (!mLRUActivities.remove(r) && hadApp) {
3439 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3440 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003441
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003442 return removedFromHistory;
3443 }
3444
Craig Mautner299f9602015-01-26 09:47:33 -08003445 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003446 final long origId = Binder.clearCallingIdentity();
3447 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003448 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003449 if (r != null) {
3450 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003452 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003453
Wale Ogunwale60454db2015-01-23 16:05:07 -08003454 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003455 if (r.state == ActivityState.DESTROYING) {
3456 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003457 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003458 }
3459 }
Craig Mautner05d29032013-05-03 13:40:13 -07003460 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003461 } finally {
3462 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003463 }
3464 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003465
Todd Kennedyaab56db2015-01-30 09:39:53 -08003466 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003467 if (hasVisibleBehindActivity() &&
3468 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003469 if (r == topRunningActivityLocked(null)) {
3470 // Don't release the top activity if it has requested to run behind the next
3471 // activity.
3472 return;
3473 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003474 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003475 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003476 " thread=" + r.app.thread);
3477 if (r != null && r.app != null && r.app.thread != null) {
3478 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003479 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003480 } catch (RemoteException e) {
3481 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003482 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003483 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003484 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003485 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003486 }
3487 }
3488 }
3489
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003490 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003491 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3492 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003493 if (r != null) {
3494 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003495 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003496 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003497 }
3498 mStackSupervisor.resumeTopActivitiesLocked();
3499 }
3500
Jose Lima4b6c6692014-08-12 17:41:12 -07003501 boolean hasVisibleBehindActivity() {
3502 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003503 }
3504
Jose Lima4b6c6692014-08-12 17:41:12 -07003505 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003506 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003507 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003508 }
3509 }
3510
Jose Lima4b6c6692014-08-12 17:41:12 -07003511 ActivityRecord getVisibleBehindActivity() {
3512 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003513 }
3514
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003515 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3516 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003517 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003518 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3519 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520 while (i > 0) {
3521 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003522 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003523 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003524 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003525 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003526 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003527 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003528 }
3529 }
3530 }
3531
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003532 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3533 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003534 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3535 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003536 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3537 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003538 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003539 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003540 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3541 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003542
3543 boolean hasVisibleActivities = false;
3544
3545 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003546 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003547 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3548 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003549 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3550 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3551 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3552 final ActivityRecord r = activities.get(activityNdx);
3553 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003554 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3555 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003556 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003557 if (r.visible) {
3558 hasVisibleActivities = true;
3559 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003560 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003561 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3562 // Don't currently have state for the activity, or
3563 // it is finishing -- always remove it.
3564 remove = true;
3565 } else if (r.launchCount > 2 &&
3566 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3567 // We have launched this activity too many times since it was
3568 // able to run, so give up and remove it.
3569 remove = true;
3570 } else {
3571 // The process may be gone, but the activity lives on!
3572 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003573 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003574 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003575 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3576 "Removing activity " + r + " from stack at " + i
3577 + ": haveState=" + r.haveState
3578 + " stateNotNeeded=" + r.stateNotNeeded
3579 + " finishing=" + r.finishing
3580 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003581 if (!r.finishing) {
3582 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3583 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3584 r.userId, System.identityHashCode(r),
3585 r.task.taskId, r.shortComponentName,
3586 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003587 if (r.state == ActivityState.RESUMED) {
3588 mService.updateUsageStats(r, false);
3589 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003590 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003591 } else {
3592 // We have the current state for this activity, so
3593 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003594 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3595 if (DEBUG_APP) Slog.v(TAG_APP,
3596 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003597 r.app = null;
3598 r.nowVisible = false;
3599 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003600 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003601 "App died, clearing saved state of " + r);
3602 r.icicle = null;
3603 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003604 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003605 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003606 if (remove) {
3607 removeActivityFromHistoryLocked(r, "appDied");
3608 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003609 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003610 }
3611 }
3612
3613 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003615
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003616 final void updateTransitLocked(int transit, Bundle options) {
3617 if (options != null) {
3618 ActivityRecord r = topRunningActivityLocked(null);
3619 if (r != null && r.state != ActivityState.RESUMED) {
3620 r.updateOptionsLocked(options);
3621 } else {
3622 ActivityOptions.abort(options);
3623 }
3624 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003625 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003626 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003627
Craig Mautner21d24a22014-04-23 11:45:37 -07003628 void updateTaskMovement(TaskRecord task, boolean toFront) {
3629 if (task.isPersistable) {
3630 task.mLastTimeMoved = System.currentTimeMillis();
3631 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3632 // recently will be most negative, tasks sent to the bottom before that will be less
3633 // negative. Similarly for recent tasks moved to the top which will be most positive.
3634 if (!toFront) {
3635 task.mLastTimeMoved *= -1;
3636 }
3637 }
3638 }
3639
Craig Mautner84984fa2014-06-19 11:19:20 -07003640 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003641 final int top = mTaskHistory.size() - 1;
3642 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3643 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003644 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003645 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3646 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003647 mTaskHistory.remove(taskNdx);
3648 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003649 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003650 return;
3651 }
3652 }
3653 }
3654
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003655 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003656 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003657 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658
Craig Mautner11bf9a52013-02-19 14:08:51 -08003659 final int numTasks = mTaskHistory.size();
3660 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003661 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003662 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003663 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003664 ActivityOptions.abort(options);
3665 } else {
3666 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3667 }
3668 return;
3669 }
3670
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003671 if (timeTracker != null) {
3672 // The caller wants a time tracker associated with this task.
3673 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3674 tr.mActivities.get(i).appTimeTracker = timeTracker;
3675 }
3676 }
3677
Craig Mautner11bf9a52013-02-19 14:08:51 -08003678 // Shift all activities with this task up to the top
3679 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003680 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003681
3682 // Set focus to the top running activity of this stack.
3683 ActivityRecord r = topRunningActivityLocked(null);
3684 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003685
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003686 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003687 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003688 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 if (r != null) {
3690 mNoAnimActivities.add(r);
3691 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003692 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003693 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003694 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003695 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003696
Craig Mautner05d29032013-05-03 13:40:13 -07003697 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003698 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003699
3700 if (VALIDATE_TOKENS) {
3701 validateAppTokensLocked();
3702 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703 }
3704
3705 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003706 * Worker method for rearranging history stack. Implements the function of moving all
3707 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003708 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003709 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3711 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003712 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003713 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003714 * @return Returns true if the move completed, false if not.
3715 */
Craig Mautner299f9602015-01-26 09:47:33 -08003716 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003717 final TaskRecord tr = taskForIdLocked(taskId);
3718 if (tr == null) {
3719 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3720 return false;
3721 }
3722
3723 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003724 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003725
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003726 // If we have a watcher, preflight the move before committing to it. First check
3727 // for *other* available tasks, but if none are available, then try again allowing the
3728 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003729 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003730 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 if (next == null) {
3732 next = topRunningActivityLocked(null, 0);
3733 }
3734 if (next != null) {
3735 // ask watcher if this is allowed
3736 boolean moveOK = true;
3737 try {
3738 moveOK = mService.mController.activityResuming(next.packageName);
3739 } catch (RemoteException e) {
3740 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003741 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742 }
3743 if (!moveOK) {
3744 return false;
3745 }
3746 }
3747 }
3748
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003749 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003750
riddle_hsuc215a4f2014-12-27 12:10:45 +08003751 boolean prevIsHome = false;
3752 if (tr.isOverHomeStack()) {
3753 final TaskRecord nextTask = getNextTask(tr);
3754 if (nextTask != null) {
3755 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3756 } else {
3757 prevIsHome = true;
3758 }
3759 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003760 mTaskHistory.remove(tr);
3761 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003762 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003763
Craig Mautnerc8143c62013-09-03 12:15:57 -07003764 // There is an assumption that moving a task to the back moves it behind the home activity.
3765 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003766 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003767 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3768 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003769 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003770 break;
3771 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003772 if (taskNdx == 1) {
3773 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003774 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003775 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003776 }
3777
Craig Mautner299f9602015-01-26 09:47:33 -08003778 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003779 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003780
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003781 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003782 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003783 }
3784
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003785 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003786 if (prevIsHome || task == tr && tr.isOverHomeStack()
3787 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003788 if (!mService.mBooting && !mService.mBooted) {
3789 // Not ready yet!
3790 return false;
3791 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003792 final int taskToReturnTo = tr.getTaskToReturnTo();
3793 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003794 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003795 }
3796
Craig Mautner05d29032013-05-03 13:40:13 -07003797 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798 return true;
3799 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003800
Craig Mautner8849a5e2013-04-02 16:41:03 -07003801 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003802 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003803 final Uri data = r.intent.getData();
3804 final String strData = data != null ? data.toSafeString() : null;
3805
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003806 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003807 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003809 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003810 }
3811
3812 /**
3813 * Make sure the given activity matches the current configuration. Returns
3814 * false if the activity had to be destroyed. Returns true if the
3815 * configuration is the same, or the activity will remain running as-is
3816 * for whatever reason. Ensures the HistoryRecord is updated with the
3817 * correct configuration and all other bookkeeping is handled.
3818 */
3819 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3820 int globalChanges) {
3821 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003822 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003823 "Skipping config check (will change): " + r);
3824 return true;
3825 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003826
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003827 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003828 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003829
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003830 // Short circuit: if the two configurations are the exact same
3831 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003832 final Configuration newConfig = mService.mConfiguration;
3833 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003834 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003835 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08003836 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003837 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003838 "Configuration unchanged in " + r);
3839 return true;
3840 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003841
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003842 // We don't worry about activities that are finishing.
3843 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003844 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003845 "Configuration doesn't matter in finishing " + r);
3846 r.stopFreezingScreenLocked(false);
3847 return true;
3848 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003849
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003850 // Okay we now are going to make this activity have the new config.
3851 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003852 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003853 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003854 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003855 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003856
3857 // Determine what has changed. May be nothing, if this is a config
3858 // that has come back from the app after going idle. In that case
3859 // we just want to leave the official config object now in the
3860 // activity and do nothing else.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003861 int taskChanges = oldTaskOverride.diff(taskConfig);
3862 if (taskChanges == 0) {
Wale Ogunwalea9281272015-02-07 14:04:19 -08003863 // {@link Configuration#diff} doesn't catch changes from unset values.
3864 // Check for changes we care about.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003865 if (oldTaskOverride.orientation != taskConfig.orientation) {
3866 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
Wale Ogunwalea9281272015-02-07 14:04:19 -08003867 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003868 if (oldTaskOverride.screenHeightDp != taskConfig.screenHeightDp
3869 || oldTaskOverride.screenWidthDp != taskConfig.screenWidthDp) {
3870 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
Wale Ogunwalea9281272015-02-07 14:04:19 -08003871 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003872 if (oldTaskOverride.smallestScreenWidthDp != taskConfig.smallestScreenWidthDp) {
3873 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwalea9281272015-02-07 14:04:19 -08003874 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003875 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003876 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003877 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003878 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003879 "Configuration no differences in " + r);
3880 return true;
3881 }
3882
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003883 // If the activity isn't currently running, just leave the new
3884 // configuration and it will pick that up next time it starts.
3885 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003886 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003887 "Configuration doesn't matter not running " + r);
3888 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003889 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003890 return true;
3891 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003892
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003893 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003894 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3895 "Checking to restart " + r.info.name + ": changed=0x"
3896 + Integer.toHexString(changes) + ", handles=0x"
3897 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3898
Dianne Hackborne6676352011-06-01 16:51:20 -07003899 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003900 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3901 r.configChangeFlags |= changes;
3902 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003903 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003904 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003905 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003906 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003907 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003908 } else if (r.state == ActivityState.PAUSING) {
3909 // A little annoying: we are waiting for this activity to
3910 // finish pausing. Let's not do anything now, but just
3911 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003912 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003913 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003914 r.configDestroy = true;
3915 return true;
3916 } else if (r.state == ActivityState.RESUMED) {
3917 // Try to optimize this case: the configuration is changing
3918 // and we need to restart the top, resumed activity.
3919 // Instead of doing the normal handshaking, just say
3920 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003921 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003922 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003923 relaunchActivityLocked(r, r.configChangeFlags, true);
3924 r.configChangeFlags = 0;
3925 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003926 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003927 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003928 relaunchActivityLocked(r, r.configChangeFlags, false);
3929 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003931
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003932 // All done... tell the caller we weren't able to keep this
3933 // activity around.
3934 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003935 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003936
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003937 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003938 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003939 // changes is always sent to all processes when they happen so it can just use whatever
3940 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003941 if (r.app != null && r.app.thread != null) {
3942 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003943 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003944 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003945 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003946 } catch (RemoteException e) {
3947 // If process died, whatever.
3948 }
3949 }
3950 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003951
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952 return true;
3953 }
3954
Craig Mautner2568c3a2015-03-26 14:22:34 -07003955 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003957 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003958 if (andResume) {
3959 results = r.results;
3960 newIntents = r.newIntents;
3961 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003962 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3963 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003964 + " andResume=" + andResume);
3965 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003966 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003967 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003969 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003970
Craig Mautner34b73df2014-01-12 21:11:08 -08003971 mStackSupervisor.removeChildActivityContainers(r);
3972
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003973 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003974 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
3975 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003976 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003977 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3978 !andResume, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003979 new Configuration(r.task.mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003980 // Note: don't need to call pauseIfSleepingLocked() here, because
3981 // the caller will only pass in 'andResume' if this activity is
3982 // currently resumed, which implies we aren't sleeping.
3983 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003984 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003985 }
3986
3987 if (andResume) {
3988 r.results = null;
3989 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003990 r.state = ActivityState.RESUMED;
3991 } else {
3992 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3993 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003994 }
3995
3996 return true;
3997 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003998
3999 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004000 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4001 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4002 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4003 final ActivityRecord r = activities.get(activityNdx);
4004 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004005 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004006 }
4007 if (r.fullscreen && !r.finishing) {
4008 return false;
4009 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004010 }
4011 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004012 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004013 if (r == null) {
4014 return false;
4015 }
4016 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4017 + " would have returned true for r=" + r);
4018 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004019 }
4020
4021 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004022 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4023 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4024 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4025 final ActivityRecord r = activities.get(activityNdx);
4026 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004027 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004028 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004029 }
4030 }
4031 }
4032
Wale Ogunwale540e1232015-05-01 15:35:39 -07004033 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4034 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004035 boolean didSomething = false;
4036 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004037 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004038 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4039 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4040 int numActivities = activities.size();
4041 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4042 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004043 final boolean sameComponent =
4044 (r.packageName.equals(packageName) && (filterByClasses == null
4045 || filterByClasses.contains(r.realActivity.getClassName())))
4046 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004047 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004048 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004049 && (r.app == null || evenPersistent || !r.app.persistent)) {
4050 if (!doit) {
4051 if (r.finishing) {
4052 // If this activity is just finishing, then it is not
4053 // interesting as far as something to stop.
4054 continue;
4055 }
4056 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004057 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004058 if (r.isHomeActivity()) {
4059 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4060 Slog.i(TAG, "Skip force-stop again " + r);
4061 continue;
4062 } else {
4063 homeActivity = r.realActivity;
4064 }
4065 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004066 didSomething = true;
4067 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004068 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004069 if (r.app != null) {
4070 r.app.removed = true;
4071 }
4072 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004073 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004074 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004075 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4076 true)) {
4077 // r has been deleted from mActivities, accommodate.
4078 --numActivities;
4079 --activityNdx;
4080 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004081 }
4082 }
4083 }
4084 return didSomething;
4085 }
4086
Dianne Hackborn09233282014-04-30 11:33:59 -07004087 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004088 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4089 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004090 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004091 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004092 if (task.getTopActivity() == null) {
4093 continue;
4094 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004095 ActivityRecord r = null;
4096 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004097 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004098 int numActivities = 0;
4099 int numRunning = 0;
4100 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004101 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004102 continue;
4103 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004104 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004105 tmp = activities.get(activityNdx);
4106 if (tmp.finishing) {
4107 continue;
4108 }
4109 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004110
Craig Mautneraab647e2013-02-28 16:31:36 -08004111 // Initialize state for next task if needed.
4112 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4113 top = r;
4114 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004115 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004116
4117 // Add 'r' into the current task.
4118 numActivities++;
4119 if (r.app != null && r.app.thread != null) {
4120 numRunning++;
4121 }
4122
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004123 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004124 TAG, r.intent.getComponent().flattenToShortString()
4125 + ": task=" + r.task);
4126 }
4127
4128 RunningTaskInfo ci = new RunningTaskInfo();
4129 ci.id = task.taskId;
4130 ci.baseActivity = r.intent.getComponent();
4131 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004132 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004133 if (focusedStack && topTask) {
4134 // Give the latest time to ensure foreground task can be sorted
4135 // at the first, because lastActiveTime of creating task is 0.
4136 ci.lastActiveTime = System.currentTimeMillis();
4137 topTask = false;
4138 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004139
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004140 if (top.task != null) {
4141 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004142 }
4143 ci.numActivities = numActivities;
4144 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004145 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004146 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004147 }
4148
4149 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004150 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004151 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004152 if (top >= 0) {
4153 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4154 int activityTop = activities.size() - 1;
4155 if (activityTop > 0) {
4156 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4157 "unhandled-back", true);
4158 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004159 }
4160 }
4161
Craig Mautner6b74cb52013-09-27 17:02:21 -07004162 /**
4163 * Reset local parameters because an app's activity died.
4164 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004165 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004166 */
4167 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004168 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004169 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004170 "App died while pausing: " + mPausingActivity);
4171 mPausingActivity = null;
4172 }
4173 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4174 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004175 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004176 }
4177
Craig Mautner19091252013-10-05 00:03:53 -07004178 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004179 }
4180
Craig Mautnercae015f2013-02-08 14:31:27 -08004181 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004182 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4183 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4184 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4185 final ActivityRecord r = activities.get(activityNdx);
4186 if (r.app == app) {
4187 Slog.w(TAG, " Force finishing activity "
4188 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004189 // Force the destroy to skip right to removal.
4190 r.app = null;
4191 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004192 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004193 }
4194 }
4195 }
4196
Dianne Hackborn390517b2013-05-30 15:03:32 -07004197 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004198 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004199 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004200 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4201 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004202 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4203 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004204 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004205 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004206 if (printed) {
4207 header = null;
4208 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004209 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004210 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004211 }
4212
4213 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4214 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4215
4216 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004217 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4218 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004219 }
4220 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004221 final int top = mTaskHistory.size() - 1;
4222 if (top >= 0) {
4223 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4224 int listTop = list.size() - 1;
4225 if (listTop >= 0) {
4226 activities.add(list.get(listTop));
4227 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004228 }
4229 } else {
4230 ItemMatcher matcher = new ItemMatcher();
4231 matcher.build(name);
4232
Craig Mautneraab647e2013-02-28 16:31:36 -08004233 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4234 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4235 if (matcher.match(r1, r1.intent.getComponent())) {
4236 activities.add(r1);
4237 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004238 }
4239 }
4240 }
4241
4242 return activities;
4243 }
4244
4245 ActivityRecord restartPackage(String packageName) {
4246 ActivityRecord starting = topRunningActivityLocked(null);
4247
4248 // All activities that came from the package must be
4249 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004250 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4251 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4252 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4253 final ActivityRecord a = activities.get(activityNdx);
4254 if (a.info.packageName.equals(packageName)) {
4255 a.forceNewConfig = true;
4256 if (starting != null && a == starting && a.visible) {
4257 a.startFreezingScreenLocked(starting.app,
4258 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4259 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004260 }
4261 }
4262 }
4263
4264 return starting;
4265 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004266
Craig Mautner299f9602015-01-26 09:47:33 -08004267 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07004268 removeTask(task, reason, true /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004269 }
4270
Wale Ogunwale000957c2015-04-03 08:19:12 -07004271 /**
4272 * Removes the input task from this stack.
4273 * @param task to remove.
4274 * @param reason for removal.
4275 * @param notMoving task to another stack. In the case we are moving we don't want to perform
4276 * some operations on the task like removing it from window manager or recents.
4277 */
4278 void removeTask(TaskRecord task, String reason, boolean notMoving) {
4279 if (notMoving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004280 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004281 mWindowManager.removeTask(task.taskId);
4282 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004283
Craig Mautner04a0ea62014-01-13 12:51:26 -08004284 final ActivityRecord r = mResumedActivity;
4285 if (r != null && r.task == task) {
4286 mResumedActivity = null;
4287 }
4288
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004289 final int taskNdx = mTaskHistory.indexOf(task);
4290 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004291 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4292 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4293 if (!nextTask.isOverHomeStack()) {
4294 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4295 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004296 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004297 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004298 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004299
Wale Ogunwale000957c2015-04-03 08:19:12 -07004300 if (notMoving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004301 final boolean isVoiceSession = task.voiceSession != null;
4302 if (isVoiceSession) {
4303 try {
4304 task.voiceSession.taskFinished(task.intent, task.taskId);
4305 } catch (RemoteException e) {
4306 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004307 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004308 if (task.autoRemoveFromRecents() || isVoiceSession) {
4309 // Task creator asked to remove this when done, or this task was a voice
4310 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004311 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004312 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004313 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004314 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004315
4316 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004317 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004318 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004319 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004320 String myReason = reason + " leftTaskHistoryEmpty";
4321 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4322 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4323 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004324 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004325 if (mStacks != null) {
4326 mStacks.remove(this);
4327 mStacks.add(0, this);
4328 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004329 if (notHomeStack) {
4330 mActivityContainer.onTaskListEmptyLocked();
4331 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004332 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004333
4334 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004335 }
4336
Dianne Hackborn91097de2014-04-04 18:02:06 -07004337 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4338 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4339 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004340 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4341 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004342 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004343 return task;
4344 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004345
4346 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004347 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004348 }
4349
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004350 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004351 task.stack = this;
4352 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004353 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004354 } else {
4355 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004356 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004357 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004358 if (!moving && task.voiceSession != null) {
4359 try {
4360 task.voiceSession.taskStarted(task.intent, task.taskId);
4361 } catch (RemoteException e) {
4362 }
4363 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004364 }
4365
4366 public int getStackId() {
4367 return mStackId;
4368 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004369
4370 @Override
4371 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004372 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4373 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004374 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004375
Craig Mautner15df08a2015-04-01 12:17:18 -07004376 void onLockTaskPackagesUpdatedLocked() {
4377 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4378 mTaskHistory.get(taskNdx).setLockTaskAuth();
4379 }
4380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004381}