blob: 172147070c817e1bb01ad591d83fb17ffc022512 [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)");
riddle_hsu9caeef72015-10-20 16:34:05 +0800996 if (r.state == ActivityState.PAUSING) {
997 r.state = ActivityState.PAUSED;
998 if (r.finishing) {
999 if (DEBUG_PAUSE) Slog.v(TAG,
1000 "Executing finish of failed to pause activity: " + r);
1001 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1002 }
louis_chang047dfd42015-04-08 16:35:55 +08001003 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001004 }
1005 }
1006 }
1007
Craig Mautnera0026042014-04-23 11:45:37 -07001008 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1009 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001010 if (r.state != ActivityState.STOPPING) {
1011 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1012 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1013 return;
1014 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001015 if (persistentState != null) {
1016 r.persistentState = persistentState;
1017 mService.notifyTaskPersisterLocked(r.task, false);
1018 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001019 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001020 if (icicle != null) {
1021 // If icicle is null, this is happening due to a timeout, so we
1022 // haven't really saved the state.
1023 r.icicle = icicle;
1024 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001025 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001026 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001027 }
1028 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001029 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001030 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1031 r.stopped = true;
1032 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001033 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1034 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001035 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001036 if (r.finishing) {
1037 r.clearOptionsLocked();
1038 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001039 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001040 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001041 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001042 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001043 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001044 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001045 }
1046 }
1047 }
1048
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001049 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001050 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001051 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001052
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001053 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001054 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001055 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001056 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001057 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001058 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001059 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001060 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001061 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1062 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001063 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001064 if (prev.configDestroy) {
1065 // The previous is being paused because the configuration
1066 // is changing, which means it is actually stopping...
1067 // To juggle the fact that we are also starting a new
1068 // instance right now, we need to first completely stop
1069 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001070 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001071 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001072 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001073 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001074 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001075 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001076 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1077 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001078 // If we already have a few activities waiting to stop,
1079 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001080 // them out. Or if r is the last of activity of the last task the stack
1081 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001082 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001083 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001084 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001085 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001086 }
1087 }
1088 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001089 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001090 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001091 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001092 // It is possible the activity was freezing the screen before it was paused.
1093 // In that case go ahead and remove the freeze this activity has on the screen
1094 // since it is no longer visible.
1095 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001096 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001097 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001098
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001099 if (resumeNext) {
1100 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1101 if (!mService.isSleepingOrShuttingDown()) {
1102 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1103 } else {
1104 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001105 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001106 if (top == null || (prev != null && top != prev)) {
1107 // If there are no more activities available to run,
1108 // do resume anyway to start something. Also if the top
1109 // activity on the stack is not the just paused activity,
1110 // we need to go ahead and resume it to ensure we complete
1111 // an in-flight app switch.
1112 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1113 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001114 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001116
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001117 if (prev != null) {
1118 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001119
Craig Mautner525f3d92013-05-07 14:01:50 -07001120 if (prev.app != null && prev.cpuTimeAtResume > 0
1121 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001122 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1123 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001124 if (diff > 0) {
1125 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1126 synchronized (bsi) {
1127 BatteryStatsImpl.Uid.Proc ps =
1128 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001129 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001130 if (ps != null) {
1131 ps.addForegroundTimeLocked(diff);
1132 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001133 }
1134 }
1135 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001136 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001137 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001138
1139 // Notfiy when the task stack has changed
1140 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 }
1142
1143 /**
1144 * Once we know that we have asked an application to put an activity in
1145 * the resumed state (either by launching it or explicitly telling it),
1146 * this function updates the rest of our state to match that fact.
1147 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001148 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001149 next.idle = false;
1150 next.results = null;
1151 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001152
Chong Zhang85ee6542015-10-02 13:36:38 -07001153 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001154 ProcessRecord app = next.task.mActivities.get(0).app;
1155 if (app != null && app != mService.mHomeProcess) {
1156 mService.mHomeProcess = app;
1157 }
1158 }
1159
Craig Mautner07566322013-09-26 16:42:55 -07001160 if (next.nowVisible) {
1161 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001162 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001163 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164
1165 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001166 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001167
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001168 mStackSupervisor.reportResumedActivityLocked(next);
1169
1170 next.resumeKeyDispatchingLocked();
1171 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001172
1173 // Mark the point when the activity is resuming
1174 // TODO: To be more accurate, the mark should be before the onCreate,
1175 // not after the onResume. But for subsequent starts, onResume is fine.
1176 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001177 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001178 } else {
1179 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1180 }
Winson Chung376543b2014-05-21 17:43:28 -07001181
George Mount6ba042b2014-07-28 11:12:28 -07001182 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001183
1184 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1185 // When resuming an activity, require it to call requestVisibleBehind() again.
1186 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1187 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001188 }
1189
Craig Mautner2568c3a2015-03-26 14:22:34 -07001190 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001191 r.visible = visible;
1192 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001193 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001194 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001195 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001196 container.setVisible(visible);
1197 }
1198 }
1199
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001200 // Find the first visible activity above the passed activity and if it is translucent return it
1201 // otherwise return null;
1202 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1203 TaskRecord task = r.task;
1204 if (task == null) {
1205 return null;
1206 }
1207
1208 ActivityStack stack = task.stack;
1209 if (stack == null) {
1210 return null;
1211 }
1212
1213 int stackNdx = mStacks.indexOf(stack);
1214
1215 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1216 int taskNdx = tasks.indexOf(task);
1217
1218 ArrayList<ActivityRecord> activities = task.mActivities;
1219 int activityNdx = activities.indexOf(r) + 1;
1220
1221 final int numStacks = mStacks.size();
1222 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001223 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001224 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001225 final int numTasks = tasks.size();
1226 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001227 final TaskRecord currentTask = tasks.get(taskNdx);
1228 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001229 final int numActivities = activities.size();
1230 while (activityNdx < numActivities) {
1231 final ActivityRecord activity = activities.get(activityNdx);
1232 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001233 return historyStack.mFullscreen
1234 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001235 }
1236 ++activityNdx;
1237 }
1238 activityNdx = 0;
1239 ++taskNdx;
1240 }
1241 taskNdx = 0;
1242 ++stackNdx;
1243 }
1244
1245 return null;
1246 }
1247
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001248 private ActivityStack getNextVisibleStackLocked() {
1249 ArrayList<ActivityStack> stacks = mStacks;
1250 final ActivityRecord parent = mActivityContainer.mParentActivity;
1251 if (parent != null) {
1252 stacks = parent.task.stack.mStacks;
1253 }
1254 if (stacks != null) {
1255 for (int i = stacks.size() - 1; i >= 0; --i) {
1256 ActivityStack stack = stacks.get(i);
1257 if (stack != this && stack.isStackVisibleLocked()) {
1258 return stack;
1259 }
1260 }
1261 }
1262 return null;
1263 }
1264
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001265 /** Returns true if the stack contains a fullscreen task. */
1266 private boolean hasFullscreenTask() {
1267 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1268 final TaskRecord task = mTaskHistory.get(i);
1269 if (task.mFullscreen) {
1270 return true;
1271 }
1272 }
1273 return false;
1274 }
1275
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001276 private boolean hasTranslucentActivity(ActivityStack stack) {
1277 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1278 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1279 final TaskRecord task = tasks.get(taskNdx);
1280 final ArrayList<ActivityRecord> activities = task.mActivities;
1281 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1282 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001283
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001284 // Conditions for an activity to obscure the stack we're
1285 // examining:
1286 // 1. Not Finishing AND Visible AND:
1287 // 2. Either:
1288 // - Full Screen Activity OR
1289 // - On top of Home and our stack is NOT home
1290 if (!r.finishing && r.visible && (r.fullscreen ||
1291 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1292 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001293 }
1294 }
1295 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001296 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001297 }
1298
1299 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001300 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001301 if (!isAttached()) {
1302 return false;
1303 }
1304
1305 if (mStackSupervisor.isFrontStack(this)) {
1306 return true;
1307 }
1308
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001309 final int stackIndex = mStacks.indexOf(this);
1310
1311 if (stackIndex == mStacks.size() - 1) {
1312 Slog.wtf(TAG,
1313 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1314 return false;
1315 }
1316
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001317 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1318 final int focusedStackId = focusedStack.mStackId;
1319
1320 if (mStackId == DOCKED_STACK_ID) {
1321 // Docked stack is always visible, except in the case where the home activity
1322 // is the top running activity in the focused home stack.
1323 if (focusedStackId != HOME_STACK_ID) {
1324 return true;
1325 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001326 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001327 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1328 }
1329
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001330 final int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1331 if (focusedStackId == DOCKED_STACK_ID && stackIndex == belowFocusedIndex) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001332 // Stacks directly behind the docked stack are always visible.
1333 return true;
1334 }
1335
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001336 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1337 && hasTranslucentActivity(focusedStack)) {
1338 // Stacks behind the fullscreen stack with a translucent activity are always
1339 // visible so they can act as a backdrop to the translucent activity.
1340 // For example, dialog activities
1341 if (stackIndex == belowFocusedIndex) {
1342 return true;
1343 }
1344 if (belowFocusedIndex >= 0) {
1345 final ActivityStack stack = mStacks.get(belowFocusedIndex);
1346 if (stack.mStackId == DOCKED_STACK_ID && stackIndex == (belowFocusedIndex - 1)) {
1347 // The stack behind the docked stack is also visible so we can have a complete
1348 // backdrop to the translucent activity when the docked stack is up.
1349 return true;
1350 }
1351 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001352 }
1353
1354 if (mStackId >= FIRST_STATIC_STACK_ID && mStackId <= LAST_STATIC_STACK_ID) {
1355 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001356 return false;
1357 }
1358
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001359 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001360 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001361
1362 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1363 continue;
1364 }
1365
1366 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001367 || stack.mStackId == HOME_STACK_ID
1368 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
1369 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001370 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001371 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001372
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001373 if (!hasTranslucentActivity(stack)) {
1374 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001375 }
1376 }
1377
1378 return true;
1379 }
1380
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001381 final int rankTaskLayers(int baseLayer) {
1382 int layer = 0;
1383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1384 final TaskRecord task = mTaskHistory.get(taskNdx);
1385 ActivityRecord r = task.topRunningActivityLocked();
1386 if (r == null || r.finishing || !r.visible) {
1387 task.mLayerRank = -1;
1388 } else {
1389 task.mLayerRank = baseLayer + layer++;
1390 }
1391 }
1392 return layer;
1393 }
1394
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395 /**
1396 * Make sure that all activities that need to be visible (that is, they
1397 * currently can be seen by the user) actually are.
1398 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001399 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1400 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001401 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001402 if (top == null) {
1403 return;
1404 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001405 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1406 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 + " configChanges=0x" + Integer.toHexString(configChanges));
1408
Craig Mautner5eda9b32013-07-02 11:58:16 -07001409 if (mTranslucentActivityWaiting != top) {
1410 mUndrawnActivitiesBelowTopTranslucent.clear();
1411 if (mTranslucentActivityWaiting != null) {
1412 // Call the callback with a timeout indication.
1413 notifyActivityDrawnLocked(null);
1414 mTranslucentActivityWaiting = null;
1415 }
1416 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1417 }
1418
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 // If the top activity is not fullscreen, then we need to
1420 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001421 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001422 final boolean stackInvisible = !isStackVisibleLocked();
1423 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001424 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001425
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001426 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001427 final TaskRecord task = mTaskHistory.get(taskNdx);
1428 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001429
Craig Mautnerd44711d2013-02-23 11:24:36 -08001430 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1431 final ActivityRecord r = activities.get(activityNdx);
1432 if (r.finishing) {
1433 continue;
1434 }
1435 if (aboveTop && r != top) {
1436 continue;
1437 }
1438 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001439 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1440 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001441 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001442 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1443 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001444 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001445
Craig Mautnerd44711d2013-02-23 11:24:36 -08001446 // First: if this is not the current activity being started, make
1447 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001448 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001449 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001450 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001451
1452 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001453 // This activity needs to be visible, but isn't even running...
1454 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001455 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1456 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001457 if (r != starting) {
1458 r.startFreezingScreenLocked(r.app, configChanges);
1459 }
1460 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001461 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1462 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001463 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001464 }
1465 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001466 mStackSupervisor.startSpecificActivityLocked(
1467 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001468 if (activityNdx >= activities.size()) {
1469 // Record may be removed if its process needs to restart.
1470 activityNdx = activities.size() - 1;
1471 } else {
1472 noStackActivityResumed = false;
1473 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001474 }
1475
1476 } else if (r.visible) {
1477 // If this activity is already visible, then there is nothing
1478 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001479 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1480 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001482 try {
George Mount6ba042b2014-07-28 11:12:28 -07001483 if (r.returningOptions != null) {
1484 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1485 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001486 }
1487 } catch(RemoteException e) {
1488 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001489 if (r.state == ActivityState.RESUMED) {
1490 noStackActivityResumed = false;
1491 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001492 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001493 // This activity is not currently visible, but is running.
1494 // Tell it to become visible.
1495 r.visible = true;
1496 if (r.state != ActivityState.RESUMED && r != starting) {
1497 // If this activity is paused, tell it
1498 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001499 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1500 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001501 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001502 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001503 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001504 mUndrawnActivitiesBelowTopTranslucent.add(r);
1505 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001506 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001507 r.sleeping = false;
1508 r.app.pendingUiClean = true;
1509 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1510 r.stopFreezingScreenLocked(false);
1511 } catch (Exception e) {
1512 // Just skip on any failure; we'll make it
1513 // visible when it next restarts.
1514 Slog.w(TAG, "Exception thrown making visibile: "
1515 + r.intent.getComponent(), e);
1516 }
1517 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001518 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519
Craig Mautnerd44711d2013-02-23 11:24:36 -08001520 // Aggregate current change flags.
1521 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522
Craig Mautnerd44711d2013-02-23 11:24:36 -08001523 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001524 // At this point, nothing else needs to be shown in this task.
1525 behindFullscreenActivity = true;
1526 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001527 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001528 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001529 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001530 behindFullscreenActivity = true;
1531 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001532 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001533 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001534 }
1535 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001536 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001537 "Make invisible? " + r + " finishing=" + r.finishing
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001538 + " state=" + r.state + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001539 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001540 // Now for any activities that aren't visible to the user, make
1541 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001543 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001545 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001546 switch (r.state) {
1547 case STOPPING:
1548 case STOPPED:
1549 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001550 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1551 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001552 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1553 }
1554 break;
1555
1556 case INITIALIZING:
1557 case RESUMED:
1558 case PAUSING:
1559 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001560 // This case created for transitioning activities from
1561 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001562 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001563 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001564 } else {
1565 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1566 mStackSupervisor.mStoppingActivities.add(r);
1567 }
1568 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001569 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001570 break;
1571
1572 default:
1573 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 }
1575 } catch (Exception e) {
1576 // Just skip on any failure; we'll make it
1577 // visible when it next restarts.
1578 Slog.w(TAG, "Exception thrown making hidden: "
1579 + r.intent.getComponent(), e);
1580 }
1581 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001582 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 }
1585 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001586 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1587 // The visibility of tasks and the activities they contain in freeform stack are
1588 // determined individually unlike other stacks where the visibility or fullscreen
1589 // status of an activity in a previous task affects other.
1590 behindFullscreenActivity = stackInvisible;
1591 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001592 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001593
1594 if (mTranslucentActivityWaiting != null &&
1595 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1596 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1597 notifyActivityDrawnLocked(null);
1598 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 }
Craig Mautner58547802013-03-05 08:23:53 -08001600
Todd Kennedyaab56db2015-01-30 09:39:53 -08001601 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001602 mTranslucentActivityWaiting = r;
1603 mUndrawnActivitiesBelowTopTranslucent.clear();
1604 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1605 }
1606
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001607 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1608 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1609 final TaskRecord task = mTaskHistory.get(taskNdx);
1610 final ArrayList<ActivityRecord> activities = task.mActivities;
1611 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1612 final ActivityRecord r = activities.get(activityNdx);
1613 if ( r.appTimeTracker != except) {
1614 r.appTimeTracker = null;
1615 }
1616 }
1617 }
1618 }
1619
Craig Mautner5eda9b32013-07-02 11:58:16 -07001620 /**
1621 * Called as activities below the top translucent activity are redrawn. When the last one is
1622 * redrawn notify the top activity by calling
1623 * {@link Activity#onTranslucentConversionComplete}.
1624 *
1625 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1626 * occurred and the activity will be notified immediately.
1627 */
1628 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001629 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001630 if ((r == null)
1631 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1632 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1633 // The last undrawn activity below the top has just been drawn. If there is an
1634 // opaque activity at the top, notify it that it can become translucent safely now.
1635 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1636 mTranslucentActivityWaiting = null;
1637 mUndrawnActivitiesBelowTopTranslucent.clear();
1638 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1639
Craig Mautner71dd1b62014-02-18 15:48:52 -08001640 if (waitingActivity != null) {
1641 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1642 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1643 try {
1644 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1645 waitingActivity.appToken, r != null);
1646 } catch (RemoteException e) {
1647 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001648 }
1649 }
1650 }
1651 }
1652
Craig Mautnera61bc652013-10-28 15:43:18 -07001653 /** If any activities below the top running one are in the INITIALIZING state and they have a
1654 * starting window displayed then remove that starting window. It is possible that the activity
1655 * in this state will never resumed in which case that starting window will be orphaned. */
1656 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001657 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001658 boolean aboveTop = true;
1659 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1660 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1661 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1662 final ActivityRecord r = activities.get(activityNdx);
1663 if (aboveTop) {
1664 if (r == topActivity) {
1665 aboveTop = false;
1666 }
1667 continue;
1668 }
1669
1670 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001671 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1672 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001673 r.mStartingWindowShown = false;
1674 mWindowManager.removeAppStartingWindow(r.appToken);
1675 }
1676 }
1677 }
1678 }
1679
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 /**
1681 * Ensure that the top activity in the stack is resumed.
1682 *
1683 * @param prev The previously resumed activity, for when in the process
1684 * of pausing; can be null to call from elsewhere.
1685 *
1686 * @return Returns true if something is being resumed, or false if
1687 * nothing happened.
1688 */
1689 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001690 return resumeTopActivityLocked(prev, null);
1691 }
1692
1693 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001694 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001695 // Don't even start recursing.
1696 return false;
1697 }
1698
1699 boolean result = false;
1700 try {
1701 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001702 mStackSupervisor.inResumeTopActivity = true;
1703 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1704 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001705 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001706 }
Craig Mautner544efa72014-09-04 16:41:20 -07001707 result = resumeTopActivityInnerLocked(prev, options);
1708 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001709 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001710 }
1711 return result;
1712 }
1713
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001714 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001715 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001716
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001717 if (!mService.mBooting && !mService.mBooted) {
1718 // Not ready yet!
1719 return false;
1720 }
1721
Craig Mautnerdf88d732014-01-27 09:21:32 -08001722 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001723 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001724 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001725 // Do not resume this stack if its parent is not resumed.
1726 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1727 return false;
1728 }
1729
Craig Mautnera61bc652013-10-28 15:43:18 -07001730 cancelInitializingActivities();
1731
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001733 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734
1735 // Remember how we'll process this pause/resume situation, and ensure
1736 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001737 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1738 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001739
Craig Mautner84984fa2014-06-19 11:19:20 -07001740 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001742 // There are no more activities!
1743 final String reason = "noMoreActivities";
1744 if (!mFullscreen) {
1745 // Try to move focus to the next visible stack with a running activity if this
1746 // stack is not covering the entire screen.
1747 final ActivityStack stack = getNextVisibleStackLocked();
1748 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1749 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1750 }
1751 }
1752 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001753 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001754 if (DEBUG_STATES) Slog.d(TAG_STATES,
1755 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001756 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001757 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001758 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1759 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1760 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001761 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762 }
1763
1764 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001765
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001766 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001767 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1768 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 // Make sure we have executed any pending transitions, since there
1770 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001771 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001773 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001774 if (DEBUG_STATES) Slog.d(TAG_STATES,
1775 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001776 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 return false;
1778 }
1779
Craig Mautner525f3d92013-05-07 14:01:50 -07001780 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001781 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001782 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001783 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001784 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001785 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001786 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001787 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001788 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001789 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001790 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001791 } else if (!isOnHomeDisplay()) {
1792 return false;
1793 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001794 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001795 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001796 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1797 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1798 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001799 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001800 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001801 }
1802
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 // If we are sleeping, and there is no resumed activity, and the top
1804 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001805 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001806 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001807 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 // Make sure we have executed any pending transitions, since there
1809 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001810 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001812 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001813 if (DEBUG_STATES) Slog.d(TAG_STATES,
1814 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001815 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001816 return false;
1817 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001818
1819 // Make sure that the user who owns this activity is started. If not,
1820 // we will just leave it as is because someone should be bringing
1821 // another user's activities to the top of the stack.
1822 if (mService.mStartedUsers.get(next.userId) == null) {
1823 Slog.w(TAG, "Skipping resume of top activity " + next
1824 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001825 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001826 return false;
1827 }
1828
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829 // The activity may be waiting for stop, but that is no longer
1830 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001831 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001832 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001833 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001834 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001836 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001838 // If we are currently pausing an activity, then don't do anything
1839 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001840 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001841 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001842 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001843 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001844 return false;
1845 }
1846
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001847 // Okay we are now going to start a switch, to 'next'. We may first
1848 // have to pause the current activity, but this is an important point
1849 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001850 // XXX "App Redirected" dialog is getting too many false positives
1851 // at this point, so turn off for now.
1852 if (false) {
1853 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1854 long now = SystemClock.uptimeMillis();
1855 final boolean inTime = mLastStartedActivity.startTime != 0
1856 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1857 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1858 final int nextUid = next.info.applicationInfo.uid;
1859 if (inTime && lastUid != nextUid
1860 && lastUid != next.launchedFromUid
1861 && mService.checkPermission(
1862 android.Manifest.permission.STOP_APP_SWITCHES,
1863 -1, next.launchedFromUid)
1864 != PackageManager.PERMISSION_GRANTED) {
1865 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1866 } else {
1867 next.startTime = now;
1868 mLastStartedActivity = next;
1869 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001870 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001871 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001872 mLastStartedActivity = next;
1873 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001874 }
Craig Mautner58547802013-03-05 08:23:53 -08001875
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001876 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1877
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001878 // We need to start pausing the current activity so the top one
1879 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001880 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1881 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001882 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001883 if (DEBUG_STATES) Slog.d(TAG_STATES,
1884 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001885 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001886 }
1887 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001888 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001889 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001890 // At this point we want to put the upcoming activity's process
1891 // at the top of the LRU list, since we know we will be needing it
1892 // very soon and it would be a waste to let it get killed if it
1893 // happens to be sitting towards the end.
1894 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001895 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001896 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001897 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 return true;
1899 }
1900
Christopher Tated3f175c2012-06-14 14:16:54 -07001901 // If the most recent activity was noHistory but was only stopped rather
1902 // than stopped+finished because the device went to sleep, we need to make
1903 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001904 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001905 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001906 if (DEBUG_STATES) Slog.d(TAG_STATES,
1907 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001908 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001909 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001910 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001911 }
1912
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001913 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001914 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1915 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001916 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001917 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1918 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001919 } else {
1920 // The next activity is already visible, so hide the previous
1921 // activity's windows right now so we can show the new one ASAP.
1922 // We only do this if the previous is finishing, which should mean
1923 // it is on top of the one being resumed so hiding it quickly
1924 // is good. Otherwise, we want to do the normal route of allowing
1925 // the resumed activity to be shown so we can decide if the
1926 // previous should actually be hidden depending on whether the
1927 // new one is found to be full-screen or not.
1928 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001929 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001930 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1931 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001932 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001933 + ", nowVisible=" + next.nowVisible);
1934 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001935 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001936 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001937 + ", waitingVisible="
1938 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1939 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001940 }
1941 }
1942 }
1943
Dianne Hackborne7f97212011-02-24 14:40:20 -08001944 // Launching this app's activity, make sure the app is no longer
1945 // considered stopped.
1946 try {
1947 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001948 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001949 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001950 } catch (IllegalArgumentException e) {
1951 Slog.w(TAG, "Failed trying to unstop package "
1952 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001953 }
1954
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 // We are starting up the next activity, so tell the window manager
1956 // that the previous one will be hidden soon. This way it can know
1957 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001958 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 if (prev != null) {
1960 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001961 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001962 "Prepare close transition: prev=" + prev);
1963 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001964 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001965 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001967 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001968 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1969 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001971 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001973 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1974 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001975 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001976 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001977 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001979 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001980 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001981 : next.mLaunchTaskBehind
1982 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1983 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 }
1985 }
Craig Mautner967212c2013-04-13 21:10:58 -07001986 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001987 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001988 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001989 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001990 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001992 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001993 }
1994 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001995
1996 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001997 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001998 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1999 if (opts != null) {
2000 resumeAnimOptions = opts.toBundle();
2001 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002002 next.applyOptionsLocked();
2003 } else {
2004 next.clearOptionsLocked();
2005 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006
Craig Mautnercf910b02013-04-23 11:23:27 -07002007 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002009 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002010
2011 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002012 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002013
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002014 // schedule launch ticks to collect information about slow apps.
2015 next.startLaunchTickingLocked();
2016
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002017 ActivityRecord lastResumedActivity =
2018 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002019 ActivityState lastState = next.state;
2020
2021 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002022
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002023 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 next.state = ActivityState.RESUMED;
2025 mResumedActivity = next;
2026 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002027 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002028 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002030 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002031
2032 // Have the window manager re-evaluate the orientation of
2033 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002034 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002035 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002036 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002037 mService.mConfiguration,
2038 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2039 if (config != null) {
2040 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002042 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002044
Craig Mautner525f3d92013-05-07 14:01:50 -07002045 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 // The configuration update wasn't able to keep the existing
2047 // instance of the activity, and instead started a new one.
2048 // We should be all done, but let's just make sure our activity
2049 // is still at the top and schedule another run if something
2050 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002051 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002052 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002053 "Activity config changed during resume: " + next
2054 + ", new next: " + nextNext);
2055 if (nextNext != next) {
2056 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002057 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002059 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002060 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002061 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002062 return true;
2063 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002064 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002065 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 }
Craig Mautner58547802013-03-05 08:23:53 -08002067
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 try {
2069 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002070 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 if (a != null) {
2072 final int N = a.size();
2073 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002074 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2075 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002076 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 }
2078 }
2079
2080 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002081 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 }
2083
Craig Mautner299f9602015-01-26 09:47:33 -08002084 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2085 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002086
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002087 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002088 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002089 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002090 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002091 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002092 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002093 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002094
Craig Mautner0eea92c2013-05-16 13:35:39 -07002095 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002097 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002098 } catch (Exception e) {
2099 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002100 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002101 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002103 if (lastStack != null) {
2104 lastStack.mResumedActivity = lastResumedActivity;
2105 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002106 Slog.i(TAG, "Restarting because process died: " + next);
2107 if (!next.hasBeenLaunched) {
2108 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002109 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2110 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002111 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002112 next.appToken, next.packageName, next.theme,
2113 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002114 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2115 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 }
George Mount2c92c972014-03-20 09:38:23 -07002117 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002118 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 return true;
2120 }
2121
2122 // From this point on, if something goes wrong there is no way
2123 // to recover the activity.
2124 try {
2125 next.visible = true;
2126 completeResumeLocked(next);
2127 } catch (Exception e) {
2128 // If any exception gets thrown, toss away this
2129 // activity and try the next one.
2130 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002131 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002132 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002133 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 return true;
2135 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 next.stopped = false;
2137
2138 } else {
2139 // Whoops, need to restart this activity!
2140 if (!next.hasBeenLaunched) {
2141 next.hasBeenLaunched = true;
2142 } else {
2143 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002144 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002145 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002146 mService.compatibilityInfoForPackageLocked(
2147 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002149 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002150 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002152 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002154 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002155 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 }
2157
Craig Mautnercf910b02013-04-23 11:23:27 -07002158 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 return true;
2160 }
2161
riddle_hsuc215a4f2014-12-27 12:10:45 +08002162 private TaskRecord getNextTask(TaskRecord targetTask) {
2163 final int index = mTaskHistory.indexOf(targetTask);
2164 if (index >= 0) {
2165 final int numTasks = mTaskHistory.size();
2166 for (int i = index + 1; i < numTasks; ++i) {
2167 TaskRecord task = mTaskHistory.get(i);
2168 if (task.userId == targetTask.userId) {
2169 return task;
2170 }
2171 }
2172 }
2173 return null;
2174 }
2175
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002176 private void insertTaskAtPosition(TaskRecord task, int position) {
2177 if (position >= mTaskHistory.size()) {
2178 insertTaskAtTop(task, null);
2179 return;
2180 }
2181 // Calculate maximum possible position for this task.
2182 int maxPosition = mTaskHistory.size();
2183 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002184 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002185 // Put non-current user tasks below current user tasks.
2186 while (maxPosition > 0) {
2187 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2188 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002189 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002190 break;
2191 }
2192 maxPosition--;
2193 }
2194 }
2195 position = Math.min(position, maxPosition);
2196 mTaskHistory.remove(task);
2197 mTaskHistory.add(position, task);
2198 updateTaskMovement(task, true);
2199 }
2200
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002201 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002202 // If the moving task is over home stack, transfer its return type to next task
2203 if (task.isOverHomeStack()) {
2204 final TaskRecord nextTask = getNextTask(task);
2205 if (nextTask != null) {
2206 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2207 }
2208 }
2209
Craig Mautner9c85c202013-10-04 20:11:26 -07002210 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002211 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002212 if (isOnHomeDisplay()) {
2213 ActivityStack lastStack = mStackSupervisor.getLastStack();
2214 final boolean fromHome = lastStack.isHomeStack();
2215 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002216 task.setTaskToReturnTo(fromHome
2217 ? lastStack.topTask() == null
2218 ? HOME_ACTIVITY_TYPE
2219 : lastStack.topTask().taskType
2220 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002221 }
2222 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002223 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002224 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002225
Craig Mautnerac6f8432013-07-17 13:24:59 -07002226 mTaskHistory.remove(task);
2227 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002228 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002229 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002230 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002231 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002232 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002233 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002234 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002235 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002236 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002237 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002238 break;
2239 }
2240 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002241 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002242 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002243 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002244 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002245 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002246
Craig Mautner8849a5e2013-04-02 16:41:03 -07002247 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002248 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002249 TaskRecord rTask = r.task;
2250 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002251 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2252 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002253 // Last activity in task had been removed or ActivityManagerService is reusing task.
2254 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002255 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002256 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002257 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002258 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002259 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 if (!newTask) {
2261 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002262 boolean startIt = true;
2263 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2264 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002265 if (task.getTopActivity() == null) {
2266 // All activities in task are finishing.
2267 continue;
2268 }
Craig Mautner70a86932013-02-28 22:37:44 -08002269 if (task == r.task) {
2270 // Here it is! Now, if this is not yet visible to the
2271 // user, then just add it without starting; it will
2272 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002273 if (!startIt) {
2274 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2275 + task, new RuntimeException("here").fillInStackTrace());
2276 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002277 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002278 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002279 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002280 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002281 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002282 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283 return;
2284 }
2285 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002286 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002287 startIt = false;
2288 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 }
2290 }
2291
2292 // Place a new activity at top of stack, so it is next to interact
2293 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002294
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 // If we are not placing the new activity frontmost, we do not want
2296 // to deliver the onUserLeaving callback to the actual frontmost
2297 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002298 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002299 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002300 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002301 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302 }
Craig Mautner70a86932013-02-28 22:37:44 -08002303
2304 task = r.task;
2305
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002307 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002308 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002309 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002310 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002311
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002312 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002313 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 // We want to show the starting preview window if we are
2315 // switching to a new task, or the next activity's process is
2316 // not currently running.
2317 boolean showStartingIcon = newTask;
2318 ProcessRecord proc = r.app;
2319 if (proc == null) {
2320 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2321 }
2322 if (proc == null || proc.thread == null) {
2323 showStartingIcon = true;
2324 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002325 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002327 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002328 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002331 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002332 ? r.mLaunchTaskBehind
2333 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2334 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002335 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 mNoAnimActivities.remove(r);
2337 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002338 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 boolean doShow = true;
2340 if (newTask) {
2341 // Even though this activity is starting fresh, we still need
2342 // to reset it to make sure we apply affinities to move any
2343 // existing activities from other tasks in to it.
2344 // If the caller has requested that the target task be
2345 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002346 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 resetTaskIfNeededLocked(r, r);
2348 doShow = topRunningNonDelayedActivityLocked(null) == r;
2349 }
George Mount70778d72014-07-01 16:33:45 -07002350 } else if (options != null && new ActivityOptions(options).getAnimationType()
2351 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2352 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002353 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002354 if (r.mLaunchTaskBehind) {
2355 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2356 // tell WindowManager that r is visible even though it is at the back of the stack.
2357 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002358 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002359 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // Figure out if we are transitioning from another activity that is
2361 // "has the same starting icon" as the next one. This allows the
2362 // window manager to keep the previous window it had previously
2363 // created, if it still had one.
2364 ActivityRecord prev = mResumedActivity;
2365 if (prev != null) {
2366 // We don't want to reuse the previous starting preview if:
2367 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002368 if (prev.task != r.task) {
2369 prev = null;
2370 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002372 else if (prev.nowVisible) {
2373 prev = null;
2374 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002376 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002377 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002378 mService.compatibilityInfoForPackageLocked(
2379 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002380 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002381 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002382 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 }
2384 } else {
2385 // If this is the first activity, don't do any fancy animations,
2386 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002387 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002388 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002389 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 }
2391 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002392 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002393 }
2394
2395 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002396 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002397 } else {
2398 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 }
2400 }
2401
Dianne Hackbornbe707852011-11-11 14:32:10 -08002402 final void validateAppTokensLocked() {
2403 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002404 mValidateAppTokens.ensureCapacity(numActivities());
2405 final int numTasks = mTaskHistory.size();
2406 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2407 TaskRecord task = mTaskHistory.get(taskNdx);
2408 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002409 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002410 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002411 }
Craig Mautner000f0022013-02-26 15:04:29 -08002412 TaskGroup group = new TaskGroup();
2413 group.taskId = task.taskId;
2414 mValidateAppTokens.add(group);
2415 final int numActivities = activities.size();
2416 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2417 final ActivityRecord r = activities.get(activityNdx);
2418 group.tokens.add(r.appToken);
2419 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002420 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002421 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002422 }
2423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 /**
2425 * Perform a reset of the given task, if needed as part of launching it.
2426 * Returns the new HistoryRecord at the top of the task.
2427 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002428 /**
2429 * Helper method for #resetTaskIfNeededLocked.
2430 * We are inside of the task being reset... we'll either finish this activity, push it out
2431 * for another task, or leave it as-is.
2432 * @param task The task containing the Activity (taskTop) that might be reset.
2433 * @param forceReset
2434 * @return An ActivityOptions that needs to be processed.
2435 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002436 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002437 ActivityOptions topOptions = null;
2438
2439 int replyChainEnd = -1;
2440 boolean canMoveOptions = true;
2441
2442 // We only do this for activities that are not the root of the task (since if we finish
2443 // the root, we may no longer have the task!).
2444 final ArrayList<ActivityRecord> activities = task.mActivities;
2445 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002446 final int rootActivityNdx = task.findEffectiveRootIndex();
2447 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002448 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002449 if (target.frontOfTask)
2450 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002451
2452 final int flags = target.info.flags;
2453 final boolean finishOnTaskLaunch =
2454 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2455 final boolean allowTaskReparenting =
2456 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2457 final boolean clearWhenTaskReset =
2458 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2459
2460 if (!finishOnTaskLaunch
2461 && !clearWhenTaskReset
2462 && target.resultTo != null) {
2463 // If this activity is sending a reply to a previous
2464 // activity, we can't do anything with it now until
2465 // we reach the start of the reply chain.
2466 // XXX note that we are assuming the result is always
2467 // to the previous activity, which is almost always
2468 // the case but we really shouldn't count on.
2469 if (replyChainEnd < 0) {
2470 replyChainEnd = i;
2471 }
2472 } else if (!finishOnTaskLaunch
2473 && !clearWhenTaskReset
2474 && allowTaskReparenting
2475 && target.taskAffinity != null
2476 && !target.taskAffinity.equals(task.affinity)) {
2477 // If this activity has an affinity for another
2478 // task, then we need to move it out of here. We will
2479 // move it as far out of the way as possible, to the
2480 // bottom of the activity stack. This also keeps it
2481 // correctly ordered with any activities we previously
2482 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002483 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002484 final ActivityRecord bottom =
2485 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002486 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002487 if (bottom != null && target.taskAffinity != null
2488 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002489 // If the activity currently at the bottom has the
2490 // same task affinity as the one we are moving,
2491 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002492 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002493 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002494 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002495 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002496 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002497 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002498 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002499 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002500 + " out to new task " + target.task);
2501 }
2502
Wale Ogunwale706ed792015-08-02 10:29:44 -07002503 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002504
Craig Mautner525f3d92013-05-07 14:01:50 -07002505 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002506 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2507 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002508 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509 if (p.finishing) {
2510 continue;
2511 }
2512
Craig Mautnere3a74d52013-02-22 14:14:58 -08002513 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002514 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002515 topOptions = p.takeOptionsLocked();
2516 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002517 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002518 }
2519 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002520 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2521 "Removing activity " + p + " from task=" + task + " adding to task="
2522 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002523 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2524 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002525 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002527
Wale Ogunwale706ed792015-08-02 10:29:44 -07002528 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002529 }
2530
Wale Ogunwale706ed792015-08-02 10:29:44 -07002531 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002532 if (VALIDATE_TOKENS) {
2533 validateAppTokensLocked();
2534 }
2535
2536 replyChainEnd = -1;
2537 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2538 // If the activity should just be removed -- either
2539 // because it asks for it, or the task should be
2540 // cleared -- then finish it and anything that is
2541 // part of its reply chain.
2542 int end;
2543 if (clearWhenTaskReset) {
2544 // In this case, we want to finish this activity
2545 // and everything above it, so be sneaky and pretend
2546 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002547 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002548 } else if (replyChainEnd < 0) {
2549 end = i;
2550 } else {
2551 end = replyChainEnd;
2552 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002553 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002554 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002555 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002556 if (p.finishing) {
2557 continue;
2558 }
2559 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002560 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002561 topOptions = p.takeOptionsLocked();
2562 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002563 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002564 }
2565 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002566 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002567 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002568 if (finishActivityLocked(
2569 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002570 end--;
2571 srcPos--;
2572 }
2573 }
2574 replyChainEnd = -1;
2575 } else {
2576 // If we were in the middle of a chain, well the
2577 // activity that started it all doesn't want anything
2578 // special, so leave it all as-is.
2579 replyChainEnd = -1;
2580 }
2581 }
2582
2583 return topOptions;
2584 }
2585
2586 /**
2587 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2588 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2589 * @param affinityTask The task we are looking for an affinity to.
2590 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2591 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2592 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2593 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002594 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002595 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002596 int replyChainEnd = -1;
2597 final int taskId = task.taskId;
2598 final String taskAffinity = task.affinity;
2599
2600 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2601 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002602 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2603
2604 // Do not operate on or below the effective root Activity.
2605 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002606 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002607 if (target.frontOfTask)
2608 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002609
2610 final int flags = target.info.flags;
2611 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2612 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2613
2614 if (target.resultTo != null) {
2615 // If this activity is sending a reply to a previous
2616 // activity, we can't do anything with it now until
2617 // we reach the start of the reply chain.
2618 // XXX note that we are assuming the result is always
2619 // to the previous activity, which is almost always
2620 // the case but we really shouldn't count on.
2621 if (replyChainEnd < 0) {
2622 replyChainEnd = i;
2623 }
2624 } else if (topTaskIsHigher
2625 && allowTaskReparenting
2626 && taskAffinity != null
2627 && taskAffinity.equals(target.taskAffinity)) {
2628 // This activity has an affinity for our task. Either remove it if we are
2629 // clearing or move it over to our task. Note that
2630 // we currently punt on the case where we are resetting a
2631 // task that is not at the top but who has activities above
2632 // with an affinity to it... this is really not a normal
2633 // case, and we will need to later pull that task to the front
2634 // and usually at that point we will do the reset and pick
2635 // up those remaining activities. (This only happens if
2636 // someone starts an activity in a new task from an activity
2637 // in a task that is not currently on top.)
2638 if (forceReset || finishOnTaskLaunch) {
2639 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002640 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2641 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002642 for (int srcPos = start; srcPos >= i; --srcPos) {
2643 final ActivityRecord p = activities.get(srcPos);
2644 if (p.finishing) {
2645 continue;
2646 }
Todd Kennedy539db512014-12-15 09:57:55 -08002647 finishActivityLocked(
2648 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002649 }
2650 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002651 if (taskInsertionPoint < 0) {
2652 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002653
Craig Mautner77878772013-03-04 19:46:24 -08002654 }
Craig Mautner77878772013-03-04 19:46:24 -08002655
2656 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002657 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2658 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2659 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002660 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002661 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002662 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002663 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002664
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002665 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2666 "Removing and adding activity " + p + " to stack at " + task
2667 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002668 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2669 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002670 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002671 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002672 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002673 if (VALIDATE_TOKENS) {
2674 validateAppTokensLocked();
2675 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002676
2677 // Now we've moved it in to place... but what if this is
2678 // a singleTop activity and we have put it on top of another
2679 // instance of the same activity? Then we drop the instance
2680 // below so it remains singleTop.
2681 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2682 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002683 int targetNdx = taskActivities.indexOf(target);
2684 if (targetNdx > 0) {
2685 ActivityRecord p = taskActivities.get(targetNdx - 1);
2686 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002687 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2688 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002689 }
2690 }
2691 }
2692 }
2693
2694 replyChainEnd = -1;
2695 }
2696 }
Craig Mautner77878772013-03-04 19:46:24 -08002697 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002698 }
2699
Craig Mautner8849a5e2013-04-02 16:41:03 -07002700 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002701 ActivityRecord newActivity) {
2702 boolean forceReset =
2703 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2704 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2705 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2706 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2707 forceReset = true;
2708 }
2709 }
2710
2711 final TaskRecord task = taskTop.task;
2712
2713 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2714 * for remaining tasks. Used for later tasks to reparent to task. */
2715 boolean taskFound = false;
2716
2717 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2718 ActivityOptions topOptions = null;
2719
Craig Mautner77878772013-03-04 19:46:24 -08002720 // Preserve the location for reparenting in the new task.
2721 int reparentInsertionPoint = -1;
2722
Craig Mautnere3a74d52013-02-22 14:14:58 -08002723 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2724 final TaskRecord targetTask = mTaskHistory.get(i);
2725
2726 if (targetTask == task) {
2727 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2728 taskFound = true;
2729 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002730 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2731 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002732 }
2733 }
2734
Craig Mautner70a86932013-02-28 22:37:44 -08002735 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002736 if (taskNdx >= 0) {
2737 do {
2738 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2739 } while (taskTop == null && taskNdx >= 0);
2740 }
Craig Mautner70a86932013-02-28 22:37:44 -08002741
Craig Mautnere3a74d52013-02-22 14:14:58 -08002742 if (topOptions != null) {
2743 // If we got some ActivityOptions from an activity on top that
2744 // was removed from the task, propagate them to the new real top.
2745 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002746 taskTop.updateOptionsLocked(topOptions);
2747 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002748 topOptions.abort();
2749 }
2750 }
2751
2752 return taskTop;
2753 }
2754
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2756 String resultWho, int requestCode, int resultCode, Intent data) {
2757
2758 if (callingUid > 0) {
2759 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002760 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 }
2762
2763 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2764 + " : who=" + resultWho + " req=" + requestCode
2765 + " res=" + resultCode + " data=" + data);
2766 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2767 try {
2768 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2769 list.add(new ResultInfo(resultWho, requestCode,
2770 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002771 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 return;
2773 } catch (Exception e) {
2774 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2775 }
2776 }
2777
2778 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2779 }
2780
Craig Mautner299f9602015-01-26 09:47:33 -08002781 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002782 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002783 ActivityRecord next = topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002784 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002785 if (next != r) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002786 if (next != null && (mStackId == FREEFORM_WORKSPACE_STACK_ID
2787 || mStackId == DOCKED_STACK_ID)) {
2788 // For freeform and docked stacks we always keep the focus within the stack as
2789 // long as there is a running activity in the stack that we can adjust focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002790 mService.setFocusedActivityLocked(next, myReason);
2791 return;
2792 } else {
2793 final TaskRecord task = r.task;
2794 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2795 // For non-fullscreen stack, we want to move the focus to the next visible
2796 // stack to prevent the home screen from moving to the top and obscuring
2797 // other visible stacks.
2798 if (!mFullscreen
2799 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2800 return;
2801 }
2802 // Move the home stack to the top if this stack is fullscreen or there is no
2803 // other visible stack.
2804 if (mStackSupervisor.moveHomeStackTaskToTop(
2805 task.getTaskToReturnTo(), myReason)) {
2806 // Activity focus was already adjusted. Nothing else to do...
2807 return;
2808 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002809 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002810 }
2811 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002812
2813 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002814 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002815 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002816 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002817 }
2818 }
2819
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002820 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2821 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2822 final String myReason = reason + " adjustFocusToNextVisibleStack";
2823 if (stack == null) {
2824 return false;
2825 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002826 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002827 if (top == null) {
2828 return false;
2829 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002830 mService.setFocusedActivityLocked(top, myReason);
2831 return true;
2832 }
2833
Craig Mautnerf3333272013-04-22 10:55:53 -07002834 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002835 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002836 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2837 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2838 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002839 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002840 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002841 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2842 "stop-no-history", false)) {
2843 // Activity was finished, no need to continue trying to schedule stop.
2844 adjustFocusedActivityLocked(r, "stopActivityFinished");
2845 r.resumeKeyDispatchingLocked();
2846 return;
2847 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002848 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002849 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002850 + " on stop because we're just sleeping");
2851 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002852 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002853 }
2854
2855 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002856 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002857 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858 try {
2859 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002860 if (DEBUG_STATES) Slog.v(TAG_STATES,
2861 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002862 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002863 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2864 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002865 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002866 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002868 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002869 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002870 r.setSleeping(true);
2871 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002872 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002873 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002874 } catch (Exception e) {
2875 // Maybe just ignore exceptions here... if the process
2876 // has crashed, our death notification will clean things
2877 // up.
2878 Slog.w(TAG, "Exception thrown during pause", e);
2879 // Just in case, assume it to be stopped.
2880 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002881 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002882 r.state = ActivityState.STOPPED;
2883 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002884 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 }
2886 }
2887 }
2888 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002889
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890 /**
2891 * @return Returns true if the activity is being finished, false if for
2892 * some reason it is being left as-is.
2893 */
2894 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002895 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002896 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002897 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2898 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002899 + ", result=" + resultCode + ", data=" + resultData
2900 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002901 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 return false;
2903 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904
Craig Mautnerd44711d2013-02-23 11:24:36 -08002905 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002906 return true;
2907 }
2908
Craig Mautnerd2328952013-03-05 12:46:26 -08002909 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002910 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2911 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2912 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2913 ActivityRecord r = activities.get(activityNdx);
2914 if (r.resultTo == self && r.requestCode == requestCode) {
2915 if ((r.resultWho == null && resultWho == null) ||
2916 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2917 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2918 false);
2919 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002920 }
2921 }
2922 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002923 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002924 }
2925
Todd Kennedy539db512014-12-15 09:57:55 -08002926 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002927 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002928 if (r != null && r.app == app) {
2929 // If the top running activity is from this crashing
2930 // process, then terminate it to avoid getting in a loop.
2931 Slog.w(TAG, " Force finishing activity "
2932 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002933 int taskNdx = mTaskHistory.indexOf(r.task);
2934 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002935 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002936 // Also terminate any activities below it that aren't yet
2937 // stopped, to avoid a situation where one will get
2938 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002939 --activityNdx;
2940 if (activityNdx < 0) {
2941 do {
2942 --taskNdx;
2943 if (taskNdx < 0) {
2944 break;
2945 }
2946 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2947 } while (activityNdx < 0);
2948 }
2949 if (activityNdx >= 0) {
2950 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002951 if (r.state == ActivityState.RESUMED
2952 || r.state == ActivityState.PAUSING
2953 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002954 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002955 Slog.w(TAG, " Force finishing activity "
2956 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002957 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002958 }
2959 }
2960 }
2961 }
2962 }
2963
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002964 final void finishVoiceTask(IVoiceInteractionSession session) {
2965 IBinder sessionBinder = session.asBinder();
2966 boolean didOne = false;
2967 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2968 TaskRecord tr = mTaskHistory.get(taskNdx);
2969 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2970 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2971 ActivityRecord r = tr.mActivities.get(activityNdx);
2972 if (!r.finishing) {
2973 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2974 false);
2975 didOne = true;
2976 }
2977 }
2978 }
2979 }
2980 if (didOne) {
2981 mService.updateOomAdjLocked();
2982 }
2983 }
2984
Craig Mautnerd2328952013-03-05 12:46:26 -08002985 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002986 ArrayList<ActivityRecord> activities = r.task.mActivities;
2987 for (int index = activities.indexOf(r); index >= 0; --index) {
2988 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002989 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002990 break;
2991 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002992 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002993 }
2994 return true;
2995 }
2996
Dianne Hackborn5c607432012-02-28 14:44:19 -08002997 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2998 // send the result
2999 ActivityRecord resultTo = r.resultTo;
3000 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003001 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003002 + " who=" + r.resultWho + " req=" + r.requestCode
3003 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003004 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003005 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003006 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003007 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003008 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003009 if (r.info.applicationInfo.uid > 0) {
3010 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3011 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003012 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003013 }
3014 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3015 resultData);
3016 r.resultTo = null;
3017 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003018 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003019
3020 // Make sure this HistoryRecord is not holding on to other resources,
3021 // because clients have remote IPC references to this object so we
3022 // can't assume that will go away and want to avoid circular IPC refs.
3023 r.results = null;
3024 r.pendingResults = null;
3025 r.newIntents = null;
3026 r.icicle = null;
3027 }
3028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 /**
3030 * @return Returns true if this activity has been removed from the history
3031 * list, or false if it is still in the list and will be removed later.
3032 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003033 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3034 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 if (r.finishing) {
3036 Slog.w(TAG, "Duplicate finish request for " + r);
3037 return false;
3038 }
3039
Wale Ogunwale7d701172015-03-11 15:36:30 -07003040 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003041 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003043 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003044 task.taskId, r.shortComponentName, reason);
3045 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003046 final int index = activities.indexOf(r);
3047 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003048 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003049 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003050 // If the caller asked that this activity (and all above it)
3051 // be cleared when the task is reset, don't lose that information,
3052 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003053 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003054 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055 }
3056 }
3057
3058 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003059
Craig Mautner299f9602015-01-26 09:47:33 -08003060 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061
Dianne Hackborn5c607432012-02-28 14:44:19 -08003062 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003063
Craig Mautnerde4ef022013-04-07 19:01:33 -07003064 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003065 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003066 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003068 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003069 ? AppTransition.TRANSIT_TASK_CLOSE
3070 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003071
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003072 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003073 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003074
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003075 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003076 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3077 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3078 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003079 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 }
3081
Craig Mautneraea74a52014-03-08 14:23:10 -08003082 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003083 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003084 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 } else if (r.state != ActivityState.PAUSING) {
3086 // If the activity is PAUSING, we will complete the finish once
3087 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003088 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003089 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003090 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003091 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003092 }
3093
3094 return false;
3095 }
3096
Craig Mautnerf3333272013-04-22 10:55:53 -07003097 static final int FINISH_IMMEDIATELY = 0;
3098 static final int FINISH_AFTER_PAUSE = 1;
3099 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100
Craig Mautnerf3333272013-04-22 10:55:53 -07003101 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 // First things first: if this activity is currently visible,
3103 // and the resumed activity is not yet visible, then hold off on
3104 // finishing until the resumed one becomes visible.
3105 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003106 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3107 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003108 if (mStackSupervisor.mStoppingActivities.size() > 3
3109 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003110 // If we already have a few activities waiting to stop,
3111 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003112 // them out. Or if r is the last of activity of the last task the stack
3113 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003114 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003115 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003116 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003117 }
3118 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003119 if (DEBUG_STATES) Slog.v(TAG_STATES,
3120 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003122 if (oomAdj) {
3123 mService.updateOomAdjLocked();
3124 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 return r;
3126 }
3127
3128 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003129 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003130 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003131 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 if (mResumedActivity == r) {
3133 mResumedActivity = null;
3134 }
3135 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003136 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 r.state = ActivityState.FINISHING;
3138
3139 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003140 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 || prevState == ActivityState.STOPPED
3142 || prevState == ActivityState.INITIALIZING) {
3143 // If this activity is already stopped, we can just finish
3144 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003145 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003146 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003147 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003148 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003149 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003150 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003151 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3152 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003153 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003155
3156 // Need to go through the full pause cycle to get this
3157 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003158 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003159 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003160 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003161 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 return r;
3163 }
3164
Craig Mautneree36c772014-07-16 14:56:05 -07003165 void finishAllActivitiesLocked(boolean immediately) {
3166 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003167 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3168 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3169 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3170 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003171 noActivitiesInStack = false;
3172 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003173 continue;
3174 }
Craig Mautneree36c772014-07-16 14:56:05 -07003175 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003176 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3177 }
3178 }
Craig Mautneree36c772014-07-16 14:56:05 -07003179 if (noActivitiesInStack) {
3180 mActivityContainer.onTaskListEmptyLocked();
3181 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003182 }
3183
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003184 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3185 // Basic case: for simple app-centric recents, we need to recreate
3186 // the task if the affinity has changed.
3187 if (srec == null || srec.task.affinity == null ||
3188 !srec.task.affinity.equals(destAffinity)) {
3189 return true;
3190 }
3191 // Document-centric case: an app may be split in to multiple documents;
3192 // they need to re-create their task if this current activity is the root
3193 // of a document, unless simply finishing it will return them to the the
3194 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003195 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3196 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003197 // Okay, this activity is at the root of its task. What to do, what to do...
3198 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3199 // Finishing won't return to an application, so we need to recreate.
3200 return true;
3201 }
3202 // We now need to get the task below it to determine what to do.
3203 int taskIdx = mTaskHistory.indexOf(srec.task);
3204 if (taskIdx <= 0) {
3205 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3206 return false;
3207 }
3208 if (taskIdx == 0) {
3209 // At the bottom of the stack, nothing to go back to.
3210 return true;
3211 }
3212 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3213 if (!srec.task.affinity.equals(prevTask.affinity)) {
3214 // These are different apps, so need to recreate.
3215 return true;
3216 }
3217 }
3218 return false;
3219 }
3220
Wale Ogunwale7d701172015-03-11 15:36:30 -07003221 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003222 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003223 final TaskRecord task = srec.task;
3224 final ArrayList<ActivityRecord> activities = task.mActivities;
3225 final int start = activities.indexOf(srec);
3226 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003227 return false;
3228 }
3229 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003230 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003231 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003232 final ComponentName dest = destIntent.getComponent();
3233 if (start > 0 && dest != null) {
3234 for (int i = finishTo; i >= 0; i--) {
3235 ActivityRecord r = activities.get(i);
3236 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003237 r.info.name.equals(dest.getClassName())) {
3238 finishTo = i;
3239 parent = r;
3240 foundParentInTask = true;
3241 break;
3242 }
3243 }
3244 }
3245
3246 IActivityController controller = mService.mController;
3247 if (controller != null) {
3248 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3249 if (next != null) {
3250 // ask watcher if this is allowed
3251 boolean resumeOK = true;
3252 try {
3253 resumeOK = controller.activityResuming(next.packageName);
3254 } catch (RemoteException e) {
3255 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003256 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003257 }
3258
3259 if (!resumeOK) {
3260 return false;
3261 }
3262 }
3263 }
3264 final long origId = Binder.clearCallingIdentity();
3265 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003266 ActivityRecord r = activities.get(i);
3267 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003268 // Only return the supplied result for the first activity finished
3269 resultCode = Activity.RESULT_CANCELED;
3270 resultData = null;
3271 }
3272
3273 if (parent != null && foundParentInTask) {
3274 final int parentLaunchMode = parent.info.launchMode;
3275 final int destIntentFlags = destIntent.getFlags();
3276 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3277 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3278 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3279 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003280 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3281 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003282 } else {
3283 try {
3284 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3285 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003286 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003287 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003288 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003289 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003290 foundParentInTask = res == ActivityManager.START_SUCCESS;
3291 } catch (RemoteException e) {
3292 foundParentInTask = false;
3293 }
3294 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003295 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003296 }
3297 }
3298 Binder.restoreCallingIdentity(origId);
3299 return foundParentInTask;
3300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003301 /**
3302 * Perform the common clean-up of an activity record. This is called both
3303 * as part of destroyActivityLocked() (when destroying the client-side
3304 * representation) and cleaning things up as a result of its hosting
3305 * processing going away, in which case there is no remaining client-side
3306 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003307 *
3308 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003309 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003310 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3311 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 if (mResumedActivity == r) {
3313 mResumedActivity = null;
3314 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003315 if (mPausingActivity == r) {
3316 mPausingActivity = null;
3317 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003318 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003319
3320 r.configDestroy = false;
3321 r.frozenBeforeDestroy = false;
3322
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003323 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003324 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003325 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003326 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003327 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003328 }
3329
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 // Make sure this record is no longer in the pending finishes list.
3331 // This could happen, for example, if we are trimming activities
3332 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003333 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003334 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003335
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 // Remove any pending results.
3337 if (r.finishing && r.pendingResults != null) {
3338 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3339 PendingIntentRecord rec = apr.get();
3340 if (rec != null) {
3341 mService.cancelIntentSenderLocked(rec, false);
3342 }
3343 }
3344 r.pendingResults = null;
3345 }
3346
3347 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003348 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 }
3350
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003351 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003352 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003353 if (getVisibleBehindActivity() == r) {
3354 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003355 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003356 }
3357
3358 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003359 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003361 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003362 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003363 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 }
3365
Craig Mautner299f9602015-01-26 09:47:33 -08003366 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003367 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003368 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003369 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003370 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3371 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3372
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003373 r.takeFromHistory();
3374 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003375 if (DEBUG_STATES) Slog.v(TAG_STATES,
3376 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003377 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003378 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003379 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003380 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003381 if (VALIDATE_TOKENS) {
3382 validateAppTokensLocked();
3383 }
Craig Mautner312ba862014-02-10 17:55:01 -08003384 final TaskRecord task = r.task;
3385 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003386 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003387 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003388 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3389 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003390 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003391 }
Craig Mautner299f9602015-01-26 09:47:33 -08003392 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003393 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003394 cleanUpActivityServicesLocked(r);
3395 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003397
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003398 /**
3399 * Perform clean-up of service connections in an activity record.
3400 */
3401 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3402 // Throw away any services that have been bound by this activity.
3403 if (r.connections != null) {
3404 Iterator<ConnectionRecord> it = r.connections.iterator();
3405 while (it.hasNext()) {
3406 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003407 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003408 }
3409 r.connections = null;
3410 }
3411 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003412
Craig Mautneree2e45a2014-06-27 12:10:03 -07003413 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003414 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003415 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003416 mHandler.sendMessage(msg);
3417 }
3418
Craig Mautneree2e45a2014-06-27 12:10:03 -07003419 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003420 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003421 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003422 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3423 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3424 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3425 final ActivityRecord r = activities.get(activityNdx);
3426 if (r.finishing) {
3427 continue;
3428 }
3429 if (r.fullscreen) {
3430 lastIsOpaque = true;
3431 }
3432 if (owner != null && r.app != owner) {
3433 continue;
3434 }
3435 if (!lastIsOpaque) {
3436 continue;
3437 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003438 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003439 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003440 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003441 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003442 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003443 activityRemoved = true;
3444 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003445 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003446 }
3447 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003448 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003449 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003450 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003451 }
3452
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003453 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3454 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003455 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3456 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003457 + " pausing=" + mPausingActivity + " for reason " + reason);
3458 return destroyActivityLocked(r, true, reason);
3459 }
3460 return false;
3461 }
3462
3463 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3464 String reason) {
3465 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003466 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003467 int maxTasks = tasks.size() / 4;
3468 if (maxTasks < 1) {
3469 maxTasks = 1;
3470 }
3471 int numReleased = 0;
3472 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3473 final TaskRecord task = mTaskHistory.get(taskNdx);
3474 if (!tasks.contains(task)) {
3475 continue;
3476 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003477 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003478 int curNum = 0;
3479 final ArrayList<ActivityRecord> activities = task.mActivities;
3480 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3481 final ActivityRecord activity = activities.get(actNdx);
3482 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003483 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003484 + " in state " + activity.state + " resumed=" + mResumedActivity
3485 + " pausing=" + mPausingActivity + " for reason " + reason);
3486 destroyActivityLocked(activity, true, reason);
3487 if (activities.get(actNdx) != activity) {
3488 // Was removed from list, back up so we don't miss the next one.
3489 actNdx--;
3490 }
3491 curNum++;
3492 }
3493 }
3494 if (curNum > 0) {
3495 numReleased += curNum;
3496 maxTasks--;
3497 if (mTaskHistory.get(taskNdx) != task) {
3498 // The entire task got removed, back up so we don't miss the next one.
3499 taskNdx--;
3500 }
3501 }
3502 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003503 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3504 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003505 return numReleased;
3506 }
3507
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 /**
3509 * Destroy the current CLIENT SIDE instance of an activity. This may be
3510 * called both when actually finishing an activity, or when performing
3511 * a configuration switch where we destroy the current client-side object
3512 * but then create a new client-side object for this same HistoryRecord.
3513 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003514 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003515 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3516 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003517 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003518 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003519 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003520 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003521
3522 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003523
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003524 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003525
3526 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003527
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003528 if (hadApp) {
3529 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003530 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3532 mService.mHeavyWeightProcess = null;
3533 mService.mHandler.sendEmptyMessage(
3534 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3535 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003536 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003537 // Update any services we are bound to that might care about whether
3538 // their client may have activities.
3539 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003540 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003541 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003542 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 }
3544 }
3545
3546 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003547
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003548 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003549 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003550 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003551 r.configChangeFlags);
3552 } catch (Exception e) {
3553 // We can just ignore exceptions here... if the process
3554 // has crashed, our death notification will clean things
3555 // up.
3556 //Slog.w(TAG, "Exception thrown during finish", e);
3557 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003558 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 removedFromHistory = true;
3560 skipDestroy = true;
3561 }
3562 }
3563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003564 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003565
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003566 // If the activity is finishing, we need to wait on removing it
3567 // from the list to give it a chance to do its cleanup. During
3568 // that time it may make calls back with its token so we need to
3569 // be able to find it on the list and so we don't want to remove
3570 // it from the list yet. Otherwise, we can just immediately put
3571 // it in the destroyed state since we are not removing it from the
3572 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003574 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003575 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003577 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3579 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003580 if (DEBUG_STATES) Slog.v(TAG_STATES,
3581 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003582 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003583 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003584 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 }
3586 } else {
3587 // remove this record from the history.
3588 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003589 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003590 removedFromHistory = true;
3591 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003592 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003593 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003594 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003595 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003596 }
3597 }
3598
3599 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003600
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003601 if (!mLRUActivities.remove(r) && hadApp) {
3602 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3603 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003604
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003605 return removedFromHistory;
3606 }
3607
Craig Mautner299f9602015-01-26 09:47:33 -08003608 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003609 final long origId = Binder.clearCallingIdentity();
3610 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003611 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003612 if (r != null) {
3613 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003615 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003616
Wale Ogunwale60454db2015-01-23 16:05:07 -08003617 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003618 if (r.state == ActivityState.DESTROYING) {
3619 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003620 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003621 }
3622 }
Craig Mautner05d29032013-05-03 13:40:13 -07003623 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003624 } finally {
3625 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003626 }
3627 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003628
Todd Kennedyaab56db2015-01-30 09:39:53 -08003629 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003630 if (hasVisibleBehindActivity() &&
3631 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003632 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003633 // Don't release the top activity if it has requested to run behind the next
3634 // activity.
3635 return;
3636 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003637 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003638 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003639 " thread=" + r.app.thread);
3640 if (r != null && r.app != null && r.app.thread != null) {
3641 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003642 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003643 } catch (RemoteException e) {
3644 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003645 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003646 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003647 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003648 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003649 }
3650 }
3651 }
3652
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003653 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003654 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3655 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003656 if (r != null) {
3657 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003658 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003659 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003660 }
3661 mStackSupervisor.resumeTopActivitiesLocked();
3662 }
3663
Jose Lima4b6c6692014-08-12 17:41:12 -07003664 boolean hasVisibleBehindActivity() {
3665 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003666 }
3667
Jose Lima4b6c6692014-08-12 17:41:12 -07003668 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003669 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003670 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003671 }
3672 }
3673
Jose Lima4b6c6692014-08-12 17:41:12 -07003674 ActivityRecord getVisibleBehindActivity() {
3675 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003676 }
3677
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003678 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3679 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003680 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003681 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3682 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003683 while (i > 0) {
3684 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003685 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003686 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003687 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003688 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003690 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003691 }
3692 }
3693 }
3694
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003695 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3696 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003697 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3698 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003699 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3700 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003701 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003702 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003703 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3704 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003705
3706 boolean hasVisibleActivities = false;
3707
3708 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003709 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003710 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3711 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003712 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3713 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3714 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3715 final ActivityRecord r = activities.get(activityNdx);
3716 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003717 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3718 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003719 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003720 if (r.visible) {
3721 hasVisibleActivities = true;
3722 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003723 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003724 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3725 // Don't currently have state for the activity, or
3726 // it is finishing -- always remove it.
3727 remove = true;
3728 } else if (r.launchCount > 2 &&
3729 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3730 // We have launched this activity too many times since it was
3731 // able to run, so give up and remove it.
3732 remove = true;
3733 } else {
3734 // The process may be gone, but the activity lives on!
3735 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003736 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003737 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003738 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3739 "Removing activity " + r + " from stack at " + i
3740 + ": haveState=" + r.haveState
3741 + " stateNotNeeded=" + r.stateNotNeeded
3742 + " finishing=" + r.finishing
3743 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003744 if (!r.finishing) {
3745 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3746 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3747 r.userId, System.identityHashCode(r),
3748 r.task.taskId, r.shortComponentName,
3749 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003750 if (r.state == ActivityState.RESUMED) {
3751 mService.updateUsageStats(r, false);
3752 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003753 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003754 } else {
3755 // We have the current state for this activity, so
3756 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003757 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3758 if (DEBUG_APP) Slog.v(TAG_APP,
3759 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003760 r.app = null;
3761 r.nowVisible = false;
3762 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003763 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003764 "App died, clearing saved state of " + r);
3765 r.icicle = null;
3766 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003767 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003768 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003769 if (remove) {
3770 removeActivityFromHistoryLocked(r, "appDied");
3771 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003772 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003773 }
3774 }
3775
3776 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003777 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003778
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003779 final void updateTransitLocked(int transit, Bundle options) {
3780 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003781 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003782 if (r != null && r.state != ActivityState.RESUMED) {
3783 r.updateOptionsLocked(options);
3784 } else {
3785 ActivityOptions.abort(options);
3786 }
3787 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003788 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003789 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003790
Craig Mautner21d24a22014-04-23 11:45:37 -07003791 void updateTaskMovement(TaskRecord task, boolean toFront) {
3792 if (task.isPersistable) {
3793 task.mLastTimeMoved = System.currentTimeMillis();
3794 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3795 // recently will be most negative, tasks sent to the bottom before that will be less
3796 // negative. Similarly for recent tasks moved to the top which will be most positive.
3797 if (!toFront) {
3798 task.mLastTimeMoved *= -1;
3799 }
3800 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003801 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003802 }
3803
Craig Mautner84984fa2014-06-19 11:19:20 -07003804 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003805 final int top = mTaskHistory.size() - 1;
3806 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3807 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003808 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003809 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3810 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003811 mTaskHistory.remove(taskNdx);
3812 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003813 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003814 return;
3815 }
3816 }
3817 }
3818
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003819 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003820 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003821 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822
Craig Mautner11bf9a52013-02-19 14:08:51 -08003823 final int numTasks = mTaskHistory.size();
3824 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003825 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003826 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003827 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003828 ActivityOptions.abort(options);
3829 } else {
3830 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3831 }
3832 return;
3833 }
3834
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003835 if (timeTracker != null) {
3836 // The caller wants a time tracker associated with this task.
3837 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3838 tr.mActivities.get(i).appTimeTracker = timeTracker;
3839 }
3840 }
3841
Craig Mautner11bf9a52013-02-19 14:08:51 -08003842 // Shift all activities with this task up to the top
3843 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003844 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003845
Chong Zhang45c25ce2015-08-10 22:18:26 -07003846 // Don't refocus if invisible to current user
3847 ActivityRecord top = tr.getTopActivity();
3848 if (!okToShowLocked(top)) {
3849 addRecentActivityLocked(top);
3850 ActivityOptions.abort(options);
3851 return;
3852 }
3853
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003854 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003855 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003856 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003857
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003858 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003859 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003860 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 if (r != null) {
3862 mNoAnimActivities.add(r);
3863 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003864 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003865 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003866 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003867 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003868
Craig Mautner05d29032013-05-03 13:40:13 -07003869 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003870 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003871
3872 if (VALIDATE_TOKENS) {
3873 validateAppTokensLocked();
3874 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003875 }
3876
3877 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003878 * Worker method for rearranging history stack. Implements the function of moving all
3879 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003881 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003882 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3883 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003884 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003885 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003886 * @return Returns true if the move completed, false if not.
3887 */
Craig Mautner299f9602015-01-26 09:47:33 -08003888 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003889 final TaskRecord tr = taskForIdLocked(taskId);
3890 if (tr == null) {
3891 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3892 return false;
3893 }
3894
3895 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003896 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003898 // If we have a watcher, preflight the move before committing to it. First check
3899 // for *other* available tasks, but if none are available, then try again allowing the
3900 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003901 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003902 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003903 if (next == null) {
3904 next = topRunningActivityLocked(null, 0);
3905 }
3906 if (next != null) {
3907 // ask watcher if this is allowed
3908 boolean moveOK = true;
3909 try {
3910 moveOK = mService.mController.activityResuming(next.packageName);
3911 } catch (RemoteException e) {
3912 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003913 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003914 }
3915 if (!moveOK) {
3916 return false;
3917 }
3918 }
3919 }
3920
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003921 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003922
riddle_hsuc215a4f2014-12-27 12:10:45 +08003923 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003924
3925 // If true, we should resume the home activity next if the task we are moving to the
3926 // back is over the home stack. We force to false if the task we are moving to back
3927 // is the home task and we don't want it resumed after moving to the back.
3928 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3929 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003930 final TaskRecord nextTask = getNextTask(tr);
3931 if (nextTask != null) {
3932 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3933 } else {
3934 prevIsHome = true;
3935 }
3936 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003937 mTaskHistory.remove(tr);
3938 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003939 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003940
Craig Mautnerc8143c62013-09-03 12:15:57 -07003941 // There is an assumption that moving a task to the back moves it behind the home activity.
3942 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003943 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003944 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3945 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003946 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003947 break;
3948 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003949 if (taskNdx == 1) {
3950 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003951 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003952 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003953 }
3954
Craig Mautner299f9602015-01-26 09:47:33 -08003955 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003956 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003957
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003958 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003959 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 }
3961
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003962 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003963 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003964 if (!mService.mBooting && !mService.mBooted) {
3965 // Not ready yet!
3966 return false;
3967 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003968 final int taskToReturnTo = tr.getTaskToReturnTo();
3969 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003970 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003971 }
3972
Craig Mautner05d29032013-05-03 13:40:13 -07003973 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003974 return true;
3975 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003976
Craig Mautner8849a5e2013-04-02 16:41:03 -07003977 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003978 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003979 final Uri data = r.intent.getData();
3980 final String strData = data != null ? data.toSafeString() : null;
3981
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003982 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003983 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003984 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003985 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003986 }
3987
3988 /**
3989 * Make sure the given activity matches the current configuration. Returns
3990 * false if the activity had to be destroyed. Returns true if the
3991 * configuration is the same, or the activity will remain running as-is
3992 * for whatever reason. Ensures the HistoryRecord is updated with the
3993 * correct configuration and all other bookkeeping is handled.
3994 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003995 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
3996 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003997 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003998 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003999 "Skipping config check (will change): " + r);
4000 return true;
4001 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004002
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004003 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004004 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004005
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006 // Short circuit: if the two configurations are the exact same
4007 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004008 final Configuration newConfig = mService.mConfiguration;
4009 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004010 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004011 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004012 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004013 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004014 "Configuration unchanged in " + r);
4015 return true;
4016 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004017
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004018 // We don't worry about activities that are finishing.
4019 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004020 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004021 "Configuration doesn't matter in finishing " + r);
4022 r.stopFreezingScreenLocked(false);
4023 return true;
4024 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004025
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004026 // Okay we now are going to make this activity have the new config.
4027 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004028 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004029 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004030 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004031 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004032
Filip Gruszczynski23493322015-07-29 17:02:59 -07004033 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004034 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004035 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004036 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004037 "Configuration no differences in " + r);
4038 return true;
4039 }
4040
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004041 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4042 "Configuration changes for " + r + " ; taskChanges="
4043 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4044 + Configuration.configurationDiffToString(changes));
4045
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004046 // If the activity isn't currently running, just leave the new
4047 // configuration and it will pick that up next time it starts.
4048 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004049 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004050 "Configuration doesn't matter not running " + r);
4051 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004052 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004053 return true;
4054 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004055
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004056 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004057 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4058 "Checking to restart " + r.info.name + ": changed=0x"
4059 + Integer.toHexString(changes) + ", handles=0x"
4060 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4061
Dianne Hackborne6676352011-06-01 16:51:20 -07004062 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004063 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4064 r.configChangeFlags |= changes;
4065 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004066 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004067 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004068 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004069 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004070 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004071 } else if (r.state == ActivityState.PAUSING) {
4072 // A little annoying: we are waiting for this activity to
4073 // finish pausing. Let's not do anything now, but just
4074 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004075 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004076 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004077 r.configDestroy = true;
4078 return true;
4079 } else if (r.state == ActivityState.RESUMED) {
4080 // Try to optimize this case: the configuration is changing
4081 // and we need to restart the top, resumed activity.
4082 // Instead of doing the normal handshaking, just say
4083 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004084 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004085 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004086 relaunchActivityLocked(r, r.configChangeFlags, true,
4087 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004088 r.configChangeFlags = 0;
4089 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004090 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004091 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004092 relaunchActivityLocked(r, r.configChangeFlags, false,
4093 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004094 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004095 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004096
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004097 // All done... tell the caller we weren't able to keep this
4098 // activity around.
4099 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004100 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004101
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004102 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004103 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004104 // changes is always sent to all processes when they happen so it can just use whatever
4105 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004106 if (r.app != null && r.app.thread != null) {
4107 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004108 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004109 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004110 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004111 } catch (RemoteException e) {
4112 // If process died, whatever.
4113 }
4114 }
4115 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004117 return true;
4118 }
4119
Filip Gruszczynski23493322015-07-29 17:02:59 -07004120 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4121 Configuration oldTaskOverride) {
4122 // Determine what has changed. May be nothing, if this is a config
4123 // that has come back from the app after going idle. In that case
4124 // we just want to leave the official config object now in the
4125 // activity and do nothing else.
4126 int taskChanges = oldTaskOverride.diff(taskConfig);
4127 // We don't want to use size changes if they don't cross boundaries that are important to
4128 // the app.
4129 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4130 final boolean crosses = record.crossesHorizontalSizeThreshold(
4131 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4132 || record.crossesVerticalSizeThreshold(
4133 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4134 if (!crosses) {
4135 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4136 }
4137 }
4138 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4139 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4140 final int newSmallest = taskConfig.smallestScreenWidthDp;
4141 final boolean crosses = record.crossesHorizontalSizeThreshold(oldSmallest, newSmallest)
4142 || record.crossesVerticalSizeThreshold(oldSmallest, newSmallest);
4143 if (!crosses) {
4144 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4145 }
4146 }
4147 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4148 }
4149
4150 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4151 Configuration oldTaskOverride, int taskChanges) {
4152 if (taskChanges == 0) {
4153 // {@link Configuration#diff} doesn't catch changes from unset values.
4154 // Check for changes we care about.
4155 if (oldTaskOverride.orientation != taskConfig.orientation) {
4156 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4157 }
4158 // We want to explicitly track situations where the size configuration goes from
4159 // undefined to defined. We don't care about crossing the threshold in that case,
4160 // because there is no threshold.
4161 final int oldHeight = oldTaskOverride.screenHeightDp;
4162 final int newHeight = taskConfig.screenHeightDp;
4163 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4164 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4165 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4166 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4167 }
4168 final int oldWidth = oldTaskOverride.screenWidthDp;
4169 final int newWidth = taskConfig.screenWidthDp;
4170 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4171 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4172 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4173 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4174 }
4175 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4176 final int newSmallest = taskConfig.smallestScreenWidthDp;
4177 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4178 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4179 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4180 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4181 }
4182 }
4183 return taskChanges;
4184 }
4185
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004186 private static boolean isResizeOnlyChange(int change) {
4187 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4188 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4189 }
4190
Wale Ogunwale83301a92015-09-24 15:54:08 -07004191 private void relaunchActivityLocked(
4192 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4193 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4194 return;
4195 }
4196
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004197 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004198 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004199 if (andResume) {
4200 results = r.results;
4201 newIntents = r.newIntents;
4202 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004203 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4204 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004205 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004206 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004207 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004208 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004211
Craig Mautner34b73df2014-01-12 21:11:08 -08004212 mStackSupervisor.removeChildActivityContainers(r);
4213
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004214 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004215 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4216 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004217 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004218 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4219 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004220 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004221 // Note: don't need to call pauseIfSleepingLocked() here, because
4222 // the caller will only pass in 'andResume' if this activity is
4223 // currently resumed, which implies we aren't sleeping.
4224 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004225 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004226 }
4227
4228 if (andResume) {
4229 r.results = null;
4230 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004231 r.state = ActivityState.RESUMED;
4232 } else {
4233 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4234 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004235 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004236 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004237
4238 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004239 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4240 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4241 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4242 final ActivityRecord r = activities.get(activityNdx);
4243 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004244 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004245 }
4246 if (r.fullscreen && !r.finishing) {
4247 return false;
4248 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004249 }
4250 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004251 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004252 if (r == null) {
4253 return false;
4254 }
4255 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4256 + " would have returned true for r=" + r);
4257 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004258 }
4259
4260 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004261 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4262 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4263 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4264 final ActivityRecord r = activities.get(activityNdx);
4265 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004266 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004267 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004268 }
4269 }
4270 }
4271
Wale Ogunwale540e1232015-05-01 15:35:39 -07004272 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4273 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004274 boolean didSomething = false;
4275 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004276 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004277 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4278 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4279 int numActivities = activities.size();
4280 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4281 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004282 final boolean sameComponent =
4283 (r.packageName.equals(packageName) && (filterByClasses == null
4284 || filterByClasses.contains(r.realActivity.getClassName())))
4285 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004286 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004287 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004288 && (r.app == null || evenPersistent || !r.app.persistent)) {
4289 if (!doit) {
4290 if (r.finishing) {
4291 // If this activity is just finishing, then it is not
4292 // interesting as far as something to stop.
4293 continue;
4294 }
4295 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004296 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004297 if (r.isHomeActivity()) {
4298 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4299 Slog.i(TAG, "Skip force-stop again " + r);
4300 continue;
4301 } else {
4302 homeActivity = r.realActivity;
4303 }
4304 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004305 didSomething = true;
4306 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004307 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004308 if (r.app != null) {
4309 r.app.removed = true;
4310 }
4311 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004312 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004313 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004314 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4315 true)) {
4316 // r has been deleted from mActivities, accommodate.
4317 --numActivities;
4318 --activityNdx;
4319 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004320 }
4321 }
4322 }
4323 return didSomething;
4324 }
4325
Dianne Hackborn09233282014-04-30 11:33:59 -07004326 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004327 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4328 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004329 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004330 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004331 if (task.getTopActivity() == null) {
4332 continue;
4333 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004334 ActivityRecord r = null;
4335 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004336 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004337 int numActivities = 0;
4338 int numRunning = 0;
4339 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004340 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004341 continue;
4342 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004343 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004344 tmp = activities.get(activityNdx);
4345 if (tmp.finishing) {
4346 continue;
4347 }
4348 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004349
Craig Mautneraab647e2013-02-28 16:31:36 -08004350 // Initialize state for next task if needed.
4351 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4352 top = r;
4353 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004354 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004355
4356 // Add 'r' into the current task.
4357 numActivities++;
4358 if (r.app != null && r.app.thread != null) {
4359 numRunning++;
4360 }
4361
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004362 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004363 TAG, r.intent.getComponent().flattenToShortString()
4364 + ": task=" + r.task);
4365 }
4366
4367 RunningTaskInfo ci = new RunningTaskInfo();
4368 ci.id = task.taskId;
4369 ci.baseActivity = r.intent.getComponent();
4370 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004371 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004372 if (focusedStack && topTask) {
4373 // Give the latest time to ensure foreground task can be sorted
4374 // at the first, because lastActiveTime of creating task is 0.
4375 ci.lastActiveTime = System.currentTimeMillis();
4376 topTask = false;
4377 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004378
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004379 if (top.task != null) {
4380 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004381 }
4382 ci.numActivities = numActivities;
4383 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004384 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004385 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004386 }
4387
4388 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004389 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004390 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004391 if (top >= 0) {
4392 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4393 int activityTop = activities.size() - 1;
4394 if (activityTop > 0) {
4395 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4396 "unhandled-back", true);
4397 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004398 }
4399 }
4400
Craig Mautner6b74cb52013-09-27 17:02:21 -07004401 /**
4402 * Reset local parameters because an app's activity died.
4403 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004404 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004405 */
4406 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004407 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004408 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004409 "App died while pausing: " + mPausingActivity);
4410 mPausingActivity = null;
4411 }
4412 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4413 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004414 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004415 }
4416
Craig Mautner19091252013-10-05 00:03:53 -07004417 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004418 }
4419
Craig Mautnercae015f2013-02-08 14:31:27 -08004420 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004421 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4422 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4423 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4424 final ActivityRecord r = activities.get(activityNdx);
4425 if (r.app == app) {
4426 Slog.w(TAG, " Force finishing activity "
4427 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004428 // Force the destroy to skip right to removal.
4429 r.app = null;
4430 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004431 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004432 }
4433 }
4434 }
4435
Dianne Hackborn390517b2013-05-30 15:03:32 -07004436 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004437 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004438 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004439 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4440 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004441 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4442 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004443 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004444 " Task id #" + task.taskId + "\n" +
4445 " mFullscreen=" + task.mFullscreen + "\n" +
4446 " mBounds=" + task.mBounds + "\n" +
4447 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004448 if (printed) {
4449 header = null;
4450 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004451 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004452 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004453 }
4454
4455 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4456 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4457
4458 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4460 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004461 }
4462 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004463 final int top = mTaskHistory.size() - 1;
4464 if (top >= 0) {
4465 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4466 int listTop = list.size() - 1;
4467 if (listTop >= 0) {
4468 activities.add(list.get(listTop));
4469 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004470 }
4471 } else {
4472 ItemMatcher matcher = new ItemMatcher();
4473 matcher.build(name);
4474
Craig Mautneraab647e2013-02-28 16:31:36 -08004475 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4476 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4477 if (matcher.match(r1, r1.intent.getComponent())) {
4478 activities.add(r1);
4479 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004480 }
4481 }
4482 }
4483
4484 return activities;
4485 }
4486
4487 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004488 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004489
4490 // All activities that came from the package must be
4491 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004492 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4493 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4494 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4495 final ActivityRecord a = activities.get(activityNdx);
4496 if (a.info.packageName.equals(packageName)) {
4497 a.forceNewConfig = true;
4498 if (starting != null && a == starting && a.visible) {
4499 a.startFreezingScreenLocked(starting.app,
4500 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4501 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004502 }
4503 }
4504 }
4505
4506 return starting;
4507 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004508
Craig Mautner299f9602015-01-26 09:47:33 -08004509 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004510 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004511 }
4512
Wale Ogunwale000957c2015-04-03 08:19:12 -07004513 /**
4514 * Removes the input task from this stack.
4515 * @param task to remove.
4516 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004517 * @param moving task to another stack. In the case we are moving we don't want to perform
4518 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004519 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004520 void removeTask(TaskRecord task, String reason, boolean moving) {
4521 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004522 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004523 mWindowManager.removeTask(task.taskId);
4524 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004525
Craig Mautner04a0ea62014-01-13 12:51:26 -08004526 final ActivityRecord r = mResumedActivity;
4527 if (r != null && r.task == task) {
4528 mResumedActivity = null;
4529 }
4530
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004531 final int taskNdx = mTaskHistory.indexOf(task);
4532 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004533 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4534 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4535 if (!nextTask.isOverHomeStack()) {
4536 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4537 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004538 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004539 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004540 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004541
Wale Ogunwale040b4702015-08-06 18:10:50 -07004542 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004543 final boolean isVoiceSession = task.voiceSession != null;
4544 if (isVoiceSession) {
4545 try {
4546 task.voiceSession.taskFinished(task.intent, task.taskId);
4547 } catch (RemoteException e) {
4548 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004549 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004550 if (task.autoRemoveFromRecents() || isVoiceSession) {
4551 // Task creator asked to remove this when done, or this task was a voice
4552 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004553 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004554 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004555 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004556 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004557
4558 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004559 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004560 // We only need to adjust focused stack if this stack is in focus.
4561 if (isOnHomeDisplay() && mStackSupervisor.isFrontStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004562 String myReason = reason + " leftTaskHistoryEmpty";
4563 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004564 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004565 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004566 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004567 if (mStacks != null) {
4568 mStacks.remove(this);
4569 mStacks.add(0, this);
4570 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004571 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004572 mActivityContainer.onTaskListEmptyLocked();
4573 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004574 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004575
4576 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004577 }
4578
Dianne Hackborn91097de2014-04-04 18:02:06 -07004579 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4580 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4581 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004582 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4583 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004584 // add the task to stack first, mTaskPositioner might need the stack association
4585 addTask(task, toTop, false);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004586 if (mTaskPositioner != null) {
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004587 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, info.layout);
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004588 } else if (mBounds != null && task.mResizeable) {
4589 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004590 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004591 return task;
4592 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004593
4594 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004595 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004596 }
4597
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004598 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004599 task.stack = this;
4600 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004601 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004602 } else {
4603 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004604 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004605 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004606 if (!moving && task.voiceSession != null) {
4607 try {
4608 task.voiceSession.taskStarted(task.intent, task.taskId);
4609 } catch (RemoteException e) {
4610 }
4611 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004612 }
4613
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004614 void positionTask(final TaskRecord task, int position, boolean moving) {
4615 task.stack = this;
4616 insertTaskAtPosition(task, position);
4617 if (!moving && task.voiceSession != null) {
4618 try {
4619 task.voiceSession.taskStarted(task.intent, task.taskId);
4620 } catch (RemoteException e) {
4621 }
4622 }
4623 }
4624
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004625 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004626 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004627 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004628 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4629 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4630 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4631 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004632 bounds, task.mOverrideConfig, !r.isHomeActivity());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004633 r.taskConfigOverride = task.mOverrideConfig;
4634 }
4635
4636 private void setAppTask(ActivityRecord r, TaskRecord task) {
4637 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004638 task.updateOverrideConfiguration(bounds);
4639 mWindowManager.setAppTask(
4640 r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004641 r.taskConfigOverride = task.mOverrideConfig;
4642 }
4643
Craig Mautnerc00204b2013-03-05 15:02:14 -08004644 public int getStackId() {
4645 return mStackId;
4646 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004647
4648 @Override
4649 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004650 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4651 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004652 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004653
Craig Mautner15df08a2015-04-01 12:17:18 -07004654 void onLockTaskPackagesUpdatedLocked() {
4655 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4656 mTaskHistory.get(taskNdx).setLockTaskAuth();
4657 }
4658 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004659}