blob: a4c557f6a9e809227a3148c6525a400ec88af853 [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 Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
27import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070028import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070029import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070030import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070031import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070032import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070033import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
34
Dianne Hackborn89ad4562014-08-24 16:45:38 -070035import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070036import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080037import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070039import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080040import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080041import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080042import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080043import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070044import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070045
46import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070047import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070048import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080050import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080058import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080059import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070061import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080062import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Handler;
64import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090065import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070067import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.RemoteException;
69import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080070import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070071import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070072import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070075import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076
Craig Mautnercae015f2013-02-08 14:31:27 -080077import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080078import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import java.lang.ref.WeakReference;
80import java.util.ArrayList;
81import java.util.Iterator;
82import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080083import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070084import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
86/**
87 * State and management of a single stack of activities.
88 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070089final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080090
Wale Ogunwalee23149f2015-03-06 15:39:44 -080091 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
92 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070093 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070094 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
95 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
96 private static final String TAG_STACK = TAG + POSTFIX_STACK;
97 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
98 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
99 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
100 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
101 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700102
103 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800104
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700105 // Ticks during which we check progress while waiting for an app to launch.
106 static final int LAUNCH_TICK = 500;
107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long we wait until giving up on the last activity to pause. This
109 // is short because it directly impacts the responsiveness of starting the
110 // next activity.
111 static final int PAUSE_TIMEOUT = 500;
112
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700113 // How long we wait for the activity to tell us it has stopped before
114 // giving up. This is a good amount of time because we really need this
115 // from the application in order to get its saved state.
116 static final int STOP_TIMEOUT = 10*1000;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we wait until giving up on an activity telling us it has
119 // finished destroying itself.
120 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800121
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700122 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800123 // disabled.
124 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800125
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700126 // How long between activity launches that we consider safe to not warn
127 // the user about an unexpected activity being launched on top.
128 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800129
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700130 // Set to false to disable the preview that is shown while a new activity
131 // is being started.
132 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800133
Craig Mautner5eda9b32013-07-02 11:58:16 -0700134 // How long to wait for all background Activities to redraw following a call to
135 // convertToTranslucent().
136 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138 enum ActivityState {
139 INITIALIZING,
140 RESUMED,
141 PAUSING,
142 PAUSED,
143 STOPPING,
144 STOPPED,
145 FINISHING,
146 DESTROYING,
147 DESTROYED
148 }
149
150 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700151 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800152 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800153
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700154 /**
155 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800156 * running) activities. It contains #TaskRecord objects.
157 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800158 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800159
160 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800161 * Used for validating app tokens with window manager.
162 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800163 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800164
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165 /**
166 * List of running activities, sorted by recent usage.
167 * The first entry in the list is the least recently used.
168 * It contains HistoryRecord objects.
169 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800170 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173 * Animations that for the current transition have requested not to
174 * be considered for the transition animation.
175 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800176 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177
178 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179 * When we are in the process of pausing an activity, before starting the
180 * next one, this variable holds the activity that is currently being paused.
181 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800182 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183
184 /**
185 * This is the last activity that we put into the paused state. This is
186 * used to determine if we need to do an activity transition while sleeping,
187 * when we normally hold the top activity paused.
188 */
189 ActivityRecord mLastPausedActivity = null;
190
191 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700192 * Activities that specify No History must be removed once the user navigates away from them.
193 * If the device goes to sleep with such an activity in the paused state then we save it here
194 * and finish it later if another activity replaces it on wakeup.
195 */
196 ActivityRecord mLastNoHistoryActivity = null;
197
198 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199 * Current activity that is resumed, or null if there is none.
200 */
201 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700204 * This is the last activity that has been started. It is only used to
205 * identify when multiple activities are started at once so that the user
206 * can be warned they may not be in the activity they think they are.
207 */
208 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800209
Craig Mautner5eda9b32013-07-02 11:58:16 -0700210 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
211 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
212 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
213 // Activity in mTranslucentActivityWaiting is notified via
214 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
215 // background activity being drawn then the same call will be made with a true value.
216 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700217 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700218 new ArrayList<ActivityRecord>();
219
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700220 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700221 * Set when we know we are going to be calling updateConfiguration()
222 * soon, so want to skip intermediate config checks.
223 */
224 boolean mConfigWillChange;
225
Todd Kennedyaab56db2015-01-30 09:39:53 -0800226 // Whether or not this stack covers the entire screen; by default stacks are full screen
227 boolean mFullscreen = true;
228
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700229 long mLaunchStartTime = 0;
230 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800231
Craig Mautner858d8a62013-04-23 17:08:34 -0700232 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700233
Craig Mautnerc00204b2013-03-05 15:02:14 -0800234 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800236 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
237 ArrayList<ActivityStack> mStacks;
238 /** The attached Display's unique identifier, or -1 if detached */
239 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240
Craig Mautner27084302013-03-25 08:05:25 -0700241 /** Run all ActivityStacks through this */
242 final ActivityStackSupervisor mStackSupervisor;
243
Wale Ogunwale60454db2015-01-23 16:05:07 -0800244 Configuration mOverrideConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800245 /** True if the stack was forced to full screen because {@link TaskRecord#mResizeable} is false
246 * and the stack was previously resized. */
247 private boolean mForcedFullscreen = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800248
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700249 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700250 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
251 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
252 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
253 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700254 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700255 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700256 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257
258 static class ScheduleDestroyArgs {
259 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700261 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700262 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700263 mReason = reason;
264 }
265 }
266
Zoran Marcetaf958b322012-08-09 20:27:12 +0900267 final Handler mHandler;
268
269 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800270
Craig Mautnerc8143c62013-09-03 12:15:57 -0700271 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900272 super(looper);
273 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274
Zoran Marcetaf958b322012-08-09 20:27:12 +0900275 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276 public void handleMessage(Message msg) {
277 switch (msg.what) {
278 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280 // We don't at this point know if the activity is fullscreen,
281 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800282 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 synchronized (mService) {
284 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700285 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700286 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700287 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800288 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700290 case LAUNCH_TICK_MSG: {
291 ActivityRecord r = (ActivityRecord)msg.obj;
292 synchronized (mService) {
293 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700294 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700295 }
296 }
297 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800299 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 // We don't at this point know if the activity is fullscreen,
301 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800302 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800303 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800304 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800305 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700307 case STOP_TIMEOUT_MSG: {
308 ActivityRecord r = (ActivityRecord)msg.obj;
309 // We don't at this point know if the activity is fullscreen,
310 // so we need to be conservative and assume it isn't.
311 Slog.w(TAG, "Activity stop timeout for " + r);
312 synchronized (mService) {
313 if (r.isInHistory()) {
314 activityStoppedLocked(r, null, null, null);
315 }
316 }
317 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700318 case DESTROY_ACTIVITIES_MSG: {
319 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
320 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700321 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700322 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700323 } break;
324 case TRANSLUCENT_TIMEOUT_MSG: {
325 synchronized (mService) {
326 notifyActivityDrawnLocked(null);
327 }
328 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700329 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700330 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700331 final ActivityRecord r = getVisibleBehindActivity();
332 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700333 if (r != null) {
334 mService.killAppAtUsersRequest(r.app, null);
335 }
336 }
337 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700338 }
339 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800340 }
341
Craig Mautner34b73df2014-01-12 21:11:08 -0800342 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800343 int count = 0;
344 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
345 count += mTaskHistory.get(taskNdx).mActivities.size();
346 }
347 return count;
348 }
349
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800350 int numTasks() {
351 return mTaskHistory.size();
352 }
353
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800354 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
355 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 mActivityContainer = activityContainer;
357 mStackSupervisor = activityContainer.getOuter();
358 mService = mStackSupervisor.mService;
359 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
360 mWindowManager = mService.mWindowManager;
361 mStackId = activityContainer.mStackId;
362 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800363 mRecentTasks = recentTasks;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800364 mOverrideConfig = Configuration.EMPTY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 }
Craig Mautner5962b122012-10-05 14:45:52 -0700366
Amith Yamasani734983f2014-03-04 16:48:05 -0800367 boolean okToShowLocked(ActivityRecord r) {
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700368 return mStackSupervisor.isCurrentProfileLocked(r.userId)
369 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0;
Craig Mautner5962b122012-10-05 14:45:52 -0700370 }
371
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800373 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700374 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
375 if (r != null) {
376 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800377 }
378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700379 return null;
380 }
381
382 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
384 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800385 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800386 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
387 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800388 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800389 return r;
390 }
391 }
392 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 return null;
394 }
395
396 /**
397 * This is a simplified version of topRunningActivityLocked that provides a number of
398 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800399 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 * @param token If non-null, any history records matching this token will be skipped.
401 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800402 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700403 * @return Returns the HistoryRecord of the next activity on the stack.
404 */
405 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800406 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
407 TaskRecord task = mTaskHistory.get(taskNdx);
408 if (task.taskId == taskId) {
409 continue;
410 }
411 ArrayList<ActivityRecord> activities = task.mActivities;
412 for (int i = activities.size() - 1; i >= 0; --i) {
413 final ActivityRecord r = activities.get(i);
414 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800415 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800416 return r;
417 }
418 }
419 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700420 return null;
421 }
422
Craig Mautner8849a5e2013-04-02 16:41:03 -0700423 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
425 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700426 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
427 final ActivityRecord r = activities.get(activityNdx);
428 if (!r.finishing) {
429 return r;
430 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700431 }
432 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700433 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700434 }
435
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700436 final TaskRecord topTask() {
437 final int size = mTaskHistory.size();
438 if (size > 0) {
439 return mTaskHistory.get(size - 1);
440 }
441 return null;
442 }
443
Craig Mautnerd2328952013-03-05 12:46:26 -0800444 TaskRecord taskForIdLocked(int id) {
445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 final TaskRecord task = mTaskHistory.get(taskNdx);
447 if (task.taskId == id) {
448 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800449 }
450 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700451 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700452 }
453
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700455 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800456 return isInStackLocked(r);
457 }
458
459 ActivityRecord isInStackLocked(ActivityRecord r) {
460 if (r == null) {
461 return null;
462 }
463 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700464 if (task != null && task.stack != null
465 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800466 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800467 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800468 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800469 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800470 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800471 }
472
Craig Mautner2420ead2013-04-01 17:13:20 -0700473 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700474 final boolean hadit = mLRUActivities.remove(r);
475 mLRUActivities.add(r);
476 return hadit;
477 }
478
Craig Mautnerde4ef022013-04-07 19:01:33 -0700479 final boolean isHomeStack() {
480 return mStackId == HOME_STACK_ID;
481 }
482
Craig Mautnere0a38842013-12-16 16:14:02 -0800483 final boolean isOnHomeDisplay() {
484 return isAttached() &&
485 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
486 }
487
Craig Mautner299f9602015-01-26 09:47:33 -0800488 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800489 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800490 final boolean homeStack = isHomeStack()
491 || (mActivityContainer.mParentActivity != null
492 && mActivityContainer.mParentActivity.isHomeActivity());
493 ActivityStack lastFocusStack = null;
494 if (!homeStack) {
495 // Need to move this stack to the front before calling
496 // {@link ActivityStackSupervisor#moveHomeStack} below.
497 lastFocusStack = mStacks.get(mStacks.size() - 1);
498 mStacks.remove(this);
499 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800500 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700501 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
502 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800503 if (isOnHomeDisplay()) {
504 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
505 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800506 final TaskRecord task = topTask();
507 if (task != null) {
508 mWindowManager.moveTaskToTop(task.taskId);
509 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800511 }
512
513 final boolean isAttached() {
514 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800515 }
516
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700517 /**
518 * Returns the top activity in any existing task matching the given
519 * Intent. Returns null if no such task is found.
520 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521 ActivityRecord findTaskLocked(ActivityRecord target) {
522 Intent intent = target.intent;
523 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700524 ComponentName cls = intent.getComponent();
525 if (info.targetActivity != null) {
526 cls = new ComponentName(info.packageName, info.targetActivity);
527 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700528 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700529 boolean isDocument = intent != null & intent.isDocument();
530 // If documentData is non-null then it must match the existing task data.
531 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800532
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700533 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800534 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
535 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700536 if (task.voiceSession != null) {
537 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700538 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700539 continue;
540 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700541 if (task.userId != userId) {
542 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700543 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700544 continue;
545 }
Craig Mautner000f0022013-02-26 15:04:29 -0800546 final ActivityRecord r = task.getTopActivity();
547 if (r == null || r.finishing || r.userId != userId ||
548 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700549 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800550 continue;
551 }
552
Craig Mautnerd00f4742014-03-12 14:17:26 -0700553 final Intent taskIntent = task.intent;
554 final Intent affinityIntent = task.affinityIntent;
555 final boolean taskIsDocument;
556 final Uri taskDocumentData;
557 if (taskIntent != null && taskIntent.isDocument()) {
558 taskIsDocument = true;
559 taskDocumentData = taskIntent.getData();
560 } else if (affinityIntent != null && affinityIntent.isDocument()) {
561 taskIsDocument = true;
562 taskDocumentData = affinityIntent.getData();
563 } else {
564 taskIsDocument = false;
565 taskDocumentData = null;
566 }
567
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700568 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700569 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700570 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700571 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700572 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
573 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 return r;
576 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700577 } else if (taskIntent != null && taskIntent.getComponent() != null &&
578 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700579 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700580 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800581 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700582 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
583 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800584 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700585 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
586 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700587 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700588 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800589 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
591 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800592 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700593 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700594 }
595
596 return null;
597 }
598
599 /**
600 * Returns the first activity (starting from the top of the stack) that
601 * is the same as the given activity. Returns null if no such activity
602 * is found.
603 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700604 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 ComponentName cls = intent.getComponent();
606 if (info.targetActivity != null) {
607 cls = new ComponentName(info.packageName, info.targetActivity);
608 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700609 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700610
Craig Mautner000f0022013-02-26 15:04:29 -0800611 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700612 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700613 final boolean notCurrentUserTask =
614 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700615 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700616
Craig Mautner000f0022013-02-26 15:04:29 -0800617 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
618 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700619 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700620 return null;
621 }
Craig Mautner000f0022013-02-26 15:04:29 -0800622 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700623 //Slog.i(TAG, "Found matching class!");
624 //dump();
625 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
626 return r;
627 }
628 }
629 }
630
631 return null;
632 }
633
Amith Yamasani742a6712011-05-04 14:49:28 -0700634 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700635 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700636 */
Craig Mautner93529a42013-10-04 15:03:13 -0700637 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800638 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700639 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800640 }
641 mCurrentUser = userId;
642
643 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800644 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800645 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700646 final TaskRecord task = mTaskHistory.get(i);
647
648 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
649 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700650 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700651 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700652 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700653 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800654 mTaskHistory.remove(i);
655 mTaskHistory.add(task);
656 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800657 // Use same value for i.
658 } else {
659 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800660 }
661 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700662 if (VALIDATE_TOKENS) {
663 validateAppTokensLocked();
664 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700665 }
666
Craig Mautner2420ead2013-04-01 17:13:20 -0700667 void minimalResumeActivityLocked(ActivityRecord r) {
668 r.state = ActivityState.RESUMED;
669 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
670 + " (starting new instance)");
671 r.stopped = false;
672 mResumedActivity = r;
673 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800674 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700675 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700676 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700677 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700678 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 }
680
Dianne Hackborncee04b52013-07-03 17:01:28 -0700681 private void startLaunchTraces() {
682 if (mFullyDrawnStartTime != 0) {
683 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
684 }
685 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
686 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
687 }
688
689 private void stopFullyDrawnTraceIfNeeded() {
690 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
691 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
692 mFullyDrawnStartTime = 0;
693 }
694 }
695
Craig Mautnere79d42682013-04-01 19:01:53 -0700696 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700697 if (r.displayStartTime == 0) {
698 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
699 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700700 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700701 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700702 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700703 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700704 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700705 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700706 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700707 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800708
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700709 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700710 // Make sure that there is no activity waiting for this to launch.
711 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
712 r.displayStartTime = r.fullyDrawnStartTime = 0;
713 } else {
714 mStackSupervisor.removeTimeoutsForActivityLocked(r);
715 mStackSupervisor.scheduleIdleTimeoutLocked(r);
716 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700717 }
718
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800719 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800720 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800721 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
722 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
723 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
724 activities.get(activityNdx).setSleeping(false);
725 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800726 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800727 if (mPausingActivity != null) {
728 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
729 activityPausedLocked(mPausingActivity.appToken, true);
730 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800731 }
732
Craig Mautner0eea92c2013-05-16 13:35:39 -0700733 /**
734 * @return true if something must be done before going to sleep.
735 */
736 boolean checkReadyForSleepLocked() {
737 if (mResumedActivity != null) {
738 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700739 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
740 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
741 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700742 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700743 return true;
744 }
745 if (mPausingActivity != null) {
746 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700747 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700748 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800749 }
750
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700751 if (hasVisibleBehindActivity()) {
752 // Stop visible behind activity before going to sleep.
753 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
754 mStackSupervisor.mStoppingActivities.add(r);
755 if (DEBUG_STATES) Slog.v(TAG, "Sleep still waiting to stop visible behind " + r);
756 return true;
757 }
758
Craig Mautner0eea92c2013-05-16 13:35:39 -0700759 return false;
760 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800761
Craig Mautner0eea92c2013-05-16 13:35:39 -0700762 void goToSleep() {
763 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800764
Craig Mautner0eea92c2013-05-16 13:35:39 -0700765 // Make sure any stopped but visible activities are now sleeping.
766 // This ensures that the activity's onStop() is called.
767 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
768 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
769 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
770 final ActivityRecord r = activities.get(activityNdx);
771 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
772 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800773 }
774 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800775 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 }
Craig Mautner59c00972012-07-30 12:10:24 -0700777
Dianne Hackbornd2835932010-12-13 16:28:46 -0800778 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700779 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800780 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700781 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800782 return null;
783 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800784
Winson Chung083baf92014-07-11 10:32:42 -0700785 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700786 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800787 // we can just go ahead and skip taking the screenshot if this is the home stack.
788 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700789 return null;
790 }
791
Winson Chung48a10a52014-08-27 14:36:51 -0700792 int w = mService.mThumbnailWidth;
793 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800794 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800795 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
796 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700797 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800798 }
Winson Chung376543b2014-05-21 17:43:28 -0700799 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800800 return null;
801 }
802
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700803 /**
804 * Start pausing the currently resumed activity. It is an error to call this if there
805 * is already an activity being paused or there is no resumed activity.
806 *
807 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
808 * @param uiSleeping True if this is happening with the user interface going to sleep (the
809 * screen turning off).
810 * @param resuming True if this is being called as part of resuming the top activity, so
811 * we shouldn't try to instigate a resume here.
812 * @param dontWait True if the caller does not want to wait for the pause to complete. If
813 * set to true, we will immediately complete the pause here before returning.
814 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
815 * it to tell us when it is done.
816 */
817 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
818 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800819 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800820 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
821 + " state=" + mPausingActivity.state);
822 if (!mService.isSleeping()) {
823 // Avoid recursion among check for sleep and complete pause during sleeping.
824 // Because activity will be paused immediately after resume, just let pause
825 // be completed by the order of activity paused from clients.
826 completePauseLocked(false);
827 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800828 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829 ActivityRecord prev = mResumedActivity;
830 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700831 if (!resuming) {
832 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
833 mStackSupervisor.resumeTopActivitiesLocked();
834 }
835 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800837
838 if (mActivityContainer.mParentActivity == null) {
839 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700840 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800841 }
842
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700843 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700844 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800846 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700848 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
849 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 prev.state = ActivityState.PAUSING;
851 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700852 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700853 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800854 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800855 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700856 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700857 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858
859 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800860
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700861 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700862 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700863 try {
864 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700865 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700867 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800868 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700869 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 } catch (Exception e) {
871 // Ignore exception, if process died other code will cleanup.
872 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800873 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700874 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700875 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700876 }
877 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800878 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700880 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700881 }
882
883 // If we are not going to sleep, we want to ensure the device is
884 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700885 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700886 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887 }
888
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800889 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700890 // Have the window manager pause its key dispatching until the new
891 // activity has started. If we're pausing the activity just because
892 // the screen is being turned off and the UI is sleeping, don't interrupt
893 // key dispatch; the same activity will pick it up again on wakeup.
894 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800895 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700896 } else if (DEBUG_PAUSE) {
897 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 }
899
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700900 if (dontWait) {
901 // If the caller said they don't want to wait for the pause, then complete
902 // the pause now.
903 completePauseLocked(false);
904 return false;
905
906 } else {
907 // Schedule a pause timeout in case the app doesn't respond.
908 // We don't give it much time because this directly impacts the
909 // responsiveness seen by the user.
910 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
911 msg.obj = prev;
912 prev.pauseTime = SystemClock.uptimeMillis();
913 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700914 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700915 return true;
916 }
917
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 } else {
919 // This activity failed to schedule the
920 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700921 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700922 if (!resuming) {
923 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
924 }
925 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926 }
927 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700928
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700929 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700930 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
931 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932
Craig Mautnerd2328952013-03-05 12:46:26 -0800933 final ActivityRecord r = isInStackLocked(token);
934 if (r != null) {
935 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
936 if (mPausingActivity == r) {
937 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
938 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700939 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800940 } else {
941 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
942 r.userId, System.identityHashCode(r), r.shortComponentName,
943 mPausingActivity != null
944 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +0800945 if (r.finishing && r.state == ActivityState.PAUSING) {
946 if (DEBUG_PAUSE) Slog.v(TAG,
947 "Executing finish of failed to pause activity: " + r);
948 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
949 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950 }
951 }
952 }
953
Craig Mautnera0026042014-04-23 11:45:37 -0700954 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
955 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700956 if (r.state != ActivityState.STOPPING) {
957 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
958 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
959 return;
960 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700961 if (persistentState != null) {
962 r.persistentState = persistentState;
963 mService.notifyTaskPersisterLocked(r.task, false);
964 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700965 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700966 if (icicle != null) {
967 // If icicle is null, this is happening due to a timeout, so we
968 // haven't really saved the state.
969 r.icicle = icicle;
970 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800971 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800972 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700973 }
974 if (!r.stopped) {
975 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
976 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
977 r.stopped = true;
978 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700979 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
980 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700981 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700982 if (r.finishing) {
983 r.clearOptionsLocked();
984 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700985 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700986 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700987 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700988 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700989 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800990 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700991 }
992 }
993 }
994
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700995 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800996 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700997 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800998
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800999 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001000 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001002 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001003 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001004 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001005 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001006 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001007 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1008 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001009 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001010 if (prev.configDestroy) {
1011 // The previous is being paused because the configuration
1012 // is changing, which means it is actually stopping...
1013 // To juggle the fact that we are also starting a new
1014 // instance right now, we need to first completely stop
1015 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001016 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001017 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001018 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001019 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001020 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001021 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001022 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1023 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001024 // If we already have a few activities waiting to stop,
1025 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001026 // them out. Or if r is the last of activity of the last task the stack
1027 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001028 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001029 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001030 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001031 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001032 }
1033 }
1034 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001035 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001036 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001037 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001038 // It is possible the activity was freezing the screen before it was paused.
1039 // In that case go ahead and remove the freeze this activity has on the screen
1040 // since it is no longer visible.
1041 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001042 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001043 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001044
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001045 if (resumeNext) {
1046 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1047 if (!mService.isSleepingOrShuttingDown()) {
1048 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1049 } else {
1050 mStackSupervisor.checkReadyForSleepLocked();
1051 ActivityRecord top = topStack.topRunningActivityLocked(null);
1052 if (top == null || (prev != null && top != prev)) {
1053 // If there are no more activities available to run,
1054 // do resume anyway to start something. Also if the top
1055 // activity on the stack is not the just paused activity,
1056 // we need to go ahead and resume it to ensure we complete
1057 // an in-flight app switch.
1058 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1059 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001060 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001062
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001063 if (prev != null) {
1064 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065
Craig Mautner525f3d92013-05-07 14:01:50 -07001066 if (prev.app != null && prev.cpuTimeAtResume > 0
1067 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001068 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1069 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001070 if (diff > 0) {
1071 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1072 synchronized (bsi) {
1073 BatteryStatsImpl.Uid.Proc ps =
1074 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001075 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001076 if (ps != null) {
1077 ps.addForegroundTimeLocked(diff);
1078 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079 }
1080 }
1081 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001082 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001084
1085 // Notfiy when the task stack has changed
1086 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 }
1088
1089 /**
1090 * Once we know that we have asked an application to put an activity in
1091 * the resumed state (either by launching it or explicitly telling it),
1092 * this function updates the rest of our state to match that fact.
1093 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001094 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001095 next.idle = false;
1096 next.results = null;
1097 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001098
1099 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1100 ProcessRecord app = next.task.mActivities.get(0).app;
1101 if (app != null && app != mService.mHomeProcess) {
1102 mService.mHomeProcess = app;
1103 }
1104 }
1105
Craig Mautner07566322013-09-26 16:42:55 -07001106 if (next.nowVisible) {
1107 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001108 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001109 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001110
1111 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001112 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001113
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001114 mStackSupervisor.reportResumedActivityLocked(next);
1115
1116 next.resumeKeyDispatchingLocked();
1117 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001118
1119 // Mark the point when the activity is resuming
1120 // TODO: To be more accurate, the mark should be before the onCreate,
1121 // not after the onResume. But for subsequent starts, onResume is fine.
1122 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001123 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001124 } else {
1125 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1126 }
Winson Chung376543b2014-05-21 17:43:28 -07001127
George Mount6ba042b2014-07-28 11:12:28 -07001128 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001129
1130 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1131 // When resuming an activity, require it to call requestVisibleBehind() again.
1132 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
1135
Craig Mautner2568c3a2015-03-26 14:22:34 -07001136 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001137 r.visible = visible;
1138 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001139 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001140 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001141 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001142 container.setVisible(visible);
1143 }
1144 }
1145
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001146 // Find the first visible activity above the passed activity and if it is translucent return it
1147 // otherwise return null;
1148 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1149 TaskRecord task = r.task;
1150 if (task == null) {
1151 return null;
1152 }
1153
1154 ActivityStack stack = task.stack;
1155 if (stack == null) {
1156 return null;
1157 }
1158
1159 int stackNdx = mStacks.indexOf(stack);
1160
1161 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1162 int taskNdx = tasks.indexOf(task);
1163
1164 ArrayList<ActivityRecord> activities = task.mActivities;
1165 int activityNdx = activities.indexOf(r) + 1;
1166
1167 final int numStacks = mStacks.size();
1168 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001169 ActivityStack historyStack = mStacks.get(stackNdx);
1170 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001171 final int numTasks = tasks.size();
1172 while (taskNdx < numTasks) {
1173 activities = tasks.get(taskNdx).mActivities;
1174 final int numActivities = activities.size();
1175 while (activityNdx < numActivities) {
1176 final ActivityRecord activity = activities.get(activityNdx);
1177 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001178 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001179 }
1180 ++activityNdx;
1181 }
1182 activityNdx = 0;
1183 ++taskNdx;
1184 }
1185 taskNdx = 0;
1186 ++stackNdx;
1187 }
1188
1189 return null;
1190 }
1191
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001192 private ActivityStack getNextVisibleStackLocked() {
1193 ArrayList<ActivityStack> stacks = mStacks;
1194 final ActivityRecord parent = mActivityContainer.mParentActivity;
1195 if (parent != null) {
1196 stacks = parent.task.stack.mStacks;
1197 }
1198 if (stacks != null) {
1199 for (int i = stacks.size() - 1; i >= 0; --i) {
1200 ActivityStack stack = stacks.get(i);
1201 if (stack != this && stack.isStackVisibleLocked()) {
1202 return stack;
1203 }
1204 }
1205 }
1206 return null;
1207 }
1208
Jose Lima7ba71252014-04-30 12:59:27 -07001209 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1210 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001211 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001212 if (!isAttached()) {
1213 return false;
1214 }
1215
1216 if (mStackSupervisor.isFrontStack(this)) {
1217 return true;
1218 }
1219
Jose Lima729cb232014-05-05 15:59:40 -07001220 /**
1221 * Start at the task above this one and go up, looking for a visible
1222 * fullscreen activity, or a translucent activity that requested the
1223 * wallpaper to be shown behind it.
1224 */
Jose Lima7ba71252014-04-30 12:59:27 -07001225 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001226 ActivityStack stack = mStacks.get(i);
1227 // stack above isn't full screen, so, we assume we're still visible. at some point
1228 // we should look at the stack bounds to see if we're occluded even if the stack
1229 // isn't fullscreen
1230 if (!stack.mFullscreen) {
1231 continue;
1232 }
1233 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1234 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001235 final TaskRecord task = tasks.get(taskNdx);
1236 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001237 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001238 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001239
1240 // Conditions for an activity to obscure the stack we're
1241 // examining:
1242 // 1. Not Finishing AND Visible AND:
1243 // 2. Either:
1244 // - Full Screen Activity OR
1245 // - On top of Home and our stack is NOT home
1246 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001247 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001248 return false;
1249 }
1250 }
1251 }
1252 }
1253
1254 return true;
1255 }
1256
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001257 /**
1258 * Make sure that all activities that need to be visible (that is, they
1259 * currently can be seen by the user) actually are.
1260 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001261 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1262 ActivityRecord top = topRunningActivityLocked(null);
1263 if (top == null) {
1264 return;
1265 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001266 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1267 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001268 + " configChanges=0x" + Integer.toHexString(configChanges));
1269
Craig Mautner5eda9b32013-07-02 11:58:16 -07001270 if (mTranslucentActivityWaiting != top) {
1271 mUndrawnActivitiesBelowTopTranslucent.clear();
1272 if (mTranslucentActivityWaiting != null) {
1273 // Call the callback with a timeout indication.
1274 notifyActivityDrawnLocked(null);
1275 mTranslucentActivityWaiting = null;
1276 }
1277 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1278 }
1279
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001280 // If the top activity is not fullscreen, then we need to
1281 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001282 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001283 boolean behindFullscreen = !isStackVisibleLocked();
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001284 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001285
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001286 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001287 final TaskRecord task = mTaskHistory.get(taskNdx);
1288 final ArrayList<ActivityRecord> activities = task.mActivities;
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.
1300 if (!behindFullscreen || 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);
1327 noStackActivityResumed = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001328 }
1329
1330 } else if (r.visible) {
1331 // If this activity is already visible, then there is nothing
1332 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001333 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1334 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001335 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001336 try {
George Mount6ba042b2014-07-28 11:12:28 -07001337 if (r.returningOptions != null) {
1338 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1339 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001340 }
1341 } catch(RemoteException e) {
1342 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001343 if (r.state == ActivityState.RESUMED) {
1344 noStackActivityResumed = false;
1345 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001346 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001347 // This activity is not currently visible, but is running.
1348 // Tell it to become visible.
1349 r.visible = true;
1350 if (r.state != ActivityState.RESUMED && r != starting) {
1351 // If this activity is paused, tell it
1352 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001353 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1354 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001355 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001356 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001357 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001358 mUndrawnActivitiesBelowTopTranslucent.add(r);
1359 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001360 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001361 r.sleeping = false;
1362 r.app.pendingUiClean = true;
1363 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1364 r.stopFreezingScreenLocked(false);
1365 } catch (Exception e) {
1366 // Just skip on any failure; we'll make it
1367 // visible when it next restarts.
1368 Slog.w(TAG, "Exception thrown making visibile: "
1369 + r.intent.getComponent(), e);
1370 }
1371 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001372 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001373
Craig Mautnerd44711d2013-02-23 11:24:36 -08001374 // Aggregate current change flags.
1375 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001376
Craig Mautnerd44711d2013-02-23 11:24:36 -08001377 if (r.fullscreen) {
1378 // At this point, nothing else needs to be shown
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001379 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r);
Craig Mautner580ea812013-04-25 12:58:38 -07001380 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001381 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001382 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001383 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001384 }
1385 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001386 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1387 "Make invisible? " + r + " finishing=" + r.finishing
1388 + " state=" + r.state + " behindFullscreen=" + behindFullscreen);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001389 // Now for any activities that aren't visible to the user, make
1390 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001391 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001392 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001393 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001394 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001395 switch (r.state) {
1396 case STOPPING:
1397 case STOPPED:
1398 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001399 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1400 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001401 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1402 }
1403 break;
1404
1405 case INITIALIZING:
1406 case RESUMED:
1407 case PAUSING:
1408 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001409 // This case created for transitioning activities from
1410 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001411 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001412 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001413 } else {
1414 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1415 mStackSupervisor.mStoppingActivities.add(r);
1416 }
1417 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001418 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001419 break;
1420
1421 default:
1422 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001423 }
1424 } catch (Exception e) {
1425 // Just skip on any failure; we'll make it
1426 // visible when it next restarts.
1427 Slog.w(TAG, "Exception thrown making hidden: "
1428 + r.intent.getComponent(), e);
1429 }
1430 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001431 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 }
1434 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001436
1437 if (mTranslucentActivityWaiting != null &&
1438 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1439 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1440 notifyActivityDrawnLocked(null);
1441 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 }
Craig Mautner58547802013-03-05 08:23:53 -08001443
Todd Kennedyaab56db2015-01-30 09:39:53 -08001444 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001445 mTranslucentActivityWaiting = r;
1446 mUndrawnActivitiesBelowTopTranslucent.clear();
1447 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1448 }
1449
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001450 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1451 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1452 final TaskRecord task = mTaskHistory.get(taskNdx);
1453 final ArrayList<ActivityRecord> activities = task.mActivities;
1454 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1455 final ActivityRecord r = activities.get(activityNdx);
1456 if ( r.appTimeTracker != except) {
1457 r.appTimeTracker = null;
1458 }
1459 }
1460 }
1461 }
1462
Craig Mautner5eda9b32013-07-02 11:58:16 -07001463 /**
1464 * Called as activities below the top translucent activity are redrawn. When the last one is
1465 * redrawn notify the top activity by calling
1466 * {@link Activity#onTranslucentConversionComplete}.
1467 *
1468 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1469 * occurred and the activity will be notified immediately.
1470 */
1471 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001472 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001473 if ((r == null)
1474 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1475 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1476 // The last undrawn activity below the top has just been drawn. If there is an
1477 // opaque activity at the top, notify it that it can become translucent safely now.
1478 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1479 mTranslucentActivityWaiting = null;
1480 mUndrawnActivitiesBelowTopTranslucent.clear();
1481 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1482
Craig Mautner71dd1b62014-02-18 15:48:52 -08001483 if (waitingActivity != null) {
1484 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1485 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1486 try {
1487 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1488 waitingActivity.appToken, r != null);
1489 } catch (RemoteException e) {
1490 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001491 }
1492 }
1493 }
1494 }
1495
Craig Mautnera61bc652013-10-28 15:43:18 -07001496 /** If any activities below the top running one are in the INITIALIZING state and they have a
1497 * starting window displayed then remove that starting window. It is possible that the activity
1498 * in this state will never resumed in which case that starting window will be orphaned. */
1499 void cancelInitializingActivities() {
1500 final ActivityRecord topActivity = topRunningActivityLocked(null);
1501 boolean aboveTop = true;
1502 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1503 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1504 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1505 final ActivityRecord r = activities.get(activityNdx);
1506 if (aboveTop) {
1507 if (r == topActivity) {
1508 aboveTop = false;
1509 }
1510 continue;
1511 }
1512
1513 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001514 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1515 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001516 r.mStartingWindowShown = false;
1517 mWindowManager.removeAppStartingWindow(r.appToken);
1518 }
1519 }
1520 }
1521 }
1522
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 /**
1524 * Ensure that the top activity in the stack is resumed.
1525 *
1526 * @param prev The previously resumed activity, for when in the process
1527 * of pausing; can be null to call from elsewhere.
1528 *
1529 * @return Returns true if something is being resumed, or false if
1530 * nothing happened.
1531 */
1532 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001533 return resumeTopActivityLocked(prev, null);
1534 }
1535
1536 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001537 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001538 // Don't even start recursing.
1539 return false;
1540 }
1541
1542 boolean result = false;
1543 try {
1544 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001545 mStackSupervisor.inResumeTopActivity = true;
1546 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1547 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001548 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001549 }
Craig Mautner544efa72014-09-04 16:41:20 -07001550 result = resumeTopActivityInnerLocked(prev, options);
1551 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001552 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001553 }
1554 return result;
1555 }
1556
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001557 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001558 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001559
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001560 if (!mService.mBooting && !mService.mBooted) {
1561 // Not ready yet!
1562 return false;
1563 }
1564
Craig Mautnerdf88d732014-01-27 09:21:32 -08001565 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001566 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001567 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001568 // Do not resume this stack if its parent is not resumed.
1569 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1570 return false;
1571 }
1572
Craig Mautnera61bc652013-10-28 15:43:18 -07001573 cancelInitializingActivities();
1574
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001576 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001577
1578 // Remember how we'll process this pause/resume situation, and ensure
1579 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001580 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1581 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582
Craig Mautner84984fa2014-06-19 11:19:20 -07001583 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001585 // There are no more activities!
1586 final String reason = "noMoreActivities";
1587 if (!mFullscreen) {
1588 // Try to move focus to the next visible stack with a running activity if this
1589 // stack is not covering the entire screen.
1590 final ActivityStack stack = getNextVisibleStackLocked();
1591 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1592 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1593 }
1594 }
1595 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001596 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001597 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001598 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001599 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001600 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1601 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1602 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001603 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001604 }
1605
1606 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001607
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001609 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1610 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 // Make sure we have executed any pending transitions, since there
1612 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001613 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001615 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001616 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001617 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 return false;
1619 }
1620
Craig Mautner525f3d92013-05-07 14:01:50 -07001621 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001622 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001623 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001624 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001625 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001626 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001627 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001628 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001629 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001630 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001631 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001632 } else if (!isOnHomeDisplay()) {
1633 return false;
1634 } else if (!isHomeStack()){
1635 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001636 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001637 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1638 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1639 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001640 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001641 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001642 }
1643
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 // If we are sleeping, and there is no resumed activity, and the top
1645 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001646 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001647 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001648 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 // Make sure we have executed any pending transitions, since there
1650 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001651 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001653 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001654 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001655 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001656 return false;
1657 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001658
1659 // Make sure that the user who owns this activity is started. If not,
1660 // we will just leave it as is because someone should be bringing
1661 // another user's activities to the top of the stack.
1662 if (mService.mStartedUsers.get(next.userId) == null) {
1663 Slog.w(TAG, "Skipping resume of top activity " + next
1664 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001665 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001666 return false;
1667 }
1668
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001669 // The activity may be waiting for stop, but that is no longer
1670 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001671 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001672 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001673 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001674 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001676 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001678 // If we are currently pausing an activity, then don't do anything
1679 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001680 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001681 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001682 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001683 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001684 return false;
1685 }
1686
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001687 // Okay we are now going to start a switch, to 'next'. We may first
1688 // have to pause the current activity, but this is an important point
1689 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001690 // XXX "App Redirected" dialog is getting too many false positives
1691 // at this point, so turn off for now.
1692 if (false) {
1693 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1694 long now = SystemClock.uptimeMillis();
1695 final boolean inTime = mLastStartedActivity.startTime != 0
1696 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1697 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1698 final int nextUid = next.info.applicationInfo.uid;
1699 if (inTime && lastUid != nextUid
1700 && lastUid != next.launchedFromUid
1701 && mService.checkPermission(
1702 android.Manifest.permission.STOP_APP_SWITCHES,
1703 -1, next.launchedFromUid)
1704 != PackageManager.PERMISSION_GRANTED) {
1705 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1706 } else {
1707 next.startTime = now;
1708 mLastStartedActivity = next;
1709 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001710 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001711 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001712 mLastStartedActivity = next;
1713 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001714 }
Craig Mautner58547802013-03-05 08:23:53 -08001715
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001716 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1717
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 // We need to start pausing the current activity so the top one
1719 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001720 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1721 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001722 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001723 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001724 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001725 }
1726 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001727 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1728 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001729 // At this point we want to put the upcoming activity's process
1730 // at the top of the LRU list, since we know we will be needing it
1731 // very soon and it would be a waste to let it get killed if it
1732 // happens to be sitting towards the end.
1733 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001734 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001735 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001736 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001737 return true;
1738 }
1739
Christopher Tated3f175c2012-06-14 14:16:54 -07001740 // If the most recent activity was noHistory but was only stopped rather
1741 // than stopped+finished because the device went to sleep, we need to make
1742 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001743 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001744 !mLastNoHistoryActivity.finishing) {
1745 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1746 " on new resume");
1747 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001748 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001749 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001750 }
1751
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001753 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1754 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001755 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001756 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1757 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758 } else {
1759 // The next activity is already visible, so hide the previous
1760 // activity's windows right now so we can show the new one ASAP.
1761 // We only do this if the previous is finishing, which should mean
1762 // it is on top of the one being resumed so hiding it quickly
1763 // is good. Otherwise, we want to do the normal route of allowing
1764 // the resumed activity to be shown so we can decide if the
1765 // previous should actually be hidden depending on whether the
1766 // new one is found to be full-screen or not.
1767 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001768 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001769 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1770 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001771 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 + ", nowVisible=" + next.nowVisible);
1773 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001774 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001775 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001776 + ", waitingVisible="
1777 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1778 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001779 }
1780 }
1781 }
1782
Dianne Hackborne7f97212011-02-24 14:40:20 -08001783 // Launching this app's activity, make sure the app is no longer
1784 // considered stopped.
1785 try {
1786 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001787 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001788 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001789 } catch (IllegalArgumentException e) {
1790 Slog.w(TAG, "Failed trying to unstop package "
1791 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001792 }
1793
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 // We are starting up the next activity, so tell the window manager
1795 // that the previous one will be hidden soon. This way it can know
1796 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001797 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001798 if (prev != null) {
1799 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001800 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 "Prepare close transition: prev=" + prev);
1802 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001803 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001804 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001805 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001806 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001807 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1808 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001810 mWindowManager.setAppWillBeHidden(prev.appToken);
1811 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001813 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1814 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001816 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001817 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001819 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001820 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001821 : next.mLaunchTaskBehind
1822 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1823 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 }
1825 }
1826 if (false) {
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 }
Craig Mautner967212c2013-04-13 21:10:58 -07001830 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001831 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
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(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837 }
1838 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001839
1840 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001841 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001842 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1843 if (opts != null) {
1844 resumeAnimOptions = opts.toBundle();
1845 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001846 next.applyOptionsLocked();
1847 } else {
1848 next.clearOptionsLocked();
1849 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001850
Craig Mautnercf910b02013-04-23 11:23:27 -07001851 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001852 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001853 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854
1855 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001856 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001858 // schedule launch ticks to collect information about slow apps.
1859 next.startLaunchTickingLocked();
1860
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001861 ActivityRecord lastResumedActivity =
1862 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001863 ActivityState lastState = next.state;
1864
1865 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001866
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001867 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 next.state = ActivityState.RESUMED;
1869 mResumedActivity = next;
1870 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001871 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001872 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001873 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001874 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001875
1876 // Have the window manager re-evaluate the orientation of
1877 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001878 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001879 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001880 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001881 mService.mConfiguration,
1882 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1883 if (config != null) {
1884 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001886 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001887 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001888
Craig Mautner525f3d92013-05-07 14:01:50 -07001889 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001890 // The configuration update wasn't able to keep the existing
1891 // instance of the activity, and instead started a new one.
1892 // We should be all done, but let's just make sure our activity
1893 // is still at the top and schedule another run if something
1894 // weird happened.
1895 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001896 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 "Activity config changed during resume: " + next
1898 + ", new next: " + nextNext);
1899 if (nextNext != next) {
1900 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001901 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001902 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001903 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001904 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001905 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001906 return true;
1907 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001908 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001909 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001910 }
Craig Mautner58547802013-03-05 08:23:53 -08001911
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 try {
1913 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001914 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915 if (a != null) {
1916 final int N = a.size();
1917 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001918 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1919 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001920 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921 }
1922 }
1923
1924 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001925 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926 }
1927
Craig Mautner299f9602015-01-26 09:47:33 -08001928 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1929 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001930
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001931 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001932 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001933 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001934 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07001935 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001936 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001937 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001938
Craig Mautner0eea92c2013-05-16 13:35:39 -07001939 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001940
Craig Mautnerac6f8432013-07-17 13:24:59 -07001941 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001942 } catch (Exception e) {
1943 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001944 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1945 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001946 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001947 if (lastStack != null) {
1948 lastStack.mResumedActivity = lastResumedActivity;
1949 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001950 Slog.i(TAG, "Restarting because process died: " + next);
1951 if (!next.hasBeenLaunched) {
1952 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001953 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1954 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001955 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001956 next.appToken, next.packageName, next.theme,
1957 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001958 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1959 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001960 }
George Mount2c92c972014-03-20 09:38:23 -07001961 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001962 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001963 return true;
1964 }
1965
1966 // From this point on, if something goes wrong there is no way
1967 // to recover the activity.
1968 try {
1969 next.visible = true;
1970 completeResumeLocked(next);
1971 } catch (Exception e) {
1972 // If any exception gets thrown, toss away this
1973 // activity and try the next one.
1974 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001975 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001976 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001977 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 return true;
1979 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 next.stopped = false;
1981
1982 } else {
1983 // Whoops, need to restart this activity!
1984 if (!next.hasBeenLaunched) {
1985 next.hasBeenLaunched = true;
1986 } else {
1987 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001988 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001989 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001990 mService.compatibilityInfoForPackageLocked(
1991 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001992 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001993 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001994 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001995 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001996 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001998 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001999 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 }
2001
Craig Mautnercf910b02013-04-23 11:23:27 -07002002 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 return true;
2004 }
2005
riddle_hsuc215a4f2014-12-27 12:10:45 +08002006 private TaskRecord getNextTask(TaskRecord targetTask) {
2007 final int index = mTaskHistory.indexOf(targetTask);
2008 if (index >= 0) {
2009 final int numTasks = mTaskHistory.size();
2010 for (int i = index + 1; i < numTasks; ++i) {
2011 TaskRecord task = mTaskHistory.get(i);
2012 if (task.userId == targetTask.userId) {
2013 return task;
2014 }
2015 }
2016 }
2017 return null;
2018 }
2019
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002020 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002021 // If the moving task is over home stack, transfer its return type to next task
2022 if (task.isOverHomeStack()) {
2023 final TaskRecord nextTask = getNextTask(task);
2024 if (nextTask != null) {
2025 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2026 }
2027 }
2028
Craig Mautner9c85c202013-10-04 20:11:26 -07002029 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002030 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002031 if (isOnHomeDisplay()) {
2032 ActivityStack lastStack = mStackSupervisor.getLastStack();
2033 final boolean fromHome = lastStack.isHomeStack();
2034 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002035 task.setTaskToReturnTo(fromHome
2036 ? lastStack.topTask() == null
2037 ? HOME_ACTIVITY_TYPE
2038 : lastStack.topTask().taskType
2039 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002040 }
2041 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002042 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002043 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002044
Craig Mautnerac6f8432013-07-17 13:24:59 -07002045 mTaskHistory.remove(task);
2046 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002047 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002048 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002049 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002050 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002051 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002052 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002053 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002054 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002055 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002056 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002057 break;
2058 }
2059 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002060 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002061 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002062 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002063 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002064 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002065
Craig Mautner8849a5e2013-04-02 16:41:03 -07002066 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002067 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002068 TaskRecord rTask = r.task;
2069 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002070 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2071 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002072 // Last activity in task had been removed or ActivityManagerService is reusing task.
2073 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002074 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002075 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002076 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002077 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002078 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 if (!newTask) {
2080 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002081 boolean startIt = true;
2082 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2083 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002084 if (task.getTopActivity() == null) {
2085 // All activities in task are finishing.
2086 continue;
2087 }
Craig Mautner70a86932013-02-28 22:37:44 -08002088 if (task == r.task) {
2089 // Here it is! Now, if this is not yet visible to the
2090 // user, then just add it without starting; it will
2091 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002092 if (!startIt) {
2093 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2094 + task, new RuntimeException("here").fillInStackTrace());
2095 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002096 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002097 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2098 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002099 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002100 r.userId, r.info.configChanges, task.voiceSession != null,
2101 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002103 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002105 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002106 return;
2107 }
2108 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002109 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 startIt = false;
2111 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 }
2113 }
2114
2115 // Place a new activity at top of stack, so it is next to interact
2116 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 // If we are not placing the new activity frontmost, we do not want
2119 // to deliver the onUserLeaving callback to the actual frontmost
2120 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002121 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002122 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002123 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002124 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002125 }
Craig Mautner70a86932013-02-28 22:37:44 -08002126
2127 task = r.task;
2128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002130 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002131 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002132 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002133 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002134
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002135 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002136 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 // We want to show the starting preview window if we are
2138 // switching to a new task, or the next activity's process is
2139 // not currently running.
2140 boolean showStartingIcon = newTask;
2141 ProcessRecord proc = r.app;
2142 if (proc == null) {
2143 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2144 }
2145 if (proc == null || proc.thread == null) {
2146 showStartingIcon = true;
2147 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002148 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002149 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002150 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002151 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002154 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002155 ? r.mLaunchTaskBehind
2156 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2157 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002158 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 mNoAnimActivities.remove(r);
2160 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002161 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002162 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002163 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002164 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 boolean doShow = true;
2166 if (newTask) {
2167 // Even though this activity is starting fresh, we still need
2168 // to reset it to make sure we apply affinities to move any
2169 // existing activities from other tasks in to it.
2170 // If the caller has requested that the target task be
2171 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002172 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002173 resetTaskIfNeededLocked(r, r);
2174 doShow = topRunningNonDelayedActivityLocked(null) == r;
2175 }
George Mount70778d72014-07-01 16:33:45 -07002176 } else if (options != null && new ActivityOptions(options).getAnimationType()
2177 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2178 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002180 if (r.mLaunchTaskBehind) {
2181 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2182 // tell WindowManager that r is visible even though it is at the back of the stack.
2183 mWindowManager.setAppVisibility(r.appToken, true);
2184 ensureActivitiesVisibleLocked(null, 0);
2185 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186 // Figure out if we are transitioning from another activity that is
2187 // "has the same starting icon" as the next one. This allows the
2188 // window manager to keep the previous window it had previously
2189 // created, if it still had one.
2190 ActivityRecord prev = mResumedActivity;
2191 if (prev != null) {
2192 // We don't want to reuse the previous starting preview if:
2193 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002194 if (prev.task != r.task) {
2195 prev = null;
2196 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002198 else if (prev.nowVisible) {
2199 prev = null;
2200 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002202 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002203 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002204 mService.compatibilityInfoForPackageLocked(
2205 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002206 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002207 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002208 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 }
2210 } else {
2211 // If this is the first activity, don't do any fancy animations,
2212 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002213 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002214 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002215 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002216 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002217 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002218 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 }
2220 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002221 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002222 }
2223
2224 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002225 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 }
2227 }
2228
Dianne Hackbornbe707852011-11-11 14:32:10 -08002229 final void validateAppTokensLocked() {
2230 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002231 mValidateAppTokens.ensureCapacity(numActivities());
2232 final int numTasks = mTaskHistory.size();
2233 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2234 TaskRecord task = mTaskHistory.get(taskNdx);
2235 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002236 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002237 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002238 }
Craig Mautner000f0022013-02-26 15:04:29 -08002239 TaskGroup group = new TaskGroup();
2240 group.taskId = task.taskId;
2241 mValidateAppTokens.add(group);
2242 final int numActivities = activities.size();
2243 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2244 final ActivityRecord r = activities.get(activityNdx);
2245 group.tokens.add(r.appToken);
2246 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002247 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002248 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002249 }
2250
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 /**
2252 * Perform a reset of the given task, if needed as part of launching it.
2253 * Returns the new HistoryRecord at the top of the task.
2254 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002255 /**
2256 * Helper method for #resetTaskIfNeededLocked.
2257 * We are inside of the task being reset... we'll either finish this activity, push it out
2258 * for another task, or leave it as-is.
2259 * @param task The task containing the Activity (taskTop) that might be reset.
2260 * @param forceReset
2261 * @return An ActivityOptions that needs to be processed.
2262 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002263 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002264 ActivityOptions topOptions = null;
2265
2266 int replyChainEnd = -1;
2267 boolean canMoveOptions = true;
2268
2269 // We only do this for activities that are not the root of the task (since if we finish
2270 // the root, we may no longer have the task!).
2271 final ArrayList<ActivityRecord> activities = task.mActivities;
2272 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002273 final int rootActivityNdx = task.findEffectiveRootIndex();
2274 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002275 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002276 if (target.frontOfTask)
2277 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002278
2279 final int flags = target.info.flags;
2280 final boolean finishOnTaskLaunch =
2281 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2282 final boolean allowTaskReparenting =
2283 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2284 final boolean clearWhenTaskReset =
2285 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2286
2287 if (!finishOnTaskLaunch
2288 && !clearWhenTaskReset
2289 && target.resultTo != null) {
2290 // If this activity is sending a reply to a previous
2291 // activity, we can't do anything with it now until
2292 // we reach the start of the reply chain.
2293 // XXX note that we are assuming the result is always
2294 // to the previous activity, which is almost always
2295 // the case but we really shouldn't count on.
2296 if (replyChainEnd < 0) {
2297 replyChainEnd = i;
2298 }
2299 } else if (!finishOnTaskLaunch
2300 && !clearWhenTaskReset
2301 && allowTaskReparenting
2302 && target.taskAffinity != null
2303 && !target.taskAffinity.equals(task.affinity)) {
2304 // If this activity has an affinity for another
2305 // task, then we need to move it out of here. We will
2306 // move it as far out of the way as possible, to the
2307 // bottom of the activity stack. This also keeps it
2308 // correctly ordered with any activities we previously
2309 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002310 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002311 final ActivityRecord bottom =
2312 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002313 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002314 if (bottom != null && target.taskAffinity != null
2315 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 // If the activity currently at the bottom has the
2317 // same task affinity as the one we are moving,
2318 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002319 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002320 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002321 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002322 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002323 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002324 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002325 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002326 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002327 + " out to new task " + target.task);
2328 }
2329
Craig Mautnere3a74d52013-02-22 14:14:58 -08002330 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002331 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002332
Craig Mautner525f3d92013-05-07 14:01:50 -07002333 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002334 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2335 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002336 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002337 if (p.finishing) {
2338 continue;
2339 }
2340
Craig Mautnere3a74d52013-02-22 14:14:58 -08002341 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002342 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002343 topOptions = p.takeOptionsLocked();
2344 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002345 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002346 }
2347 }
2348 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002349 + task + " adding to task=" + targetTask
2350 + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002351 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2352 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002353 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002354 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002355
Craig Mautner83162a92015-01-26 14:43:30 -08002356 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002357 }
2358
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002359 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002360 if (VALIDATE_TOKENS) {
2361 validateAppTokensLocked();
2362 }
2363
2364 replyChainEnd = -1;
2365 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2366 // If the activity should just be removed -- either
2367 // because it asks for it, or the task should be
2368 // cleared -- then finish it and anything that is
2369 // part of its reply chain.
2370 int end;
2371 if (clearWhenTaskReset) {
2372 // In this case, we want to finish this activity
2373 // and everything above it, so be sneaky and pretend
2374 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002375 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002376 } else if (replyChainEnd < 0) {
2377 end = i;
2378 } else {
2379 end = replyChainEnd;
2380 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002381 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002382 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002383 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002384 if (p.finishing) {
2385 continue;
2386 }
2387 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002388 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002389 topOptions = p.takeOptionsLocked();
2390 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002391 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002392 }
2393 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002394 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002395 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002396 if (finishActivityLocked(
2397 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002398 end--;
2399 srcPos--;
2400 }
2401 }
2402 replyChainEnd = -1;
2403 } else {
2404 // If we were in the middle of a chain, well the
2405 // activity that started it all doesn't want anything
2406 // special, so leave it all as-is.
2407 replyChainEnd = -1;
2408 }
2409 }
2410
2411 return topOptions;
2412 }
2413
2414 /**
2415 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2416 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2417 * @param affinityTask The task we are looking for an affinity to.
2418 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2419 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2420 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2421 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002422 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002423 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002424 int replyChainEnd = -1;
2425 final int taskId = task.taskId;
2426 final String taskAffinity = task.affinity;
2427
2428 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2429 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002430 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2431
2432 // Do not operate on or below the effective root Activity.
2433 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002434 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002435 if (target.frontOfTask)
2436 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002437
2438 final int flags = target.info.flags;
2439 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2440 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2441
2442 if (target.resultTo != null) {
2443 // If this activity is sending a reply to a previous
2444 // activity, we can't do anything with it now until
2445 // we reach the start of the reply chain.
2446 // XXX note that we are assuming the result is always
2447 // to the previous activity, which is almost always
2448 // the case but we really shouldn't count on.
2449 if (replyChainEnd < 0) {
2450 replyChainEnd = i;
2451 }
2452 } else if (topTaskIsHigher
2453 && allowTaskReparenting
2454 && taskAffinity != null
2455 && taskAffinity.equals(target.taskAffinity)) {
2456 // This activity has an affinity for our task. Either remove it if we are
2457 // clearing or move it over to our task. Note that
2458 // we currently punt on the case where we are resetting a
2459 // task that is not at the top but who has activities above
2460 // with an affinity to it... this is really not a normal
2461 // case, and we will need to later pull that task to the front
2462 // and usually at that point we will do the reset and pick
2463 // up those remaining activities. (This only happens if
2464 // someone starts an activity in a new task from an activity
2465 // in a task that is not currently on top.)
2466 if (forceReset || finishOnTaskLaunch) {
2467 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002468 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2469 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002470 for (int srcPos = start; srcPos >= i; --srcPos) {
2471 final ActivityRecord p = activities.get(srcPos);
2472 if (p.finishing) {
2473 continue;
2474 }
Todd Kennedy539db512014-12-15 09:57:55 -08002475 finishActivityLocked(
2476 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002477 }
2478 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002479 if (taskInsertionPoint < 0) {
2480 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002481
Craig Mautner77878772013-03-04 19:46:24 -08002482 }
Craig Mautner77878772013-03-04 19:46:24 -08002483
2484 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002485 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2486 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2487 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002488 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002489 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002490 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002491 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002492
Craig Mautnere3a74d52013-02-22 14:14:58 -08002493 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2494 + " to stack at " + task,
2495 new RuntimeException("here").fillInStackTrace());
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002496 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2497 + " from " + srcPos + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002498 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002499 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002500 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002501 if (VALIDATE_TOKENS) {
2502 validateAppTokensLocked();
2503 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002504
2505 // Now we've moved it in to place... but what if this is
2506 // a singleTop activity and we have put it on top of another
2507 // instance of the same activity? Then we drop the instance
2508 // below so it remains singleTop.
2509 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2510 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002511 int targetNdx = taskActivities.indexOf(target);
2512 if (targetNdx > 0) {
2513 ActivityRecord p = taskActivities.get(targetNdx - 1);
2514 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002515 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2516 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002517 }
2518 }
2519 }
2520 }
2521
2522 replyChainEnd = -1;
2523 }
2524 }
Craig Mautner77878772013-03-04 19:46:24 -08002525 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 }
2527
Craig Mautner8849a5e2013-04-02 16:41:03 -07002528 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002529 ActivityRecord newActivity) {
2530 boolean forceReset =
2531 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2532 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2533 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2534 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2535 forceReset = true;
2536 }
2537 }
2538
2539 final TaskRecord task = taskTop.task;
2540
2541 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2542 * for remaining tasks. Used for later tasks to reparent to task. */
2543 boolean taskFound = false;
2544
2545 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2546 ActivityOptions topOptions = null;
2547
Craig Mautner77878772013-03-04 19:46:24 -08002548 // Preserve the location for reparenting in the new task.
2549 int reparentInsertionPoint = -1;
2550
Craig Mautnere3a74d52013-02-22 14:14:58 -08002551 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2552 final TaskRecord targetTask = mTaskHistory.get(i);
2553
2554 if (targetTask == task) {
2555 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2556 taskFound = true;
2557 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002558 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2559 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002560 }
2561 }
2562
Craig Mautner70a86932013-02-28 22:37:44 -08002563 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002564 if (taskNdx >= 0) {
2565 do {
2566 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2567 } while (taskTop == null && taskNdx >= 0);
2568 }
Craig Mautner70a86932013-02-28 22:37:44 -08002569
Craig Mautnere3a74d52013-02-22 14:14:58 -08002570 if (topOptions != null) {
2571 // If we got some ActivityOptions from an activity on top that
2572 // was removed from the task, propagate them to the new real top.
2573 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002574 taskTop.updateOptionsLocked(topOptions);
2575 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002576 topOptions.abort();
2577 }
2578 }
2579
2580 return taskTop;
2581 }
2582
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002583 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2584 String resultWho, int requestCode, int resultCode, Intent data) {
2585
2586 if (callingUid > 0) {
2587 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002588 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002589 }
2590
2591 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2592 + " : who=" + resultWho + " req=" + requestCode
2593 + " res=" + resultCode + " data=" + data);
2594 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2595 try {
2596 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2597 list.add(new ResultInfo(resultWho, requestCode,
2598 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002599 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 return;
2601 } catch (Exception e) {
2602 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2603 }
2604 }
2605
2606 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2607 }
2608
Craig Mautner299f9602015-01-26 09:47:33 -08002609 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002610 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2611 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002612 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002613 if (next != r) {
2614 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002615 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002616 // For non-fullscreen stack, we want to move the focus to the next visible
2617 // stack to prevent the home screen from moving to the top and obscuring
2618 // other visible stacks.
2619 if (!mFullscreen
2620 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2621 return;
2622 }
2623 // Move the home stack to the top if this stack is fullscreen or there is no
2624 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002625 if (mStackSupervisor.moveHomeStackTaskToTop(
2626 task.getTaskToReturnTo(), myReason)) {
2627 // Activity focus was already adjusted. Nothing else to do...
2628 return;
2629 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002630 }
2631 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002632
2633 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002634 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002635 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002636 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002637 }
2638 }
2639
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002640 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2641 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2642 final String myReason = reason + " adjustFocusToNextVisibleStack";
2643 if (stack == null) {
2644 return false;
2645 }
2646 final ActivityRecord top = stack.topRunningActivityLocked(null);
2647 if (top == null) {
2648 return false;
2649 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002650 mService.setFocusedActivityLocked(top, myReason);
2651 return true;
2652 }
2653
Craig Mautnerf3333272013-04-22 10:55:53 -07002654 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002655 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2657 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2658 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002659 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002660 if (DEBUG_STATES) {
2661 Slog.d(TAG, "no-history finish of " + r);
2662 }
2663 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002664 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002665 } else {
2666 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2667 + " on stop because we're just sleeping");
2668 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002669 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002670 }
2671
2672 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002673 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002674 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675 try {
2676 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002677 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2678 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002679 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002680 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2681 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002683 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002684 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002685 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002686 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002687 r.setSleeping(true);
2688 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002689 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002690 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002691 } catch (Exception e) {
2692 // Maybe just ignore exceptions here... if the process
2693 // has crashed, our death notification will clean things
2694 // up.
2695 Slog.w(TAG, "Exception thrown during pause", e);
2696 // Just in case, assume it to be stopped.
2697 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002698 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 r.state = ActivityState.STOPPED;
2700 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002701 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002702 }
2703 }
2704 }
2705 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 /**
2708 * @return Returns true if the activity is being finished, false if for
2709 * some reason it is being left as-is.
2710 */
2711 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002712 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002713 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002714 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002715 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002716 + ", result=" + resultCode + ", data=" + resultData
2717 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002718 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 return false;
2720 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721
Craig Mautnerd44711d2013-02-23 11:24:36 -08002722 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 return true;
2724 }
2725
Craig Mautnerd2328952013-03-05 12:46:26 -08002726 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002727 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2728 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2729 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2730 ActivityRecord r = activities.get(activityNdx);
2731 if (r.resultTo == self && r.requestCode == requestCode) {
2732 if ((r.resultWho == null && resultWho == null) ||
2733 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2734 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2735 false);
2736 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002737 }
2738 }
2739 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002740 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002741 }
2742
Todd Kennedy539db512014-12-15 09:57:55 -08002743 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002744 ActivityRecord r = topRunningActivityLocked(null);
2745 if (r != null && r.app == app) {
2746 // If the top running activity is from this crashing
2747 // process, then terminate it to avoid getting in a loop.
2748 Slog.w(TAG, " Force finishing activity "
2749 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002750 int taskNdx = mTaskHistory.indexOf(r.task);
2751 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002752 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002753 // Also terminate any activities below it that aren't yet
2754 // stopped, to avoid a situation where one will get
2755 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002756 --activityNdx;
2757 if (activityNdx < 0) {
2758 do {
2759 --taskNdx;
2760 if (taskNdx < 0) {
2761 break;
2762 }
2763 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2764 } while (activityNdx < 0);
2765 }
2766 if (activityNdx >= 0) {
2767 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002768 if (r.state == ActivityState.RESUMED
2769 || r.state == ActivityState.PAUSING
2770 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002771 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002772 Slog.w(TAG, " Force finishing activity "
2773 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002774 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002775 }
2776 }
2777 }
2778 }
2779 }
2780
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002781 final void finishVoiceTask(IVoiceInteractionSession session) {
2782 IBinder sessionBinder = session.asBinder();
2783 boolean didOne = false;
2784 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2785 TaskRecord tr = mTaskHistory.get(taskNdx);
2786 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2787 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2788 ActivityRecord r = tr.mActivities.get(activityNdx);
2789 if (!r.finishing) {
2790 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2791 false);
2792 didOne = true;
2793 }
2794 }
2795 }
2796 }
2797 if (didOne) {
2798 mService.updateOomAdjLocked();
2799 }
2800 }
2801
Craig Mautnerd2328952013-03-05 12:46:26 -08002802 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002803 ArrayList<ActivityRecord> activities = r.task.mActivities;
2804 for (int index = activities.indexOf(r); index >= 0; --index) {
2805 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002806 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002807 break;
2808 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002809 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002810 }
2811 return true;
2812 }
2813
Dianne Hackborn5c607432012-02-28 14:44:19 -08002814 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2815 // send the result
2816 ActivityRecord resultTo = r.resultTo;
2817 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002818 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002819 + " who=" + r.resultWho + " req=" + r.requestCode
2820 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002821 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002822 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002823 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002824 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002825 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002826 if (r.info.applicationInfo.uid > 0) {
2827 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2828 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002829 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002830 }
2831 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2832 resultData);
2833 r.resultTo = null;
2834 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002835 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002836
2837 // Make sure this HistoryRecord is not holding on to other resources,
2838 // because clients have remote IPC references to this object so we
2839 // can't assume that will go away and want to avoid circular IPC refs.
2840 r.results = null;
2841 r.pendingResults = null;
2842 r.newIntents = null;
2843 r.icicle = null;
2844 }
2845
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 /**
2847 * @return Returns true if this activity has been removed from the history
2848 * list, or false if it is still in the list and will be removed later.
2849 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002850 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2851 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002852 if (r.finishing) {
2853 Slog.w(TAG, "Duplicate finish request for " + r);
2854 return false;
2855 }
2856
Wale Ogunwale7d701172015-03-11 15:36:30 -07002857 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002858 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002859 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002860 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002861 task.taskId, r.shortComponentName, reason);
2862 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002863 final int index = activities.indexOf(r);
2864 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002865 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002866 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002867 // If the caller asked that this activity (and all above it)
2868 // be cleared when the task is reset, don't lose that information,
2869 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002870 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002871 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872 }
2873 }
2874
2875 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002876
Craig Mautner299f9602015-01-26 09:47:33 -08002877 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878
Dianne Hackborn5c607432012-02-28 14:44:19 -08002879 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002880
Craig Mautnerde4ef022013-04-07 19:01:33 -07002881 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002882 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002883 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002885 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002886 ? AppTransition.TRANSIT_TASK_CLOSE
2887 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002888
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002890 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002891
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002892 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002893 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
2894 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2895 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002896 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 }
2898
Craig Mautneraea74a52014-03-08 14:23:10 -08002899 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07002900 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002901 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 } else if (r.state != ActivityState.PAUSING) {
2903 // If the activity is PAUSING, we will complete the finish once
2904 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002905 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002906 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002907 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002908 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002909 }
2910
2911 return false;
2912 }
2913
Craig Mautnerf3333272013-04-22 10:55:53 -07002914 static final int FINISH_IMMEDIATELY = 0;
2915 static final int FINISH_AFTER_PAUSE = 1;
2916 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917
Craig Mautnerf3333272013-04-22 10:55:53 -07002918 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 // First things first: if this activity is currently visible,
2920 // and the resumed activity is not yet visible, then hold off on
2921 // finishing until the resumed one becomes visible.
2922 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002923 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2924 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002925 if (mStackSupervisor.mStoppingActivities.size() > 3
2926 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 // If we already have a few activities waiting to stop,
2928 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002929 // them out. Or if r is the last of activity of the last task the stack
2930 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002931 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002932 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002933 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 }
2935 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002936 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2937 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002939 if (oomAdj) {
2940 mService.updateOomAdjLocked();
2941 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002942 return r;
2943 }
2944
2945 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002946 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002947 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002948 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 if (mResumedActivity == r) {
2950 mResumedActivity = null;
2951 }
2952 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002953 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002954 r.state = ActivityState.FINISHING;
2955
2956 if (mode == FINISH_IMMEDIATELY
2957 || prevState == ActivityState.STOPPED
2958 || prevState == ActivityState.INITIALIZING) {
2959 // If this activity is already stopped, we can just finish
2960 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002961 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002962 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002963 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002964 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002965 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002966 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002967 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2968 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002969 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002971
2972 // Need to go through the full pause cycle to get this
2973 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002974 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002975 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002976 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002977 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002978 return r;
2979 }
2980
Craig Mautneree36c772014-07-16 14:56:05 -07002981 void finishAllActivitiesLocked(boolean immediately) {
2982 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002983 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2984 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2985 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2986 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002987 noActivitiesInStack = false;
2988 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002989 continue;
2990 }
Craig Mautneree36c772014-07-16 14:56:05 -07002991 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002992 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2993 }
2994 }
Craig Mautneree36c772014-07-16 14:56:05 -07002995 if (noActivitiesInStack) {
2996 mActivityContainer.onTaskListEmptyLocked();
2997 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002998 }
2999
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003000 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3001 // Basic case: for simple app-centric recents, we need to recreate
3002 // the task if the affinity has changed.
3003 if (srec == null || srec.task.affinity == null ||
3004 !srec.task.affinity.equals(destAffinity)) {
3005 return true;
3006 }
3007 // Document-centric case: an app may be split in to multiple documents;
3008 // they need to re-create their task if this current activity is the root
3009 // of a document, unless simply finishing it will return them to the the
3010 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003011 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3012 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003013 // Okay, this activity is at the root of its task. What to do, what to do...
3014 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3015 // Finishing won't return to an application, so we need to recreate.
3016 return true;
3017 }
3018 // We now need to get the task below it to determine what to do.
3019 int taskIdx = mTaskHistory.indexOf(srec.task);
3020 if (taskIdx <= 0) {
3021 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3022 return false;
3023 }
3024 if (taskIdx == 0) {
3025 // At the bottom of the stack, nothing to go back to.
3026 return true;
3027 }
3028 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3029 if (!srec.task.affinity.equals(prevTask.affinity)) {
3030 // These are different apps, so need to recreate.
3031 return true;
3032 }
3033 }
3034 return false;
3035 }
3036
Wale Ogunwale7d701172015-03-11 15:36:30 -07003037 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003038 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003039 final TaskRecord task = srec.task;
3040 final ArrayList<ActivityRecord> activities = task.mActivities;
3041 final int start = activities.indexOf(srec);
3042 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003043 return false;
3044 }
3045 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003046 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003047 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003048 final ComponentName dest = destIntent.getComponent();
3049 if (start > 0 && dest != null) {
3050 for (int i = finishTo; i >= 0; i--) {
3051 ActivityRecord r = activities.get(i);
3052 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003053 r.info.name.equals(dest.getClassName())) {
3054 finishTo = i;
3055 parent = r;
3056 foundParentInTask = true;
3057 break;
3058 }
3059 }
3060 }
3061
3062 IActivityController controller = mService.mController;
3063 if (controller != null) {
3064 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3065 if (next != null) {
3066 // ask watcher if this is allowed
3067 boolean resumeOK = true;
3068 try {
3069 resumeOK = controller.activityResuming(next.packageName);
3070 } catch (RemoteException e) {
3071 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003072 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003073 }
3074
3075 if (!resumeOK) {
3076 return false;
3077 }
3078 }
3079 }
3080 final long origId = Binder.clearCallingIdentity();
3081 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003082 ActivityRecord r = activities.get(i);
3083 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003084 // Only return the supplied result for the first activity finished
3085 resultCode = Activity.RESULT_CANCELED;
3086 resultData = null;
3087 }
3088
3089 if (parent != null && foundParentInTask) {
3090 final int parentLaunchMode = parent.info.launchMode;
3091 final int destIntentFlags = destIntent.getFlags();
3092 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3093 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3094 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3095 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003096 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3097 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003098 } else {
3099 try {
3100 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3101 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003102 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003103 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003104 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003105 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003106 foundParentInTask = res == ActivityManager.START_SUCCESS;
3107 } catch (RemoteException e) {
3108 foundParentInTask = false;
3109 }
3110 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003111 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003112 }
3113 }
3114 Binder.restoreCallingIdentity(origId);
3115 return foundParentInTask;
3116 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003117 /**
3118 * Perform the common clean-up of an activity record. This is called both
3119 * as part of destroyActivityLocked() (when destroying the client-side
3120 * representation) and cleaning things up as a result of its hosting
3121 * processing going away, in which case there is no remaining client-side
3122 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003123 *
3124 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003126 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3127 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003128 if (mResumedActivity == r) {
3129 mResumedActivity = null;
3130 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003131 if (mPausingActivity == r) {
3132 mPausingActivity = null;
3133 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003134 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135
3136 r.configDestroy = false;
3137 r.frozenBeforeDestroy = false;
3138
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003139 if (setState) {
3140 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3141 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003142 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003143 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003144 }
3145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 // Make sure this record is no longer in the pending finishes list.
3147 // This could happen, for example, if we are trimming activities
3148 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003149 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003150 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 // Remove any pending results.
3153 if (r.finishing && r.pendingResults != null) {
3154 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3155 PendingIntentRecord rec = apr.get();
3156 if (rec != null) {
3157 mService.cancelIntentSenderLocked(rec, false);
3158 }
3159 }
3160 r.pendingResults = null;
3161 }
3162
3163 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003164 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 }
3166
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003167 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003168 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003169 if (getVisibleBehindActivity() == r) {
3170 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003171 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003172 }
3173
3174 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003175 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003177 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003178 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003179 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 }
3181
Craig Mautner299f9602015-01-26 09:47:33 -08003182 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003183 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003184 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003185 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003186 if (DEBUG_ADD_REMOVE) {
3187 RuntimeException here = new RuntimeException("here");
3188 here.fillInStackTrace();
3189 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003190 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003191 r.takeFromHistory();
3192 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003193 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003194 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003195 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003196 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003197 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003198 if (VALIDATE_TOKENS) {
3199 validateAppTokensLocked();
3200 }
Craig Mautner312ba862014-02-10 17:55:01 -08003201 final TaskRecord task = r.task;
3202 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003203 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003204 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003205 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3206 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003207 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003208 }
Craig Mautner299f9602015-01-26 09:47:33 -08003209 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003210 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003211 cleanUpActivityServicesLocked(r);
3212 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003214
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 /**
3216 * Perform clean-up of service connections in an activity record.
3217 */
3218 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3219 // Throw away any services that have been bound by this activity.
3220 if (r.connections != null) {
3221 Iterator<ConnectionRecord> it = r.connections.iterator();
3222 while (it.hasNext()) {
3223 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003224 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 }
3226 r.connections = null;
3227 }
3228 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003229
Craig Mautneree2e45a2014-06-27 12:10:03 -07003230 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003231 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003232 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003233 mHandler.sendMessage(msg);
3234 }
3235
Craig Mautneree2e45a2014-06-27 12:10:03 -07003236 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003237 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003238 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003239 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3240 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3241 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3242 final ActivityRecord r = activities.get(activityNdx);
3243 if (r.finishing) {
3244 continue;
3245 }
3246 if (r.fullscreen) {
3247 lastIsOpaque = true;
3248 }
3249 if (owner != null && r.app != owner) {
3250 continue;
3251 }
3252 if (!lastIsOpaque) {
3253 continue;
3254 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003255 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003256 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003257 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003258 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003259 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003260 activityRemoved = true;
3261 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003262 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003263 }
3264 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003265 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003266 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003267 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003268 }
3269
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003270 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3271 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003272 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3273 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003274 + " pausing=" + mPausingActivity + " for reason " + reason);
3275 return destroyActivityLocked(r, true, reason);
3276 }
3277 return false;
3278 }
3279
3280 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3281 String reason) {
3282 // Iterate over tasks starting at the back (oldest) first.
3283 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3284 int maxTasks = tasks.size() / 4;
3285 if (maxTasks < 1) {
3286 maxTasks = 1;
3287 }
3288 int numReleased = 0;
3289 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3290 final TaskRecord task = mTaskHistory.get(taskNdx);
3291 if (!tasks.contains(task)) {
3292 continue;
3293 }
3294 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3295 int curNum = 0;
3296 final ArrayList<ActivityRecord> activities = task.mActivities;
3297 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3298 final ActivityRecord activity = activities.get(actNdx);
3299 if (activity.app == app && activity.isDestroyable()) {
3300 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3301 + " in state " + activity.state + " resumed=" + mResumedActivity
3302 + " pausing=" + mPausingActivity + " for reason " + reason);
3303 destroyActivityLocked(activity, true, reason);
3304 if (activities.get(actNdx) != activity) {
3305 // Was removed from list, back up so we don't miss the next one.
3306 actNdx--;
3307 }
3308 curNum++;
3309 }
3310 }
3311 if (curNum > 0) {
3312 numReleased += curNum;
3313 maxTasks--;
3314 if (mTaskHistory.get(taskNdx) != task) {
3315 // The entire task got removed, back up so we don't miss the next one.
3316 taskNdx--;
3317 }
3318 }
3319 }
3320 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3321 return numReleased;
3322 }
3323
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 /**
3325 * Destroy the current CLIENT SIDE instance of an activity. This may be
3326 * called both when actually finishing an activity, or when performing
3327 * a configuration switch where we destroy the current client-side object
3328 * but then create a new client-side object for this same HistoryRecord.
3329 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003330 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003331 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3332 "Removing activity from " + reason + ": token=" + r
3333 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003335 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003336 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003337
3338 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003339
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003340 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341
3342 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003343
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 if (hadApp) {
3345 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003346 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3348 mService.mHeavyWeightProcess = null;
3349 mService.mHandler.sendEmptyMessage(
3350 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3351 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003352 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003353 // Update any services we are bound to that might care about whether
3354 // their client may have activities.
3355 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003356 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003357 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003358 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003359 }
3360 }
3361
3362 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003363
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003365 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003366 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 r.configChangeFlags);
3368 } catch (Exception e) {
3369 // We can just ignore exceptions here... if the process
3370 // has crashed, our death notification will clean things
3371 // up.
3372 //Slog.w(TAG, "Exception thrown during finish", e);
3373 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003374 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 removedFromHistory = true;
3376 skipDestroy = true;
3377 }
3378 }
3379
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003380 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003381
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003382 // If the activity is finishing, we need to wait on removing it
3383 // from the list to give it a chance to do its cleanup. During
3384 // that time it may make calls back with its token so we need to
3385 // be able to find it on the list and so we don't want to remove
3386 // it from the list yet. Otherwise, we can just immediately put
3387 // it in the destroyed state since we are not removing it from the
3388 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003389 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003390 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3391 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003392 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003393 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003394 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3395 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003396 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003397 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003398 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003399 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003400 }
3401 } else {
3402 // remove this record from the history.
3403 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003404 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003405 removedFromHistory = true;
3406 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003407 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003408 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003409 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003410 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003411 }
3412 }
3413
3414 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003415
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003416 if (!mLRUActivities.remove(r) && hadApp) {
3417 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3418 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003419
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003420 return removedFromHistory;
3421 }
3422
Craig Mautner299f9602015-01-26 09:47:33 -08003423 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003424 final long origId = Binder.clearCallingIdentity();
3425 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003426 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003427 if (r != null) {
3428 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003429 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003430 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003431
Wale Ogunwale60454db2015-01-23 16:05:07 -08003432 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003433 if (r.state == ActivityState.DESTROYING) {
3434 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003435 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003436 }
3437 }
Craig Mautner05d29032013-05-03 13:40:13 -07003438 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003439 } finally {
3440 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003441 }
3442 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003443
Todd Kennedyaab56db2015-01-30 09:39:53 -08003444 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003445 if (hasVisibleBehindActivity() &&
3446 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003447 if (r == topRunningActivityLocked(null)) {
3448 // Don't release the top activity if it has requested to run behind the next
3449 // activity.
3450 return;
3451 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003452 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3453 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003454 " thread=" + r.app.thread);
3455 if (r != null && r.app != null && r.app.thread != null) {
3456 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003457 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003458 } catch (RemoteException e) {
3459 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003460 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003461 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003462 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003463 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003464 }
3465 }
3466 }
3467
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003468 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003469 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3470 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003471 if (r != null) {
3472 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003473 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003474 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003475 }
3476 mStackSupervisor.resumeTopActivitiesLocked();
3477 }
3478
Jose Lima4b6c6692014-08-12 17:41:12 -07003479 boolean hasVisibleBehindActivity() {
3480 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003481 }
3482
Jose Lima4b6c6692014-08-12 17:41:12 -07003483 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003484 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003485 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003486 }
3487 }
3488
Jose Lima4b6c6692014-08-12 17:41:12 -07003489 ActivityRecord getVisibleBehindActivity() {
3490 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003491 }
3492
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003493 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3494 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003496 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3497 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498 while (i > 0) {
3499 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003500 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003501 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003503 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003504 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003505 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003506 }
3507 }
3508 }
3509
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003510 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3511 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003512 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3513 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003514 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3515 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003516 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003517 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003518 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3519 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003520
3521 boolean hasVisibleActivities = false;
3522
3523 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003524 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003525 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3526 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003527 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3528 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3529 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3530 final ActivityRecord r = activities.get(activityNdx);
3531 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003532 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3533 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003534 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003535 if (r.visible) {
3536 hasVisibleActivities = true;
3537 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003538 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003539 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3540 // Don't currently have state for the activity, or
3541 // it is finishing -- always remove it.
3542 remove = true;
3543 } else if (r.launchCount > 2 &&
3544 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3545 // We have launched this activity too many times since it was
3546 // able to run, so give up and remove it.
3547 remove = true;
3548 } else {
3549 // The process may be gone, but the activity lives on!
3550 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003551 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003552 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003553 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003554 RuntimeException here = new RuntimeException("here");
3555 here.fillInStackTrace();
3556 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3557 + ": haveState=" + r.haveState
3558 + " stateNotNeeded=" + r.stateNotNeeded
3559 + " finishing=" + r.finishing
3560 + " state=" + r.state, here);
3561 }
3562 if (!r.finishing) {
3563 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3564 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3565 r.userId, System.identityHashCode(r),
3566 r.task.taskId, r.shortComponentName,
3567 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003568 if (r.state == ActivityState.RESUMED) {
3569 mService.updateUsageStats(r, false);
3570 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003571 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003572 } else {
3573 // We have the current state for this activity, so
3574 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003575 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003576 TAG, "Keeping entry, setting app to null");
Craig Mautner0247fc82013-02-28 14:32:06 -08003577 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3578 + r);
3579 r.app = null;
3580 r.nowVisible = false;
3581 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003582 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003583 "App died, clearing saved state of " + r);
3584 r.icicle = null;
3585 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003586 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003587 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003588 if (remove) {
3589 removeActivityFromHistoryLocked(r, "appDied");
3590 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003591 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003592 }
3593 }
3594
3595 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003596 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003597
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003598 final void updateTransitLocked(int transit, Bundle options) {
3599 if (options != null) {
3600 ActivityRecord r = topRunningActivityLocked(null);
3601 if (r != null && r.state != ActivityState.RESUMED) {
3602 r.updateOptionsLocked(options);
3603 } else {
3604 ActivityOptions.abort(options);
3605 }
3606 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003607 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003608 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003609
Craig Mautner21d24a22014-04-23 11:45:37 -07003610 void updateTaskMovement(TaskRecord task, boolean toFront) {
3611 if (task.isPersistable) {
3612 task.mLastTimeMoved = System.currentTimeMillis();
3613 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3614 // recently will be most negative, tasks sent to the bottom before that will be less
3615 // negative. Similarly for recent tasks moved to the top which will be most positive.
3616 if (!toFront) {
3617 task.mLastTimeMoved *= -1;
3618 }
3619 }
3620 }
3621
Craig Mautner84984fa2014-06-19 11:19:20 -07003622 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003623 final int top = mTaskHistory.size() - 1;
3624 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3625 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003626 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003627 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3628 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003629 mTaskHistory.remove(taskNdx);
3630 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003631 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003632 return;
3633 }
3634 }
3635 }
3636
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003637 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003638 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003639 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640
Craig Mautner11bf9a52013-02-19 14:08:51 -08003641 final int numTasks = mTaskHistory.size();
3642 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003643 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003644 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003645 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003646 ActivityOptions.abort(options);
3647 } else {
3648 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3649 }
3650 return;
3651 }
3652
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003653 if (timeTracker != null) {
3654 // The caller wants a time tracker associated with this task.
3655 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3656 tr.mActivities.get(i).appTimeTracker = timeTracker;
3657 }
3658 }
3659
Craig Mautner11bf9a52013-02-19 14:08:51 -08003660 // Shift all activities with this task up to the top
3661 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003662 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003663
3664 // Set focus to the top running activity of this stack.
3665 ActivityRecord r = topRunningActivityLocked(null);
3666 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003667
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003668 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003669 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003670 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003671 if (r != null) {
3672 mNoAnimActivities.add(r);
3673 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003674 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003675 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003676 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003677 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003678
Craig Mautner05d29032013-05-03 13:40:13 -07003679 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003680 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003681
3682 if (VALIDATE_TOKENS) {
3683 validateAppTokensLocked();
3684 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003685 }
3686
3687 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003688 * Worker method for rearranging history stack. Implements the function of moving all
3689 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003690 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003691 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3693 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003694 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003695 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003696 * @return Returns true if the move completed, false if not.
3697 */
Craig Mautner299f9602015-01-26 09:47:33 -08003698 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003699 final TaskRecord tr = taskForIdLocked(taskId);
3700 if (tr == null) {
3701 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3702 return false;
3703 }
3704
3705 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003706 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003707
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003708 // If we have a watcher, preflight the move before committing to it. First check
3709 // for *other* available tasks, but if none are available, then try again allowing the
3710 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003711 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003712 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 if (next == null) {
3714 next = topRunningActivityLocked(null, 0);
3715 }
3716 if (next != null) {
3717 // ask watcher if this is allowed
3718 boolean moveOK = true;
3719 try {
3720 moveOK = mService.mController.activityResuming(next.packageName);
3721 } catch (RemoteException e) {
3722 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003723 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003724 }
3725 if (!moveOK) {
3726 return false;
3727 }
3728 }
3729 }
3730
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003731 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003732
riddle_hsuc215a4f2014-12-27 12:10:45 +08003733 boolean prevIsHome = false;
3734 if (tr.isOverHomeStack()) {
3735 final TaskRecord nextTask = getNextTask(tr);
3736 if (nextTask != null) {
3737 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3738 } else {
3739 prevIsHome = true;
3740 }
3741 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003742 mTaskHistory.remove(tr);
3743 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003744 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745
Craig Mautnerc8143c62013-09-03 12:15:57 -07003746 // There is an assumption that moving a task to the back moves it behind the home activity.
3747 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003748 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003749 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3750 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003751 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003752 break;
3753 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003754 if (taskNdx == 1) {
3755 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003756 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003757 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003758 }
3759
Craig Mautner299f9602015-01-26 09:47:33 -08003760 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003761 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003762
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003763 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003764 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003765 }
3766
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003767 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003768 if (prevIsHome || task == tr && tr.isOverHomeStack()
3769 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003770 if (!mService.mBooting && !mService.mBooted) {
3771 // Not ready yet!
3772 return false;
3773 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003774 final int taskToReturnTo = tr.getTaskToReturnTo();
3775 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003776 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003777 }
3778
Craig Mautner05d29032013-05-03 13:40:13 -07003779 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003780 return true;
3781 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003782
Craig Mautner8849a5e2013-04-02 16:41:03 -07003783 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003785 final Uri data = r.intent.getData();
3786 final String strData = data != null ? data.toSafeString() : null;
3787
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003788 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003789 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003790 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003791 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003792 }
3793
3794 /**
3795 * Make sure the given activity matches the current configuration. Returns
3796 * false if the activity had to be destroyed. Returns true if the
3797 * configuration is the same, or the activity will remain running as-is
3798 * for whatever reason. Ensures the HistoryRecord is updated with the
3799 * correct configuration and all other bookkeeping is handled.
3800 */
3801 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3802 int globalChanges) {
3803 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003804 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003805 "Skipping config check (will change): " + r);
3806 return true;
3807 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003808
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003809 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003810 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003811
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003812 // Make sure the current stack override configuration is supported by the top task
3813 // before continuing.
3814 final TaskRecord topTask = topTask();
3815 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3816 || (!topTask.mResizeable && !mFullscreen))) {
3817 final boolean prevFullscreen = mFullscreen;
3818 final Configuration newOverrideConfig =
3819 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3820 updateOverrideConfiguration(newOverrideConfig);
3821 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003822 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003823 "Updated stack config to support task=" + topTask
3824 + " resizeable=" + topTask.mResizeable
3825 + " mForcedFullscreen=" + mForcedFullscreen
3826 + " prevFullscreen=" + prevFullscreen
3827 + " mFullscreen=" + mFullscreen);
3828 }
3829
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.
3832 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003833 if (r.configuration == newConfig
3834 && r.stackConfigOverride == mOverrideConfig
3835 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003836 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003837 "Configuration unchanged in " + r);
3838 return true;
3839 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003840
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003841 // We don't worry about activities that are finishing.
3842 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003843 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003844 "Configuration doesn't matter in finishing " + r);
3845 r.stopFreezingScreenLocked(false);
3846 return true;
3847 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003848
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003849 // Okay we now are going to make this activity have the new config.
3850 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003851 final Configuration oldConfig = r.configuration;
3852 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003853 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003854 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003855
3856 // Determine what has changed. May be nothing, if this is a config
3857 // that has come back from the app after going idle. In that case
3858 // we just want to leave the official config object now in the
3859 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003860 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003861 if (stackChanges == 0) {
3862 // {@link Configuration#diff} doesn't catch changes from unset values.
3863 // Check for changes we care about.
3864 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3865 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3866 }
3867 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3868 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3869 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3870 }
3871 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3872 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3873 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003874 }
3875 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003876 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003877 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003878 "Configuration no differences in " + r);
3879 return true;
3880 }
3881
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003882 // If the activity isn't currently running, just leave the new
3883 // configuration and it will pick that up next time it starts.
3884 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003885 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003886 "Configuration doesn't matter not running " + r);
3887 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003888 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003889 return true;
3890 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003891
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003892 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003893 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3894 "Checking to restart " + r.info.name + ": changed=0x"
3895 + Integer.toHexString(changes) + ", handles=0x"
3896 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3897
Dianne Hackborne6676352011-06-01 16:51:20 -07003898 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003899 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3900 r.configChangeFlags |= changes;
3901 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003902 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003903 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003904 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003905 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003906 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003907 } else if (r.state == ActivityState.PAUSING) {
3908 // A little annoying: we are waiting for this activity to
3909 // finish pausing. Let's not do anything now, but just
3910 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003911 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003912 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003913 r.configDestroy = true;
3914 return true;
3915 } else if (r.state == ActivityState.RESUMED) {
3916 // Try to optimize this case: the configuration is changing
3917 // and we need to restart the top, resumed activity.
3918 // Instead of doing the normal handshaking, just say
3919 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003920 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003921 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003922 relaunchActivityLocked(r, r.configChangeFlags, true);
3923 r.configChangeFlags = 0;
3924 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003925 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003926 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003927 relaunchActivityLocked(r, r.configChangeFlags, false);
3928 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003929 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003930
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003931 // All done... tell the caller we weren't able to keep this
3932 // activity around.
3933 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003934 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003935
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003936 // Default case: the activity can handle this new configuration, so hand it over.
3937 // NOTE: We only forward the stack override configuration as the system level configuration
3938 // changes is always sent to all processes when they happen so it can just use whatever
3939 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003940 if (r.app != null && r.app.thread != null) {
3941 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003942 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003943 r.app.thread.scheduleActivityConfigurationChanged(
3944 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945 } catch (RemoteException e) {
3946 // If process died, whatever.
3947 }
3948 }
3949 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003950
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003951 return true;
3952 }
3953
Craig Mautner2568c3a2015-03-26 14:22:34 -07003954 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003955 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003956 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003957 if (andResume) {
3958 results = r.results;
3959 newIntents = r.newIntents;
3960 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003961 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3962 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003963 + " andResume=" + andResume);
3964 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003965 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003966 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003967
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003968 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003969
Craig Mautner34b73df2014-01-12 21:11:08 -08003970 mStackSupervisor.removeChildActivityContainers(r);
3971
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003972 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003973 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
3974 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003975 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003976 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3977 !andResume, new Configuration(mService.mConfiguration),
3978 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 // Note: don't need to call pauseIfSleepingLocked() here, because
3980 // the caller will only pass in 'andResume' if this activity is
3981 // currently resumed, which implies we aren't sleeping.
3982 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003983 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003984 }
3985
3986 if (andResume) {
3987 r.results = null;
3988 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003989 r.state = ActivityState.RESUMED;
3990 } else {
3991 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3992 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003993 }
3994
3995 return true;
3996 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003997
3998 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003999 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4000 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4001 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4002 final ActivityRecord r = activities.get(activityNdx);
4003 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004004 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004005 }
4006 if (r.fullscreen && !r.finishing) {
4007 return false;
4008 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004009 }
4010 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004011 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004012 if (r == null) {
4013 return false;
4014 }
4015 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4016 + " would have returned true for r=" + r);
4017 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004018 }
4019
4020 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004021 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4022 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4023 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4024 final ActivityRecord r = activities.get(activityNdx);
4025 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004026 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004027 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004028 }
4029 }
4030 }
4031
Wale Ogunwale540e1232015-05-01 15:35:39 -07004032 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4033 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004034 boolean didSomething = false;
4035 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004036 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004037 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4038 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4039 int numActivities = activities.size();
4040 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4041 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004042 final boolean sameComponent =
4043 (r.packageName.equals(packageName) && (filterByClasses == null
4044 || filterByClasses.contains(r.realActivity.getClassName())))
4045 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004046 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004047 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004048 && (r.app == null || evenPersistent || !r.app.persistent)) {
4049 if (!doit) {
4050 if (r.finishing) {
4051 // If this activity is just finishing, then it is not
4052 // interesting as far as something to stop.
4053 continue;
4054 }
4055 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004056 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004057 if (r.isHomeActivity()) {
4058 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4059 Slog.i(TAG, "Skip force-stop again " + r);
4060 continue;
4061 } else {
4062 homeActivity = r.realActivity;
4063 }
4064 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004065 didSomething = true;
4066 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004067 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004068 if (r.app != null) {
4069 r.app.removed = true;
4070 }
4071 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004072 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004073 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004074 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4075 true)) {
4076 // r has been deleted from mActivities, accommodate.
4077 --numActivities;
4078 --activityNdx;
4079 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004080 }
4081 }
4082 }
4083 return didSomething;
4084 }
4085
Dianne Hackborn09233282014-04-30 11:33:59 -07004086 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004087 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4088 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004089 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004090 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004091 if (task.getTopActivity() == null) {
4092 continue;
4093 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004094 ActivityRecord r = null;
4095 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004096 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004097 int numActivities = 0;
4098 int numRunning = 0;
4099 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004100 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004101 continue;
4102 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004103 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004104 tmp = activities.get(activityNdx);
4105 if (tmp.finishing) {
4106 continue;
4107 }
4108 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004109
Craig Mautneraab647e2013-02-28 16:31:36 -08004110 // Initialize state for next task if needed.
4111 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4112 top = r;
4113 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004114 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004115
4116 // Add 'r' into the current task.
4117 numActivities++;
4118 if (r.app != null && r.app.thread != null) {
4119 numRunning++;
4120 }
4121
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004122 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004123 TAG, r.intent.getComponent().flattenToShortString()
4124 + ": task=" + r.task);
4125 }
4126
4127 RunningTaskInfo ci = new RunningTaskInfo();
4128 ci.id = task.taskId;
4129 ci.baseActivity = r.intent.getComponent();
4130 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004131 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004132 if (focusedStack && topTask) {
4133 // Give the latest time to ensure foreground task can be sorted
4134 // at the first, because lastActiveTime of creating task is 0.
4135 ci.lastActiveTime = System.currentTimeMillis();
4136 topTask = false;
4137 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004138
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004139 if (top.task != null) {
4140 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004141 }
4142 ci.numActivities = numActivities;
4143 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004144 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004145 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004146 }
4147
4148 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004149 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004150 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004151 if (top >= 0) {
4152 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4153 int activityTop = activities.size() - 1;
4154 if (activityTop > 0) {
4155 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4156 "unhandled-back", true);
4157 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004158 }
4159 }
4160
Craig Mautner6b74cb52013-09-27 17:02:21 -07004161 /**
4162 * Reset local parameters because an app's activity died.
4163 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004164 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004165 */
4166 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004167 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004168 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004169 "App died while pausing: " + mPausingActivity);
4170 mPausingActivity = null;
4171 }
4172 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4173 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004174 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004175 }
4176
Craig Mautner19091252013-10-05 00:03:53 -07004177 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004178 }
4179
Craig Mautnercae015f2013-02-08 14:31:27 -08004180 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004181 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4182 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4183 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4184 final ActivityRecord r = activities.get(activityNdx);
4185 if (r.app == app) {
4186 Slog.w(TAG, " Force finishing activity "
4187 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004188 // Force the destroy to skip right to removal.
4189 r.app = null;
4190 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004191 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004192 }
4193 }
4194 }
4195
Dianne Hackborn390517b2013-05-30 15:03:32 -07004196 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004197 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004198 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004199 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4200 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004201 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4202 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004203 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004204 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004205 if (printed) {
4206 header = null;
4207 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004208 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004209 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004210 }
4211
4212 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4213 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4214
4215 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004216 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4217 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004218 }
4219 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004220 final int top = mTaskHistory.size() - 1;
4221 if (top >= 0) {
4222 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4223 int listTop = list.size() - 1;
4224 if (listTop >= 0) {
4225 activities.add(list.get(listTop));
4226 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004227 }
4228 } else {
4229 ItemMatcher matcher = new ItemMatcher();
4230 matcher.build(name);
4231
Craig Mautneraab647e2013-02-28 16:31:36 -08004232 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4233 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4234 if (matcher.match(r1, r1.intent.getComponent())) {
4235 activities.add(r1);
4236 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004237 }
4238 }
4239 }
4240
4241 return activities;
4242 }
4243
4244 ActivityRecord restartPackage(String packageName) {
4245 ActivityRecord starting = topRunningActivityLocked(null);
4246
4247 // All activities that came from the package must be
4248 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004249 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4250 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4251 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4252 final ActivityRecord a = activities.get(activityNdx);
4253 if (a.info.packageName.equals(packageName)) {
4254 a.forceNewConfig = true;
4255 if (starting != null && a == starting && a.visible) {
4256 a.startFreezingScreenLocked(starting.app,
4257 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4258 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004259 }
4260 }
4261 }
4262
4263 return starting;
4264 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004265
Craig Mautner299f9602015-01-26 09:47:33 -08004266 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07004267 removeTask(task, reason, true /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004268 }
4269
Wale Ogunwale000957c2015-04-03 08:19:12 -07004270 /**
4271 * Removes the input task from this stack.
4272 * @param task to remove.
4273 * @param reason for removal.
4274 * @param notMoving task to another stack. In the case we are moving we don't want to perform
4275 * some operations on the task like removing it from window manager or recents.
4276 */
4277 void removeTask(TaskRecord task, String reason, boolean notMoving) {
4278 if (notMoving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004279 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004280 mWindowManager.removeTask(task.taskId);
4281 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004282
Craig Mautner04a0ea62014-01-13 12:51:26 -08004283 final ActivityRecord r = mResumedActivity;
4284 if (r != null && r.task == task) {
4285 mResumedActivity = null;
4286 }
4287
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004288 final int taskNdx = mTaskHistory.indexOf(task);
4289 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004290 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4291 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4292 if (!nextTask.isOverHomeStack()) {
4293 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4294 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004295 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004296 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004297 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004298
Wale Ogunwale000957c2015-04-03 08:19:12 -07004299 if (notMoving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004300 final boolean isVoiceSession = task.voiceSession != null;
4301 if (isVoiceSession) {
4302 try {
4303 task.voiceSession.taskFinished(task.intent, task.taskId);
4304 } catch (RemoteException e) {
4305 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004306 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004307 if (task.autoRemoveFromRecents() || isVoiceSession) {
4308 // Task creator asked to remove this when done, or this task was a voice
4309 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004310 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004311 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004312 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004313 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004314
4315 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004316 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004317 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004318 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004319 String myReason = reason + " leftTaskHistoryEmpty";
4320 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4321 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4322 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004323 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004324 if (mStacks != null) {
4325 mStacks.remove(this);
4326 mStacks.add(0, this);
4327 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004328 if (notHomeStack) {
4329 mActivityContainer.onTaskListEmptyLocked();
4330 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004331 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004332
4333 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004334 }
4335
Dianne Hackborn91097de2014-04-04 18:02:06 -07004336 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4337 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4338 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004339 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4340 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004341 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004342 return task;
4343 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004344
4345 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004346 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004347 }
4348
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004349 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004350 task.stack = this;
4351 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004352 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004353 } else {
4354 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004355 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004356 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004357 if (!moving && task.voiceSession != null) {
4358 try {
4359 task.voiceSession.taskStarted(task.intent, task.taskId);
4360 } catch (RemoteException e) {
4361 }
4362 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004363 }
4364
4365 public int getStackId() {
4366 return mStackId;
4367 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004368
4369 @Override
4370 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004371 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4372 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004373 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004374
4375 boolean updateOverrideConfiguration(Configuration newConfig) {
4376 Configuration oldConfig = mOverrideConfig;
4377 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004378 // We override the configuration only when the stack's dimensions are different from
4379 // the display. In this manner, we know that if the override configuration is empty,
4380 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004381 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004382 return !mOverrideConfig.equals(oldConfig);
4383 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004384
4385 void onLockTaskPackagesUpdatedLocked() {
4386 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4387 mTaskHistory.get(taskNdx).setLockTaskAuth();
4388 }
4389 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004390}