blob: b4a8381a8201c0a34832280d306b25f487fc0ebf [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 Ogunwale6dfdfd62015-04-15 12:01:38 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070026
Wale Ogunwalee23149f2015-03-06 15:39:44 -080027import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070031
Wale Ogunwale040b4702015-08-06 18:10:50 -070032import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070033import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070034
Wale Ogunwale706ed792015-08-02 10:29:44 -070035import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070036import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070037
Dianne Hackborn91097de2014-04-04 18:02:06 -070038import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080039import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070050import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080052import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080054import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080060import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080061import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070063import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080064import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Handler;
66import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090067import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070069import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.RemoteException;
71import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070074import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070077import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import java.lang.ref.WeakReference;
82import java.util.ArrayList;
83import java.util.Iterator;
84import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080085import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070086import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087
88/**
89 * State and management of a single stack of activities.
90 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070091final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080092
Wale Ogunwalee23149f2015-03-06 15:39:44 -080093 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070094 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
95 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080096 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070097 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070098 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070099 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700100 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700101 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700102 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
103 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700104 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700105 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700106 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
107 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
108 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
109 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
110 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700111
112 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800113
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700114 // Ticks during which we check progress while waiting for an app to launch.
115 static final int LAUNCH_TICK = 500;
116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // How long we wait until giving up on the last activity to pause. This
118 // is short because it directly impacts the responsiveness of starting the
119 // next activity.
120 static final int PAUSE_TIMEOUT = 500;
121
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700122 // How long we wait for the activity to tell us it has stopped before
123 // giving up. This is a good amount of time because we really need this
124 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700125 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 // How long we wait until giving up on an activity telling us it has
128 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700129 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800130
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700131 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800132 // disabled.
133 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800134
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700135 // How long between activity launches that we consider safe to not warn
136 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700137 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800138
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700139 // Set to false to disable the preview that is shown while a new activity
140 // is being started.
141 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800142
Craig Mautner5eda9b32013-07-02 11:58:16 -0700143 // How long to wait for all background Activities to redraw following a call to
144 // convertToTranslucent().
145 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
146
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700147 enum ActivityState {
148 INITIALIZING,
149 RESUMED,
150 PAUSING,
151 PAUSED,
152 STOPPING,
153 STOPPED,
154 FINISHING,
155 DESTROYING,
156 DESTROYED
157 }
158
159 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700160 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800161 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800162
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700163 /**
164 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800165 * running) activities. It contains #TaskRecord objects.
166 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800167 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800168
169 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800170 * Used for validating app tokens with window manager.
171 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800172 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 /**
175 * List of running activities, sorted by recent usage.
176 * The first entry in the list is the least recently used.
177 * It contains HistoryRecord objects.
178 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800179 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180
181 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700182 * Animations that for the current transition have requested not to
183 * be considered for the transition animation.
184 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800185 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700186
187 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700188 * When we are in the process of pausing an activity, before starting the
189 * next one, this variable holds the activity that is currently being paused.
190 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800191 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192
193 /**
194 * This is the last activity that we put into the paused state. This is
195 * used to determine if we need to do an activity transition while sleeping,
196 * when we normally hold the top activity paused.
197 */
198 ActivityRecord mLastPausedActivity = null;
199
200 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700201 * Activities that specify No History must be removed once the user navigates away from them.
202 * If the device goes to sleep with such an activity in the paused state then we save it here
203 * and finish it later if another activity replaces it on wakeup.
204 */
205 ActivityRecord mLastNoHistoryActivity = null;
206
207 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700208 * Current activity that is resumed, or null if there is none.
209 */
210 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800211
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700212 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700213 * This is the last activity that has been started. It is only used to
214 * identify when multiple activities are started at once so that the user
215 * can be warned they may not be in the activity they think they are.
216 */
217 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800218
Craig Mautner5eda9b32013-07-02 11:58:16 -0700219 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
220 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
221 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
222 // Activity in mTranslucentActivityWaiting is notified via
223 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
224 // background activity being drawn then the same call will be made with a true value.
225 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700226 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700227
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700228 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700229 * Set when we know we are going to be calling updateConfiguration()
230 * soon, so want to skip intermediate config checks.
231 */
232 boolean mConfigWillChange;
233
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700234 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800235 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700236 // Current bounds of the stack or null if fullscreen.
237 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800238
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700239 long mLaunchStartTime = 0;
240 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800241
Craig Mautner858d8a62013-04-23 17:08:34 -0700242 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700243
Craig Mautnerc00204b2013-03-05 15:02:14 -0800244 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800245 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800246 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
247 ArrayList<ActivityStack> mStacks;
248 /** The attached Display's unique identifier, or -1 if detached */
249 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800250
Craig Mautner27084302013-03-25 08:05:25 -0700251 /** Run all ActivityStacks through this */
252 final ActivityStackSupervisor mStackSupervisor;
253
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700254 private final LaunchingTaskPositioner mTaskPositioner;
255
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700256 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700257 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
258 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
259 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
260 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700261 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700262 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700263 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700264
265 static class ScheduleDestroyArgs {
266 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700267 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700268 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700269 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700270 mReason = reason;
271 }
272 }
273
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 final Handler mHandler;
275
276 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800277
Craig Mautnerc8143c62013-09-03 12:15:57 -0700278 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900279 super(looper);
280 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700281
Zoran Marcetaf958b322012-08-09 20:27:12 +0900282 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700283 public void handleMessage(Message msg) {
284 switch (msg.what) {
285 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800286 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 // We don't at this point know if the activity is fullscreen,
288 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800289 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700290 synchronized (mService) {
291 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700292 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700293 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700294 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800295 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700297 case LAUNCH_TICK_MSG: {
298 ActivityRecord r = (ActivityRecord)msg.obj;
299 synchronized (mService) {
300 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700301 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700302 }
303 }
304 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800306 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700307 // We don't at this point know if the activity is fullscreen,
308 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800309 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800310 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800311 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800312 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700313 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700314 case STOP_TIMEOUT_MSG: {
315 ActivityRecord r = (ActivityRecord)msg.obj;
316 // We don't at this point know if the activity is fullscreen,
317 // so we need to be conservative and assume it isn't.
318 Slog.w(TAG, "Activity stop timeout for " + r);
319 synchronized (mService) {
320 if (r.isInHistory()) {
321 activityStoppedLocked(r, null, null, null);
322 }
323 }
324 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700325 case DESTROY_ACTIVITIES_MSG: {
326 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
327 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700328 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700329 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700330 } break;
331 case TRANSLUCENT_TIMEOUT_MSG: {
332 synchronized (mService) {
333 notifyActivityDrawnLocked(null);
334 }
335 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700336 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700337 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700338 final ActivityRecord r = getVisibleBehindActivity();
339 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700340 if (r != null) {
341 mService.killAppAtUsersRequest(r.app, null);
342 }
343 }
344 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700345 }
346 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800347 }
348
Craig Mautner34b73df2014-01-12 21:11:08 -0800349 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800350 int count = 0;
351 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
352 count += mTaskHistory.get(taskNdx).mActivities.size();
353 }
354 return count;
355 }
356
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800357 int numTasks() {
358 return mTaskHistory.size();
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;
369 mCurrentUser = mService.mCurrentUserId;
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;
373 }
374
375 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
376 mDisplayId = activityDisplay.mDisplayId;
377 mStacks = activityDisplay.mStacks;
378 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
379 mFullscreen = mBounds == null;
380 if (mTaskPositioner != null) {
381 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
382 mTaskPositioner.configure(mBounds);
383 }
384 }
385
386 void detachDisplay() {
387 mDisplayId = Display.INVALID_DISPLAY;
388 mStacks = null;
389 if (mTaskPositioner != null) {
390 mTaskPositioner.reset();
391 }
392 mWindowManager.detachStack(mStackId);
393 }
394
395 void setBounds(Rect bounds) {
396 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700397 if (mTaskPositioner != null) {
398 mTaskPositioner.configure(bounds);
399 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 }
Craig Mautner5962b122012-10-05 14:45:52 -0700401
Amith Yamasani734983f2014-03-04 16:48:05 -0800402 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700403 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700404 }
405
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700406 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700408 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700409 if (r != null) {
410 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800411 }
412 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700413 return null;
414 }
415
416 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800417 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
418 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800419 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800420 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
421 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800422 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800423 return r;
424 }
425 }
426 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700427 return null;
428 }
429
430 /**
431 * This is a simplified version of topRunningActivityLocked that provides a number of
432 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800433 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700434 * @param token If non-null, any history records matching this token will be skipped.
435 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800436 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700437 * @return Returns the HistoryRecord of the next activity on the stack.
438 */
439 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800440 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
441 TaskRecord task = mTaskHistory.get(taskNdx);
442 if (task.taskId == taskId) {
443 continue;
444 }
445 ArrayList<ActivityRecord> activities = task.mActivities;
446 for (int i = activities.size() - 1; i >= 0; --i) {
447 final ActivityRecord r = activities.get(i);
448 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800449 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800450 return r;
451 }
452 }
453 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700454 return null;
455 }
456
Craig Mautner8849a5e2013-04-02 16:41:03 -0700457 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700458 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
459 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700460 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
461 final ActivityRecord r = activities.get(activityNdx);
462 if (!r.finishing) {
463 return r;
464 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700465 }
466 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700467 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700468 }
469
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700470 final TaskRecord topTask() {
471 final int size = mTaskHistory.size();
472 if (size > 0) {
473 return mTaskHistory.get(size - 1);
474 }
475 return null;
476 }
477
Craig Mautnerd2328952013-03-05 12:46:26 -0800478 TaskRecord taskForIdLocked(int id) {
479 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
480 final TaskRecord task = mTaskHistory.get(taskNdx);
481 if (task.taskId == id) {
482 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800483 }
484 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700485 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700486 }
487
Craig Mautnerd2328952013-03-05 12:46:26 -0800488 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700489 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800490 return isInStackLocked(r);
491 }
492
493 ActivityRecord isInStackLocked(ActivityRecord r) {
494 if (r == null) {
495 return null;
496 }
497 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700498 if (task != null && task.stack != null
499 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800500 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800501 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800502 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800503 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800504 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800505 }
506
Craig Mautner2420ead2013-04-01 17:13:20 -0700507 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700508 final boolean hadit = mLRUActivities.remove(r);
509 mLRUActivities.add(r);
510 return hadit;
511 }
512
Craig Mautnerde4ef022013-04-07 19:01:33 -0700513 final boolean isHomeStack() {
514 return mStackId == HOME_STACK_ID;
515 }
516
Craig Mautnere0a38842013-12-16 16:14:02 -0800517 final boolean isOnHomeDisplay() {
518 return isAttached() &&
519 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
520 }
521
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700522 void moveToFront(String reason) {
523 moveToFront(reason, null);
524 }
525
526 /**
527 * @param reason The reason for moving the stack to the front.
528 * @param task If non-null, the task will be moved to the top of the stack.
529 * */
530 void moveToFront(String reason, TaskRecord task) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800531 if (isAttached()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700532 final ActivityStack lastFocusStack = mStacks.get(mStacks.size() - 1);
533 // Need to move this stack to the front before calling
534 // {@link ActivityStackSupervisor#setFocusStack} below.
535 mStacks.remove(this);
536 mStacks.add(this);
537
538 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800539 if (isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700540 mStackSupervisor.setFocusStack(reason, lastFocusStack);
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800541 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700542 if (task != null) {
543 insertTaskAtTop(task, null);
544 } else {
545 task = topTask();
546 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800547 if (task != null) {
548 mWindowManager.moveTaskToTop(task.taskId);
549 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800551 }
552
553 final boolean isAttached() {
554 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800555 }
556
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700557 /**
558 * Returns the top activity in any existing task matching the given
559 * Intent. Returns null if no such task is found.
560 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700561 ActivityRecord findTaskLocked(ActivityRecord target) {
562 Intent intent = target.intent;
563 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700564 ComponentName cls = intent.getComponent();
565 if (info.targetActivity != null) {
566 cls = new ComponentName(info.packageName, info.targetActivity);
567 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700568 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700569 boolean isDocument = intent != null & intent.isDocument();
570 // If documentData is non-null then it must match the existing task data.
571 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800572
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700573 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800574 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
575 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700576 if (task.voiceSession != null) {
577 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700578 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700579 continue;
580 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700581 if (task.userId != userId) {
582 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700584 continue;
585 }
Craig Mautner000f0022013-02-26 15:04:29 -0800586 final ActivityRecord r = task.getTopActivity();
587 if (r == null || r.finishing || r.userId != userId ||
588 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800590 continue;
591 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700592 if (r.mActivityType != target.mActivityType) {
593 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
594 continue;
595 }
Craig Mautner000f0022013-02-26 15:04:29 -0800596
Craig Mautnerd00f4742014-03-12 14:17:26 -0700597 final Intent taskIntent = task.intent;
598 final Intent affinityIntent = task.affinityIntent;
599 final boolean taskIsDocument;
600 final Uri taskDocumentData;
601 if (taskIntent != null && taskIntent.isDocument()) {
602 taskIsDocument = true;
603 taskDocumentData = taskIntent.getData();
604 } else if (affinityIntent != null && affinityIntent.isDocument()) {
605 taskIsDocument = true;
606 taskDocumentData = affinityIntent.getData();
607 } else {
608 taskIsDocument = false;
609 taskDocumentData = null;
610 }
611
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700612 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700613 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700614 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700615 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700616 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
617 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700618 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619 return r;
620 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700621 } else if (taskIntent != null && taskIntent.getComponent() != null &&
622 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700623 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700624 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800625 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700626 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
627 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800628 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700629 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
630 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700631 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700632 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800633 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700634 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
635 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800636 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700637 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700638 }
639
640 return null;
641 }
642
643 /**
644 * Returns the first activity (starting from the top of the stack) that
645 * is the same as the given activity. Returns null if no such activity
646 * is found.
647 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700648 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700649 ComponentName cls = intent.getComponent();
650 if (info.targetActivity != null) {
651 cls = new ComponentName(info.packageName, info.targetActivity);
652 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700653 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700654
Craig Mautner000f0022013-02-26 15:04:29 -0800655 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700656 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700657 final boolean notCurrentUserTask =
658 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700659 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700660
Craig Mautner000f0022013-02-26 15:04:29 -0800661 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
662 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700663 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700664 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700665 }
Craig Mautner000f0022013-02-26 15:04:29 -0800666 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700667 return r;
668 }
669 }
670 }
671
672 return null;
673 }
674
Amith Yamasani742a6712011-05-04 14:49:28 -0700675 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700676 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700677 */
Craig Mautner93529a42013-10-04 15:03:13 -0700678 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800679 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700680 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800681 }
682 mCurrentUser = userId;
683
684 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800685 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800686 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700687 final TaskRecord task = mTaskHistory.get(i);
688
689 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
690 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700691 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700692 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700693 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700694 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 mTaskHistory.remove(i);
696 mTaskHistory.add(task);
697 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800698 // Use same value for i.
699 } else {
700 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800701 }
702 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700703 if (VALIDATE_TOKENS) {
704 validateAppTokensLocked();
705 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700706 }
707
Craig Mautner2420ead2013-04-01 17:13:20 -0700708 void minimalResumeActivityLocked(ActivityRecord r) {
709 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700710 if (DEBUG_STATES) Slog.v(TAG_STATES,
711 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700712 r.stopped = false;
713 mResumedActivity = r;
714 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800715 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700716 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700717 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700718 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700719 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
720 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700721 }
722
Chong Zhang45c25ce2015-08-10 22:18:26 -0700723 private void addRecentActivityLocked(ActivityRecord r) {
724 if (r != null) {
725 mRecentTasks.addLocked(r.task);
726 r.task.touchActiveTime();
727 }
728 }
729
Narayan Kamath7829c812015-06-08 17:39:43 +0100730 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700731 if (mFullyDrawnStartTime != 0) {
732 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
733 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100734 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700735 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
736 }
737
738 private void stopFullyDrawnTraceIfNeeded() {
739 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
740 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
741 mFullyDrawnStartTime = 0;
742 }
743 }
744
Craig Mautnere79d42682013-04-01 19:01:53 -0700745 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700746 if (r.displayStartTime == 0) {
747 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
748 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100749 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700750 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700751 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700752 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100753 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700754 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700756 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800757
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700758 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700759 // Make sure that there is no activity waiting for this to launch.
760 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
761 r.displayStartTime = r.fullyDrawnStartTime = 0;
762 } else {
763 mStackSupervisor.removeTimeoutsForActivityLocked(r);
764 mStackSupervisor.scheduleIdleTimeoutLocked(r);
765 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700766 }
767
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800768 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800769 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800770 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
771 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
772 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
773 activities.get(activityNdx).setSleeping(false);
774 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800775 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800776 if (mPausingActivity != null) {
777 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
778 activityPausedLocked(mPausingActivity.appToken, true);
779 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800780 }
781
Craig Mautner0eea92c2013-05-16 13:35:39 -0700782 /**
783 * @return true if something must be done before going to sleep.
784 */
785 boolean checkReadyForSleepLocked() {
786 if (mResumedActivity != null) {
787 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700788 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
789 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
790 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700791 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700792 return true;
793 }
794 if (mPausingActivity != null) {
795 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700796 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700797 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800798 }
799
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700800 if (hasVisibleBehindActivity()) {
801 // Stop visible behind activity before going to sleep.
802 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
803 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700804 if (DEBUG_STATES) Slog.v(TAG_STATES,
805 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700806 return true;
807 }
808
Craig Mautner0eea92c2013-05-16 13:35:39 -0700809 return false;
810 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800811
Craig Mautner0eea92c2013-05-16 13:35:39 -0700812 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700813 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800814
Craig Mautner0eea92c2013-05-16 13:35:39 -0700815 // Make sure any stopped but visible activities are now sleeping.
816 // This ensures that the activity's onStop() is called.
817 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
818 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
819 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
820 final ActivityRecord r = activities.get(activityNdx);
821 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
822 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800823 }
824 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800825 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 }
Craig Mautner59c00972012-07-30 12:10:24 -0700827
Dianne Hackbornd2835932010-12-13 16:28:46 -0800828 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700829 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800830 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700831 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800832 return null;
833 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800834
Winson Chung083baf92014-07-11 10:32:42 -0700835 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700836 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800837 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700838 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700839 return null;
840 }
841
Winson Chung48a10a52014-08-27 14:36:51 -0700842 int w = mService.mThumbnailWidth;
843 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800844 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700845 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800846 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700847 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800848 }
Winson Chung376543b2014-05-21 17:43:28 -0700849 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800850 return null;
851 }
852
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700853 /**
854 * Start pausing the currently resumed activity. It is an error to call this if there
855 * is already an activity being paused or there is no resumed activity.
856 *
857 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
858 * @param uiSleeping True if this is happening with the user interface going to sleep (the
859 * screen turning off).
860 * @param resuming True if this is being called as part of resuming the top activity, so
861 * we shouldn't try to instigate a resume here.
862 * @param dontWait True if the caller does not want to wait for the pause to complete. If
863 * set to true, we will immediately complete the pause here before returning.
864 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
865 * it to tell us when it is done.
866 */
867 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
868 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800869 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800870 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
871 + " state=" + mPausingActivity.state);
872 if (!mService.isSleeping()) {
873 // Avoid recursion among check for sleep and complete pause during sleeping.
874 // Because activity will be paused immediately after resume, just let pause
875 // be completed by the order of activity paused from clients.
876 completePauseLocked(false);
877 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800878 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 ActivityRecord prev = mResumedActivity;
880 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700881 if (!resuming) {
882 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
883 mStackSupervisor.resumeTopActivitiesLocked();
884 }
885 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800887
888 if (mActivityContainer.mParentActivity == null) {
889 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700890 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800891 }
892
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700893 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700894 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700895 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800896 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700897 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700898 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
899 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 prev.state = ActivityState.PAUSING;
901 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700902 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700903 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700904 if (mService.mHasRecents
905 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800906 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700907 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700908 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909
910 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800911
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 try {
915 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700916 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700918 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800919 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700920 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921 } catch (Exception e) {
922 // Ignore exception, if process died other code will cleanup.
923 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800924 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700926 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 }
928 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800929 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700930 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700931 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
933
934 // If we are not going to sleep, we want to ensure the device is
935 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700936 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700937 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
939
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800940 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 // Have the window manager pause its key dispatching until the new
942 // activity has started. If we're pausing the activity just because
943 // the screen is being turned off and the UI is sleeping, don't interrupt
944 // key dispatch; the same activity will pick it up again on wakeup.
945 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800946 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700947 } else if (DEBUG_PAUSE) {
948 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 }
950
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700951 if (dontWait) {
952 // If the caller said they don't want to wait for the pause, then complete
953 // the pause now.
954 completePauseLocked(false);
955 return false;
956
957 } else {
958 // Schedule a pause timeout in case the app doesn't respond.
959 // We don't give it much time because this directly impacts the
960 // responsiveness seen by the user.
961 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
962 msg.obj = prev;
963 prev.pauseTime = SystemClock.uptimeMillis();
964 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700965 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700966 return true;
967 }
968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700969 } else {
970 // This activity failed to schedule the
971 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700972 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700973 if (!resuming) {
974 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
975 }
976 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700977 }
978 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700979
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700980 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700981 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
982 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983
Craig Mautnerd2328952013-03-05 12:46:26 -0800984 final ActivityRecord r = isInStackLocked(token);
985 if (r != null) {
986 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
987 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700988 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -0800989 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700990 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800991 } else {
992 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
993 r.userId, System.identityHashCode(r), r.shortComponentName,
994 mPausingActivity != null
995 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +0800996 if (r.finishing && r.state == ActivityState.PAUSING) {
997 if (DEBUG_PAUSE) Slog.v(TAG,
998 "Executing finish of failed to pause activity: " + r);
999 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1000 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001001 }
1002 }
1003 }
1004
Craig Mautnera0026042014-04-23 11:45:37 -07001005 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1006 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001007 if (r.state != ActivityState.STOPPING) {
1008 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1009 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1010 return;
1011 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001012 if (persistentState != null) {
1013 r.persistentState = persistentState;
1014 mService.notifyTaskPersisterLocked(r.task, false);
1015 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001016 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001017 if (icicle != null) {
1018 // If icicle is null, this is happening due to a timeout, so we
1019 // haven't really saved the state.
1020 r.icicle = icicle;
1021 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001022 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001023 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001024 }
1025 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001026 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001027 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1028 r.stopped = true;
1029 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001030 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1031 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001032 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001033 if (r.finishing) {
1034 r.clearOptionsLocked();
1035 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001036 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001037 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001038 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001039 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001040 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001041 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001042 }
1043 }
1044 }
1045
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001046 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001047 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001048 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001049
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001050 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001051 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001052 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001053 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001054 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001055 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001056 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001057 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001058 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1059 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001060 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001061 if (prev.configDestroy) {
1062 // The previous is being paused because the configuration
1063 // is changing, which means it is actually stopping...
1064 // To juggle the fact that we are also starting a new
1065 // instance right now, we need to first completely stop
1066 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001067 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001068 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001069 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001070 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001071 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001072 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001073 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1074 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001075 // If we already have a few activities waiting to stop,
1076 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001077 // them out. Or if r is the last of activity of the last task the stack
1078 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001080 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001081 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001082 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001083 }
1084 }
1085 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001086 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001087 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001088 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001089 // It is possible the activity was freezing the screen before it was paused.
1090 // In that case go ahead and remove the freeze this activity has on the screen
1091 // since it is no longer visible.
1092 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001093 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001095
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001096 if (resumeNext) {
1097 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1098 if (!mService.isSleepingOrShuttingDown()) {
1099 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1100 } else {
1101 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001102 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001103 if (top == null || (prev != null && top != prev)) {
1104 // If there are no more activities available to run,
1105 // do resume anyway to start something. Also if the top
1106 // activity on the stack is not the just paused activity,
1107 // we need to go ahead and resume it to ensure we complete
1108 // an in-flight app switch.
1109 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1110 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001111 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001112 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001113
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001114 if (prev != null) {
1115 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116
Craig Mautner525f3d92013-05-07 14:01:50 -07001117 if (prev.app != null && prev.cpuTimeAtResume > 0
1118 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001119 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1120 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001121 if (diff > 0) {
1122 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1123 synchronized (bsi) {
1124 BatteryStatsImpl.Uid.Proc ps =
1125 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001126 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001127 if (ps != null) {
1128 ps.addForegroundTimeLocked(diff);
1129 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001130 }
1131 }
1132 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001133 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001135
1136 // Notfiy when the task stack has changed
1137 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 }
1139
1140 /**
1141 * Once we know that we have asked an application to put an activity in
1142 * the resumed state (either by launching it or explicitly telling it),
1143 * this function updates the rest of our state to match that fact.
1144 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001145 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 next.idle = false;
1147 next.results = null;
1148 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001149
Chong Zhang85ee6542015-10-02 13:36:38 -07001150 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001151 ProcessRecord app = next.task.mActivities.get(0).app;
1152 if (app != null && app != mService.mHomeProcess) {
1153 mService.mHomeProcess = app;
1154 }
1155 }
1156
Craig Mautner07566322013-09-26 16:42:55 -07001157 if (next.nowVisible) {
1158 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001159 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001160 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001161
1162 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001163 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001165 mStackSupervisor.reportResumedActivityLocked(next);
1166
1167 next.resumeKeyDispatchingLocked();
1168 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001169
1170 // Mark the point when the activity is resuming
1171 // TODO: To be more accurate, the mark should be before the onCreate,
1172 // not after the onResume. But for subsequent starts, onResume is fine.
1173 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001174 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 } else {
1176 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1177 }
Winson Chung376543b2014-05-21 17:43:28 -07001178
George Mount6ba042b2014-07-28 11:12:28 -07001179 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001180
1181 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1182 // When resuming an activity, require it to call requestVisibleBehind() again.
1183 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1184 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185 }
1186
Craig Mautner2568c3a2015-03-26 14:22:34 -07001187 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001188 r.visible = visible;
1189 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001190 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001191 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001192 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001193 container.setVisible(visible);
1194 }
1195 }
1196
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001197 // Find the first visible activity above the passed activity and if it is translucent return it
1198 // otherwise return null;
1199 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1200 TaskRecord task = r.task;
1201 if (task == null) {
1202 return null;
1203 }
1204
1205 ActivityStack stack = task.stack;
1206 if (stack == null) {
1207 return null;
1208 }
1209
1210 int stackNdx = mStacks.indexOf(stack);
1211
1212 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1213 int taskNdx = tasks.indexOf(task);
1214
1215 ArrayList<ActivityRecord> activities = task.mActivities;
1216 int activityNdx = activities.indexOf(r) + 1;
1217
1218 final int numStacks = mStacks.size();
1219 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001220 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001221 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001222 final int numTasks = tasks.size();
1223 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001224 final TaskRecord currentTask = tasks.get(taskNdx);
1225 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001226 final int numActivities = activities.size();
1227 while (activityNdx < numActivities) {
1228 final ActivityRecord activity = activities.get(activityNdx);
1229 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001230 return historyStack.mFullscreen
1231 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001232 }
1233 ++activityNdx;
1234 }
1235 activityNdx = 0;
1236 ++taskNdx;
1237 }
1238 taskNdx = 0;
1239 ++stackNdx;
1240 }
1241
1242 return null;
1243 }
1244
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001245 private ActivityStack getNextVisibleStackLocked() {
1246 ArrayList<ActivityStack> stacks = mStacks;
1247 final ActivityRecord parent = mActivityContainer.mParentActivity;
1248 if (parent != null) {
1249 stacks = parent.task.stack.mStacks;
1250 }
1251 if (stacks != null) {
1252 for (int i = stacks.size() - 1; i >= 0; --i) {
1253 ActivityStack stack = stacks.get(i);
1254 if (stack != this && stack.isStackVisibleLocked()) {
1255 return stack;
1256 }
1257 }
1258 }
1259 return null;
1260 }
1261
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001262 /** Returns true if the stack contains a fullscreen task. */
1263 private boolean hasFullscreenTask() {
1264 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1265 final TaskRecord task = mTaskHistory.get(i);
1266 if (task.mFullscreen) {
1267 return true;
1268 }
1269 }
1270 return false;
1271 }
1272
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001273 private boolean hasTranslucentActivity(ActivityStack stack) {
1274 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1275 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1276 final TaskRecord task = tasks.get(taskNdx);
1277 final ArrayList<ActivityRecord> activities = task.mActivities;
1278 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1279 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001280
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001281 // Conditions for an activity to obscure the stack we're
1282 // examining:
1283 // 1. Not Finishing AND Visible AND:
1284 // 2. Either:
1285 // - Full Screen Activity OR
1286 // - On top of Home and our stack is NOT home
1287 if (!r.finishing && r.visible && (r.fullscreen ||
1288 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1289 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001290 }
1291 }
1292 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001293 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001294 }
1295
1296 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001297 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001298 if (!isAttached()) {
1299 return false;
1300 }
1301
1302 if (mStackSupervisor.isFrontStack(this)) {
1303 return true;
1304 }
1305
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001306 final int stackIndex = mStacks.indexOf(this);
1307
1308 if (stackIndex == mStacks.size() - 1) {
1309 Slog.wtf(TAG,
1310 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1311 return false;
1312 }
1313
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001314 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1315 final int focusedStackId = focusedStack.mStackId;
1316
1317 if (mStackId == DOCKED_STACK_ID) {
1318 // Docked stack is always visible, except in the case where the home activity
1319 // is the top running activity in the focused home stack.
1320 if (focusedStackId != HOME_STACK_ID) {
1321 return true;
1322 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001323 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001324 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1325 }
1326
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001327 final int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1328 if (focusedStackId == DOCKED_STACK_ID && stackIndex == belowFocusedIndex) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001329 // Stacks directly behind the docked stack are always visible.
1330 return true;
1331 }
1332
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001333 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1334 && hasTranslucentActivity(focusedStack)) {
1335 // Stacks behind the fullscreen stack with a translucent activity are always
1336 // visible so they can act as a backdrop to the translucent activity.
1337 // For example, dialog activities
1338 if (stackIndex == belowFocusedIndex) {
1339 return true;
1340 }
1341 if (belowFocusedIndex >= 0) {
1342 final ActivityStack stack = mStacks.get(belowFocusedIndex);
1343 if (stack.mStackId == DOCKED_STACK_ID && stackIndex == (belowFocusedIndex - 1)) {
1344 // The stack behind the docked stack is also visible so we can have a complete
1345 // backdrop to the translucent activity when the docked stack is up.
1346 return true;
1347 }
1348 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001349 }
1350
1351 if (mStackId >= FIRST_STATIC_STACK_ID && mStackId <= LAST_STATIC_STACK_ID) {
1352 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001353 return false;
1354 }
1355
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001356 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001357 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001358
1359 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1360 continue;
1361 }
1362
1363 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001364 || stack.mStackId == HOME_STACK_ID
1365 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
1366 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001367 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001368 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001369
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001370 if (!hasTranslucentActivity(stack)) {
1371 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001372 }
1373 }
1374
1375 return true;
1376 }
1377
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378 /**
1379 * Make sure that all activities that need to be visible (that is, they
1380 * currently can be seen by the user) actually are.
1381 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001382 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1383 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001384 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001385 if (top == null) {
1386 return;
1387 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001388 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1389 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 + " configChanges=0x" + Integer.toHexString(configChanges));
1391
Craig Mautner5eda9b32013-07-02 11:58:16 -07001392 if (mTranslucentActivityWaiting != top) {
1393 mUndrawnActivitiesBelowTopTranslucent.clear();
1394 if (mTranslucentActivityWaiting != null) {
1395 // Call the callback with a timeout indication.
1396 notifyActivityDrawnLocked(null);
1397 mTranslucentActivityWaiting = null;
1398 }
1399 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1400 }
1401
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 // If the top activity is not fullscreen, then we need to
1403 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001404 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001405 final boolean stackInvisible = !isStackVisibleLocked();
1406 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001407 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001408
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001409 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001410 final TaskRecord task = mTaskHistory.get(taskNdx);
1411 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001412
Craig Mautnerd44711d2013-02-23 11:24:36 -08001413 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1414 final ActivityRecord r = activities.get(activityNdx);
1415 if (r.finishing) {
1416 continue;
1417 }
1418 if (aboveTop && r != top) {
1419 continue;
1420 }
1421 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001422 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1423 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001424 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001425 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1426 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001427 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001428
Craig Mautnerd44711d2013-02-23 11:24:36 -08001429 // First: if this is not the current activity being started, make
1430 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001431 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001432 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001434
1435 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001436 // This activity needs to be visible, but isn't even running...
1437 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001438 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1439 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001440 if (r != starting) {
1441 r.startFreezingScreenLocked(r.app, configChanges);
1442 }
1443 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001444 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1445 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001446 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001447 }
1448 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001449 mStackSupervisor.startSpecificActivityLocked(
1450 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001451 if (activityNdx >= activities.size()) {
1452 // Record may be removed if its process needs to restart.
1453 activityNdx = activities.size() - 1;
1454 } else {
1455 noStackActivityResumed = false;
1456 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001457 }
1458
1459 } else if (r.visible) {
1460 // If this activity is already visible, then there is nothing
1461 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001462 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1463 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001465 try {
George Mount6ba042b2014-07-28 11:12:28 -07001466 if (r.returningOptions != null) {
1467 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1468 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001469 }
1470 } catch(RemoteException e) {
1471 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001472 if (r.state == ActivityState.RESUMED) {
1473 noStackActivityResumed = false;
1474 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001475 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001476 // This activity is not currently visible, but is running.
1477 // Tell it to become visible.
1478 r.visible = true;
1479 if (r.state != ActivityState.RESUMED && r != starting) {
1480 // If this activity is paused, tell it
1481 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001482 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1483 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001484 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001485 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001486 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001487 mUndrawnActivitiesBelowTopTranslucent.add(r);
1488 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001489 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001490 r.sleeping = false;
1491 r.app.pendingUiClean = true;
1492 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1493 r.stopFreezingScreenLocked(false);
1494 } catch (Exception e) {
1495 // Just skip on any failure; we'll make it
1496 // visible when it next restarts.
1497 Slog.w(TAG, "Exception thrown making visibile: "
1498 + r.intent.getComponent(), e);
1499 }
1500 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502
Craig Mautnerd44711d2013-02-23 11:24:36 -08001503 // Aggregate current change flags.
1504 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505
Craig Mautnerd44711d2013-02-23 11:24:36 -08001506 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001507 // At this point, nothing else needs to be shown in this task.
1508 behindFullscreenActivity = true;
1509 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001510 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001511 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001512 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001513 behindFullscreenActivity = true;
1514 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001515 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001516 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001517 }
1518 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001519 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001520 "Make invisible? " + r + " finishing=" + r.finishing
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001521 + " state=" + r.state + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001522 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001523 // Now for any activities that aren't visible to the user, make
1524 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001525 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001526 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001527 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001528 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001529 switch (r.state) {
1530 case STOPPING:
1531 case STOPPED:
1532 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001533 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1534 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001535 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1536 }
1537 break;
1538
1539 case INITIALIZING:
1540 case RESUMED:
1541 case PAUSING:
1542 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001543 // This case created for transitioning activities from
1544 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001545 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001546 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001547 } else {
1548 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1549 mStackSupervisor.mStoppingActivities.add(r);
1550 }
1551 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001552 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001553 break;
1554
1555 default:
1556 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001557 }
1558 } catch (Exception e) {
1559 // Just skip on any failure; we'll make it
1560 // visible when it next restarts.
1561 Slog.w(TAG, "Exception thrown making hidden: "
1562 + r.intent.getComponent(), e);
1563 }
1564 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001565 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 }
1568 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001569 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1570 // The visibility of tasks and the activities they contain in freeform stack are
1571 // determined individually unlike other stacks where the visibility or fullscreen
1572 // status of an activity in a previous task affects other.
1573 behindFullscreenActivity = stackInvisible;
1574 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001576
1577 if (mTranslucentActivityWaiting != null &&
1578 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1579 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1580 notifyActivityDrawnLocked(null);
1581 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 }
Craig Mautner58547802013-03-05 08:23:53 -08001583
Todd Kennedyaab56db2015-01-30 09:39:53 -08001584 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001585 mTranslucentActivityWaiting = r;
1586 mUndrawnActivitiesBelowTopTranslucent.clear();
1587 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1588 }
1589
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001590 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1591 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1592 final TaskRecord task = mTaskHistory.get(taskNdx);
1593 final ArrayList<ActivityRecord> activities = task.mActivities;
1594 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1595 final ActivityRecord r = activities.get(activityNdx);
1596 if ( r.appTimeTracker != except) {
1597 r.appTimeTracker = null;
1598 }
1599 }
1600 }
1601 }
1602
Craig Mautner5eda9b32013-07-02 11:58:16 -07001603 /**
1604 * Called as activities below the top translucent activity are redrawn. When the last one is
1605 * redrawn notify the top activity by calling
1606 * {@link Activity#onTranslucentConversionComplete}.
1607 *
1608 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1609 * occurred and the activity will be notified immediately.
1610 */
1611 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001612 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001613 if ((r == null)
1614 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1615 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1616 // The last undrawn activity below the top has just been drawn. If there is an
1617 // opaque activity at the top, notify it that it can become translucent safely now.
1618 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1619 mTranslucentActivityWaiting = null;
1620 mUndrawnActivitiesBelowTopTranslucent.clear();
1621 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1622
Craig Mautner71dd1b62014-02-18 15:48:52 -08001623 if (waitingActivity != null) {
1624 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1625 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1626 try {
1627 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1628 waitingActivity.appToken, r != null);
1629 } catch (RemoteException e) {
1630 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001631 }
1632 }
1633 }
1634 }
1635
Craig Mautnera61bc652013-10-28 15:43:18 -07001636 /** If any activities below the top running one are in the INITIALIZING state and they have a
1637 * starting window displayed then remove that starting window. It is possible that the activity
1638 * in this state will never resumed in which case that starting window will be orphaned. */
1639 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001640 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001641 boolean aboveTop = true;
1642 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1643 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1644 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1645 final ActivityRecord r = activities.get(activityNdx);
1646 if (aboveTop) {
1647 if (r == topActivity) {
1648 aboveTop = false;
1649 }
1650 continue;
1651 }
1652
1653 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001654 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1655 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001656 r.mStartingWindowShown = false;
1657 mWindowManager.removeAppStartingWindow(r.appToken);
1658 }
1659 }
1660 }
1661 }
1662
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663 /**
1664 * Ensure that the top activity in the stack is resumed.
1665 *
1666 * @param prev The previously resumed activity, for when in the process
1667 * of pausing; can be null to call from elsewhere.
1668 *
1669 * @return Returns true if something is being resumed, or false if
1670 * nothing happened.
1671 */
1672 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001673 return resumeTopActivityLocked(prev, null);
1674 }
1675
1676 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001677 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001678 // Don't even start recursing.
1679 return false;
1680 }
1681
1682 boolean result = false;
1683 try {
1684 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001685 mStackSupervisor.inResumeTopActivity = true;
1686 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1687 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001688 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001689 }
Craig Mautner544efa72014-09-04 16:41:20 -07001690 result = resumeTopActivityInnerLocked(prev, options);
1691 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001692 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001693 }
1694 return result;
1695 }
1696
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001697 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001698 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001699
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001700 if (!mService.mBooting && !mService.mBooted) {
1701 // Not ready yet!
1702 return false;
1703 }
1704
Craig Mautnerdf88d732014-01-27 09:21:32 -08001705 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001706 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001707 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001708 // Do not resume this stack if its parent is not resumed.
1709 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1710 return false;
1711 }
1712
Craig Mautnera61bc652013-10-28 15:43:18 -07001713 cancelInitializingActivities();
1714
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001716 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001717
1718 // Remember how we'll process this pause/resume situation, and ensure
1719 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001720 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1721 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722
Craig Mautner84984fa2014-06-19 11:19:20 -07001723 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001725 // There are no more activities!
1726 final String reason = "noMoreActivities";
1727 if (!mFullscreen) {
1728 // Try to move focus to the next visible stack with a running activity if this
1729 // stack is not covering the entire screen.
1730 final ActivityStack stack = getNextVisibleStackLocked();
1731 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1732 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1733 }
1734 }
1735 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001736 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001737 if (DEBUG_STATES) Slog.d(TAG_STATES,
1738 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001739 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001740 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001741 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1742 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1743 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001744 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745 }
1746
1747 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001748
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001750 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1751 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752 // Make sure we have executed any pending transitions, since there
1753 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001754 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001756 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001757 if (DEBUG_STATES) Slog.d(TAG_STATES,
1758 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001759 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001760 return false;
1761 }
1762
Craig Mautner525f3d92013-05-07 14:01:50 -07001763 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001764 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001765 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001766 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001767 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001768 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001769 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001770 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001771 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001772 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001773 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001774 } else if (!isOnHomeDisplay()) {
1775 return false;
1776 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001777 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001778 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001779 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1780 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1781 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001782 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001783 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001784 }
1785
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 // If we are sleeping, and there is no resumed activity, and the top
1787 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001788 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001789 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001790 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 // Make sure we have executed any pending transitions, since there
1792 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001793 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001795 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001796 if (DEBUG_STATES) Slog.d(TAG_STATES,
1797 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001798 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 return false;
1800 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001801
1802 // Make sure that the user who owns this activity is started. If not,
1803 // we will just leave it as is because someone should be bringing
1804 // another user's activities to the top of the stack.
1805 if (mService.mStartedUsers.get(next.userId) == null) {
1806 Slog.w(TAG, "Skipping resume of top activity " + next
1807 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001808 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001809 return false;
1810 }
1811
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 // The activity may be waiting for stop, but that is no longer
1813 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001814 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001815 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001816 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001817 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001819 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001821 // If we are currently pausing an activity, then don't do anything
1822 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001823 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001824 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001825 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001826 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001827 return false;
1828 }
1829
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001830 // Okay we are now going to start a switch, to 'next'. We may first
1831 // have to pause the current activity, but this is an important point
1832 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001833 // XXX "App Redirected" dialog is getting too many false positives
1834 // at this point, so turn off for now.
1835 if (false) {
1836 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1837 long now = SystemClock.uptimeMillis();
1838 final boolean inTime = mLastStartedActivity.startTime != 0
1839 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1840 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1841 final int nextUid = next.info.applicationInfo.uid;
1842 if (inTime && lastUid != nextUid
1843 && lastUid != next.launchedFromUid
1844 && mService.checkPermission(
1845 android.Manifest.permission.STOP_APP_SWITCHES,
1846 -1, next.launchedFromUid)
1847 != PackageManager.PERMISSION_GRANTED) {
1848 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1849 } else {
1850 next.startTime = now;
1851 mLastStartedActivity = next;
1852 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001853 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001854 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001855 mLastStartedActivity = next;
1856 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001857 }
Craig Mautner58547802013-03-05 08:23:53 -08001858
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001859 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1860
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861 // We need to start pausing the current activity so the top one
1862 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001863 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1864 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001865 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001866 if (DEBUG_STATES) Slog.d(TAG_STATES,
1867 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001868 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001869 }
1870 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001871 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001872 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001873 // At this point we want to put the upcoming activity's process
1874 // at the top of the LRU list, since we know we will be needing it
1875 // very soon and it would be a waste to let it get killed if it
1876 // happens to be sitting towards the end.
1877 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001878 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001879 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001880 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 return true;
1882 }
1883
Christopher Tated3f175c2012-06-14 14:16:54 -07001884 // If the most recent activity was noHistory but was only stopped rather
1885 // than stopped+finished because the device went to sleep, we need to make
1886 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001887 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001888 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001889 if (DEBUG_STATES) Slog.d(TAG_STATES,
1890 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001891 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001892 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001893 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001894 }
1895
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001896 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001897 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1898 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001899 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001900 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1901 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001902 } else {
1903 // The next activity is already visible, so hide the previous
1904 // activity's windows right now so we can show the new one ASAP.
1905 // We only do this if the previous is finishing, which should mean
1906 // it is on top of the one being resumed so hiding it quickly
1907 // is good. Otherwise, we want to do the normal route of allowing
1908 // the resumed activity to be shown so we can decide if the
1909 // previous should actually be hidden depending on whether the
1910 // new one is found to be full-screen or not.
1911 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001912 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001913 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1914 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001915 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001916 + ", nowVisible=" + next.nowVisible);
1917 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001918 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001919 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001920 + ", waitingVisible="
1921 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1922 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 }
1924 }
1925 }
1926
Dianne Hackborne7f97212011-02-24 14:40:20 -08001927 // Launching this app's activity, make sure the app is no longer
1928 // considered stopped.
1929 try {
1930 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001931 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001932 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001933 } catch (IllegalArgumentException e) {
1934 Slog.w(TAG, "Failed trying to unstop package "
1935 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001936 }
1937
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001938 // We are starting up the next activity, so tell the window manager
1939 // that the previous one will be hidden soon. This way it can know
1940 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001941 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001942 if (prev != null) {
1943 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001944 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001945 "Prepare close transition: prev=" + prev);
1946 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001947 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001948 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001950 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001951 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1952 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001953 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001954 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001956 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1957 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001959 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001961 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001962 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001963 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001964 : next.mLaunchTaskBehind
1965 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1966 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 }
1968 }
Craig Mautner967212c2013-04-13 21:10:58 -07001969 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001970 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001971 if (mNoAnimActivities.contains(next)) {
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(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001976 }
1977 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001978
1979 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001980 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001981 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1982 if (opts != null) {
1983 resumeAnimOptions = opts.toBundle();
1984 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001985 next.applyOptionsLocked();
1986 } else {
1987 next.clearOptionsLocked();
1988 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989
Craig Mautnercf910b02013-04-23 11:23:27 -07001990 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001992 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001993
1994 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001995 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001996
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001997 // schedule launch ticks to collect information about slow apps.
1998 next.startLaunchTickingLocked();
1999
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002000 ActivityRecord lastResumedActivity =
2001 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002002 ActivityState lastState = next.state;
2003
2004 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002005
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002006 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002007 next.state = ActivityState.RESUMED;
2008 mResumedActivity = next;
2009 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002010 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002011 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002012 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002013 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002014
2015 // Have the window manager re-evaluate the orientation of
2016 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002017 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002018 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002019 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002020 mService.mConfiguration,
2021 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2022 if (config != null) {
2023 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002025 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002026 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002027
Craig Mautner525f3d92013-05-07 14:01:50 -07002028 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 // The configuration update wasn't able to keep the existing
2030 // instance of the activity, and instead started a new one.
2031 // We should be all done, but let's just make sure our activity
2032 // is still at the top and schedule another run if something
2033 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002034 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002035 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002036 "Activity config changed during resume: " + next
2037 + ", new next: " + nextNext);
2038 if (nextNext != next) {
2039 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002040 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002042 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002043 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002044 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002045 return true;
2046 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002047 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002048 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049 }
Craig Mautner58547802013-03-05 08:23:53 -08002050
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 try {
2052 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002053 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 if (a != null) {
2055 final int N = a.size();
2056 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002057 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2058 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002059 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 }
2061 }
2062
2063 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002064 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 }
2066
Craig Mautner299f9602015-01-26 09:47:33 -08002067 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2068 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002069
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002070 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002071 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002072 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002073 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002074 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002075 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002076 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002077
Craig Mautner0eea92c2013-05-16 13:35:39 -07002078 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002080 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 } catch (Exception e) {
2082 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002083 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002084 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002085 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002086 if (lastStack != null) {
2087 lastStack.mResumedActivity = lastResumedActivity;
2088 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 Slog.i(TAG, "Restarting because process died: " + next);
2090 if (!next.hasBeenLaunched) {
2091 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002092 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2093 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002094 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002095 next.appToken, next.packageName, next.theme,
2096 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002097 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2098 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 }
George Mount2c92c972014-03-20 09:38:23 -07002100 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002101 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 return true;
2103 }
2104
2105 // From this point on, if something goes wrong there is no way
2106 // to recover the activity.
2107 try {
2108 next.visible = true;
2109 completeResumeLocked(next);
2110 } catch (Exception e) {
2111 // If any exception gets thrown, toss away this
2112 // activity and try the next one.
2113 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002114 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002115 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002116 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 return true;
2118 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 next.stopped = false;
2120
2121 } else {
2122 // Whoops, need to restart this activity!
2123 if (!next.hasBeenLaunched) {
2124 next.hasBeenLaunched = true;
2125 } else {
2126 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002127 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002128 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002129 mService.compatibilityInfoForPackageLocked(
2130 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002132 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002133 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002135 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002137 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002138 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 }
2140
Craig Mautnercf910b02013-04-23 11:23:27 -07002141 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 return true;
2143 }
2144
riddle_hsuc215a4f2014-12-27 12:10:45 +08002145 private TaskRecord getNextTask(TaskRecord targetTask) {
2146 final int index = mTaskHistory.indexOf(targetTask);
2147 if (index >= 0) {
2148 final int numTasks = mTaskHistory.size();
2149 for (int i = index + 1; i < numTasks; ++i) {
2150 TaskRecord task = mTaskHistory.get(i);
2151 if (task.userId == targetTask.userId) {
2152 return task;
2153 }
2154 }
2155 }
2156 return null;
2157 }
2158
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002159 private void insertTaskAtPosition(TaskRecord task, int position) {
2160 if (position >= mTaskHistory.size()) {
2161 insertTaskAtTop(task, null);
2162 return;
2163 }
2164 // Calculate maximum possible position for this task.
2165 int maxPosition = mTaskHistory.size();
2166 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002167 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002168 // Put non-current user tasks below current user tasks.
2169 while (maxPosition > 0) {
2170 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2171 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002172 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002173 break;
2174 }
2175 maxPosition--;
2176 }
2177 }
2178 position = Math.min(position, maxPosition);
2179 mTaskHistory.remove(task);
2180 mTaskHistory.add(position, task);
2181 updateTaskMovement(task, true);
2182 }
2183
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002184 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002185 // If the moving task is over home stack, transfer its return type to next task
2186 if (task.isOverHomeStack()) {
2187 final TaskRecord nextTask = getNextTask(task);
2188 if (nextTask != null) {
2189 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2190 }
2191 }
2192
Craig Mautner9c85c202013-10-04 20:11:26 -07002193 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002194 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002195 if (isOnHomeDisplay()) {
2196 ActivityStack lastStack = mStackSupervisor.getLastStack();
2197 final boolean fromHome = lastStack.isHomeStack();
2198 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002199 task.setTaskToReturnTo(fromHome
2200 ? lastStack.topTask() == null
2201 ? HOME_ACTIVITY_TYPE
2202 : lastStack.topTask().taskType
2203 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002204 }
2205 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002206 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002207 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002208
Craig Mautnerac6f8432013-07-17 13:24:59 -07002209 mTaskHistory.remove(task);
2210 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002211 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002212 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002213 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002214 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002215 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002216 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002217 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002218 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002219 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002220 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002221 break;
2222 }
2223 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002224 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002225 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002226 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002227 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002228 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002229
Craig Mautner8849a5e2013-04-02 16:41:03 -07002230 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002231 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002232 TaskRecord rTask = r.task;
2233 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002234 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2235 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002236 // Last activity in task had been removed or ActivityManagerService is reusing task.
2237 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002238 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002239 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002240 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002241 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002242 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 if (!newTask) {
2244 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002245 boolean startIt = true;
2246 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2247 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002248 if (task.getTopActivity() == null) {
2249 // All activities in task are finishing.
2250 continue;
2251 }
Craig Mautner70a86932013-02-28 22:37:44 -08002252 if (task == r.task) {
2253 // Here it is! Now, if this is not yet visible to the
2254 // user, then just add it without starting; it will
2255 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002256 if (!startIt) {
2257 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2258 + task, new RuntimeException("here").fillInStackTrace());
2259 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002260 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002261 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002262 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002263 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002264 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002265 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266 return;
2267 }
2268 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002269 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002270 startIt = false;
2271 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 }
2273 }
2274
2275 // Place a new activity at top of stack, so it is next to interact
2276 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 // If we are not placing the new activity frontmost, we do not want
2279 // to deliver the onUserLeaving callback to the actual frontmost
2280 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002281 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002282 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002283 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002284 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 }
Craig Mautner70a86932013-02-28 22:37:44 -08002286
2287 task = r.task;
2288
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002290 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002291 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002292 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002293 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002294
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002295 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002296 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002297 // We want to show the starting preview window if we are
2298 // switching to a new task, or the next activity's process is
2299 // not currently running.
2300 boolean showStartingIcon = newTask;
2301 ProcessRecord proc = r.app;
2302 if (proc == null) {
2303 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2304 }
2305 if (proc == null || proc.thread == null) {
2306 showStartingIcon = true;
2307 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002308 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002310 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002311 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002313 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002314 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002315 ? r.mLaunchTaskBehind
2316 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2317 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002318 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 mNoAnimActivities.remove(r);
2320 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002321 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 boolean doShow = true;
2323 if (newTask) {
2324 // Even though this activity is starting fresh, we still need
2325 // to reset it to make sure we apply affinities to move any
2326 // existing activities from other tasks in to it.
2327 // If the caller has requested that the target task be
2328 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002329 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 resetTaskIfNeededLocked(r, r);
2331 doShow = topRunningNonDelayedActivityLocked(null) == r;
2332 }
George Mount70778d72014-07-01 16:33:45 -07002333 } else if (options != null && new ActivityOptions(options).getAnimationType()
2334 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2335 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002337 if (r.mLaunchTaskBehind) {
2338 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2339 // tell WindowManager that r is visible even though it is at the back of the stack.
2340 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002341 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002342 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 // Figure out if we are transitioning from another activity that is
2344 // "has the same starting icon" as the next one. This allows the
2345 // window manager to keep the previous window it had previously
2346 // created, if it still had one.
2347 ActivityRecord prev = mResumedActivity;
2348 if (prev != null) {
2349 // We don't want to reuse the previous starting preview if:
2350 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002351 if (prev.task != r.task) {
2352 prev = null;
2353 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002354 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002355 else if (prev.nowVisible) {
2356 prev = null;
2357 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002359 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002360 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002361 mService.compatibilityInfoForPackageLocked(
2362 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002363 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002364 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002365 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366 }
2367 } else {
2368 // If this is the first activity, don't do any fancy animations,
2369 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002370 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002371 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002372 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002373 }
2374 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002375 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 }
2377
2378 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002379 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002380 } else {
2381 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 }
2383 }
2384
Dianne Hackbornbe707852011-11-11 14:32:10 -08002385 final void validateAppTokensLocked() {
2386 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002387 mValidateAppTokens.ensureCapacity(numActivities());
2388 final int numTasks = mTaskHistory.size();
2389 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2390 TaskRecord task = mTaskHistory.get(taskNdx);
2391 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002392 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002393 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002394 }
Craig Mautner000f0022013-02-26 15:04:29 -08002395 TaskGroup group = new TaskGroup();
2396 group.taskId = task.taskId;
2397 mValidateAppTokens.add(group);
2398 final int numActivities = activities.size();
2399 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2400 final ActivityRecord r = activities.get(activityNdx);
2401 group.tokens.add(r.appToken);
2402 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002403 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002404 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002405 }
2406
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002407 /**
2408 * Perform a reset of the given task, if needed as part of launching it.
2409 * Returns the new HistoryRecord at the top of the task.
2410 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002411 /**
2412 * Helper method for #resetTaskIfNeededLocked.
2413 * We are inside of the task being reset... we'll either finish this activity, push it out
2414 * for another task, or leave it as-is.
2415 * @param task The task containing the Activity (taskTop) that might be reset.
2416 * @param forceReset
2417 * @return An ActivityOptions that needs to be processed.
2418 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002419 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002420 ActivityOptions topOptions = null;
2421
2422 int replyChainEnd = -1;
2423 boolean canMoveOptions = true;
2424
2425 // We only do this for activities that are not the root of the task (since if we finish
2426 // the root, we may no longer have the task!).
2427 final ArrayList<ActivityRecord> activities = task.mActivities;
2428 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002429 final int rootActivityNdx = task.findEffectiveRootIndex();
2430 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002431 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002432 if (target.frontOfTask)
2433 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002434
2435 final int flags = target.info.flags;
2436 final boolean finishOnTaskLaunch =
2437 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2438 final boolean allowTaskReparenting =
2439 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2440 final boolean clearWhenTaskReset =
2441 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2442
2443 if (!finishOnTaskLaunch
2444 && !clearWhenTaskReset
2445 && target.resultTo != null) {
2446 // If this activity is sending a reply to a previous
2447 // activity, we can't do anything with it now until
2448 // we reach the start of the reply chain.
2449 // XXX note that we are assuming the result is always
2450 // to the previous activity, which is almost always
2451 // the case but we really shouldn't count on.
2452 if (replyChainEnd < 0) {
2453 replyChainEnd = i;
2454 }
2455 } else if (!finishOnTaskLaunch
2456 && !clearWhenTaskReset
2457 && allowTaskReparenting
2458 && target.taskAffinity != null
2459 && !target.taskAffinity.equals(task.affinity)) {
2460 // If this activity has an affinity for another
2461 // task, then we need to move it out of here. We will
2462 // move it as far out of the way as possible, to the
2463 // bottom of the activity stack. This also keeps it
2464 // correctly ordered with any activities we previously
2465 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002466 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002467 final ActivityRecord bottom =
2468 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002469 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002470 if (bottom != null && target.taskAffinity != null
2471 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002472 // If the activity currently at the bottom has the
2473 // same task affinity as the one we are moving,
2474 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002475 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002476 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002477 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002478 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002479 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002480 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002481 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002482 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002483 + " out to new task " + target.task);
2484 }
2485
Wale Ogunwale706ed792015-08-02 10:29:44 -07002486 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002487
Craig Mautner525f3d92013-05-07 14:01:50 -07002488 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002489 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2490 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002491 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002492 if (p.finishing) {
2493 continue;
2494 }
2495
Craig Mautnere3a74d52013-02-22 14:14:58 -08002496 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002497 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002498 topOptions = p.takeOptionsLocked();
2499 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002500 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002501 }
2502 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002503 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2504 "Removing activity " + p + " from task=" + task + " adding to task="
2505 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002506 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2507 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002508 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002510
Wale Ogunwale706ed792015-08-02 10:29:44 -07002511 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002512 }
2513
Wale Ogunwale706ed792015-08-02 10:29:44 -07002514 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002515 if (VALIDATE_TOKENS) {
2516 validateAppTokensLocked();
2517 }
2518
2519 replyChainEnd = -1;
2520 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2521 // If the activity should just be removed -- either
2522 // because it asks for it, or the task should be
2523 // cleared -- then finish it and anything that is
2524 // part of its reply chain.
2525 int end;
2526 if (clearWhenTaskReset) {
2527 // In this case, we want to finish this activity
2528 // and everything above it, so be sneaky and pretend
2529 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002530 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002531 } else if (replyChainEnd < 0) {
2532 end = i;
2533 } else {
2534 end = replyChainEnd;
2535 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002536 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002537 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002538 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002539 if (p.finishing) {
2540 continue;
2541 }
2542 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002543 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002544 topOptions = p.takeOptionsLocked();
2545 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002546 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002547 }
2548 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002549 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002550 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002551 if (finishActivityLocked(
2552 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002553 end--;
2554 srcPos--;
2555 }
2556 }
2557 replyChainEnd = -1;
2558 } else {
2559 // If we were in the middle of a chain, well the
2560 // activity that started it all doesn't want anything
2561 // special, so leave it all as-is.
2562 replyChainEnd = -1;
2563 }
2564 }
2565
2566 return topOptions;
2567 }
2568
2569 /**
2570 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2571 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2572 * @param affinityTask The task we are looking for an affinity to.
2573 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2574 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2575 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2576 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002577 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002578 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002579 int replyChainEnd = -1;
2580 final int taskId = task.taskId;
2581 final String taskAffinity = task.affinity;
2582
2583 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2584 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002585 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2586
2587 // Do not operate on or below the effective root Activity.
2588 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002589 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002590 if (target.frontOfTask)
2591 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002592
2593 final int flags = target.info.flags;
2594 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2595 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2596
2597 if (target.resultTo != null) {
2598 // If this activity is sending a reply to a previous
2599 // activity, we can't do anything with it now until
2600 // we reach the start of the reply chain.
2601 // XXX note that we are assuming the result is always
2602 // to the previous activity, which is almost always
2603 // the case but we really shouldn't count on.
2604 if (replyChainEnd < 0) {
2605 replyChainEnd = i;
2606 }
2607 } else if (topTaskIsHigher
2608 && allowTaskReparenting
2609 && taskAffinity != null
2610 && taskAffinity.equals(target.taskAffinity)) {
2611 // This activity has an affinity for our task. Either remove it if we are
2612 // clearing or move it over to our task. Note that
2613 // we currently punt on the case where we are resetting a
2614 // task that is not at the top but who has activities above
2615 // with an affinity to it... this is really not a normal
2616 // case, and we will need to later pull that task to the front
2617 // and usually at that point we will do the reset and pick
2618 // up those remaining activities. (This only happens if
2619 // someone starts an activity in a new task from an activity
2620 // in a task that is not currently on top.)
2621 if (forceReset || finishOnTaskLaunch) {
2622 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002623 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2624 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002625 for (int srcPos = start; srcPos >= i; --srcPos) {
2626 final ActivityRecord p = activities.get(srcPos);
2627 if (p.finishing) {
2628 continue;
2629 }
Todd Kennedy539db512014-12-15 09:57:55 -08002630 finishActivityLocked(
2631 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002632 }
2633 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002634 if (taskInsertionPoint < 0) {
2635 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002636
Craig Mautner77878772013-03-04 19:46:24 -08002637 }
Craig Mautner77878772013-03-04 19:46:24 -08002638
2639 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002640 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2641 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2642 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002643 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002644 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002645 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002646 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002647
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002648 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2649 "Removing and adding activity " + p + " to stack at " + task
2650 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002651 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2652 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002653 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002654 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002655 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002656 if (VALIDATE_TOKENS) {
2657 validateAppTokensLocked();
2658 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002659
2660 // Now we've moved it in to place... but what if this is
2661 // a singleTop activity and we have put it on top of another
2662 // instance of the same activity? Then we drop the instance
2663 // below so it remains singleTop.
2664 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2665 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002666 int targetNdx = taskActivities.indexOf(target);
2667 if (targetNdx > 0) {
2668 ActivityRecord p = taskActivities.get(targetNdx - 1);
2669 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002670 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2671 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002672 }
2673 }
2674 }
2675 }
2676
2677 replyChainEnd = -1;
2678 }
2679 }
Craig Mautner77878772013-03-04 19:46:24 -08002680 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002681 }
2682
Craig Mautner8849a5e2013-04-02 16:41:03 -07002683 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002684 ActivityRecord newActivity) {
2685 boolean forceReset =
2686 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2687 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2688 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2689 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2690 forceReset = true;
2691 }
2692 }
2693
2694 final TaskRecord task = taskTop.task;
2695
2696 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2697 * for remaining tasks. Used for later tasks to reparent to task. */
2698 boolean taskFound = false;
2699
2700 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2701 ActivityOptions topOptions = null;
2702
Craig Mautner77878772013-03-04 19:46:24 -08002703 // Preserve the location for reparenting in the new task.
2704 int reparentInsertionPoint = -1;
2705
Craig Mautnere3a74d52013-02-22 14:14:58 -08002706 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2707 final TaskRecord targetTask = mTaskHistory.get(i);
2708
2709 if (targetTask == task) {
2710 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2711 taskFound = true;
2712 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002713 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2714 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002715 }
2716 }
2717
Craig Mautner70a86932013-02-28 22:37:44 -08002718 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002719 if (taskNdx >= 0) {
2720 do {
2721 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2722 } while (taskTop == null && taskNdx >= 0);
2723 }
Craig Mautner70a86932013-02-28 22:37:44 -08002724
Craig Mautnere3a74d52013-02-22 14:14:58 -08002725 if (topOptions != null) {
2726 // If we got some ActivityOptions from an activity on top that
2727 // was removed from the task, propagate them to the new real top.
2728 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002729 taskTop.updateOptionsLocked(topOptions);
2730 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002731 topOptions.abort();
2732 }
2733 }
2734
2735 return taskTop;
2736 }
2737
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002738 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2739 String resultWho, int requestCode, int resultCode, Intent data) {
2740
2741 if (callingUid > 0) {
2742 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002743 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002744 }
2745
2746 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2747 + " : who=" + resultWho + " req=" + requestCode
2748 + " res=" + resultCode + " data=" + data);
2749 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2750 try {
2751 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2752 list.add(new ResultInfo(resultWho, requestCode,
2753 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002754 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 return;
2756 } catch (Exception e) {
2757 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2758 }
2759 }
2760
2761 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2762 }
2763
Craig Mautner299f9602015-01-26 09:47:33 -08002764 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002765 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002766 ActivityRecord next = topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002767 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002768 if (next != r) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002769 if (next != null && (mStackId == FREEFORM_WORKSPACE_STACK_ID
2770 || mStackId == DOCKED_STACK_ID)) {
2771 // For freeform and docked stacks we always keep the focus within the stack as
2772 // long as there is a running activity in the stack that we can adjust focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002773 mService.setFocusedActivityLocked(next, myReason);
2774 return;
2775 } else {
2776 final TaskRecord task = r.task;
2777 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2778 // For non-fullscreen stack, we want to move the focus to the next visible
2779 // stack to prevent the home screen from moving to the top and obscuring
2780 // other visible stacks.
2781 if (!mFullscreen
2782 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2783 return;
2784 }
2785 // Move the home stack to the top if this stack is fullscreen or there is no
2786 // other visible stack.
2787 if (mStackSupervisor.moveHomeStackTaskToTop(
2788 task.getTaskToReturnTo(), myReason)) {
2789 // Activity focus was already adjusted. Nothing else to do...
2790 return;
2791 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002792 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002793 }
2794 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002795
2796 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002797 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002798 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002799 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002800 }
2801 }
2802
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002803 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2804 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2805 final String myReason = reason + " adjustFocusToNextVisibleStack";
2806 if (stack == null) {
2807 return false;
2808 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002809 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002810 if (top == null) {
2811 return false;
2812 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002813 mService.setFocusedActivityLocked(top, myReason);
2814 return true;
2815 }
2816
Craig Mautnerf3333272013-04-22 10:55:53 -07002817 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002818 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2820 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2821 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002822 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002823 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002824 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2825 "stop-no-history", false)) {
2826 // Activity was finished, no need to continue trying to schedule stop.
2827 adjustFocusedActivityLocked(r, "stopActivityFinished");
2828 r.resumeKeyDispatchingLocked();
2829 return;
2830 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002831 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002832 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002833 + " on stop because we're just sleeping");
2834 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002836 }
2837
2838 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002839 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002840 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002841 try {
2842 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002843 if (DEBUG_STATES) Slog.v(TAG_STATES,
2844 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002846 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2847 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002849 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002850 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002851 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002852 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002853 r.setSleeping(true);
2854 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002855 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002856 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857 } catch (Exception e) {
2858 // Maybe just ignore exceptions here... if the process
2859 // has crashed, our death notification will clean things
2860 // up.
2861 Slog.w(TAG, "Exception thrown during pause", e);
2862 // Just in case, assume it to be stopped.
2863 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002864 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002865 r.state = ActivityState.STOPPED;
2866 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002867 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 }
2869 }
2870 }
2871 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002872
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 /**
2874 * @return Returns true if the activity is being finished, false if for
2875 * some reason it is being left as-is.
2876 */
2877 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002878 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002879 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002880 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2881 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002882 + ", result=" + resultCode + ", data=" + resultData
2883 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002884 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 return false;
2886 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887
Craig Mautnerd44711d2013-02-23 11:24:36 -08002888 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 return true;
2890 }
2891
Craig Mautnerd2328952013-03-05 12:46:26 -08002892 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002893 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2894 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2895 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2896 ActivityRecord r = activities.get(activityNdx);
2897 if (r.resultTo == self && r.requestCode == requestCode) {
2898 if ((r.resultWho == null && resultWho == null) ||
2899 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2900 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2901 false);
2902 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002903 }
2904 }
2905 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002906 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002907 }
2908
Todd Kennedy539db512014-12-15 09:57:55 -08002909 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002910 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002911 if (r != null && r.app == app) {
2912 // If the top running activity is from this crashing
2913 // process, then terminate it to avoid getting in a loop.
2914 Slog.w(TAG, " Force finishing activity "
2915 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002916 int taskNdx = mTaskHistory.indexOf(r.task);
2917 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002918 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002919 // Also terminate any activities below it that aren't yet
2920 // stopped, to avoid a situation where one will get
2921 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002922 --activityNdx;
2923 if (activityNdx < 0) {
2924 do {
2925 --taskNdx;
2926 if (taskNdx < 0) {
2927 break;
2928 }
2929 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2930 } while (activityNdx < 0);
2931 }
2932 if (activityNdx >= 0) {
2933 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002934 if (r.state == ActivityState.RESUMED
2935 || r.state == ActivityState.PAUSING
2936 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002937 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002938 Slog.w(TAG, " Force finishing activity "
2939 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002940 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002941 }
2942 }
2943 }
2944 }
2945 }
2946
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002947 final void finishVoiceTask(IVoiceInteractionSession session) {
2948 IBinder sessionBinder = session.asBinder();
2949 boolean didOne = false;
2950 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2951 TaskRecord tr = mTaskHistory.get(taskNdx);
2952 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2953 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2954 ActivityRecord r = tr.mActivities.get(activityNdx);
2955 if (!r.finishing) {
2956 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2957 false);
2958 didOne = true;
2959 }
2960 }
2961 }
2962 }
2963 if (didOne) {
2964 mService.updateOomAdjLocked();
2965 }
2966 }
2967
Craig Mautnerd2328952013-03-05 12:46:26 -08002968 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002969 ArrayList<ActivityRecord> activities = r.task.mActivities;
2970 for (int index = activities.indexOf(r); index >= 0; --index) {
2971 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002972 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002973 break;
2974 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002975 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002976 }
2977 return true;
2978 }
2979
Dianne Hackborn5c607432012-02-28 14:44:19 -08002980 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2981 // send the result
2982 ActivityRecord resultTo = r.resultTo;
2983 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002984 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002985 + " who=" + r.resultWho + " req=" + r.requestCode
2986 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002987 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002988 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01002989 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002990 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002991 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002992 if (r.info.applicationInfo.uid > 0) {
2993 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2994 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002995 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002996 }
2997 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2998 resultData);
2999 r.resultTo = null;
3000 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003001 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003002
3003 // Make sure this HistoryRecord is not holding on to other resources,
3004 // because clients have remote IPC references to this object so we
3005 // can't assume that will go away and want to avoid circular IPC refs.
3006 r.results = null;
3007 r.pendingResults = null;
3008 r.newIntents = null;
3009 r.icicle = null;
3010 }
3011
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 /**
3013 * @return Returns true if this activity has been removed from the history
3014 * list, or false if it is still in the list and will be removed later.
3015 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003016 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3017 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003018 if (r.finishing) {
3019 Slog.w(TAG, "Duplicate finish request for " + r);
3020 return false;
3021 }
3022
Wale Ogunwale7d701172015-03-11 15:36:30 -07003023 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003024 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003025 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003026 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003027 task.taskId, r.shortComponentName, reason);
3028 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003029 final int index = activities.indexOf(r);
3030 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003031 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003032 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003033 // If the caller asked that this activity (and all above it)
3034 // be cleared when the task is reset, don't lose that information,
3035 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003036 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003037 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 }
3039 }
3040
3041 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003042
Craig Mautner299f9602015-01-26 09:47:33 -08003043 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044
Dianne Hackborn5c607432012-02-28 14:44:19 -08003045 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003046
Craig Mautnerde4ef022013-04-07 19:01:33 -07003047 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003048 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003049 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003051 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003052 ? AppTransition.TRANSIT_TASK_CLOSE
3053 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003054
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003056 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003057
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003058 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003059 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3060 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3061 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003062 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003063 }
3064
Craig Mautneraea74a52014-03-08 14:23:10 -08003065 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003066 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003067 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003068 } else if (r.state != ActivityState.PAUSING) {
3069 // If the activity is PAUSING, we will complete the finish once
3070 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003071 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003072 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003073 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003074 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 }
3076
3077 return false;
3078 }
3079
Craig Mautnerf3333272013-04-22 10:55:53 -07003080 static final int FINISH_IMMEDIATELY = 0;
3081 static final int FINISH_AFTER_PAUSE = 1;
3082 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083
Craig Mautnerf3333272013-04-22 10:55:53 -07003084 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 // First things first: if this activity is currently visible,
3086 // and the resumed activity is not yet visible, then hold off on
3087 // finishing until the resumed one becomes visible.
3088 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003089 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3090 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003091 if (mStackSupervisor.mStoppingActivities.size() > 3
3092 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003093 // If we already have a few activities waiting to stop,
3094 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003095 // them out. Or if r is the last of activity of the last task the stack
3096 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003097 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003098 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003099 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 }
3101 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003102 if (DEBUG_STATES) Slog.v(TAG_STATES,
3103 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003105 if (oomAdj) {
3106 mService.updateOomAdjLocked();
3107 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108 return r;
3109 }
3110
3111 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003112 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003113 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003114 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003115 if (mResumedActivity == r) {
3116 mResumedActivity = null;
3117 }
3118 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003119 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 r.state = ActivityState.FINISHING;
3121
3122 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003123 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124 || prevState == ActivityState.STOPPED
3125 || prevState == ActivityState.INITIALIZING) {
3126 // If this activity is already stopped, we can just finish
3127 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003128 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003129 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003130 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003131 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003132 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003133 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003134 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3135 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003136 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003138
3139 // Need to go through the full pause cycle to get this
3140 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003141 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003142 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003143 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003144 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003145 return r;
3146 }
3147
Craig Mautneree36c772014-07-16 14:56:05 -07003148 void finishAllActivitiesLocked(boolean immediately) {
3149 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003150 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3151 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3152 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3153 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003154 noActivitiesInStack = false;
3155 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003156 continue;
3157 }
Craig Mautneree36c772014-07-16 14:56:05 -07003158 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003159 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3160 }
3161 }
Craig Mautneree36c772014-07-16 14:56:05 -07003162 if (noActivitiesInStack) {
3163 mActivityContainer.onTaskListEmptyLocked();
3164 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003165 }
3166
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003167 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3168 // Basic case: for simple app-centric recents, we need to recreate
3169 // the task if the affinity has changed.
3170 if (srec == null || srec.task.affinity == null ||
3171 !srec.task.affinity.equals(destAffinity)) {
3172 return true;
3173 }
3174 // Document-centric case: an app may be split in to multiple documents;
3175 // they need to re-create their task if this current activity is the root
3176 // of a document, unless simply finishing it will return them to the the
3177 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003178 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3179 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003180 // Okay, this activity is at the root of its task. What to do, what to do...
3181 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3182 // Finishing won't return to an application, so we need to recreate.
3183 return true;
3184 }
3185 // We now need to get the task below it to determine what to do.
3186 int taskIdx = mTaskHistory.indexOf(srec.task);
3187 if (taskIdx <= 0) {
3188 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3189 return false;
3190 }
3191 if (taskIdx == 0) {
3192 // At the bottom of the stack, nothing to go back to.
3193 return true;
3194 }
3195 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3196 if (!srec.task.affinity.equals(prevTask.affinity)) {
3197 // These are different apps, so need to recreate.
3198 return true;
3199 }
3200 }
3201 return false;
3202 }
3203
Wale Ogunwale7d701172015-03-11 15:36:30 -07003204 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003205 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003206 final TaskRecord task = srec.task;
3207 final ArrayList<ActivityRecord> activities = task.mActivities;
3208 final int start = activities.indexOf(srec);
3209 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003210 return false;
3211 }
3212 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003213 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003214 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003215 final ComponentName dest = destIntent.getComponent();
3216 if (start > 0 && dest != null) {
3217 for (int i = finishTo; i >= 0; i--) {
3218 ActivityRecord r = activities.get(i);
3219 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003220 r.info.name.equals(dest.getClassName())) {
3221 finishTo = i;
3222 parent = r;
3223 foundParentInTask = true;
3224 break;
3225 }
3226 }
3227 }
3228
3229 IActivityController controller = mService.mController;
3230 if (controller != null) {
3231 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3232 if (next != null) {
3233 // ask watcher if this is allowed
3234 boolean resumeOK = true;
3235 try {
3236 resumeOK = controller.activityResuming(next.packageName);
3237 } catch (RemoteException e) {
3238 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003239 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003240 }
3241
3242 if (!resumeOK) {
3243 return false;
3244 }
3245 }
3246 }
3247 final long origId = Binder.clearCallingIdentity();
3248 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003249 ActivityRecord r = activities.get(i);
3250 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003251 // Only return the supplied result for the first activity finished
3252 resultCode = Activity.RESULT_CANCELED;
3253 resultData = null;
3254 }
3255
3256 if (parent != null && foundParentInTask) {
3257 final int parentLaunchMode = parent.info.launchMode;
3258 final int destIntentFlags = destIntent.getFlags();
3259 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3260 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3261 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3262 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003263 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3264 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003265 } else {
3266 try {
3267 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3268 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003269 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003270 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003271 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003272 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003273 foundParentInTask = res == ActivityManager.START_SUCCESS;
3274 } catch (RemoteException e) {
3275 foundParentInTask = false;
3276 }
3277 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003278 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003279 }
3280 }
3281 Binder.restoreCallingIdentity(origId);
3282 return foundParentInTask;
3283 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003284 /**
3285 * Perform the common clean-up of an activity record. This is called both
3286 * as part of destroyActivityLocked() (when destroying the client-side
3287 * representation) and cleaning things up as a result of its hosting
3288 * processing going away, in which case there is no remaining client-side
3289 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003290 *
3291 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003293 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3294 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 if (mResumedActivity == r) {
3296 mResumedActivity = null;
3297 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003298 if (mPausingActivity == r) {
3299 mPausingActivity = null;
3300 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003301 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003302
3303 r.configDestroy = false;
3304 r.frozenBeforeDestroy = false;
3305
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003306 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003307 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003308 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003309 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003310 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003311 }
3312
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003313 // Make sure this record is no longer in the pending finishes list.
3314 // This could happen, for example, if we are trimming activities
3315 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003316 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003317 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003318
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003319 // Remove any pending results.
3320 if (r.finishing && r.pendingResults != null) {
3321 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3322 PendingIntentRecord rec = apr.get();
3323 if (rec != null) {
3324 mService.cancelIntentSenderLocked(rec, false);
3325 }
3326 }
3327 r.pendingResults = null;
3328 }
3329
3330 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003331 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003332 }
3333
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003335 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003336 if (getVisibleBehindActivity() == r) {
3337 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003338 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003339 }
3340
3341 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003342 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003344 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003345 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003346 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 }
3348
Craig Mautner299f9602015-01-26 09:47:33 -08003349 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003350 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003351 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003352 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003353 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3354 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3355
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003356 r.takeFromHistory();
3357 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003358 if (DEBUG_STATES) Slog.v(TAG_STATES,
3359 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003360 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003361 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003362 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003363 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003364 if (VALIDATE_TOKENS) {
3365 validateAppTokensLocked();
3366 }
Craig Mautner312ba862014-02-10 17:55:01 -08003367 final TaskRecord task = r.task;
3368 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003369 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003370 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003371 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3372 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003373 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003374 }
Craig Mautner299f9602015-01-26 09:47:33 -08003375 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003376 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003377 cleanUpActivityServicesLocked(r);
3378 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003379 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003380
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003381 /**
3382 * Perform clean-up of service connections in an activity record.
3383 */
3384 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3385 // Throw away any services that have been bound by this activity.
3386 if (r.connections != null) {
3387 Iterator<ConnectionRecord> it = r.connections.iterator();
3388 while (it.hasNext()) {
3389 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003390 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003391 }
3392 r.connections = null;
3393 }
3394 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003395
Craig Mautneree2e45a2014-06-27 12:10:03 -07003396 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003397 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003398 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003399 mHandler.sendMessage(msg);
3400 }
3401
Craig Mautneree2e45a2014-06-27 12:10:03 -07003402 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003403 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003404 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003405 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3406 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3407 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3408 final ActivityRecord r = activities.get(activityNdx);
3409 if (r.finishing) {
3410 continue;
3411 }
3412 if (r.fullscreen) {
3413 lastIsOpaque = true;
3414 }
3415 if (owner != null && r.app != owner) {
3416 continue;
3417 }
3418 if (!lastIsOpaque) {
3419 continue;
3420 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003421 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003422 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003423 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003424 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003425 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003426 activityRemoved = true;
3427 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003428 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003429 }
3430 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003431 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003432 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003433 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003434 }
3435
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003436 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3437 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003438 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3439 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003440 + " pausing=" + mPausingActivity + " for reason " + reason);
3441 return destroyActivityLocked(r, true, reason);
3442 }
3443 return false;
3444 }
3445
3446 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3447 String reason) {
3448 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003449 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003450 int maxTasks = tasks.size() / 4;
3451 if (maxTasks < 1) {
3452 maxTasks = 1;
3453 }
3454 int numReleased = 0;
3455 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3456 final TaskRecord task = mTaskHistory.get(taskNdx);
3457 if (!tasks.contains(task)) {
3458 continue;
3459 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003460 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003461 int curNum = 0;
3462 final ArrayList<ActivityRecord> activities = task.mActivities;
3463 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3464 final ActivityRecord activity = activities.get(actNdx);
3465 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003466 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003467 + " in state " + activity.state + " resumed=" + mResumedActivity
3468 + " pausing=" + mPausingActivity + " for reason " + reason);
3469 destroyActivityLocked(activity, true, reason);
3470 if (activities.get(actNdx) != activity) {
3471 // Was removed from list, back up so we don't miss the next one.
3472 actNdx--;
3473 }
3474 curNum++;
3475 }
3476 }
3477 if (curNum > 0) {
3478 numReleased += curNum;
3479 maxTasks--;
3480 if (mTaskHistory.get(taskNdx) != task) {
3481 // The entire task got removed, back up so we don't miss the next one.
3482 taskNdx--;
3483 }
3484 }
3485 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003486 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3487 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003488 return numReleased;
3489 }
3490
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003491 /**
3492 * Destroy the current CLIENT SIDE instance of an activity. This may be
3493 * called both when actually finishing an activity, or when performing
3494 * a configuration switch where we destroy the current client-side object
3495 * but then create a new client-side object for this same HistoryRecord.
3496 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003497 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003498 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3499 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003500 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003501 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003502 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003503 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003504
3505 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003506
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003507 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508
3509 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003510
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003511 if (hadApp) {
3512 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003513 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003514 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3515 mService.mHeavyWeightProcess = null;
3516 mService.mHandler.sendEmptyMessage(
3517 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3518 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003519 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003520 // Update any services we are bound to that might care about whether
3521 // their client may have activities.
3522 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003523 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003524 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003525 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003526 }
3527 }
3528
3529 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003530
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003532 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003533 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003534 r.configChangeFlags);
3535 } catch (Exception e) {
3536 // We can just ignore exceptions here... if the process
3537 // has crashed, our death notification will clean things
3538 // up.
3539 //Slog.w(TAG, "Exception thrown during finish", e);
3540 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003541 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003542 removedFromHistory = true;
3543 skipDestroy = true;
3544 }
3545 }
3546
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003547 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003548
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003549 // If the activity is finishing, we need to wait on removing it
3550 // from the list to give it a chance to do its cleanup. During
3551 // that time it may make calls back with its token so we need to
3552 // be able to find it on the list and so we don't want to remove
3553 // it from the list yet. Otherwise, we can just immediately put
3554 // it in the destroyed state since we are not removing it from the
3555 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003556 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003557 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003558 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003560 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3562 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003563 if (DEBUG_STATES) Slog.v(TAG_STATES,
3564 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003565 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003566 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003567 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 }
3569 } else {
3570 // remove this record from the history.
3571 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003572 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 removedFromHistory = true;
3574 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003575 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003577 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003578 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003579 }
3580 }
3581
3582 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003583
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003584 if (!mLRUActivities.remove(r) && hadApp) {
3585 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3586 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 return removedFromHistory;
3589 }
3590
Craig Mautner299f9602015-01-26 09:47:33 -08003591 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003592 final long origId = Binder.clearCallingIdentity();
3593 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003594 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003595 if (r != null) {
3596 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003597 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003598 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003599
Wale Ogunwale60454db2015-01-23 16:05:07 -08003600 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003601 if (r.state == ActivityState.DESTROYING) {
3602 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003603 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003604 }
3605 }
Craig Mautner05d29032013-05-03 13:40:13 -07003606 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003607 } finally {
3608 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003609 }
3610 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003611
Todd Kennedyaab56db2015-01-30 09:39:53 -08003612 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003613 if (hasVisibleBehindActivity() &&
3614 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003615 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003616 // Don't release the top activity if it has requested to run behind the next
3617 // activity.
3618 return;
3619 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003620 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003621 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003622 " thread=" + r.app.thread);
3623 if (r != null && r.app != null && r.app.thread != null) {
3624 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003625 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003626 } catch (RemoteException e) {
3627 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003628 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003629 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003630 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003631 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003632 }
3633 }
3634 }
3635
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003636 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003637 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3638 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003639 if (r != null) {
3640 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003641 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003642 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003643 }
3644 mStackSupervisor.resumeTopActivitiesLocked();
3645 }
3646
Jose Lima4b6c6692014-08-12 17:41:12 -07003647 boolean hasVisibleBehindActivity() {
3648 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003649 }
3650
Jose Lima4b6c6692014-08-12 17:41:12 -07003651 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003652 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003653 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003654 }
3655 }
3656
Jose Lima4b6c6692014-08-12 17:41:12 -07003657 ActivityRecord getVisibleBehindActivity() {
3658 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003659 }
3660
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003661 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3662 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003663 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003664 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3665 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 while (i > 0) {
3667 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003668 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003669 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003670 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003671 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003672 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003673 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003674 }
3675 }
3676 }
3677
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003678 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3679 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003680 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3681 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003682 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3683 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003684 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003685 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003686 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3687 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003688
3689 boolean hasVisibleActivities = false;
3690
3691 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003692 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003693 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3694 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003695 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3696 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3697 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3698 final ActivityRecord r = activities.get(activityNdx);
3699 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003700 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3701 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003702 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003703 if (r.visible) {
3704 hasVisibleActivities = true;
3705 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003706 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003707 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3708 // Don't currently have state for the activity, or
3709 // it is finishing -- always remove it.
3710 remove = true;
3711 } else if (r.launchCount > 2 &&
3712 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3713 // We have launched this activity too many times since it was
3714 // able to run, so give up and remove it.
3715 remove = true;
3716 } else {
3717 // The process may be gone, but the activity lives on!
3718 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003719 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003720 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003721 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3722 "Removing activity " + r + " from stack at " + i
3723 + ": haveState=" + r.haveState
3724 + " stateNotNeeded=" + r.stateNotNeeded
3725 + " finishing=" + r.finishing
3726 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003727 if (!r.finishing) {
3728 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3729 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3730 r.userId, System.identityHashCode(r),
3731 r.task.taskId, r.shortComponentName,
3732 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003733 if (r.state == ActivityState.RESUMED) {
3734 mService.updateUsageStats(r, false);
3735 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003736 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003737 } else {
3738 // We have the current state for this activity, so
3739 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003740 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3741 if (DEBUG_APP) Slog.v(TAG_APP,
3742 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003743 r.app = null;
3744 r.nowVisible = false;
3745 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003746 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003747 "App died, clearing saved state of " + r);
3748 r.icicle = null;
3749 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003750 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003751 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003752 if (remove) {
3753 removeActivityFromHistoryLocked(r, "appDied");
3754 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003755 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003756 }
3757 }
3758
3759 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003760 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003761
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003762 final void updateTransitLocked(int transit, Bundle options) {
3763 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003764 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003765 if (r != null && r.state != ActivityState.RESUMED) {
3766 r.updateOptionsLocked(options);
3767 } else {
3768 ActivityOptions.abort(options);
3769 }
3770 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003771 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003772 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003773
Craig Mautner21d24a22014-04-23 11:45:37 -07003774 void updateTaskMovement(TaskRecord task, boolean toFront) {
3775 if (task.isPersistable) {
3776 task.mLastTimeMoved = System.currentTimeMillis();
3777 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3778 // recently will be most negative, tasks sent to the bottom before that will be less
3779 // negative. Similarly for recent tasks moved to the top which will be most positive.
3780 if (!toFront) {
3781 task.mLastTimeMoved *= -1;
3782 }
3783 }
3784 }
3785
Craig Mautner84984fa2014-06-19 11:19:20 -07003786 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003787 final int top = mTaskHistory.size() - 1;
3788 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3789 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003790 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003791 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3792 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003793 mTaskHistory.remove(taskNdx);
3794 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003795 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003796 return;
3797 }
3798 }
3799 }
3800
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003801 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003802 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003803 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003804
Craig Mautner11bf9a52013-02-19 14:08:51 -08003805 final int numTasks = mTaskHistory.size();
3806 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003807 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003808 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003809 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003810 ActivityOptions.abort(options);
3811 } else {
3812 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3813 }
3814 return;
3815 }
3816
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003817 if (timeTracker != null) {
3818 // The caller wants a time tracker associated with this task.
3819 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3820 tr.mActivities.get(i).appTimeTracker = timeTracker;
3821 }
3822 }
3823
Craig Mautner11bf9a52013-02-19 14:08:51 -08003824 // Shift all activities with this task up to the top
3825 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003826 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003827
Chong Zhang45c25ce2015-08-10 22:18:26 -07003828 // Don't refocus if invisible to current user
3829 ActivityRecord top = tr.getTopActivity();
3830 if (!okToShowLocked(top)) {
3831 addRecentActivityLocked(top);
3832 ActivityOptions.abort(options);
3833 return;
3834 }
3835
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003836 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003837 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003838 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003839
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003840 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003841 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003842 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003843 if (r != null) {
3844 mNoAnimActivities.add(r);
3845 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003846 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003847 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003848 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003849 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003850
Craig Mautner05d29032013-05-03 13:40:13 -07003851 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003852 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003853
3854 if (VALIDATE_TOKENS) {
3855 validateAppTokensLocked();
3856 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003857 }
3858
3859 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003860 * Worker method for rearranging history stack. Implements the function of moving all
3861 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003862 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003863 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003864 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3865 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003866 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003867 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003868 * @return Returns true if the move completed, false if not.
3869 */
Craig Mautner299f9602015-01-26 09:47:33 -08003870 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003871 final TaskRecord tr = taskForIdLocked(taskId);
3872 if (tr == null) {
3873 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3874 return false;
3875 }
3876
3877 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003878 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003879
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 // If we have a watcher, preflight the move before committing to it. First check
3881 // for *other* available tasks, but if none are available, then try again allowing the
3882 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003883 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003884 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003885 if (next == null) {
3886 next = topRunningActivityLocked(null, 0);
3887 }
3888 if (next != null) {
3889 // ask watcher if this is allowed
3890 boolean moveOK = true;
3891 try {
3892 moveOK = mService.mController.activityResuming(next.packageName);
3893 } catch (RemoteException e) {
3894 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003895 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003896 }
3897 if (!moveOK) {
3898 return false;
3899 }
3900 }
3901 }
3902
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003903 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003904
riddle_hsuc215a4f2014-12-27 12:10:45 +08003905 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003906
3907 // If true, we should resume the home activity next if the task we are moving to the
3908 // back is over the home stack. We force to false if the task we are moving to back
3909 // is the home task and we don't want it resumed after moving to the back.
3910 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3911 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003912 final TaskRecord nextTask = getNextTask(tr);
3913 if (nextTask != null) {
3914 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3915 } else {
3916 prevIsHome = true;
3917 }
3918 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003919 mTaskHistory.remove(tr);
3920 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003921 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003922
Craig Mautnerc8143c62013-09-03 12:15:57 -07003923 // There is an assumption that moving a task to the back moves it behind the home activity.
3924 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003925 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003926 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3927 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003928 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003929 break;
3930 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003931 if (taskNdx == 1) {
3932 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003933 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003934 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003935 }
3936
Craig Mautner299f9602015-01-26 09:47:33 -08003937 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003938 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003939
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003940 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003941 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003942 }
3943
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003944 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003945 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003946 if (!mService.mBooting && !mService.mBooted) {
3947 // Not ready yet!
3948 return false;
3949 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003950 final int taskToReturnTo = tr.getTaskToReturnTo();
3951 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003952 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003953 }
3954
Craig Mautner05d29032013-05-03 13:40:13 -07003955 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 return true;
3957 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003958
Craig Mautner8849a5e2013-04-02 16:41:03 -07003959 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003961 final Uri data = r.intent.getData();
3962 final String strData = data != null ? data.toSafeString() : null;
3963
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003964 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003965 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003966 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003967 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003968 }
3969
3970 /**
3971 * Make sure the given activity matches the current configuration. Returns
3972 * false if the activity had to be destroyed. Returns true if the
3973 * configuration is the same, or the activity will remain running as-is
3974 * for whatever reason. Ensures the HistoryRecord is updated with the
3975 * correct configuration and all other bookkeeping is handled.
3976 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003977 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
3978 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003980 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003981 "Skipping config check (will change): " + r);
3982 return true;
3983 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003984
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003985 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003986 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003988 // Short circuit: if the two configurations are the exact same
3989 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003990 final Configuration newConfig = mService.mConfiguration;
3991 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003992 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003993 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08003994 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003995 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003996 "Configuration unchanged in " + r);
3997 return true;
3998 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003999
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004000 // We don't worry about activities that are finishing.
4001 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004002 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004003 "Configuration doesn't matter in finishing " + r);
4004 r.stopFreezingScreenLocked(false);
4005 return true;
4006 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004007
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004008 // Okay we now are going to make this activity have the new config.
4009 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004010 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004011 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004012 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004013 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004014
Filip Gruszczynski23493322015-07-29 17:02:59 -07004015 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004016 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004017 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004018 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004019 "Configuration no differences in " + r);
4020 return true;
4021 }
4022
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004023 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4024 "Configuration changes for " + r + " ; taskChanges="
4025 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4026 + Configuration.configurationDiffToString(changes));
4027
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004028 // If the activity isn't currently running, just leave the new
4029 // configuration and it will pick that up next time it starts.
4030 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004031 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004032 "Configuration doesn't matter not running " + r);
4033 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004034 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004035 return true;
4036 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004037
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004038 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004039 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4040 "Checking to restart " + r.info.name + ": changed=0x"
4041 + Integer.toHexString(changes) + ", handles=0x"
4042 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4043
Dianne Hackborne6676352011-06-01 16:51:20 -07004044 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004045 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4046 r.configChangeFlags |= changes;
4047 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004048 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004049 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004050 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004051 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004052 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004053 } else if (r.state == ActivityState.PAUSING) {
4054 // A little annoying: we are waiting for this activity to
4055 // finish pausing. Let's not do anything now, but just
4056 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004057 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004058 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004059 r.configDestroy = true;
4060 return true;
4061 } else if (r.state == ActivityState.RESUMED) {
4062 // Try to optimize this case: the configuration is changing
4063 // and we need to restart the top, resumed activity.
4064 // Instead of doing the normal handshaking, just say
4065 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004066 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004067 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004068 relaunchActivityLocked(r, r.configChangeFlags, true,
4069 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004070 r.configChangeFlags = 0;
4071 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004072 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004073 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004074 relaunchActivityLocked(r, r.configChangeFlags, false,
4075 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004076 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004077 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004078
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004079 // All done... tell the caller we weren't able to keep this
4080 // activity around.
4081 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004083
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004084 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004085 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004086 // changes is always sent to all processes when they happen so it can just use whatever
4087 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004088 if (r.app != null && r.app.thread != null) {
4089 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004090 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004091 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004092 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004093 } catch (RemoteException e) {
4094 // If process died, whatever.
4095 }
4096 }
4097 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004098
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004099 return true;
4100 }
4101
Filip Gruszczynski23493322015-07-29 17:02:59 -07004102 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4103 Configuration oldTaskOverride) {
4104 // Determine what has changed. May be nothing, if this is a config
4105 // that has come back from the app after going idle. In that case
4106 // we just want to leave the official config object now in the
4107 // activity and do nothing else.
4108 int taskChanges = oldTaskOverride.diff(taskConfig);
4109 // We don't want to use size changes if they don't cross boundaries that are important to
4110 // the app.
4111 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4112 final boolean crosses = record.crossesHorizontalSizeThreshold(
4113 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4114 || record.crossesVerticalSizeThreshold(
4115 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4116 if (!crosses) {
4117 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4118 }
4119 }
4120 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4121 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4122 final int newSmallest = taskConfig.smallestScreenWidthDp;
4123 final boolean crosses = record.crossesHorizontalSizeThreshold(oldSmallest, newSmallest)
4124 || record.crossesVerticalSizeThreshold(oldSmallest, newSmallest);
4125 if (!crosses) {
4126 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4127 }
4128 }
4129 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4130 }
4131
4132 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4133 Configuration oldTaskOverride, int taskChanges) {
4134 if (taskChanges == 0) {
4135 // {@link Configuration#diff} doesn't catch changes from unset values.
4136 // Check for changes we care about.
4137 if (oldTaskOverride.orientation != taskConfig.orientation) {
4138 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4139 }
4140 // We want to explicitly track situations where the size configuration goes from
4141 // undefined to defined. We don't care about crossing the threshold in that case,
4142 // because there is no threshold.
4143 final int oldHeight = oldTaskOverride.screenHeightDp;
4144 final int newHeight = taskConfig.screenHeightDp;
4145 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4146 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4147 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4148 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4149 }
4150 final int oldWidth = oldTaskOverride.screenWidthDp;
4151 final int newWidth = taskConfig.screenWidthDp;
4152 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4153 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4154 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4155 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4156 }
4157 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4158 final int newSmallest = taskConfig.smallestScreenWidthDp;
4159 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4160 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4161 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4162 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4163 }
4164 }
4165 return taskChanges;
4166 }
4167
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004168 private static boolean isResizeOnlyChange(int change) {
4169 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4170 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4171 }
4172
Wale Ogunwale83301a92015-09-24 15:54:08 -07004173 private void relaunchActivityLocked(
4174 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4175 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4176 return;
4177 }
4178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004179 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004180 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004181 if (andResume) {
4182 results = r.results;
4183 newIntents = r.newIntents;
4184 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004185 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4186 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004187 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004188 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004189 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004190 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004192 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004193
Craig Mautner34b73df2014-01-12 21:11:08 -08004194 mStackSupervisor.removeChildActivityContainers(r);
4195
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004196 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004197 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4198 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004199 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004200 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4201 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004202 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004203 // Note: don't need to call pauseIfSleepingLocked() here, because
4204 // the caller will only pass in 'andResume' if this activity is
4205 // currently resumed, which implies we aren't sleeping.
4206 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004207 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004208 }
4209
4210 if (andResume) {
4211 r.results = null;
4212 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004213 r.state = ActivityState.RESUMED;
4214 } else {
4215 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4216 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004217 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004218 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004219
4220 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004221 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4222 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4223 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4224 final ActivityRecord r = activities.get(activityNdx);
4225 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004226 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004227 }
4228 if (r.fullscreen && !r.finishing) {
4229 return false;
4230 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004231 }
4232 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004233 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004234 if (r == null) {
4235 return false;
4236 }
4237 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4238 + " would have returned true for r=" + r);
4239 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004240 }
4241
4242 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004243 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4244 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4245 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4246 final ActivityRecord r = activities.get(activityNdx);
4247 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004248 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004249 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004250 }
4251 }
4252 }
4253
Wale Ogunwale540e1232015-05-01 15:35:39 -07004254 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4255 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004256 boolean didSomething = false;
4257 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004258 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004259 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4260 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4261 int numActivities = activities.size();
4262 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4263 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004264 final boolean sameComponent =
4265 (r.packageName.equals(packageName) && (filterByClasses == null
4266 || filterByClasses.contains(r.realActivity.getClassName())))
4267 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004268 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004269 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004270 && (r.app == null || evenPersistent || !r.app.persistent)) {
4271 if (!doit) {
4272 if (r.finishing) {
4273 // If this activity is just finishing, then it is not
4274 // interesting as far as something to stop.
4275 continue;
4276 }
4277 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004278 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004279 if (r.isHomeActivity()) {
4280 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4281 Slog.i(TAG, "Skip force-stop again " + r);
4282 continue;
4283 } else {
4284 homeActivity = r.realActivity;
4285 }
4286 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004287 didSomething = true;
4288 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004289 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004290 if (r.app != null) {
4291 r.app.removed = true;
4292 }
4293 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004294 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004295 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004296 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4297 true)) {
4298 // r has been deleted from mActivities, accommodate.
4299 --numActivities;
4300 --activityNdx;
4301 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004302 }
4303 }
4304 }
4305 return didSomething;
4306 }
4307
Dianne Hackborn09233282014-04-30 11:33:59 -07004308 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004309 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4310 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004311 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004312 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004313 if (task.getTopActivity() == null) {
4314 continue;
4315 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004316 ActivityRecord r = null;
4317 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004318 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004319 int numActivities = 0;
4320 int numRunning = 0;
4321 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004322 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004323 continue;
4324 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004325 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004326 tmp = activities.get(activityNdx);
4327 if (tmp.finishing) {
4328 continue;
4329 }
4330 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004331
Craig Mautneraab647e2013-02-28 16:31:36 -08004332 // Initialize state for next task if needed.
4333 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4334 top = r;
4335 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004336 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004337
4338 // Add 'r' into the current task.
4339 numActivities++;
4340 if (r.app != null && r.app.thread != null) {
4341 numRunning++;
4342 }
4343
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004344 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004345 TAG, r.intent.getComponent().flattenToShortString()
4346 + ": task=" + r.task);
4347 }
4348
4349 RunningTaskInfo ci = new RunningTaskInfo();
4350 ci.id = task.taskId;
4351 ci.baseActivity = r.intent.getComponent();
4352 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004353 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004354 if (focusedStack && topTask) {
4355 // Give the latest time to ensure foreground task can be sorted
4356 // at the first, because lastActiveTime of creating task is 0.
4357 ci.lastActiveTime = System.currentTimeMillis();
4358 topTask = false;
4359 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004360
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004361 if (top.task != null) {
4362 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004363 }
4364 ci.numActivities = numActivities;
4365 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004366 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004367 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004368 }
4369
4370 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004371 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004372 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004373 if (top >= 0) {
4374 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4375 int activityTop = activities.size() - 1;
4376 if (activityTop > 0) {
4377 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4378 "unhandled-back", true);
4379 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004380 }
4381 }
4382
Craig Mautner6b74cb52013-09-27 17:02:21 -07004383 /**
4384 * Reset local parameters because an app's activity died.
4385 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004386 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004387 */
4388 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004389 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004390 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004391 "App died while pausing: " + mPausingActivity);
4392 mPausingActivity = null;
4393 }
4394 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4395 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004396 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004397 }
4398
Craig Mautner19091252013-10-05 00:03:53 -07004399 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004400 }
4401
Craig Mautnercae015f2013-02-08 14:31:27 -08004402 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4404 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4405 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4406 final ActivityRecord r = activities.get(activityNdx);
4407 if (r.app == app) {
4408 Slog.w(TAG, " Force finishing activity "
4409 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004410 // Force the destroy to skip right to removal.
4411 r.app = null;
4412 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004413 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004414 }
4415 }
4416 }
4417
Dianne Hackborn390517b2013-05-30 15:03:32 -07004418 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004419 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004420 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004421 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4422 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004423 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4424 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004425 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004426 " Task id #" + task.taskId + "\n" +
4427 " mFullscreen=" + task.mFullscreen + "\n" +
4428 " mBounds=" + task.mBounds + "\n" +
4429 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004430 if (printed) {
4431 header = null;
4432 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004433 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004434 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004435 }
4436
4437 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4438 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4439
4440 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4442 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004443 }
4444 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004445 final int top = mTaskHistory.size() - 1;
4446 if (top >= 0) {
4447 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4448 int listTop = list.size() - 1;
4449 if (listTop >= 0) {
4450 activities.add(list.get(listTop));
4451 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004452 }
4453 } else {
4454 ItemMatcher matcher = new ItemMatcher();
4455 matcher.build(name);
4456
Craig Mautneraab647e2013-02-28 16:31:36 -08004457 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4458 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4459 if (matcher.match(r1, r1.intent.getComponent())) {
4460 activities.add(r1);
4461 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004462 }
4463 }
4464 }
4465
4466 return activities;
4467 }
4468
4469 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004470 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004471
4472 // All activities that came from the package must be
4473 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004474 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4475 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4476 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4477 final ActivityRecord a = activities.get(activityNdx);
4478 if (a.info.packageName.equals(packageName)) {
4479 a.forceNewConfig = true;
4480 if (starting != null && a == starting && a.visible) {
4481 a.startFreezingScreenLocked(starting.app,
4482 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4483 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004484 }
4485 }
4486 }
4487
4488 return starting;
4489 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004490
Craig Mautner299f9602015-01-26 09:47:33 -08004491 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004492 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004493 }
4494
Wale Ogunwale000957c2015-04-03 08:19:12 -07004495 /**
4496 * Removes the input task from this stack.
4497 * @param task to remove.
4498 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004499 * @param moving task to another stack. In the case we are moving we don't want to perform
4500 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004501 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004502 void removeTask(TaskRecord task, String reason, boolean moving) {
4503 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004504 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004505 mWindowManager.removeTask(task.taskId);
4506 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004507
Craig Mautner04a0ea62014-01-13 12:51:26 -08004508 final ActivityRecord r = mResumedActivity;
4509 if (r != null && r.task == task) {
4510 mResumedActivity = null;
4511 }
4512
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004513 final int taskNdx = mTaskHistory.indexOf(task);
4514 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004515 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4516 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4517 if (!nextTask.isOverHomeStack()) {
4518 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4519 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004520 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004521 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004522 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004523
Wale Ogunwale040b4702015-08-06 18:10:50 -07004524 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004525 final boolean isVoiceSession = task.voiceSession != null;
4526 if (isVoiceSession) {
4527 try {
4528 task.voiceSession.taskFinished(task.intent, task.taskId);
4529 } catch (RemoteException e) {
4530 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004531 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004532 if (task.autoRemoveFromRecents() || isVoiceSession) {
4533 // Task creator asked to remove this when done, or this task was a voice
4534 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004535 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004536 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004537 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004538 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004539
4540 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004541 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004542 // We only need to adjust focused stack if this stack is in focus.
4543 if (isOnHomeDisplay() && mStackSupervisor.isFrontStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004544 String myReason = reason + " leftTaskHistoryEmpty";
4545 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004546 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004547 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004548 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004549 if (mStacks != null) {
4550 mStacks.remove(this);
4551 mStacks.add(0, this);
4552 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004553 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004554 mActivityContainer.onTaskListEmptyLocked();
4555 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004556 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004557
4558 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004559 }
4560
Dianne Hackborn91097de2014-04-04 18:02:06 -07004561 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4562 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4563 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004564 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4565 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004566 // add the task to stack first, mTaskPositioner might need the stack association
4567 addTask(task, toTop, false);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004568 if (mTaskPositioner != null) {
Filip Gruszczynski9b1ce522015-08-20 18:37:19 -07004569 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, info.initialLayout);
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004570 } else if (mBounds != null && task.mResizeable) {
4571 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004572 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004573 return task;
4574 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004575
4576 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004577 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004578 }
4579
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004580 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004581 task.stack = this;
4582 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004583 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004584 } else {
4585 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004586 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004587 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004588 if (!moving && task.voiceSession != null) {
4589 try {
4590 task.voiceSession.taskStarted(task.intent, task.taskId);
4591 } catch (RemoteException e) {
4592 }
4593 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004594 }
4595
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004596 void positionTask(final TaskRecord task, int position, boolean moving) {
4597 task.stack = this;
4598 insertTaskAtPosition(task, position);
4599 if (!moving && task.voiceSession != null) {
4600 try {
4601 task.voiceSession.taskStarted(task.intent, task.taskId);
4602 } catch (RemoteException e) {
4603 }
4604 }
4605 }
4606
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004607 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004608 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004609 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004610 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4611 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4612 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4613 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004614 bounds, task.mOverrideConfig, !r.isHomeActivity());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004615 r.taskConfigOverride = task.mOverrideConfig;
4616 }
4617
4618 private void setAppTask(ActivityRecord r, TaskRecord task) {
4619 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004620 task.updateOverrideConfiguration(bounds);
4621 mWindowManager.setAppTask(
4622 r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004623 r.taskConfigOverride = task.mOverrideConfig;
4624 }
4625
Craig Mautnerc00204b2013-03-05 15:02:14 -08004626 public int getStackId() {
4627 return mStackId;
4628 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004629
4630 @Override
4631 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004632 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4633 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004634 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004635
Craig Mautner15df08a2015-04-01 12:17:18 -07004636 void onLockTaskPackagesUpdatedLocked() {
4637 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4638 mTaskHistory.get(taskNdx).setLockTaskAuth();
4639 }
4640 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004641}