blob: dd172fe86c09b11efadd87248421e78f1204aae7 [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 Ogunwaleb34a7ad2015-08-14 11:05:30 -070019import static android.app.ActivityManager.DOCKED_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070020import static android.app.ActivityManager.FIRST_STATIC_STACK_ID;
Wale Ogunwale61803ee2015-08-07 19:59:47 -070021import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070022import static android.app.ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -070023import static android.app.ActivityManager.HOME_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070024import static android.app.ActivityManager.LAST_STATIC_STACK_ID;
Wale Ogunwale99db1862015-10-23 20:08:22 -070025import static android.app.ActivityManager.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070026import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070027
Wale Ogunwalee23149f2015-03-06 15:39:44 -080028import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070029
Craig Mautner84984fa2014-06-19 11:19:20 -070030import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070032
Wale Ogunwale040b4702015-08-06 18:10:50 -070033import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070034import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070035
Wale Ogunwale706ed792015-08-02 10:29:44 -070036import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070037import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070038
Dianne Hackborn91097de2014-04-04 18:02:06 -070039import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080040import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070042import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080043import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080044import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080045import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080046import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048
49import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070050import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070075import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080086import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070087import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
89/**
90 * State and management of a single stack of activities.
91 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070092final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Wale Ogunwalee23149f2015-03-06 15:39:44 -080094 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070095 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
96 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080097 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070098 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070099 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700100 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700101 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700102 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700103 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
104 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700105 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700106 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700107 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
108 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
109 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
110 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
111 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700112
113 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800114
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700115 // Ticks during which we check progress while waiting for an app to launch.
116 static final int LAUNCH_TICK = 500;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we wait until giving up on the last activity to pause. This
119 // is short because it directly impacts the responsiveness of starting the
120 // next activity.
121 static final int PAUSE_TIMEOUT = 500;
122
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700123 // How long we wait for the activity to tell us it has stopped before
124 // giving up. This is a good amount of time because we really need this
125 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700126 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long we wait until giving up on an activity telling us it has
129 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700130 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800133 // disabled.
134 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800135
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700136 // How long between activity launches that we consider safe to not warn
137 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700138 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 // Set to false to disable the preview that is shown while a new activity
141 // is being started.
142 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Craig Mautner5eda9b32013-07-02 11:58:16 -0700144 // How long to wait for all background Activities to redraw following a call to
145 // convertToTranslucent().
146 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 enum ActivityState {
149 INITIALIZING,
150 RESUMED,
151 PAUSING,
152 PAUSED,
153 STOPPING,
154 STOPPED,
155 FINISHING,
156 DESTROYING,
157 DESTROYED
158 }
159
160 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700161 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800162 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 /**
165 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800166 * running) activities. It contains #TaskRecord objects.
167 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800168 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169
170 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800171 * Used for validating app tokens with window manager.
172 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800173 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 /**
176 * List of running activities, sorted by recent usage.
177 * The first entry in the list is the least recently used.
178 * It contains HistoryRecord objects.
179 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800180 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181
182 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183 * Animations that for the current transition have requested not to
184 * be considered for the transition animation.
185 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800186 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * When we are in the process of pausing an activity, before starting the
190 * next one, this variable holds the activity that is currently being paused.
191 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800192 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193
194 /**
195 * This is the last activity that we put into the paused state. This is
196 * used to determine if we need to do an activity transition while sleeping,
197 * when we normally hold the top activity paused.
198 */
199 ActivityRecord mLastPausedActivity = null;
200
201 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700202 * Activities that specify No History must be removed once the user navigates away from them.
203 * If the device goes to sleep with such an activity in the paused state then we save it here
204 * and finish it later if another activity replaces it on wakeup.
205 */
206 ActivityRecord mLastNoHistoryActivity = null;
207
208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Current activity that is resumed, or null if there is none.
210 */
211 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700213 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 * This is the last activity that has been started. It is only used to
215 * identify when multiple activities are started at once so that the user
216 * can be warned they may not be in the activity they think they are.
217 */
218 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800219
Craig Mautner5eda9b32013-07-02 11:58:16 -0700220 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
221 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
222 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
223 // Activity in mTranslucentActivityWaiting is notified via
224 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
225 // background activity being drawn then the same call will be made with a true value.
226 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700228
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700229 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 * Set when we know we are going to be calling updateConfiguration()
231 * soon, so want to skip intermediate config checks.
232 */
233 boolean mConfigWillChange;
234
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700235 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800236 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700237 // Current bounds of the stack or null if fullscreen.
238 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800239
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700240 long mLaunchStartTime = 0;
241 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800242
Craig Mautner858d8a62013-04-23 17:08:34 -0700243 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700244
Craig Mautnerc00204b2013-03-05 15:02:14 -0800245 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800246 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800247 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
248 ArrayList<ActivityStack> mStacks;
249 /** The attached Display's unique identifier, or -1 if detached */
250 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800251
Craig Mautner27084302013-03-25 08:05:25 -0700252 /** Run all ActivityStacks through this */
253 final ActivityStackSupervisor mStackSupervisor;
254
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700255 private final LaunchingTaskPositioner mTaskPositioner;
256
Wale Ogunwale99db1862015-10-23 20:08:22 -0700257 // If the bounds of task contained in this stack should be persisted across power cycles.
258 final boolean mPersistTaskBounds;
259
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700260 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700261 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
262 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
263 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
264 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700265 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700266 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700267 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700268
269 static class ScheduleDestroyArgs {
270 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700271 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700272 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700273 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700274 mReason = reason;
275 }
276 }
277
Zoran Marcetaf958b322012-08-09 20:27:12 +0900278 final Handler mHandler;
279
280 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800281
Craig Mautnerc8143c62013-09-03 12:15:57 -0700282 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900283 super(looper);
284 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700285
Zoran Marcetaf958b322012-08-09 20:27:12 +0900286 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 public void handleMessage(Message msg) {
288 switch (msg.what) {
289 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 // We don't at this point know if the activity is fullscreen,
292 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800293 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 synchronized (mService) {
295 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700296 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700297 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700298 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800299 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700301 case LAUNCH_TICK_MSG: {
302 ActivityRecord r = (ActivityRecord)msg.obj;
303 synchronized (mService) {
304 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700305 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700306 }
307 }
308 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700311 // We don't at this point know if the activity is fullscreen,
312 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800313 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800314 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800315 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700318 case STOP_TIMEOUT_MSG: {
319 ActivityRecord r = (ActivityRecord)msg.obj;
320 // We don't at this point know if the activity is fullscreen,
321 // so we need to be conservative and assume it isn't.
322 Slog.w(TAG, "Activity stop timeout for " + r);
323 synchronized (mService) {
324 if (r.isInHistory()) {
325 activityStoppedLocked(r, null, null, null);
326 }
327 }
328 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700329 case DESTROY_ACTIVITIES_MSG: {
330 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
331 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700332 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700333 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700334 } break;
335 case TRANSLUCENT_TIMEOUT_MSG: {
336 synchronized (mService) {
337 notifyActivityDrawnLocked(null);
338 }
339 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700340 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700341 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700342 final ActivityRecord r = getVisibleBehindActivity();
343 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700344 if (r != null) {
345 mService.killAppAtUsersRequest(r.app, null);
346 }
347 }
348 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700349 }
350 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800351 }
352
Craig Mautner34b73df2014-01-12 21:11:08 -0800353 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800354 int count = 0;
355 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
356 count += mTaskHistory.get(taskNdx).mActivities.size();
357 }
358 return count;
359 }
360
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800361 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
362 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800363 mActivityContainer = activityContainer;
364 mStackSupervisor = activityContainer.getOuter();
365 mService = mStackSupervisor.mService;
366 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
367 mWindowManager = mService.mWindowManager;
368 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700369 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800370 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700371 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
372 ? new LaunchingTaskPositioner() : null;
Wale Ogunwale99db1862015-10-23 20:08:22 -0700373 mPersistTaskBounds = mStackId != DOCKED_STACK_ID && mStackId != PINNED_STACK_ID;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700374 }
375
376 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
377 mDisplayId = activityDisplay.mDisplayId;
378 mStacks = activityDisplay.mStacks;
379 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
380 mFullscreen = mBounds == null;
381 if (mTaskPositioner != null) {
382 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
383 mTaskPositioner.configure(mBounds);
384 }
385 }
386
387 void detachDisplay() {
388 mDisplayId = Display.INVALID_DISPLAY;
389 mStacks = null;
390 if (mTaskPositioner != null) {
391 mTaskPositioner.reset();
392 }
393 mWindowManager.detachStack(mStackId);
394 }
395
396 void setBounds(Rect bounds) {
397 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700398 if (mTaskPositioner != null) {
399 mTaskPositioner.configure(bounds);
400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 }
Craig Mautner5962b122012-10-05 14:45:52 -0700402
Amith Yamasani734983f2014-03-04 16:48:05 -0800403 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700404 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700405 }
406
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700407 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700409 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700410 if (r != null) {
411 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800412 }
413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 return null;
415 }
416
417 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800420 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800421 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
422 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800423 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 return r;
425 }
426 }
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 return null;
429 }
430
431 /**
432 * This is a simplified version of topRunningActivityLocked that provides a number of
433 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800434 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 * @param token If non-null, any history records matching this token will be skipped.
436 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800437 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 * @return Returns the HistoryRecord of the next activity on the stack.
439 */
440 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 TaskRecord task = mTaskHistory.get(taskNdx);
443 if (task.taskId == taskId) {
444 continue;
445 }
446 ArrayList<ActivityRecord> activities = task.mActivities;
447 for (int i = activities.size() - 1; i >= 0; --i) {
448 final ActivityRecord r = activities.get(i);
449 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
Craig Mautner8849a5e2013-04-02 16:41:03 -0700458 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
460 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700461 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
462 final ActivityRecord r = activities.get(activityNdx);
463 if (!r.finishing) {
464 return r;
465 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700466 }
467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700468 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700469 }
470
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700471 final TaskRecord topTask() {
472 final int size = mTaskHistory.size();
473 if (size > 0) {
474 return mTaskHistory.get(size - 1);
475 }
476 return null;
477 }
478
Craig Mautnerd2328952013-03-05 12:46:26 -0800479 TaskRecord taskForIdLocked(int id) {
480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
481 final TaskRecord task = mTaskHistory.get(taskNdx);
482 if (task.taskId == id) {
483 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800484 }
485 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700486 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700487 }
488
Craig Mautnerd2328952013-03-05 12:46:26 -0800489 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700490 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800491 return isInStackLocked(r);
492 }
493
494 ActivityRecord isInStackLocked(ActivityRecord r) {
495 if (r == null) {
496 return null;
497 }
498 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700499 if (task != null && task.stack != null
500 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800501 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800502 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800503 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800504 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800505 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800506 }
507
Craig Mautner2420ead2013-04-01 17:13:20 -0700508 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700509 final boolean hadit = mLRUActivities.remove(r);
510 mLRUActivities.add(r);
511 return hadit;
512 }
513
Craig Mautnerde4ef022013-04-07 19:01:33 -0700514 final boolean isHomeStack() {
515 return mStackId == HOME_STACK_ID;
516 }
517
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 final boolean isOnHomeDisplay() {
519 return isAttached() &&
520 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
521 }
522
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700523 void moveToFront(String reason) {
524 moveToFront(reason, null);
525 }
526
527 /**
528 * @param reason The reason for moving the stack to the front.
529 * @param task If non-null, the task will be moved to the top of the stack.
530 * */
531 void moveToFront(String reason, TaskRecord task) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800532 if (isAttached()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700533 final ActivityStack lastFocusStack = mStacks.get(mStacks.size() - 1);
534 // Need to move this stack to the front before calling
535 // {@link ActivityStackSupervisor#setFocusStack} below.
536 mStacks.remove(this);
537 mStacks.add(this);
538
539 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800540 if (isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700541 mStackSupervisor.setFocusStack(reason, lastFocusStack);
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800542 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700543 if (task != null) {
544 insertTaskAtTop(task, null);
545 } else {
546 task = topTask();
547 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800548 if (task != null) {
549 mWindowManager.moveTaskToTop(task.taskId);
550 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800552 }
553
554 final boolean isAttached() {
555 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 }
557
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700558 /**
559 * Returns the top activity in any existing task matching the given
560 * Intent. Returns null if no such task is found.
561 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700562 ActivityRecord findTaskLocked(ActivityRecord target) {
563 Intent intent = target.intent;
564 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700565 ComponentName cls = intent.getComponent();
566 if (info.targetActivity != null) {
567 cls = new ComponentName(info.packageName, info.targetActivity);
568 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700569 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700570 boolean isDocument = intent != null & intent.isDocument();
571 // If documentData is non-null then it must match the existing task data.
572 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800573
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800575 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
576 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700577 if (task.voiceSession != null) {
578 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700579 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700580 continue;
581 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700582 if (task.userId != userId) {
583 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700584 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700585 continue;
586 }
Craig Mautner000f0022013-02-26 15:04:29 -0800587 final ActivityRecord r = task.getTopActivity();
588 if (r == null || r.finishing || r.userId != userId ||
589 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800591 continue;
592 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700593 if (r.mActivityType != target.mActivityType) {
594 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
595 continue;
596 }
Craig Mautner000f0022013-02-26 15:04:29 -0800597
Craig Mautnerd00f4742014-03-12 14:17:26 -0700598 final Intent taskIntent = task.intent;
599 final Intent affinityIntent = task.affinityIntent;
600 final boolean taskIsDocument;
601 final Uri taskDocumentData;
602 if (taskIntent != null && taskIntent.isDocument()) {
603 taskIsDocument = true;
604 taskDocumentData = taskIntent.getData();
605 } else if (affinityIntent != null && affinityIntent.isDocument()) {
606 taskIsDocument = true;
607 taskDocumentData = affinityIntent.getData();
608 } else {
609 taskIsDocument = false;
610 taskDocumentData = null;
611 }
612
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700613 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700614 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700615 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700616 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700617 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
618 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700619 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 return r;
621 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700622 } else if (taskIntent != null && taskIntent.getComponent() != null &&
623 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700624 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700625 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800626 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700627 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
628 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800629 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700630 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
631 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700632 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700633 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800634 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700635 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
636 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800637 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700639 }
640
641 return null;
642 }
643
644 /**
645 * Returns the first activity (starting from the top of the stack) that
646 * is the same as the given activity. Returns null if no such activity
647 * is found.
648 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700649 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700650 ComponentName cls = intent.getComponent();
651 if (info.targetActivity != null) {
652 cls = new ComponentName(info.packageName, info.targetActivity);
653 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700654 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700655
Craig Mautner000f0022013-02-26 15:04:29 -0800656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700657 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700658 final boolean notCurrentUserTask =
659 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700660 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700661
Craig Mautner000f0022013-02-26 15:04:29 -0800662 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
663 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700664 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700665 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700666 }
Craig Mautner000f0022013-02-26 15:04:29 -0800667 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700668 return r;
669 }
670 }
671 }
672
673 return null;
674 }
675
Amith Yamasani742a6712011-05-04 14:49:28 -0700676 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700677 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700678 */
Craig Mautner93529a42013-10-04 15:03:13 -0700679 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800680 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700681 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800682 }
683 mCurrentUser = userId;
684
685 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800686 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800687 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700688 final TaskRecord task = mTaskHistory.get(i);
689
690 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
691 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700692 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700693 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700694 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700695 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800696 mTaskHistory.remove(i);
697 mTaskHistory.add(task);
698 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800699 // Use same value for i.
700 } else {
701 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800702 }
703 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700704 if (VALIDATE_TOKENS) {
705 validateAppTokensLocked();
706 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700707 }
708
Craig Mautner2420ead2013-04-01 17:13:20 -0700709 void minimalResumeActivityLocked(ActivityRecord r) {
710 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700711 if (DEBUG_STATES) Slog.v(TAG_STATES,
712 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700713 r.stopped = false;
714 mResumedActivity = r;
715 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800716 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700717 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700718 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700719 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700720 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
721 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722 }
723
Chong Zhang45c25ce2015-08-10 22:18:26 -0700724 private void addRecentActivityLocked(ActivityRecord r) {
725 if (r != null) {
726 mRecentTasks.addLocked(r.task);
727 r.task.touchActiveTime();
728 }
729 }
730
Narayan Kamath7829c812015-06-08 17:39:43 +0100731 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700732 if (mFullyDrawnStartTime != 0) {
733 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
734 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100735 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700736 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
737 }
738
739 private void stopFullyDrawnTraceIfNeeded() {
740 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
741 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
742 mFullyDrawnStartTime = 0;
743 }
744 }
745
Craig Mautnere79d42682013-04-01 19:01:53 -0700746 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700747 if (r.displayStartTime == 0) {
748 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
749 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100750 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700751 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700752 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700753 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100754 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700755 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700756 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700757 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800758
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700759 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700760 // Make sure that there is no activity waiting for this to launch.
761 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
762 r.displayStartTime = r.fullyDrawnStartTime = 0;
763 } else {
764 mStackSupervisor.removeTimeoutsForActivityLocked(r);
765 mStackSupervisor.scheduleIdleTimeoutLocked(r);
766 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700767 }
768
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800769 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800770 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800771 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
772 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
773 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
774 activities.get(activityNdx).setSleeping(false);
775 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800776 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800777 if (mPausingActivity != null) {
778 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
779 activityPausedLocked(mPausingActivity.appToken, true);
780 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800781 }
782
Craig Mautner0eea92c2013-05-16 13:35:39 -0700783 /**
784 * @return true if something must be done before going to sleep.
785 */
786 boolean checkReadyForSleepLocked() {
787 if (mResumedActivity != null) {
788 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700789 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
790 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
791 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700792 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700793 return true;
794 }
795 if (mPausingActivity != null) {
796 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700797 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700798 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800799 }
800
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700801 if (hasVisibleBehindActivity()) {
802 // Stop visible behind activity before going to sleep.
803 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
804 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700805 if (DEBUG_STATES) Slog.v(TAG_STATES,
806 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700807 return true;
808 }
809
Craig Mautner0eea92c2013-05-16 13:35:39 -0700810 return false;
811 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800812
Craig Mautner0eea92c2013-05-16 13:35:39 -0700813 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700814 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800815
Craig Mautner0eea92c2013-05-16 13:35:39 -0700816 // Make sure any stopped but visible activities are now sleeping.
817 // This ensures that the activity's onStop() is called.
818 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
819 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
820 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
821 final ActivityRecord r = activities.get(activityNdx);
822 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
823 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800824 }
825 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800826 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 }
Craig Mautner59c00972012-07-30 12:10:24 -0700828
Dianne Hackbornd2835932010-12-13 16:28:46 -0800829 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700830 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800831 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700832 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800833 return null;
834 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800835
Winson Chung083baf92014-07-11 10:32:42 -0700836 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700837 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800838 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700839 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700840 return null;
841 }
842
Winson Chung48a10a52014-08-27 14:36:51 -0700843 int w = mService.mThumbnailWidth;
844 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800845 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700846 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800847 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700848 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800849 }
Winson Chung376543b2014-05-21 17:43:28 -0700850 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800851 return null;
852 }
853
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700854 /**
855 * Start pausing the currently resumed activity. It is an error to call this if there
856 * is already an activity being paused or there is no resumed activity.
857 *
858 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
859 * @param uiSleeping True if this is happening with the user interface going to sleep (the
860 * screen turning off).
861 * @param resuming True if this is being called as part of resuming the top activity, so
862 * we shouldn't try to instigate a resume here.
863 * @param dontWait True if the caller does not want to wait for the pause to complete. If
864 * set to true, we will immediately complete the pause here before returning.
865 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
866 * it to tell us when it is done.
867 */
868 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
869 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800870 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800871 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
872 + " state=" + mPausingActivity.state);
873 if (!mService.isSleeping()) {
874 // Avoid recursion among check for sleep and complete pause during sleeping.
875 // Because activity will be paused immediately after resume, just let pause
876 // be completed by the order of activity paused from clients.
877 completePauseLocked(false);
878 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 ActivityRecord prev = mResumedActivity;
881 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700882 if (!resuming) {
883 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
884 mStackSupervisor.resumeTopActivitiesLocked();
885 }
886 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800888
889 if (mActivityContainer.mParentActivity == null) {
890 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700891 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800892 }
893
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700894 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700895 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700896 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800897 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700899 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
900 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700901 prev.state = ActivityState.PAUSING;
902 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700903 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700904 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700905 if (mService.mHasRecents
906 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800907 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700908 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700909 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700910
911 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800912
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700914 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915 try {
916 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700917 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700919 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800920 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700922 } catch (Exception e) {
923 // Ignore exception, if process died other code will cleanup.
924 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800925 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700927 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928 }
929 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800930 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700932 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 }
934
935 // If we are not going to sleep, we want to ensure the device is
936 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700937 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700938 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700939 }
940
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800941 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700942 // Have the window manager pause its key dispatching until the new
943 // activity has started. If we're pausing the activity just because
944 // the screen is being turned off and the UI is sleeping, don't interrupt
945 // key dispatch; the same activity will pick it up again on wakeup.
946 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800947 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700948 } else if (DEBUG_PAUSE) {
949 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950 }
951
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700952 if (dontWait) {
953 // If the caller said they don't want to wait for the pause, then complete
954 // the pause now.
955 completePauseLocked(false);
956 return false;
957
958 } else {
959 // Schedule a pause timeout in case the app doesn't respond.
960 // We don't give it much time because this directly impacts the
961 // responsiveness seen by the user.
962 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
963 msg.obj = prev;
964 prev.pauseTime = SystemClock.uptimeMillis();
965 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700966 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700967 return true;
968 }
969
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700970 } else {
971 // This activity failed to schedule the
972 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700974 if (!resuming) {
975 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
976 }
977 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700978 }
979 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700980
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700981 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700982 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
983 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700984
Craig Mautnerd2328952013-03-05 12:46:26 -0800985 final ActivityRecord r = isInStackLocked(token);
986 if (r != null) {
987 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
988 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700989 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -0800990 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700991 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800992 } else {
993 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
994 r.userId, System.identityHashCode(r), r.shortComponentName,
995 mPausingActivity != null
996 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +0800997 if (r.state == ActivityState.PAUSING) {
998 r.state = ActivityState.PAUSED;
999 if (r.finishing) {
1000 if (DEBUG_PAUSE) Slog.v(TAG,
1001 "Executing finish of failed to pause activity: " + r);
1002 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1003 }
louis_chang047dfd42015-04-08 16:35:55 +08001004 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001005 }
1006 }
1007 }
1008
Craig Mautnera0026042014-04-23 11:45:37 -07001009 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1010 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001011 if (r.state != ActivityState.STOPPING) {
1012 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1013 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1014 return;
1015 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001016 if (persistentState != null) {
1017 r.persistentState = persistentState;
1018 mService.notifyTaskPersisterLocked(r.task, false);
1019 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001020 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001021 if (icicle != null) {
1022 // If icicle is null, this is happening due to a timeout, so we
1023 // haven't really saved the state.
1024 r.icicle = icicle;
1025 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001026 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001027 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001028 }
1029 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001030 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001031 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1032 r.stopped = true;
1033 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001034 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1035 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001036 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001037 if (r.finishing) {
1038 r.clearOptionsLocked();
1039 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001040 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001041 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001042 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001043 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001044 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001045 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001046 }
1047 }
1048 }
1049
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001050 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001051 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001052 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001053
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001054 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001055 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001056 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001057 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001058 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001059 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001060 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001061 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001062 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1063 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001064 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001065 if (prev.configDestroy) {
1066 // The previous is being paused because the configuration
1067 // is changing, which means it is actually stopping...
1068 // To juggle the fact that we are also starting a new
1069 // instance right now, we need to first completely stop
1070 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001071 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001072 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001073 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001074 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001075 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001076 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001077 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1078 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001079 // If we already have a few activities waiting to stop,
1080 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001081 // them out. Or if r is the last of activity of the last task the stack
1082 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001083 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001084 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001085 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001086 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001087 }
1088 }
1089 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001090 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001091 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001092 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001093 // It is possible the activity was freezing the screen before it was paused.
1094 // In that case go ahead and remove the freeze this activity has on the screen
1095 // since it is no longer visible.
1096 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001097 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001098 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001099
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001100 if (resumeNext) {
1101 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1102 if (!mService.isSleepingOrShuttingDown()) {
1103 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1104 } else {
1105 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001106 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001107 if (top == null || (prev != null && top != prev)) {
1108 // If there are no more activities available to run,
1109 // do resume anyway to start something. Also if the top
1110 // activity on the stack is not the just paused activity,
1111 // we need to go ahead and resume it to ensure we complete
1112 // an in-flight app switch.
1113 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1114 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001115 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001117
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001118 if (prev != null) {
1119 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001120
Craig Mautner525f3d92013-05-07 14:01:50 -07001121 if (prev.app != null && prev.cpuTimeAtResume > 0
1122 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001123 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1124 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001125 if (diff > 0) {
1126 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1127 synchronized (bsi) {
1128 BatteryStatsImpl.Uid.Proc ps =
1129 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001130 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001131 if (ps != null) {
1132 ps.addForegroundTimeLocked(diff);
1133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
1135 }
1136 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001137 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001139
1140 // Notfiy when the task stack has changed
1141 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001142 }
1143
1144 /**
1145 * Once we know that we have asked an application to put an activity in
1146 * the resumed state (either by launching it or explicitly telling it),
1147 * this function updates the rest of our state to match that fact.
1148 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001149 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001150 next.idle = false;
1151 next.results = null;
1152 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001153
Chong Zhang85ee6542015-10-02 13:36:38 -07001154 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001155 ProcessRecord app = next.task.mActivities.get(0).app;
1156 if (app != null && app != mService.mHomeProcess) {
1157 mService.mHomeProcess = app;
1158 }
1159 }
1160
Craig Mautner07566322013-09-26 16:42:55 -07001161 if (next.nowVisible) {
1162 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001163 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001164 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001165
1166 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001167 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001168
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001169 mStackSupervisor.reportResumedActivityLocked(next);
1170
1171 next.resumeKeyDispatchingLocked();
1172 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173
1174 // Mark the point when the activity is resuming
1175 // TODO: To be more accurate, the mark should be before the onCreate,
1176 // not after the onResume. But for subsequent starts, onResume is fine.
1177 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001178 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001179 } else {
1180 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1181 }
Winson Chung376543b2014-05-21 17:43:28 -07001182
George Mount6ba042b2014-07-28 11:12:28 -07001183 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001184
1185 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1186 // When resuming an activity, require it to call requestVisibleBehind() again.
1187 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1188 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001189 }
1190
Craig Mautner2568c3a2015-03-26 14:22:34 -07001191 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001192 r.visible = visible;
1193 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001194 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001195 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001196 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001197 container.setVisible(visible);
1198 }
1199 }
1200
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001201 // Find the first visible activity above the passed activity and if it is translucent return it
1202 // otherwise return null;
1203 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1204 TaskRecord task = r.task;
1205 if (task == null) {
1206 return null;
1207 }
1208
1209 ActivityStack stack = task.stack;
1210 if (stack == null) {
1211 return null;
1212 }
1213
1214 int stackNdx = mStacks.indexOf(stack);
1215
1216 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1217 int taskNdx = tasks.indexOf(task);
1218
1219 ArrayList<ActivityRecord> activities = task.mActivities;
1220 int activityNdx = activities.indexOf(r) + 1;
1221
1222 final int numStacks = mStacks.size();
1223 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001224 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001225 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001226 final int numTasks = tasks.size();
1227 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001228 final TaskRecord currentTask = tasks.get(taskNdx);
1229 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001230 final int numActivities = activities.size();
1231 while (activityNdx < numActivities) {
1232 final ActivityRecord activity = activities.get(activityNdx);
1233 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001234 return historyStack.mFullscreen
1235 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001236 }
1237 ++activityNdx;
1238 }
1239 activityNdx = 0;
1240 ++taskNdx;
1241 }
1242 taskNdx = 0;
1243 ++stackNdx;
1244 }
1245
1246 return null;
1247 }
1248
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001249 private ActivityStack getNextVisibleStackLocked() {
1250 ArrayList<ActivityStack> stacks = mStacks;
1251 final ActivityRecord parent = mActivityContainer.mParentActivity;
1252 if (parent != null) {
1253 stacks = parent.task.stack.mStacks;
1254 }
1255 if (stacks != null) {
1256 for (int i = stacks.size() - 1; i >= 0; --i) {
1257 ActivityStack stack = stacks.get(i);
1258 if (stack != this && stack.isStackVisibleLocked()) {
1259 return stack;
1260 }
1261 }
1262 }
1263 return null;
1264 }
1265
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001266 /** Returns true if the stack contains a fullscreen task. */
1267 private boolean hasFullscreenTask() {
1268 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1269 final TaskRecord task = mTaskHistory.get(i);
1270 if (task.mFullscreen) {
1271 return true;
1272 }
1273 }
1274 return false;
1275 }
1276
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001277 private boolean hasTranslucentActivity(ActivityStack stack) {
1278 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1279 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1280 final TaskRecord task = tasks.get(taskNdx);
1281 final ArrayList<ActivityRecord> activities = task.mActivities;
1282 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1283 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001284
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001285 // Conditions for an activity to obscure the stack we're
1286 // examining:
1287 // 1. Not Finishing AND Visible AND:
1288 // 2. Either:
1289 // - Full Screen Activity OR
1290 // - On top of Home and our stack is NOT home
1291 if (!r.finishing && r.visible && (r.fullscreen ||
1292 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1293 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001294 }
1295 }
1296 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001297 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001298 }
1299
1300 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001301 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001302 if (!isAttached()) {
1303 return false;
1304 }
1305
1306 if (mStackSupervisor.isFrontStack(this)) {
1307 return true;
1308 }
1309
Wale Ogunwale99db1862015-10-23 20:08:22 -07001310 if (mStackId == PINNED_STACK_ID) {
1311 // Pinned stack is always visible if it exist.
1312 return true;
1313 }
1314
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001315 final int stackIndex = mStacks.indexOf(this);
1316
1317 if (stackIndex == mStacks.size() - 1) {
1318 Slog.wtf(TAG,
1319 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1320 return false;
1321 }
1322
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001323 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1324 final int focusedStackId = focusedStack.mStackId;
1325
1326 if (mStackId == DOCKED_STACK_ID) {
1327 // Docked stack is always visible, except in the case where the home activity
1328 // is the top running activity in the focused home stack.
1329 if (focusedStackId != HOME_STACK_ID) {
1330 return true;
1331 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001332 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001333 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1334 }
1335
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001336 final int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001337 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1338 && stackIndex == belowFocusedIndex) {
1339 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001340 return true;
1341 }
1342
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001343 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1344 && hasTranslucentActivity(focusedStack)) {
1345 // Stacks behind the fullscreen stack with a translucent activity are always
1346 // visible so they can act as a backdrop to the translucent activity.
1347 // For example, dialog activities
1348 if (stackIndex == belowFocusedIndex) {
1349 return true;
1350 }
1351 if (belowFocusedIndex >= 0) {
1352 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001353 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1354 && stackIndex == (belowFocusedIndex - 1)) {
1355 // The stack behind the docked or pinned stack is also visible so we can have a
1356 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001357 return true;
1358 }
1359 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001360 }
1361
1362 if (mStackId >= FIRST_STATIC_STACK_ID && mStackId <= LAST_STATIC_STACK_ID) {
1363 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001364 return false;
1365 }
1366
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001367 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001368 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001369
1370 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1371 continue;
1372 }
1373
1374 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001375 || stack.mStackId == HOME_STACK_ID
1376 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
1377 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001378 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001379 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001380
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001381 if (!hasTranslucentActivity(stack)) {
1382 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001383 }
1384 }
1385
1386 return true;
1387 }
1388
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001389 final int rankTaskLayers(int baseLayer) {
1390 int layer = 0;
1391 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1392 final TaskRecord task = mTaskHistory.get(taskNdx);
1393 ActivityRecord r = task.topRunningActivityLocked();
1394 if (r == null || r.finishing || !r.visible) {
1395 task.mLayerRank = -1;
1396 } else {
1397 task.mLayerRank = baseLayer + layer++;
1398 }
1399 }
1400 return layer;
1401 }
1402
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001403 /**
1404 * Make sure that all activities that need to be visible (that is, they
1405 * currently can be seen by the user) actually are.
1406 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001407 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1408 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001409 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001410 if (top == null) {
1411 return;
1412 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001413 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1414 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001415 + " configChanges=0x" + Integer.toHexString(configChanges));
1416
Craig Mautner5eda9b32013-07-02 11:58:16 -07001417 if (mTranslucentActivityWaiting != top) {
1418 mUndrawnActivitiesBelowTopTranslucent.clear();
1419 if (mTranslucentActivityWaiting != null) {
1420 // Call the callback with a timeout indication.
1421 notifyActivityDrawnLocked(null);
1422 mTranslucentActivityWaiting = null;
1423 }
1424 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1425 }
1426
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001427 // If the top activity is not fullscreen, then we need to
1428 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001429 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001430 final boolean stackInvisible = !isStackVisibleLocked();
1431 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001432 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001433
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001434 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001435 final TaskRecord task = mTaskHistory.get(taskNdx);
1436 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001437
Craig Mautnerd44711d2013-02-23 11:24:36 -08001438 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1439 final ActivityRecord r = activities.get(activityNdx);
1440 if (r.finishing) {
1441 continue;
1442 }
1443 if (aboveTop && r != top) {
1444 continue;
1445 }
1446 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001447 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1448 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001449 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001450 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1451 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001452 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001453
Craig Mautnerd44711d2013-02-23 11:24:36 -08001454 // First: if this is not the current activity being started, make
1455 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001456 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001457 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001459
1460 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001461 // This activity needs to be visible, but isn't even running...
1462 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001463 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1464 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001465 if (r != starting) {
1466 r.startFreezingScreenLocked(r.app, configChanges);
1467 }
1468 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001469 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1470 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001471 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001472 }
1473 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001474 mStackSupervisor.startSpecificActivityLocked(
1475 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001476 if (activityNdx >= activities.size()) {
1477 // Record may be removed if its process needs to restart.
1478 activityNdx = activities.size() - 1;
1479 } else {
1480 noStackActivityResumed = false;
1481 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001482 }
1483
1484 } else if (r.visible) {
1485 // If this activity is already visible, then there is nothing
1486 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001487 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1488 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001490 try {
George Mount6ba042b2014-07-28 11:12:28 -07001491 if (r.returningOptions != null) {
1492 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1493 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001494 }
1495 } catch(RemoteException e) {
1496 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001497 if (r.state == ActivityState.RESUMED) {
1498 noStackActivityResumed = false;
1499 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001500 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001501 // This activity is not currently visible, but is running.
1502 // Tell it to become visible.
1503 r.visible = true;
1504 if (r.state != ActivityState.RESUMED && r != starting) {
1505 // If this activity is paused, tell it
1506 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001507 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1508 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001509 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001510 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001511 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001512 mUndrawnActivitiesBelowTopTranslucent.add(r);
1513 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001514 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001515 r.sleeping = false;
1516 r.app.pendingUiClean = true;
1517 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1518 r.stopFreezingScreenLocked(false);
1519 } catch (Exception e) {
1520 // Just skip on any failure; we'll make it
1521 // visible when it next restarts.
1522 Slog.w(TAG, "Exception thrown making visibile: "
1523 + r.intent.getComponent(), e);
1524 }
1525 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001527
Craig Mautnerd44711d2013-02-23 11:24:36 -08001528 // Aggregate current change flags.
1529 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530
Craig Mautnerd44711d2013-02-23 11:24:36 -08001531 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001532 // At this point, nothing else needs to be shown in this task.
1533 behindFullscreenActivity = true;
1534 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001535 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001536 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001537 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001538 behindFullscreenActivity = true;
1539 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001540 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001541 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001542 }
1543 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001544 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001545 "Make invisible? " + r + " finishing=" + r.finishing
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001546 + " state=" + r.state + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001547 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001548 // Now for any activities that aren't visible to the user, make
1549 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001550 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001551 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001552 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001553 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001554 switch (r.state) {
1555 case STOPPING:
1556 case STOPPED:
1557 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001558 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1559 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001560 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1561 }
1562 break;
1563
1564 case INITIALIZING:
1565 case RESUMED:
1566 case PAUSING:
1567 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001568 // This case created for transitioning activities from
1569 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001570 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001571 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001572 } else {
1573 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1574 mStackSupervisor.mStoppingActivities.add(r);
1575 }
1576 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001577 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001578 break;
1579
1580 default:
1581 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 }
1583 } catch (Exception e) {
1584 // Just skip on any failure; we'll make it
1585 // visible when it next restarts.
1586 Slog.w(TAG, "Exception thrown making hidden: "
1587 + r.intent.getComponent(), e);
1588 }
1589 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001590 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001592 }
1593 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001594 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1595 // The visibility of tasks and the activities they contain in freeform stack are
1596 // determined individually unlike other stacks where the visibility or fullscreen
1597 // status of an activity in a previous task affects other.
1598 behindFullscreenActivity = stackInvisible;
1599 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001600 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001601
1602 if (mTranslucentActivityWaiting != null &&
1603 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1604 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1605 notifyActivityDrawnLocked(null);
1606 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 }
Craig Mautner58547802013-03-05 08:23:53 -08001608
Todd Kennedyaab56db2015-01-30 09:39:53 -08001609 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001610 mTranslucentActivityWaiting = r;
1611 mUndrawnActivitiesBelowTopTranslucent.clear();
1612 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1613 }
1614
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001615 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1616 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1617 final TaskRecord task = mTaskHistory.get(taskNdx);
1618 final ArrayList<ActivityRecord> activities = task.mActivities;
1619 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1620 final ActivityRecord r = activities.get(activityNdx);
1621 if ( r.appTimeTracker != except) {
1622 r.appTimeTracker = null;
1623 }
1624 }
1625 }
1626 }
1627
Craig Mautner5eda9b32013-07-02 11:58:16 -07001628 /**
1629 * Called as activities below the top translucent activity are redrawn. When the last one is
1630 * redrawn notify the top activity by calling
1631 * {@link Activity#onTranslucentConversionComplete}.
1632 *
1633 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1634 * occurred and the activity will be notified immediately.
1635 */
1636 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001637 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001638 if ((r == null)
1639 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1640 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1641 // The last undrawn activity below the top has just been drawn. If there is an
1642 // opaque activity at the top, notify it that it can become translucent safely now.
1643 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1644 mTranslucentActivityWaiting = null;
1645 mUndrawnActivitiesBelowTopTranslucent.clear();
1646 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1647
Craig Mautner71dd1b62014-02-18 15:48:52 -08001648 if (waitingActivity != null) {
1649 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1650 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1651 try {
1652 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1653 waitingActivity.appToken, r != null);
1654 } catch (RemoteException e) {
1655 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001656 }
1657 }
1658 }
1659 }
1660
Craig Mautnera61bc652013-10-28 15:43:18 -07001661 /** If any activities below the top running one are in the INITIALIZING state and they have a
1662 * starting window displayed then remove that starting window. It is possible that the activity
1663 * in this state will never resumed in which case that starting window will be orphaned. */
1664 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001665 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001666 boolean aboveTop = true;
1667 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1668 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1669 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1670 final ActivityRecord r = activities.get(activityNdx);
1671 if (aboveTop) {
1672 if (r == topActivity) {
1673 aboveTop = false;
1674 }
1675 continue;
1676 }
1677
1678 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001679 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1680 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001681 r.mStartingWindowShown = false;
1682 mWindowManager.removeAppStartingWindow(r.appToken);
1683 }
1684 }
1685 }
1686 }
1687
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 /**
1689 * Ensure that the top activity in the stack is resumed.
1690 *
1691 * @param prev The previously resumed activity, for when in the process
1692 * of pausing; can be null to call from elsewhere.
1693 *
1694 * @return Returns true if something is being resumed, or false if
1695 * nothing happened.
1696 */
1697 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001698 return resumeTopActivityLocked(prev, null);
1699 }
1700
1701 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001702 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001703 // Don't even start recursing.
1704 return false;
1705 }
1706
1707 boolean result = false;
1708 try {
1709 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001710 mStackSupervisor.inResumeTopActivity = true;
1711 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1712 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001713 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001714 }
Craig Mautner544efa72014-09-04 16:41:20 -07001715 result = resumeTopActivityInnerLocked(prev, options);
1716 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001717 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001718 }
1719 return result;
1720 }
1721
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001722 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001723 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001724
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001725 if (!mService.mBooting && !mService.mBooted) {
1726 // Not ready yet!
1727 return false;
1728 }
1729
Craig Mautnerdf88d732014-01-27 09:21:32 -08001730 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001731 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001732 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001733 // Do not resume this stack if its parent is not resumed.
1734 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1735 return false;
1736 }
1737
Craig Mautnera61bc652013-10-28 15:43:18 -07001738 cancelInitializingActivities();
1739
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001741 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742
1743 // Remember how we'll process this pause/resume situation, and ensure
1744 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001745 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1746 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747
Craig Mautner84984fa2014-06-19 11:19:20 -07001748 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001750 // There are no more activities!
1751 final String reason = "noMoreActivities";
1752 if (!mFullscreen) {
1753 // Try to move focus to the next visible stack with a running activity if this
1754 // stack is not covering the entire screen.
1755 final ActivityStack stack = getNextVisibleStackLocked();
1756 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1757 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1758 }
1759 }
1760 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001761 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001762 if (DEBUG_STATES) Slog.d(TAG_STATES,
1763 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001764 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001765 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001766 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1767 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1768 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001769 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 }
1771
1772 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001773
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001775 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1776 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 // Make sure we have executed any pending transitions, since there
1778 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001781 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001782 if (DEBUG_STATES) Slog.d(TAG_STATES,
1783 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001784 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 return false;
1786 }
1787
Craig Mautner525f3d92013-05-07 14:01:50 -07001788 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001789 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001790 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001791 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001792 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001793 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001794 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001795 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001796 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001797 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001798 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001799 } else if (!isOnHomeDisplay()) {
1800 return false;
1801 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001802 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001803 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001804 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1805 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1806 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001807 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001808 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001809 }
1810
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811 // If we are sleeping, and there is no resumed activity, and the top
1812 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001813 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001814 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001815 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001816 // Make sure we have executed any pending transitions, since there
1817 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001818 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001820 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001821 if (DEBUG_STATES) Slog.d(TAG_STATES,
1822 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001823 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 return false;
1825 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001826
1827 // Make sure that the user who owns this activity is started. If not,
1828 // we will just leave it as is because someone should be bringing
1829 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001830 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001831 Slog.w(TAG, "Skipping resume of top activity " + next
1832 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001833 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001834 return false;
1835 }
1836
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837 // The activity may be waiting for stop, but that is no longer
1838 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001839 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001840 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001841 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001842 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001844 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001846 // If we are currently pausing an activity, then don't do anything
1847 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001848 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001849 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001850 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001851 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001852 return false;
1853 }
1854
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001855 // Okay we are now going to start a switch, to 'next'. We may first
1856 // have to pause the current activity, but this is an important point
1857 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001858 // XXX "App Redirected" dialog is getting too many false positives
1859 // at this point, so turn off for now.
1860 if (false) {
1861 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1862 long now = SystemClock.uptimeMillis();
1863 final boolean inTime = mLastStartedActivity.startTime != 0
1864 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1865 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1866 final int nextUid = next.info.applicationInfo.uid;
1867 if (inTime && lastUid != nextUid
1868 && lastUid != next.launchedFromUid
1869 && mService.checkPermission(
1870 android.Manifest.permission.STOP_APP_SWITCHES,
1871 -1, next.launchedFromUid)
1872 != PackageManager.PERMISSION_GRANTED) {
1873 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1874 } else {
1875 next.startTime = now;
1876 mLastStartedActivity = next;
1877 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001878 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001879 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001880 mLastStartedActivity = next;
1881 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001882 }
Craig Mautner58547802013-03-05 08:23:53 -08001883
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001884 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1885
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001886 // We need to start pausing the current activity so the top one
1887 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001888 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1889 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001890 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001891 if (DEBUG_STATES) Slog.d(TAG_STATES,
1892 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001893 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001894 }
1895 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001896 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001897 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001898 // At this point we want to put the upcoming activity's process
1899 // at the top of the LRU list, since we know we will be needing it
1900 // very soon and it would be a waste to let it get killed if it
1901 // happens to be sitting towards the end.
1902 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001903 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001904 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001905 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906 return true;
1907 }
1908
Christopher Tated3f175c2012-06-14 14:16:54 -07001909 // If the most recent activity was noHistory but was only stopped rather
1910 // than stopped+finished because the device went to sleep, we need to make
1911 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001912 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001913 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001914 if (DEBUG_STATES) Slog.d(TAG_STATES,
1915 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001916 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001917 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001918 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001919 }
1920
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001922 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1923 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001924 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001925 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1926 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001927 } else {
1928 // The next activity is already visible, so hide the previous
1929 // activity's windows right now so we can show the new one ASAP.
1930 // We only do this if the previous is finishing, which should mean
1931 // it is on top of the one being resumed so hiding it quickly
1932 // is good. Otherwise, we want to do the normal route of allowing
1933 // the resumed activity to be shown so we can decide if the
1934 // previous should actually be hidden depending on whether the
1935 // new one is found to be full-screen or not.
1936 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001937 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001938 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1939 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001940 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 + ", nowVisible=" + next.nowVisible);
1942 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001943 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001944 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001945 + ", waitingVisible="
1946 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1947 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001948 }
1949 }
1950 }
1951
Dianne Hackborne7f97212011-02-24 14:40:20 -08001952 // Launching this app's activity, make sure the app is no longer
1953 // considered stopped.
1954 try {
1955 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001956 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001957 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001958 } catch (IllegalArgumentException e) {
1959 Slog.w(TAG, "Failed trying to unstop package "
1960 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001961 }
1962
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001963 // We are starting up the next activity, so tell the window manager
1964 // that the previous one will be hidden soon. This way it can know
1965 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001966 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 if (prev != null) {
1968 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001969 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 "Prepare close transition: prev=" + prev);
1971 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001972 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001973 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001975 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001976 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1977 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001979 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001981 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1982 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001983 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001984 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001985 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001986 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001987 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001988 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001989 : next.mLaunchTaskBehind
1990 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1991 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001992 }
1993 }
Craig Mautner967212c2013-04-13 21:10:58 -07001994 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001995 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001996 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001997 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001998 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002000 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 }
2002 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002003
2004 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002005 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002006 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2007 if (opts != null) {
2008 resumeAnimOptions = opts.toBundle();
2009 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002010 next.applyOptionsLocked();
2011 } else {
2012 next.clearOptionsLocked();
2013 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002014
Craig Mautnercf910b02013-04-23 11:23:27 -07002015 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002017 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018
2019 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002020 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002021
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002022 // schedule launch ticks to collect information about slow apps.
2023 next.startLaunchTickingLocked();
2024
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002025 ActivityRecord lastResumedActivity =
2026 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 ActivityState lastState = next.state;
2028
2029 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002030
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002031 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002032 next.state = ActivityState.RESUMED;
2033 mResumedActivity = next;
2034 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002035 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002036 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002037 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002038 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002039
2040 // Have the window manager re-evaluate the orientation of
2041 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002042 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002043 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002044 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002045 mService.mConfiguration,
2046 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2047 if (config != null) {
2048 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002050 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002052
Craig Mautner525f3d92013-05-07 14:01:50 -07002053 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 // The configuration update wasn't able to keep the existing
2055 // instance of the activity, and instead started a new one.
2056 // We should be all done, but let's just make sure our activity
2057 // is still at the top and schedule another run if something
2058 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002059 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002060 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002061 "Activity config changed during resume: " + next
2062 + ", new next: " + nextNext);
2063 if (nextNext != next) {
2064 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002065 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002067 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002068 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002069 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002070 return true;
2071 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002072 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002073 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 }
Craig Mautner58547802013-03-05 08:23:53 -08002075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002076 try {
2077 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002078 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 if (a != null) {
2080 final int N = a.size();
2081 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002082 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2083 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002084 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002085 }
2086 }
2087
2088 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002089 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 }
2091
Craig Mautner299f9602015-01-26 09:47:33 -08002092 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2093 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002094
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002095 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002096 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002097 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002098 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002099 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002100 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002101 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002102
Craig Mautner0eea92c2013-05-16 13:35:39 -07002103 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002105 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002106 } catch (Exception e) {
2107 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002108 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002109 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002111 if (lastStack != null) {
2112 lastStack.mResumedActivity = lastResumedActivity;
2113 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 Slog.i(TAG, "Restarting because process died: " + next);
2115 if (!next.hasBeenLaunched) {
2116 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002117 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2118 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002119 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002120 next.appToken, next.packageName, next.theme,
2121 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002122 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2123 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 }
George Mount2c92c972014-03-20 09:38:23 -07002125 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002126 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 return true;
2128 }
2129
2130 // From this point on, if something goes wrong there is no way
2131 // to recover the activity.
2132 try {
2133 next.visible = true;
2134 completeResumeLocked(next);
2135 } catch (Exception e) {
2136 // If any exception gets thrown, toss away this
2137 // activity and try the next one.
2138 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002139 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002140 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002141 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 return true;
2143 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 next.stopped = false;
2145
2146 } else {
2147 // Whoops, need to restart this activity!
2148 if (!next.hasBeenLaunched) {
2149 next.hasBeenLaunched = true;
2150 } else {
2151 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002152 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002153 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002154 mService.compatibilityInfoForPackageLocked(
2155 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002157 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002158 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002160 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002162 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002163 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002164 }
2165
Craig Mautnercf910b02013-04-23 11:23:27 -07002166 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 return true;
2168 }
2169
riddle_hsuc215a4f2014-12-27 12:10:45 +08002170 private TaskRecord getNextTask(TaskRecord targetTask) {
2171 final int index = mTaskHistory.indexOf(targetTask);
2172 if (index >= 0) {
2173 final int numTasks = mTaskHistory.size();
2174 for (int i = index + 1; i < numTasks; ++i) {
2175 TaskRecord task = mTaskHistory.get(i);
2176 if (task.userId == targetTask.userId) {
2177 return task;
2178 }
2179 }
2180 }
2181 return null;
2182 }
2183
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002184 private void insertTaskAtPosition(TaskRecord task, int position) {
2185 if (position >= mTaskHistory.size()) {
2186 insertTaskAtTop(task, null);
2187 return;
2188 }
2189 // Calculate maximum possible position for this task.
2190 int maxPosition = mTaskHistory.size();
2191 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002192 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002193 // Put non-current user tasks below current user tasks.
2194 while (maxPosition > 0) {
2195 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2196 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002197 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002198 break;
2199 }
2200 maxPosition--;
2201 }
2202 }
2203 position = Math.min(position, maxPosition);
2204 mTaskHistory.remove(task);
2205 mTaskHistory.add(position, task);
2206 updateTaskMovement(task, true);
2207 }
2208
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002209 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002210 // If the moving task is over home stack, transfer its return type to next task
2211 if (task.isOverHomeStack()) {
2212 final TaskRecord nextTask = getNextTask(task);
2213 if (nextTask != null) {
2214 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2215 }
2216 }
2217
Craig Mautner9c85c202013-10-04 20:11:26 -07002218 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002219 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002220 if (isOnHomeDisplay()) {
2221 ActivityStack lastStack = mStackSupervisor.getLastStack();
2222 final boolean fromHome = lastStack.isHomeStack();
2223 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002224 task.setTaskToReturnTo(fromHome
2225 ? lastStack.topTask() == null
2226 ? HOME_ACTIVITY_TYPE
2227 : lastStack.topTask().taskType
2228 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002229 }
2230 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002231 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002232 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002233
Craig Mautnerac6f8432013-07-17 13:24:59 -07002234 mTaskHistory.remove(task);
2235 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002236 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002237 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002238 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002239 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002240 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002241 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002242 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002243 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002244 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002245 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002246 break;
2247 }
2248 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002249 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002250 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002251 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002252 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002253 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002254
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002256 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002257 TaskRecord rTask = r.task;
2258 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002259 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2260 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002261 // Last activity in task had been removed or ActivityManagerService is reusing task.
2262 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002263 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002264 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002265 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002266 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002267 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 if (!newTask) {
2269 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002270 boolean startIt = true;
2271 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2272 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002273 if (task.getTopActivity() == null) {
2274 // All activities in task are finishing.
2275 continue;
2276 }
Craig Mautner70a86932013-02-28 22:37:44 -08002277 if (task == r.task) {
2278 // Here it is! Now, if this is not yet visible to the
2279 // user, then just add it without starting; it will
2280 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002281 if (!startIt) {
2282 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2283 + task, new RuntimeException("here").fillInStackTrace());
2284 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002285 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002286 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002287 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002288 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002290 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002291 return;
2292 }
2293 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002294 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 startIt = false;
2296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002297 }
2298 }
2299
2300 // Place a new activity at top of stack, so it is next to interact
2301 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002302
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 // If we are not placing the new activity frontmost, we do not want
2304 // to deliver the onUserLeaving callback to the actual frontmost
2305 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002306 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002307 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002308 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002309 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002310 }
Craig Mautner70a86932013-02-28 22:37:44 -08002311
2312 task = r.task;
2313
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002315 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002316 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002317 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002318 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002319
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002320 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002321 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 // We want to show the starting preview window if we are
2323 // switching to a new task, or the next activity's process is
2324 // not currently running.
2325 boolean showStartingIcon = newTask;
2326 ProcessRecord proc = r.app;
2327 if (proc == null) {
2328 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2329 }
2330 if (proc == null || proc.thread == null) {
2331 showStartingIcon = true;
2332 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002333 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002335 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002336 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002339 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002340 ? r.mLaunchTaskBehind
2341 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2342 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002343 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002344 mNoAnimActivities.remove(r);
2345 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002346 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 boolean doShow = true;
2348 if (newTask) {
2349 // Even though this activity is starting fresh, we still need
2350 // to reset it to make sure we apply affinities to move any
2351 // existing activities from other tasks in to it.
2352 // If the caller has requested that the target task be
2353 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002354 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 resetTaskIfNeededLocked(r, r);
2356 doShow = topRunningNonDelayedActivityLocked(null) == r;
2357 }
George Mount70778d72014-07-01 16:33:45 -07002358 } else if (options != null && new ActivityOptions(options).getAnimationType()
2359 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2360 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002361 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002362 if (r.mLaunchTaskBehind) {
2363 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2364 // tell WindowManager that r is visible even though it is at the back of the stack.
2365 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002366 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002367 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 // Figure out if we are transitioning from another activity that is
2369 // "has the same starting icon" as the next one. This allows the
2370 // window manager to keep the previous window it had previously
2371 // created, if it still had one.
2372 ActivityRecord prev = mResumedActivity;
2373 if (prev != null) {
2374 // We don't want to reuse the previous starting preview if:
2375 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002376 if (prev.task != r.task) {
2377 prev = null;
2378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002380 else if (prev.nowVisible) {
2381 prev = null;
2382 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002384 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002385 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002386 mService.compatibilityInfoForPackageLocked(
2387 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002388 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002389 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002390 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 }
2392 } else {
2393 // If this is the first activity, don't do any fancy animations,
2394 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002395 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002396 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002397 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 }
2399 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002400 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 }
2402
2403 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002404 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002405 } else {
2406 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002407 }
2408 }
2409
Dianne Hackbornbe707852011-11-11 14:32:10 -08002410 final void validateAppTokensLocked() {
2411 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002412 mValidateAppTokens.ensureCapacity(numActivities());
2413 final int numTasks = mTaskHistory.size();
2414 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2415 TaskRecord task = mTaskHistory.get(taskNdx);
2416 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002417 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002418 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002419 }
Craig Mautner000f0022013-02-26 15:04:29 -08002420 TaskGroup group = new TaskGroup();
2421 group.taskId = task.taskId;
2422 mValidateAppTokens.add(group);
2423 final int numActivities = activities.size();
2424 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2425 final ActivityRecord r = activities.get(activityNdx);
2426 group.tokens.add(r.appToken);
2427 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002428 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002429 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002430 }
2431
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 /**
2433 * Perform a reset of the given task, if needed as part of launching it.
2434 * Returns the new HistoryRecord at the top of the task.
2435 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002436 /**
2437 * Helper method for #resetTaskIfNeededLocked.
2438 * We are inside of the task being reset... we'll either finish this activity, push it out
2439 * for another task, or leave it as-is.
2440 * @param task The task containing the Activity (taskTop) that might be reset.
2441 * @param forceReset
2442 * @return An ActivityOptions that needs to be processed.
2443 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002444 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002445 ActivityOptions topOptions = null;
2446
2447 int replyChainEnd = -1;
2448 boolean canMoveOptions = true;
2449
2450 // We only do this for activities that are not the root of the task (since if we finish
2451 // the root, we may no longer have the task!).
2452 final ArrayList<ActivityRecord> activities = task.mActivities;
2453 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002454 final int rootActivityNdx = task.findEffectiveRootIndex();
2455 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002456 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002457 if (target.frontOfTask)
2458 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002459
2460 final int flags = target.info.flags;
2461 final boolean finishOnTaskLaunch =
2462 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2463 final boolean allowTaskReparenting =
2464 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2465 final boolean clearWhenTaskReset =
2466 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2467
2468 if (!finishOnTaskLaunch
2469 && !clearWhenTaskReset
2470 && target.resultTo != null) {
2471 // If this activity is sending a reply to a previous
2472 // activity, we can't do anything with it now until
2473 // we reach the start of the reply chain.
2474 // XXX note that we are assuming the result is always
2475 // to the previous activity, which is almost always
2476 // the case but we really shouldn't count on.
2477 if (replyChainEnd < 0) {
2478 replyChainEnd = i;
2479 }
2480 } else if (!finishOnTaskLaunch
2481 && !clearWhenTaskReset
2482 && allowTaskReparenting
2483 && target.taskAffinity != null
2484 && !target.taskAffinity.equals(task.affinity)) {
2485 // If this activity has an affinity for another
2486 // task, then we need to move it out of here. We will
2487 // move it as far out of the way as possible, to the
2488 // bottom of the activity stack. This also keeps it
2489 // correctly ordered with any activities we previously
2490 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002491 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002492 final ActivityRecord bottom =
2493 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002494 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002495 if (bottom != null && target.taskAffinity != null
2496 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002497 // If the activity currently at the bottom has the
2498 // same task affinity as the one we are moving,
2499 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002500 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002501 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002502 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002503 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002504 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002505 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002506 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002507 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002508 + " out to new task " + target.task);
2509 }
2510
Wale Ogunwale706ed792015-08-02 10:29:44 -07002511 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002512
Craig Mautner525f3d92013-05-07 14:01:50 -07002513 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002514 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2515 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002516 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002517 if (p.finishing) {
2518 continue;
2519 }
2520
Craig Mautnere3a74d52013-02-22 14:14:58 -08002521 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002522 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002523 topOptions = p.takeOptionsLocked();
2524 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002525 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 }
2527 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002528 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2529 "Removing activity " + p + " from task=" + task + " adding to task="
2530 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002531 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2532 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002533 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002534 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002535
Wale Ogunwale706ed792015-08-02 10:29:44 -07002536 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002537 }
2538
Wale Ogunwale706ed792015-08-02 10:29:44 -07002539 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002540 if (VALIDATE_TOKENS) {
2541 validateAppTokensLocked();
2542 }
2543
2544 replyChainEnd = -1;
2545 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2546 // If the activity should just be removed -- either
2547 // because it asks for it, or the task should be
2548 // cleared -- then finish it and anything that is
2549 // part of its reply chain.
2550 int end;
2551 if (clearWhenTaskReset) {
2552 // In this case, we want to finish this activity
2553 // and everything above it, so be sneaky and pretend
2554 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002555 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002556 } else if (replyChainEnd < 0) {
2557 end = i;
2558 } else {
2559 end = replyChainEnd;
2560 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002561 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002562 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002563 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002564 if (p.finishing) {
2565 continue;
2566 }
2567 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002568 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002569 topOptions = p.takeOptionsLocked();
2570 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002571 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002572 }
2573 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002574 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002575 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002576 if (finishActivityLocked(
2577 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002578 end--;
2579 srcPos--;
2580 }
2581 }
2582 replyChainEnd = -1;
2583 } else {
2584 // If we were in the middle of a chain, well the
2585 // activity that started it all doesn't want anything
2586 // special, so leave it all as-is.
2587 replyChainEnd = -1;
2588 }
2589 }
2590
2591 return topOptions;
2592 }
2593
2594 /**
2595 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2596 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2597 * @param affinityTask The task we are looking for an affinity to.
2598 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2599 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2600 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2601 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002602 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002603 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002604 int replyChainEnd = -1;
2605 final int taskId = task.taskId;
2606 final String taskAffinity = task.affinity;
2607
2608 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2609 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002610 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2611
2612 // Do not operate on or below the effective root Activity.
2613 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002614 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002615 if (target.frontOfTask)
2616 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002617
2618 final int flags = target.info.flags;
2619 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2620 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2621
2622 if (target.resultTo != null) {
2623 // If this activity is sending a reply to a previous
2624 // activity, we can't do anything with it now until
2625 // we reach the start of the reply chain.
2626 // XXX note that we are assuming the result is always
2627 // to the previous activity, which is almost always
2628 // the case but we really shouldn't count on.
2629 if (replyChainEnd < 0) {
2630 replyChainEnd = i;
2631 }
2632 } else if (topTaskIsHigher
2633 && allowTaskReparenting
2634 && taskAffinity != null
2635 && taskAffinity.equals(target.taskAffinity)) {
2636 // This activity has an affinity for our task. Either remove it if we are
2637 // clearing or move it over to our task. Note that
2638 // we currently punt on the case where we are resetting a
2639 // task that is not at the top but who has activities above
2640 // with an affinity to it... this is really not a normal
2641 // case, and we will need to later pull that task to the front
2642 // and usually at that point we will do the reset and pick
2643 // up those remaining activities. (This only happens if
2644 // someone starts an activity in a new task from an activity
2645 // in a task that is not currently on top.)
2646 if (forceReset || finishOnTaskLaunch) {
2647 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002648 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2649 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002650 for (int srcPos = start; srcPos >= i; --srcPos) {
2651 final ActivityRecord p = activities.get(srcPos);
2652 if (p.finishing) {
2653 continue;
2654 }
Todd Kennedy539db512014-12-15 09:57:55 -08002655 finishActivityLocked(
2656 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002657 }
2658 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002659 if (taskInsertionPoint < 0) {
2660 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002661
Craig Mautner77878772013-03-04 19:46:24 -08002662 }
Craig Mautner77878772013-03-04 19:46:24 -08002663
2664 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002665 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2666 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2667 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002668 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002669 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002670 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002671 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002672
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002673 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2674 "Removing and adding activity " + p + " to stack at " + task
2675 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002676 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2677 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002678 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002679 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002680 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002681 if (VALIDATE_TOKENS) {
2682 validateAppTokensLocked();
2683 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002684
2685 // Now we've moved it in to place... but what if this is
2686 // a singleTop activity and we have put it on top of another
2687 // instance of the same activity? Then we drop the instance
2688 // below so it remains singleTop.
2689 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2690 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002691 int targetNdx = taskActivities.indexOf(target);
2692 if (targetNdx > 0) {
2693 ActivityRecord p = taskActivities.get(targetNdx - 1);
2694 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002695 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2696 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002697 }
2698 }
2699 }
2700 }
2701
2702 replyChainEnd = -1;
2703 }
2704 }
Craig Mautner77878772013-03-04 19:46:24 -08002705 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002706 }
2707
Craig Mautner8849a5e2013-04-02 16:41:03 -07002708 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002709 ActivityRecord newActivity) {
2710 boolean forceReset =
2711 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2712 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2713 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2714 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2715 forceReset = true;
2716 }
2717 }
2718
2719 final TaskRecord task = taskTop.task;
2720
2721 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2722 * for remaining tasks. Used for later tasks to reparent to task. */
2723 boolean taskFound = false;
2724
2725 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2726 ActivityOptions topOptions = null;
2727
Craig Mautner77878772013-03-04 19:46:24 -08002728 // Preserve the location for reparenting in the new task.
2729 int reparentInsertionPoint = -1;
2730
Craig Mautnere3a74d52013-02-22 14:14:58 -08002731 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2732 final TaskRecord targetTask = mTaskHistory.get(i);
2733
2734 if (targetTask == task) {
2735 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2736 taskFound = true;
2737 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002738 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2739 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002740 }
2741 }
2742
Craig Mautner70a86932013-02-28 22:37:44 -08002743 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002744 if (taskNdx >= 0) {
2745 do {
2746 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2747 } while (taskTop == null && taskNdx >= 0);
2748 }
Craig Mautner70a86932013-02-28 22:37:44 -08002749
Craig Mautnere3a74d52013-02-22 14:14:58 -08002750 if (topOptions != null) {
2751 // If we got some ActivityOptions from an activity on top that
2752 // was removed from the task, propagate them to the new real top.
2753 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002754 taskTop.updateOptionsLocked(topOptions);
2755 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002756 topOptions.abort();
2757 }
2758 }
2759
2760 return taskTop;
2761 }
2762
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2764 String resultWho, int requestCode, int resultCode, Intent data) {
2765
2766 if (callingUid > 0) {
2767 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002768 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 }
2770
2771 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2772 + " : who=" + resultWho + " req=" + requestCode
2773 + " res=" + resultCode + " data=" + data);
2774 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2775 try {
2776 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2777 list.add(new ResultInfo(resultWho, requestCode,
2778 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002779 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002780 return;
2781 } catch (Exception e) {
2782 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2783 }
2784 }
2785
2786 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2787 }
2788
Craig Mautner299f9602015-01-26 09:47:33 -08002789 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002790 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002791 ActivityRecord next = topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002792 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002793 if (next != r) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002794 if (next != null && (mStackId == FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale99db1862015-10-23 20:08:22 -07002795 || mStackId == DOCKED_STACK_ID || mStackId == PINNED_STACK_ID)) {
2796 // For freeform, docked, and pinned stacks we always keep the focus within the
2797 // stack as long as there is a running activity in the stack that we can adjust
2798 // focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002799 mService.setFocusedActivityLocked(next, myReason);
2800 return;
2801 } else {
2802 final TaskRecord task = r.task;
2803 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2804 // For non-fullscreen stack, we want to move the focus to the next visible
2805 // stack to prevent the home screen from moving to the top and obscuring
2806 // other visible stacks.
2807 if (!mFullscreen
2808 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2809 return;
2810 }
2811 // Move the home stack to the top if this stack is fullscreen or there is no
2812 // other visible stack.
2813 if (mStackSupervisor.moveHomeStackTaskToTop(
2814 task.getTaskToReturnTo(), myReason)) {
2815 // Activity focus was already adjusted. Nothing else to do...
2816 return;
2817 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002818 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002819 }
2820 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002821
2822 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002823 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002824 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002825 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002826 }
2827 }
2828
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002829 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2830 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2831 final String myReason = reason + " adjustFocusToNextVisibleStack";
2832 if (stack == null) {
2833 return false;
2834 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002835 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002836 if (top == null) {
2837 return false;
2838 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002839 mService.setFocusedActivityLocked(top, myReason);
2840 return true;
2841 }
2842
Craig Mautnerf3333272013-04-22 10:55:53 -07002843 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002844 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2846 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2847 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002848 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002849 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002850 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2851 "stop-no-history", false)) {
2852 // Activity was finished, no need to continue trying to schedule stop.
2853 adjustFocusedActivityLocked(r, "stopActivityFinished");
2854 r.resumeKeyDispatchingLocked();
2855 return;
2856 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002857 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002858 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002859 + " on stop because we're just sleeping");
2860 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002861 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002862 }
2863
2864 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002865 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002866 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 try {
2868 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002869 if (DEBUG_STATES) Slog.v(TAG_STATES,
2870 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002872 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2873 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002874 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002875 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002876 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002877 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002878 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002879 r.setSleeping(true);
2880 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002881 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002882 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 } catch (Exception e) {
2884 // Maybe just ignore exceptions here... if the process
2885 // has crashed, our death notification will clean things
2886 // up.
2887 Slog.w(TAG, "Exception thrown during pause", e);
2888 // Just in case, assume it to be stopped.
2889 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002890 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002891 r.state = ActivityState.STOPPED;
2892 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002893 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 }
2895 }
2896 }
2897 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002898
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 /**
2900 * @return Returns true if the activity is being finished, false if for
2901 * some reason it is being left as-is.
2902 */
2903 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002904 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002905 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002906 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2907 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002908 + ", result=" + resultCode + ", data=" + resultData
2909 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002910 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 return false;
2912 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913
Craig Mautnerd44711d2013-02-23 11:24:36 -08002914 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002915 return true;
2916 }
2917
Craig Mautnerd2328952013-03-05 12:46:26 -08002918 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002919 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2920 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2921 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2922 ActivityRecord r = activities.get(activityNdx);
2923 if (r.resultTo == self && r.requestCode == requestCode) {
2924 if ((r.resultWho == null && resultWho == null) ||
2925 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2926 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2927 false);
2928 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002929 }
2930 }
2931 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002932 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002933 }
2934
Todd Kennedy539db512014-12-15 09:57:55 -08002935 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002936 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002937 if (r != null && r.app == app) {
2938 // If the top running activity is from this crashing
2939 // process, then terminate it to avoid getting in a loop.
2940 Slog.w(TAG, " Force finishing activity "
2941 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002942 int taskNdx = mTaskHistory.indexOf(r.task);
2943 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002944 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002945 // Also terminate any activities below it that aren't yet
2946 // stopped, to avoid a situation where one will get
2947 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002948 --activityNdx;
2949 if (activityNdx < 0) {
2950 do {
2951 --taskNdx;
2952 if (taskNdx < 0) {
2953 break;
2954 }
2955 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2956 } while (activityNdx < 0);
2957 }
2958 if (activityNdx >= 0) {
2959 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002960 if (r.state == ActivityState.RESUMED
2961 || r.state == ActivityState.PAUSING
2962 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002963 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002964 Slog.w(TAG, " Force finishing activity "
2965 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002966 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002967 }
2968 }
2969 }
2970 }
2971 }
2972
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002973 final void finishVoiceTask(IVoiceInteractionSession session) {
2974 IBinder sessionBinder = session.asBinder();
2975 boolean didOne = false;
2976 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2977 TaskRecord tr = mTaskHistory.get(taskNdx);
2978 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2979 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2980 ActivityRecord r = tr.mActivities.get(activityNdx);
2981 if (!r.finishing) {
2982 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2983 false);
2984 didOne = true;
2985 }
2986 }
2987 }
2988 }
2989 if (didOne) {
2990 mService.updateOomAdjLocked();
2991 }
2992 }
2993
Craig Mautnerd2328952013-03-05 12:46:26 -08002994 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002995 ArrayList<ActivityRecord> activities = r.task.mActivities;
2996 for (int index = activities.indexOf(r); index >= 0; --index) {
2997 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002998 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002999 break;
3000 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003001 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003002 }
3003 return true;
3004 }
3005
Dianne Hackborn5c607432012-02-28 14:44:19 -08003006 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3007 // send the result
3008 ActivityRecord resultTo = r.resultTo;
3009 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003010 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003011 + " who=" + r.resultWho + " req=" + r.requestCode
3012 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003013 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003014 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003015 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003016 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003017 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003018 if (r.info.applicationInfo.uid > 0) {
3019 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3020 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003021 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003022 }
3023 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3024 resultData);
3025 r.resultTo = null;
3026 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003027 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003028
3029 // Make sure this HistoryRecord is not holding on to other resources,
3030 // because clients have remote IPC references to this object so we
3031 // can't assume that will go away and want to avoid circular IPC refs.
3032 r.results = null;
3033 r.pendingResults = null;
3034 r.newIntents = null;
3035 r.icicle = null;
3036 }
3037
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 /**
3039 * @return Returns true if this activity has been removed from the history
3040 * list, or false if it is still in the list and will be removed later.
3041 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003042 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3043 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044 if (r.finishing) {
3045 Slog.w(TAG, "Duplicate finish request for " + r);
3046 return false;
3047 }
3048
Wale Ogunwale7d701172015-03-11 15:36:30 -07003049 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003050 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003051 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003052 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003053 task.taskId, r.shortComponentName, reason);
3054 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003055 final int index = activities.indexOf(r);
3056 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003057 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003058 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003059 // If the caller asked that this activity (and all above it)
3060 // be cleared when the task is reset, don't lose that information,
3061 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003062 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003063 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064 }
3065 }
3066
3067 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003068
Craig Mautner299f9602015-01-26 09:47:33 -08003069 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070
Dianne Hackborn5c607432012-02-28 14:44:19 -08003071 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003072
Craig Mautnerde4ef022013-04-07 19:01:33 -07003073 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003074 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003075 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003077 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003078 ? AppTransition.TRANSIT_TASK_CLOSE
3079 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003080
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003082 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003083
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003084 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003085 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3086 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3087 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003088 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 }
3090
Craig Mautneraea74a52014-03-08 14:23:10 -08003091 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003092 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003093 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 } else if (r.state != ActivityState.PAUSING) {
3095 // If the activity is PAUSING, we will complete the finish once
3096 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003097 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003098 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003100 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003101 }
3102
3103 return false;
3104 }
3105
Craig Mautnerf3333272013-04-22 10:55:53 -07003106 static final int FINISH_IMMEDIATELY = 0;
3107 static final int FINISH_AFTER_PAUSE = 1;
3108 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003109
Craig Mautnerf3333272013-04-22 10:55:53 -07003110 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 // First things first: if this activity is currently visible,
3112 // and the resumed activity is not yet visible, then hold off on
3113 // finishing until the resumed one becomes visible.
3114 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003115 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3116 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003117 if (mStackSupervisor.mStoppingActivities.size() > 3
3118 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003119 // If we already have a few activities waiting to stop,
3120 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003121 // them out. Or if r is the last of activity of the last task the stack
3122 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003123 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003124 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003125 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003126 }
3127 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003128 if (DEBUG_STATES) Slog.v(TAG_STATES,
3129 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003131 if (oomAdj) {
3132 mService.updateOomAdjLocked();
3133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003134 return r;
3135 }
3136
3137 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003138 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003139 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003140 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 if (mResumedActivity == r) {
3142 mResumedActivity = null;
3143 }
3144 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003145 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 r.state = ActivityState.FINISHING;
3147
3148 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003149 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150 || prevState == ActivityState.STOPPED
3151 || prevState == ActivityState.INITIALIZING) {
3152 // If this activity is already stopped, we can just finish
3153 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003154 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003155 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003156 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003157 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003158 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003159 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003160 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3161 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003162 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003164
3165 // Need to go through the full pause cycle to get this
3166 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003167 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003168 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003169 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003170 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003171 return r;
3172 }
3173
Craig Mautneree36c772014-07-16 14:56:05 -07003174 void finishAllActivitiesLocked(boolean immediately) {
3175 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003176 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3177 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3178 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3179 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003180 noActivitiesInStack = false;
3181 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003182 continue;
3183 }
Craig Mautneree36c772014-07-16 14:56:05 -07003184 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003185 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3186 }
3187 }
Craig Mautneree36c772014-07-16 14:56:05 -07003188 if (noActivitiesInStack) {
3189 mActivityContainer.onTaskListEmptyLocked();
3190 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003191 }
3192
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003193 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3194 // Basic case: for simple app-centric recents, we need to recreate
3195 // the task if the affinity has changed.
3196 if (srec == null || srec.task.affinity == null ||
3197 !srec.task.affinity.equals(destAffinity)) {
3198 return true;
3199 }
3200 // Document-centric case: an app may be split in to multiple documents;
3201 // they need to re-create their task if this current activity is the root
3202 // of a document, unless simply finishing it will return them to the the
3203 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003204 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3205 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003206 // Okay, this activity is at the root of its task. What to do, what to do...
3207 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3208 // Finishing won't return to an application, so we need to recreate.
3209 return true;
3210 }
3211 // We now need to get the task below it to determine what to do.
3212 int taskIdx = mTaskHistory.indexOf(srec.task);
3213 if (taskIdx <= 0) {
3214 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3215 return false;
3216 }
3217 if (taskIdx == 0) {
3218 // At the bottom of the stack, nothing to go back to.
3219 return true;
3220 }
3221 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3222 if (!srec.task.affinity.equals(prevTask.affinity)) {
3223 // These are different apps, so need to recreate.
3224 return true;
3225 }
3226 }
3227 return false;
3228 }
3229
Wale Ogunwale7d701172015-03-11 15:36:30 -07003230 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003231 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003232 final TaskRecord task = srec.task;
3233 final ArrayList<ActivityRecord> activities = task.mActivities;
3234 final int start = activities.indexOf(srec);
3235 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003236 return false;
3237 }
3238 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003239 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003240 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003241 final ComponentName dest = destIntent.getComponent();
3242 if (start > 0 && dest != null) {
3243 for (int i = finishTo; i >= 0; i--) {
3244 ActivityRecord r = activities.get(i);
3245 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003246 r.info.name.equals(dest.getClassName())) {
3247 finishTo = i;
3248 parent = r;
3249 foundParentInTask = true;
3250 break;
3251 }
3252 }
3253 }
3254
3255 IActivityController controller = mService.mController;
3256 if (controller != null) {
3257 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3258 if (next != null) {
3259 // ask watcher if this is allowed
3260 boolean resumeOK = true;
3261 try {
3262 resumeOK = controller.activityResuming(next.packageName);
3263 } catch (RemoteException e) {
3264 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003265 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003266 }
3267
3268 if (!resumeOK) {
3269 return false;
3270 }
3271 }
3272 }
3273 final long origId = Binder.clearCallingIdentity();
3274 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003275 ActivityRecord r = activities.get(i);
3276 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003277 // Only return the supplied result for the first activity finished
3278 resultCode = Activity.RESULT_CANCELED;
3279 resultData = null;
3280 }
3281
3282 if (parent != null && foundParentInTask) {
3283 final int parentLaunchMode = parent.info.launchMode;
3284 final int destIntentFlags = destIntent.getFlags();
3285 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3286 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3287 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3288 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003289 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3290 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003291 } else {
3292 try {
3293 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3294 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003295 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003296 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003297 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003298 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003299 foundParentInTask = res == ActivityManager.START_SUCCESS;
3300 } catch (RemoteException e) {
3301 foundParentInTask = false;
3302 }
3303 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003304 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003305 }
3306 }
3307 Binder.restoreCallingIdentity(origId);
3308 return foundParentInTask;
3309 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003310 /**
3311 * Perform the common clean-up of an activity record. This is called both
3312 * as part of destroyActivityLocked() (when destroying the client-side
3313 * representation) and cleaning things up as a result of its hosting
3314 * processing going away, in which case there is no remaining client-side
3315 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003316 *
3317 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003319 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3320 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 if (mResumedActivity == r) {
3322 mResumedActivity = null;
3323 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003324 if (mPausingActivity == r) {
3325 mPausingActivity = null;
3326 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003327 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328
3329 r.configDestroy = false;
3330 r.frozenBeforeDestroy = false;
3331
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003332 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003333 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003334 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003335 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003336 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003337 }
3338
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003339 // Make sure this record is no longer in the pending finishes list.
3340 // This could happen, for example, if we are trimming activities
3341 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003342 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003343 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 // Remove any pending results.
3346 if (r.finishing && r.pendingResults != null) {
3347 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3348 PendingIntentRecord rec = apr.get();
3349 if (rec != null) {
3350 mService.cancelIntentSenderLocked(rec, false);
3351 }
3352 }
3353 r.pendingResults = null;
3354 }
3355
3356 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003357 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 }
3359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003361 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003362 if (getVisibleBehindActivity() == r) {
3363 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003364 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003365 }
3366
3367 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003368 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003370 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003371 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003372 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003373 }
3374
Craig Mautner299f9602015-01-26 09:47:33 -08003375 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003376 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003377 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003378 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003379 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3380 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3381
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003382 r.takeFromHistory();
3383 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003384 if (DEBUG_STATES) Slog.v(TAG_STATES,
3385 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003386 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003387 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003388 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003389 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003390 if (VALIDATE_TOKENS) {
3391 validateAppTokensLocked();
3392 }
Craig Mautner312ba862014-02-10 17:55:01 -08003393 final TaskRecord task = r.task;
3394 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003395 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003396 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003397 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3398 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003399 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003400 }
Craig Mautner299f9602015-01-26 09:47:33 -08003401 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003402 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003403 cleanUpActivityServicesLocked(r);
3404 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003405 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003406
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003407 /**
3408 * Perform clean-up of service connections in an activity record.
3409 */
3410 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3411 // Throw away any services that have been bound by this activity.
3412 if (r.connections != null) {
3413 Iterator<ConnectionRecord> it = r.connections.iterator();
3414 while (it.hasNext()) {
3415 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003416 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003417 }
3418 r.connections = null;
3419 }
3420 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003421
Craig Mautneree2e45a2014-06-27 12:10:03 -07003422 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003423 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003424 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003425 mHandler.sendMessage(msg);
3426 }
3427
Craig Mautneree2e45a2014-06-27 12:10:03 -07003428 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003429 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003430 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003431 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3432 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3433 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3434 final ActivityRecord r = activities.get(activityNdx);
3435 if (r.finishing) {
3436 continue;
3437 }
3438 if (r.fullscreen) {
3439 lastIsOpaque = true;
3440 }
3441 if (owner != null && r.app != owner) {
3442 continue;
3443 }
3444 if (!lastIsOpaque) {
3445 continue;
3446 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003447 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003448 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003449 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003450 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003451 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003452 activityRemoved = true;
3453 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003454 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003455 }
3456 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003457 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003458 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003459 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003460 }
3461
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003462 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3463 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003464 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3465 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003466 + " pausing=" + mPausingActivity + " for reason " + reason);
3467 return destroyActivityLocked(r, true, reason);
3468 }
3469 return false;
3470 }
3471
3472 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3473 String reason) {
3474 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003475 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003476 int maxTasks = tasks.size() / 4;
3477 if (maxTasks < 1) {
3478 maxTasks = 1;
3479 }
3480 int numReleased = 0;
3481 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3482 final TaskRecord task = mTaskHistory.get(taskNdx);
3483 if (!tasks.contains(task)) {
3484 continue;
3485 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003486 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003487 int curNum = 0;
3488 final ArrayList<ActivityRecord> activities = task.mActivities;
3489 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3490 final ActivityRecord activity = activities.get(actNdx);
3491 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003492 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003493 + " in state " + activity.state + " resumed=" + mResumedActivity
3494 + " pausing=" + mPausingActivity + " for reason " + reason);
3495 destroyActivityLocked(activity, true, reason);
3496 if (activities.get(actNdx) != activity) {
3497 // Was removed from list, back up so we don't miss the next one.
3498 actNdx--;
3499 }
3500 curNum++;
3501 }
3502 }
3503 if (curNum > 0) {
3504 numReleased += curNum;
3505 maxTasks--;
3506 if (mTaskHistory.get(taskNdx) != task) {
3507 // The entire task got removed, back up so we don't miss the next one.
3508 taskNdx--;
3509 }
3510 }
3511 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003512 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3513 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003514 return numReleased;
3515 }
3516
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003517 /**
3518 * Destroy the current CLIENT SIDE instance of an activity. This may be
3519 * called both when actually finishing an activity, or when performing
3520 * a configuration switch where we destroy the current client-side object
3521 * but then create a new client-side object for this same HistoryRecord.
3522 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003523 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003524 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3525 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003526 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003527 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003528 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003529 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530
3531 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003532
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003533 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003534
3535 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003536
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003537 if (hadApp) {
3538 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003539 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003540 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3541 mService.mHeavyWeightProcess = null;
3542 mService.mHandler.sendEmptyMessage(
3543 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3544 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003545 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003546 // Update any services we are bound to that might care about whether
3547 // their client may have activities.
3548 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003549 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003550 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003551 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003552 }
3553 }
3554
3555 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003556
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003558 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003559 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003560 r.configChangeFlags);
3561 } catch (Exception e) {
3562 // We can just ignore exceptions here... if the process
3563 // has crashed, our death notification will clean things
3564 // up.
3565 //Slog.w(TAG, "Exception thrown during finish", e);
3566 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003567 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 removedFromHistory = true;
3569 skipDestroy = true;
3570 }
3571 }
3572
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003574
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003575 // If the activity is finishing, we need to wait on removing it
3576 // from the list to give it a chance to do its cleanup. During
3577 // that time it may make calls back with its token so we need to
3578 // be able to find it on the list and so we don't want to remove
3579 // it from the list yet. Otherwise, we can just immediately put
3580 // it in the destroyed state since we are not removing it from the
3581 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003582 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003583 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003584 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003586 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003587 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3588 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003589 if (DEBUG_STATES) Slog.v(TAG_STATES,
3590 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003591 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003592 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003593 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 }
3595 } else {
3596 // remove this record from the history.
3597 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003598 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003599 removedFromHistory = true;
3600 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003601 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003602 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003603 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003604 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003605 }
3606 }
3607
3608 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003609
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003610 if (!mLRUActivities.remove(r) && hadApp) {
3611 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3612 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003613
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614 return removedFromHistory;
3615 }
3616
Craig Mautner299f9602015-01-26 09:47:33 -08003617 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003618 final long origId = Binder.clearCallingIdentity();
3619 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003620 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003621 if (r != null) {
3622 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003624 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003625
Wale Ogunwale60454db2015-01-23 16:05:07 -08003626 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003627 if (r.state == ActivityState.DESTROYING) {
3628 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003629 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003630 }
3631 }
Craig Mautner05d29032013-05-03 13:40:13 -07003632 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003633 } finally {
3634 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003635 }
3636 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003637
Todd Kennedyaab56db2015-01-30 09:39:53 -08003638 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003639 if (hasVisibleBehindActivity() &&
3640 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003641 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003642 // Don't release the top activity if it has requested to run behind the next
3643 // activity.
3644 return;
3645 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003646 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003647 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003648 " thread=" + r.app.thread);
3649 if (r != null && r.app != null && r.app.thread != null) {
3650 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003651 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003652 } catch (RemoteException e) {
3653 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003654 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003655 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003656 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003657 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003658 }
3659 }
3660 }
3661
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003662 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003663 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3664 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003665 if (r != null) {
3666 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003667 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003668 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003669 }
3670 mStackSupervisor.resumeTopActivitiesLocked();
3671 }
3672
Jose Lima4b6c6692014-08-12 17:41:12 -07003673 boolean hasVisibleBehindActivity() {
3674 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003675 }
3676
Jose Lima4b6c6692014-08-12 17:41:12 -07003677 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003678 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003679 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003680 }
3681 }
3682
Jose Lima4b6c6692014-08-12 17:41:12 -07003683 ActivityRecord getVisibleBehindActivity() {
3684 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003685 }
3686
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003687 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3688 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003690 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3691 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 while (i > 0) {
3693 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003694 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003695 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003696 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003697 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003698 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003699 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003700 }
3701 }
3702 }
3703
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003704 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3705 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003706 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3707 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003708 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3709 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003710 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003711 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003712 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3713 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003714
3715 boolean hasVisibleActivities = false;
3716
3717 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003718 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003719 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3720 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003721 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3722 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3723 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3724 final ActivityRecord r = activities.get(activityNdx);
3725 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003726 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3727 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003728 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003729 if (r.visible) {
3730 hasVisibleActivities = true;
3731 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003732 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003733 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3734 // Don't currently have state for the activity, or
3735 // it is finishing -- always remove it.
3736 remove = true;
3737 } else if (r.launchCount > 2 &&
3738 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3739 // We have launched this activity too many times since it was
3740 // able to run, so give up and remove it.
3741 remove = true;
3742 } else {
3743 // The process may be gone, but the activity lives on!
3744 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003745 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003746 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003747 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3748 "Removing activity " + r + " from stack at " + i
3749 + ": haveState=" + r.haveState
3750 + " stateNotNeeded=" + r.stateNotNeeded
3751 + " finishing=" + r.finishing
3752 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003753 if (!r.finishing) {
3754 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3755 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3756 r.userId, System.identityHashCode(r),
3757 r.task.taskId, r.shortComponentName,
3758 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003759 if (r.state == ActivityState.RESUMED) {
3760 mService.updateUsageStats(r, false);
3761 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003762 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003763 } else {
3764 // We have the current state for this activity, so
3765 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003766 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3767 if (DEBUG_APP) Slog.v(TAG_APP,
3768 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003769 r.app = null;
3770 r.nowVisible = false;
3771 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003772 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003773 "App died, clearing saved state of " + r);
3774 r.icicle = null;
3775 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003776 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003777 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003778 if (remove) {
3779 removeActivityFromHistoryLocked(r, "appDied");
3780 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003781 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003782 }
3783 }
3784
3785 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003786 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003787
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003788 final void updateTransitLocked(int transit, Bundle options) {
3789 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003790 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003791 if (r != null && r.state != ActivityState.RESUMED) {
3792 r.updateOptionsLocked(options);
3793 } else {
3794 ActivityOptions.abort(options);
3795 }
3796 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003797 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003798 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003799
Craig Mautner21d24a22014-04-23 11:45:37 -07003800 void updateTaskMovement(TaskRecord task, boolean toFront) {
3801 if (task.isPersistable) {
3802 task.mLastTimeMoved = System.currentTimeMillis();
3803 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3804 // recently will be most negative, tasks sent to the bottom before that will be less
3805 // negative. Similarly for recent tasks moved to the top which will be most positive.
3806 if (!toFront) {
3807 task.mLastTimeMoved *= -1;
3808 }
3809 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003810 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003811 }
3812
Craig Mautner84984fa2014-06-19 11:19:20 -07003813 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003814 final int top = mTaskHistory.size() - 1;
3815 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3816 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003817 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003818 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3819 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003820 mTaskHistory.remove(taskNdx);
3821 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003822 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003823 return;
3824 }
3825 }
3826 }
3827
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003828 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003829 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003830 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003831
Craig Mautner11bf9a52013-02-19 14:08:51 -08003832 final int numTasks = mTaskHistory.size();
3833 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003834 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003835 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003836 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003837 ActivityOptions.abort(options);
3838 } else {
3839 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3840 }
3841 return;
3842 }
3843
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003844 if (timeTracker != null) {
3845 // The caller wants a time tracker associated with this task.
3846 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3847 tr.mActivities.get(i).appTimeTracker = timeTracker;
3848 }
3849 }
3850
Craig Mautner11bf9a52013-02-19 14:08:51 -08003851 // Shift all activities with this task up to the top
3852 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003853 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003854
Chong Zhang45c25ce2015-08-10 22:18:26 -07003855 // Don't refocus if invisible to current user
3856 ActivityRecord top = tr.getTopActivity();
3857 if (!okToShowLocked(top)) {
3858 addRecentActivityLocked(top);
3859 ActivityOptions.abort(options);
3860 return;
3861 }
3862
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003863 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003864 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003865 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003866
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003867 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003868 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003869 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003870 if (r != null) {
3871 mNoAnimActivities.add(r);
3872 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003873 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003874 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003875 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003876 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003877
Craig Mautner05d29032013-05-03 13:40:13 -07003878 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003879 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003880
3881 if (VALIDATE_TOKENS) {
3882 validateAppTokensLocked();
3883 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003884 }
3885
3886 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003887 * Worker method for rearranging history stack. Implements the function of moving all
3888 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003889 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003890 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003891 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3892 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003893 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003894 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003895 * @return Returns true if the move completed, false if not.
3896 */
Craig Mautner299f9602015-01-26 09:47:33 -08003897 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003898 final TaskRecord tr = taskForIdLocked(taskId);
3899 if (tr == null) {
3900 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3901 return false;
3902 }
3903
3904 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003905 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003906
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003907 // If we have a watcher, preflight the move before committing to it. First check
3908 // for *other* available tasks, but if none are available, then try again allowing the
3909 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003910 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003911 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003912 if (next == null) {
3913 next = topRunningActivityLocked(null, 0);
3914 }
3915 if (next != null) {
3916 // ask watcher if this is allowed
3917 boolean moveOK = true;
3918 try {
3919 moveOK = mService.mController.activityResuming(next.packageName);
3920 } catch (RemoteException e) {
3921 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003922 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003923 }
3924 if (!moveOK) {
3925 return false;
3926 }
3927 }
3928 }
3929
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003930 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003931
riddle_hsuc215a4f2014-12-27 12:10:45 +08003932 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003933
3934 // If true, we should resume the home activity next if the task we are moving to the
3935 // back is over the home stack. We force to false if the task we are moving to back
3936 // is the home task and we don't want it resumed after moving to the back.
3937 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3938 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003939 final TaskRecord nextTask = getNextTask(tr);
3940 if (nextTask != null) {
3941 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3942 } else {
3943 prevIsHome = true;
3944 }
3945 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003946 mTaskHistory.remove(tr);
3947 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003948 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949
Craig Mautnerc8143c62013-09-03 12:15:57 -07003950 // There is an assumption that moving a task to the back moves it behind the home activity.
3951 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003952 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003953 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3954 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003955 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003956 break;
3957 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003958 if (taskNdx == 1) {
3959 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003960 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003961 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003962 }
3963
Craig Mautner299f9602015-01-26 09:47:33 -08003964 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003965 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003966
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003967 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003968 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003969 }
3970
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003971 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003972 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003973 if (!mService.mBooting && !mService.mBooted) {
3974 // Not ready yet!
3975 return false;
3976 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003977 final int taskToReturnTo = tr.getTaskToReturnTo();
3978 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003979 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003980 }
3981
Craig Mautner05d29032013-05-03 13:40:13 -07003982 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003983 return true;
3984 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003985
Craig Mautner8849a5e2013-04-02 16:41:03 -07003986 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003987 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003988 final Uri data = r.intent.getData();
3989 final String strData = data != null ? data.toSafeString() : null;
3990
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003991 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003992 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003993 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003994 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003995 }
3996
3997 /**
3998 * Make sure the given activity matches the current configuration. Returns
3999 * false if the activity had to be destroyed. Returns true if the
4000 * configuration is the same, or the activity will remain running as-is
4001 * for whatever reason. Ensures the HistoryRecord is updated with the
4002 * correct configuration and all other bookkeeping is handled.
4003 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004004 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4005 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004007 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004008 "Skipping config check (will change): " + r);
4009 return true;
4010 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004011
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004012 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004013 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004014
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004015 // Short circuit: if the two configurations are the exact same
4016 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004017 final Configuration newConfig = mService.mConfiguration;
4018 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004019 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004020 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004021 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004022 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004023 "Configuration unchanged in " + r);
4024 return true;
4025 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004026
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004027 // We don't worry about activities that are finishing.
4028 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004029 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004030 "Configuration doesn't matter in finishing " + r);
4031 r.stopFreezingScreenLocked(false);
4032 return true;
4033 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004034
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004035 // Okay we now are going to make this activity have the new config.
4036 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004037 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004038 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004039 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004040 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004041
Filip Gruszczynski23493322015-07-29 17:02:59 -07004042 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004043 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004044 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004045 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004046 "Configuration no differences in " + r);
4047 return true;
4048 }
4049
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004050 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4051 "Configuration changes for " + r + " ; taskChanges="
4052 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4053 + Configuration.configurationDiffToString(changes));
4054
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004055 // If the activity isn't currently running, just leave the new
4056 // configuration and it will pick that up next time it starts.
4057 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004058 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004059 "Configuration doesn't matter not running " + r);
4060 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004061 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004062 return true;
4063 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004064
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004065 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004066 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4067 "Checking to restart " + r.info.name + ": changed=0x"
4068 + Integer.toHexString(changes) + ", handles=0x"
4069 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4070
Dianne Hackborne6676352011-06-01 16:51:20 -07004071 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004072 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4073 r.configChangeFlags |= changes;
4074 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004075 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004076 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004077 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004078 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004079 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004080 } else if (r.state == ActivityState.PAUSING) {
4081 // A little annoying: we are waiting for this activity to
4082 // finish pausing. Let's not do anything now, but just
4083 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004084 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004085 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004086 r.configDestroy = true;
4087 return true;
4088 } else if (r.state == ActivityState.RESUMED) {
4089 // Try to optimize this case: the configuration is changing
4090 // and we need to restart the top, resumed activity.
4091 // Instead of doing the normal handshaking, just say
4092 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004093 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004094 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004095 relaunchActivityLocked(r, r.configChangeFlags, true,
4096 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004097 r.configChangeFlags = 0;
4098 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004099 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004100 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004101 relaunchActivityLocked(r, r.configChangeFlags, false,
4102 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004103 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004104 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004105
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004106 // All done... tell the caller we weren't able to keep this
4107 // activity around.
4108 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004109 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004110
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004111 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004112 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004113 // changes is always sent to all processes when they happen so it can just use whatever
4114 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004115 if (r.app != null && r.app.thread != null) {
4116 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004117 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004118 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004119 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004120 } catch (RemoteException e) {
4121 // If process died, whatever.
4122 }
4123 }
4124 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004126 return true;
4127 }
4128
Filip Gruszczynski23493322015-07-29 17:02:59 -07004129 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4130 Configuration oldTaskOverride) {
4131 // Determine what has changed. May be nothing, if this is a config
4132 // that has come back from the app after going idle. In that case
4133 // we just want to leave the official config object now in the
4134 // activity and do nothing else.
4135 int taskChanges = oldTaskOverride.diff(taskConfig);
4136 // We don't want to use size changes if they don't cross boundaries that are important to
4137 // the app.
4138 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4139 final boolean crosses = record.crossesHorizontalSizeThreshold(
4140 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4141 || record.crossesVerticalSizeThreshold(
4142 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4143 if (!crosses) {
4144 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4145 }
4146 }
4147 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4148 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4149 final int newSmallest = taskConfig.smallestScreenWidthDp;
4150 final boolean crosses = record.crossesHorizontalSizeThreshold(oldSmallest, newSmallest)
4151 || record.crossesVerticalSizeThreshold(oldSmallest, newSmallest);
4152 if (!crosses) {
4153 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4154 }
4155 }
4156 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4157 }
4158
4159 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4160 Configuration oldTaskOverride, int taskChanges) {
4161 if (taskChanges == 0) {
4162 // {@link Configuration#diff} doesn't catch changes from unset values.
4163 // Check for changes we care about.
4164 if (oldTaskOverride.orientation != taskConfig.orientation) {
4165 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4166 }
4167 // We want to explicitly track situations where the size configuration goes from
4168 // undefined to defined. We don't care about crossing the threshold in that case,
4169 // because there is no threshold.
4170 final int oldHeight = oldTaskOverride.screenHeightDp;
4171 final int newHeight = taskConfig.screenHeightDp;
4172 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4173 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4174 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4175 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4176 }
4177 final int oldWidth = oldTaskOverride.screenWidthDp;
4178 final int newWidth = taskConfig.screenWidthDp;
4179 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4180 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4181 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4182 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4183 }
4184 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4185 final int newSmallest = taskConfig.smallestScreenWidthDp;
4186 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4187 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4188 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4189 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4190 }
4191 }
4192 return taskChanges;
4193 }
4194
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004195 private static boolean isResizeOnlyChange(int change) {
4196 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4197 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4198 }
4199
Wale Ogunwale83301a92015-09-24 15:54:08 -07004200 private void relaunchActivityLocked(
4201 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4202 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4203 return;
4204 }
4205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004206 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004207 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004208 if (andResume) {
4209 results = r.results;
4210 newIntents = r.newIntents;
4211 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004212 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4213 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004214 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004215 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004216 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004217 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004218
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004219 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004220
Craig Mautner34b73df2014-01-12 21:11:08 -08004221 mStackSupervisor.removeChildActivityContainers(r);
4222
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004223 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004224 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4225 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004226 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004227 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4228 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004229 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004230 // Note: don't need to call pauseIfSleepingLocked() here, because
4231 // the caller will only pass in 'andResume' if this activity is
4232 // currently resumed, which implies we aren't sleeping.
4233 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004234 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004235 }
4236
4237 if (andResume) {
4238 r.results = null;
4239 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004240 r.state = ActivityState.RESUMED;
4241 } else {
4242 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4243 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004244 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004245 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004246
4247 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004248 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4249 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4250 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4251 final ActivityRecord r = activities.get(activityNdx);
4252 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004253 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004254 }
4255 if (r.fullscreen && !r.finishing) {
4256 return false;
4257 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004258 }
4259 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004260 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004261 if (r == null) {
4262 return false;
4263 }
4264 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4265 + " would have returned true for r=" + r);
4266 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004267 }
4268
4269 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004270 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4271 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4272 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4273 final ActivityRecord r = activities.get(activityNdx);
4274 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004275 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004276 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004277 }
4278 }
4279 }
4280
Wale Ogunwale540e1232015-05-01 15:35:39 -07004281 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4282 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004283 boolean didSomething = false;
4284 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004285 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004286 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4287 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4288 int numActivities = activities.size();
4289 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4290 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004291 final boolean sameComponent =
4292 (r.packageName.equals(packageName) && (filterByClasses == null
4293 || filterByClasses.contains(r.realActivity.getClassName())))
4294 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004295 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004296 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004297 && (r.app == null || evenPersistent || !r.app.persistent)) {
4298 if (!doit) {
4299 if (r.finishing) {
4300 // If this activity is just finishing, then it is not
4301 // interesting as far as something to stop.
4302 continue;
4303 }
4304 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004305 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004306 if (r.isHomeActivity()) {
4307 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4308 Slog.i(TAG, "Skip force-stop again " + r);
4309 continue;
4310 } else {
4311 homeActivity = r.realActivity;
4312 }
4313 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004314 didSomething = true;
4315 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004316 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004317 if (r.app != null) {
4318 r.app.removed = true;
4319 }
4320 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004321 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004322 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004323 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4324 true)) {
4325 // r has been deleted from mActivities, accommodate.
4326 --numActivities;
4327 --activityNdx;
4328 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004329 }
4330 }
4331 }
4332 return didSomething;
4333 }
4334
Dianne Hackborn09233282014-04-30 11:33:59 -07004335 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004336 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4337 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004338 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004339 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004340 if (task.getTopActivity() == null) {
4341 continue;
4342 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004343 ActivityRecord r = null;
4344 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004345 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004346 int numActivities = 0;
4347 int numRunning = 0;
4348 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004349 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004350 continue;
4351 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004352 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004353 tmp = activities.get(activityNdx);
4354 if (tmp.finishing) {
4355 continue;
4356 }
4357 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004358
Craig Mautneraab647e2013-02-28 16:31:36 -08004359 // Initialize state for next task if needed.
4360 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4361 top = r;
4362 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004363 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004364
4365 // Add 'r' into the current task.
4366 numActivities++;
4367 if (r.app != null && r.app.thread != null) {
4368 numRunning++;
4369 }
4370
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004371 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004372 TAG, r.intent.getComponent().flattenToShortString()
4373 + ": task=" + r.task);
4374 }
4375
4376 RunningTaskInfo ci = new RunningTaskInfo();
4377 ci.id = task.taskId;
4378 ci.baseActivity = r.intent.getComponent();
4379 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004380 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004381 if (focusedStack && topTask) {
4382 // Give the latest time to ensure foreground task can be sorted
4383 // at the first, because lastActiveTime of creating task is 0.
4384 ci.lastActiveTime = System.currentTimeMillis();
4385 topTask = false;
4386 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004387
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004388 if (top.task != null) {
4389 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004390 }
4391 ci.numActivities = numActivities;
4392 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004393 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004394 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004395 }
4396
4397 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004398 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004399 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004400 if (top >= 0) {
4401 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4402 int activityTop = activities.size() - 1;
4403 if (activityTop > 0) {
4404 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4405 "unhandled-back", true);
4406 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004407 }
4408 }
4409
Craig Mautner6b74cb52013-09-27 17:02:21 -07004410 /**
4411 * Reset local parameters because an app's activity died.
4412 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004413 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004414 */
4415 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004416 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004417 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004418 "App died while pausing: " + mPausingActivity);
4419 mPausingActivity = null;
4420 }
4421 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4422 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004423 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004424 }
4425
Craig Mautner19091252013-10-05 00:03:53 -07004426 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004427 }
4428
Craig Mautnercae015f2013-02-08 14:31:27 -08004429 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004430 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4431 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4432 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4433 final ActivityRecord r = activities.get(activityNdx);
4434 if (r.app == app) {
4435 Slog.w(TAG, " Force finishing activity "
4436 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004437 // Force the destroy to skip right to removal.
4438 r.app = null;
4439 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004440 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004441 }
4442 }
4443 }
4444
Dianne Hackborn390517b2013-05-30 15:03:32 -07004445 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004446 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004447 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004448 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4449 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004450 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4451 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004452 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004453 " Task id #" + task.taskId + "\n" +
4454 " mFullscreen=" + task.mFullscreen + "\n" +
4455 " mBounds=" + task.mBounds + "\n" +
4456 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004457 if (printed) {
4458 header = null;
4459 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004460 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004461 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004462 }
4463
4464 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4465 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4466
4467 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4469 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004470 }
4471 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004472 final int top = mTaskHistory.size() - 1;
4473 if (top >= 0) {
4474 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4475 int listTop = list.size() - 1;
4476 if (listTop >= 0) {
4477 activities.add(list.get(listTop));
4478 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004479 }
4480 } else {
4481 ItemMatcher matcher = new ItemMatcher();
4482 matcher.build(name);
4483
Craig Mautneraab647e2013-02-28 16:31:36 -08004484 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4485 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4486 if (matcher.match(r1, r1.intent.getComponent())) {
4487 activities.add(r1);
4488 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004489 }
4490 }
4491 }
4492
4493 return activities;
4494 }
4495
4496 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004497 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004498
4499 // All activities that came from the package must be
4500 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004501 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4502 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4503 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4504 final ActivityRecord a = activities.get(activityNdx);
4505 if (a.info.packageName.equals(packageName)) {
4506 a.forceNewConfig = true;
4507 if (starting != null && a == starting && a.visible) {
4508 a.startFreezingScreenLocked(starting.app,
4509 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4510 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004511 }
4512 }
4513 }
4514
4515 return starting;
4516 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004517
Craig Mautner299f9602015-01-26 09:47:33 -08004518 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004519 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004520 }
4521
Wale Ogunwale000957c2015-04-03 08:19:12 -07004522 /**
4523 * Removes the input task from this stack.
4524 * @param task to remove.
4525 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004526 * @param moving task to another stack. In the case we are moving we don't want to perform
4527 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004528 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004529 void removeTask(TaskRecord task, String reason, boolean moving) {
4530 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004531 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004532 mWindowManager.removeTask(task.taskId);
4533 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004534
Craig Mautner04a0ea62014-01-13 12:51:26 -08004535 final ActivityRecord r = mResumedActivity;
4536 if (r != null && r.task == task) {
4537 mResumedActivity = null;
4538 }
4539
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004540 final int taskNdx = mTaskHistory.indexOf(task);
4541 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004542 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4543 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4544 if (!nextTask.isOverHomeStack()) {
4545 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4546 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004547 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004548 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004549 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004550
Wale Ogunwale040b4702015-08-06 18:10:50 -07004551 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004552 final boolean isVoiceSession = task.voiceSession != null;
4553 if (isVoiceSession) {
4554 try {
4555 task.voiceSession.taskFinished(task.intent, task.taskId);
4556 } catch (RemoteException e) {
4557 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004558 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004559 if (task.autoRemoveFromRecents() || isVoiceSession) {
4560 // Task creator asked to remove this when done, or this task was a voice
4561 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004562 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004563 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004564 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004565 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004566
4567 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004568 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004569 // We only need to adjust focused stack if this stack is in focus.
4570 if (isOnHomeDisplay() && mStackSupervisor.isFrontStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004571 String myReason = reason + " leftTaskHistoryEmpty";
4572 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004573 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004574 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004575 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004576 if (mStacks != null) {
4577 mStacks.remove(this);
4578 mStacks.add(0, this);
4579 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004580 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004581 mActivityContainer.onTaskListEmptyLocked();
4582 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004583 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004584
4585 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004586 }
4587
Dianne Hackborn91097de2014-04-04 18:02:06 -07004588 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4589 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4590 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004591 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4592 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004593 // add the task to stack first, mTaskPositioner might need the stack association
4594 addTask(task, toTop, false);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004595 if (mTaskPositioner != null) {
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004596 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, info.layout);
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004597 } else if (mBounds != null && task.mResizeable) {
4598 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004599 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004600 return task;
4601 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004602
4603 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004604 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004605 }
4606
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004607 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004608 task.stack = this;
4609 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004610 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004611 } else {
4612 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004613 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004614 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004615 if (!moving && task.voiceSession != null) {
4616 try {
4617 task.voiceSession.taskStarted(task.intent, task.taskId);
4618 } catch (RemoteException e) {
4619 }
4620 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004621 }
4622
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004623 void positionTask(final TaskRecord task, int position, boolean moving) {
4624 task.stack = this;
4625 insertTaskAtPosition(task, position);
4626 if (!moving && task.voiceSession != null) {
4627 try {
4628 task.voiceSession.taskStarted(task.intent, task.taskId);
4629 } catch (RemoteException e) {
4630 }
4631 }
4632 }
4633
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004634 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004635 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004636 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004637 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4638 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4639 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4640 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004641 bounds, task.mOverrideConfig, !r.isHomeActivity());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004642 r.taskConfigOverride = task.mOverrideConfig;
4643 }
4644
Wale Ogunwale079a0042015-10-24 11:44:07 -07004645 void setFocusAndResumeStateIfNeeded(
4646 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4647 // If the activity had focus before move focus to this stack.
4648 if (setFocus) {
4649 // If the activity owns the last resumed activity, transfer that together,
4650 // so that we don't resume the same activity again in the new stack.
4651 // Apps may depend on onResume()/onPause() being called in pairs.
4652 if (setResume) {
4653 mResumedActivity = r;
4654 // Move the stack in which we are placing the activity to the front. We don't use
4655 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4656 // already focused, the call will short-circuit and do nothing.
4657 moveToFront(reason);
4658 } else {
4659 // We need to not only move the stack to the front, but also have the activity
4660 // focused. This will achieve both goals.
4661 mService.setFocusedActivityLocked(r, reason);
4662 }
4663 }
4664 }
4665
4666 /**
4667 * Moves the input activity from its current stack to this one.
4668 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4669 * created on this stack which the activity is added to.
4670 * */
4671 void moveActivityToStack(ActivityRecord r) {
4672 final ActivityStack prevStack = r.task.stack;
4673 if (prevStack.mStackId == mStackId) {
4674 // You are already in the right stack silly...
4675 return;
4676 }
4677
4678 final boolean wasFocused = mStackSupervisor.isFrontStack(prevStack)
4679 && (mStackSupervisor.topRunningActivityLocked() == r);
4680 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4681
4682 final TaskRecord task = createTaskRecord(
4683 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4684 r.setTask(task, null);
4685 task.addActivityToTop(r);
4686 setAppTask(r, task);
4687 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4688 }
4689
Wale Ogunwale706ed792015-08-02 10:29:44 -07004690 private void setAppTask(ActivityRecord r, TaskRecord task) {
4691 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004692 task.updateOverrideConfiguration(bounds);
4693 mWindowManager.setAppTask(
4694 r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004695 r.taskConfigOverride = task.mOverrideConfig;
4696 }
4697
Craig Mautnerc00204b2013-03-05 15:02:14 -08004698 public int getStackId() {
4699 return mStackId;
4700 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004701
4702 @Override
4703 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004704 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4705 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004706 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004707
Craig Mautner15df08a2015-04-01 12:17:18 -07004708 void onLockTaskPackagesUpdatedLocked() {
4709 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4710 mTaskHistory.get(taskNdx).setLockTaskAuth();
4711 }
4712 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004713}