blob: 6b8bdf86303e81d0f74a3455c8af475d76752296 [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 Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070024import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070025
Wale Ogunwalee23149f2015-03-06 15:39:44 -080026import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070027
Craig Mautner84984fa2014-06-19 11:19:20 -070028import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070030
Wale Ogunwale040b4702015-08-06 18:10:50 -070031import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070032import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070033
Wale Ogunwale706ed792015-08-02 10:29:44 -070034import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070035import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070036
Dianne Hackborn91097de2014-04-04 18:02:06 -070037import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080038import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070039import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070040import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080041import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080042import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080043import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080044import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046
47import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070048import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070049import android.app.ActivityManager.StackId;
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 Ogunwalec82f2f52014-12-09 09:32:50 -0800357 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
358 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800359 mActivityContainer = activityContainer;
360 mStackSupervisor = activityContainer.getOuter();
361 mService = mStackSupervisor.mService;
362 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
363 mWindowManager = mService.mWindowManager;
364 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700365 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800366 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700367 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
368 ? new LaunchingTaskPositioner() : null;
369 }
370
371 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
372 mDisplayId = activityDisplay.mDisplayId;
373 mStacks = activityDisplay.mStacks;
374 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
375 mFullscreen = mBounds == null;
376 if (mTaskPositioner != null) {
377 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
378 mTaskPositioner.configure(mBounds);
379 }
380 }
381
382 void detachDisplay() {
383 mDisplayId = Display.INVALID_DISPLAY;
384 mStacks = null;
385 if (mTaskPositioner != null) {
386 mTaskPositioner.reset();
387 }
388 mWindowManager.detachStack(mStackId);
389 }
390
391 void setBounds(Rect bounds) {
392 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700393 if (mTaskPositioner != null) {
394 mTaskPositioner.configure(bounds);
395 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700396 }
Craig Mautner5962b122012-10-05 14:45:52 -0700397
Amith Yamasani734983f2014-03-04 16:48:05 -0800398 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700399 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700400 }
401
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700402 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700404 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700405 if (r != null) {
406 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800407 }
408 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700409 return null;
410 }
411
412 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
414 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800415 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800416 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
417 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800418 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800419 return r;
420 }
421 }
422 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700423 return null;
424 }
425
426 /**
427 * This is a simplified version of topRunningActivityLocked that provides a number of
428 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800429 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700430 * @param token If non-null, any history records matching this token will be skipped.
431 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800432 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700433 * @return Returns the HistoryRecord of the next activity on the stack.
434 */
435 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800436 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
437 TaskRecord task = mTaskHistory.get(taskNdx);
438 if (task.taskId == taskId) {
439 continue;
440 }
441 ArrayList<ActivityRecord> activities = task.mActivities;
442 for (int i = activities.size() - 1; i >= 0; --i) {
443 final ActivityRecord r = activities.get(i);
444 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800445 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800446 return r;
447 }
448 }
449 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700450 return null;
451 }
452
Craig Mautner8849a5e2013-04-02 16:41:03 -0700453 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700454 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
455 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700456 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
457 final ActivityRecord r = activities.get(activityNdx);
458 if (!r.finishing) {
459 return r;
460 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700461 }
462 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700463 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700464 }
465
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700466 final TaskRecord topTask() {
467 final int size = mTaskHistory.size();
468 if (size > 0) {
469 return mTaskHistory.get(size - 1);
470 }
471 return null;
472 }
473
Craig Mautnerd2328952013-03-05 12:46:26 -0800474 TaskRecord taskForIdLocked(int id) {
475 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
476 final TaskRecord task = mTaskHistory.get(taskNdx);
477 if (task.taskId == id) {
478 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800479 }
480 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700481 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700482 }
483
Craig Mautnerd2328952013-03-05 12:46:26 -0800484 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700485 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800486 return isInStackLocked(r);
487 }
488
489 ActivityRecord isInStackLocked(ActivityRecord r) {
490 if (r == null) {
491 return null;
492 }
493 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700494 if (task != null && task.stack != null
495 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800496 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800497 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800498 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800499 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800500 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800501 }
502
Craig Mautner2420ead2013-04-01 17:13:20 -0700503 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 final boolean hadit = mLRUActivities.remove(r);
505 mLRUActivities.add(r);
506 return hadit;
507 }
508
Craig Mautnerde4ef022013-04-07 19:01:33 -0700509 final boolean isHomeStack() {
510 return mStackId == HOME_STACK_ID;
511 }
512
Craig Mautnere0a38842013-12-16 16:14:02 -0800513 final boolean isOnHomeDisplay() {
514 return isAttached() &&
515 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
516 }
517
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700518 void moveToFront(String reason) {
519 moveToFront(reason, null);
520 }
521
522 /**
523 * @param reason The reason for moving the stack to the front.
524 * @param task If non-null, the task will be moved to the top of the stack.
525 * */
526 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700527 if (!isAttached()) {
528 return;
529 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700530
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700531 mStacks.remove(this);
532 int addIndex = mStacks.size();
533
534 if (addIndex > 0) {
535 final ActivityStack topStack = mStacks.get(addIndex - 1);
536 if (topStack.mStackId == PINNED_STACK_ID && topStack != this) {
537 // The pinned stack is always the top most stack (always-on-top).
538 // So, stack is moved just below the pinned stack.
539 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800540 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700541 }
542
543 mStacks.add(addIndex, this);
544
545 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
546 if (isOnHomeDisplay()) {
547 mStackSupervisor.setFocusStack(reason, this);
548 }
549 if (task != null) {
550 insertTaskAtTop(task, null);
551 } else {
552 task = topTask();
553 }
554 if (task != null) {
555 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800557 }
558
559 final boolean isAttached() {
560 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 }
562
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700563 /**
564 * Returns the top activity in any existing task matching the given
565 * Intent. Returns null if no such task is found.
566 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700567 ActivityRecord findTaskLocked(ActivityRecord target) {
568 Intent intent = target.intent;
569 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700570 ComponentName cls = intent.getComponent();
571 if (info.targetActivity != null) {
572 cls = new ComponentName(info.packageName, info.targetActivity);
573 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700574 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700575 boolean isDocument = intent != null & intent.isDocument();
576 // If documentData is non-null then it must match the existing task data.
577 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800578
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700579 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800580 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
581 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700582 if (task.voiceSession != null) {
583 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700584 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700585 continue;
586 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700587 if (task.userId != userId) {
588 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700590 continue;
591 }
Craig Mautner000f0022013-02-26 15:04:29 -0800592 final ActivityRecord r = task.getTopActivity();
593 if (r == null || r.finishing || r.userId != userId ||
594 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700595 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800596 continue;
597 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700598 if (r.mActivityType != target.mActivityType) {
599 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
600 continue;
601 }
Craig Mautner000f0022013-02-26 15:04:29 -0800602
Craig Mautnerd00f4742014-03-12 14:17:26 -0700603 final Intent taskIntent = task.intent;
604 final Intent affinityIntent = task.affinityIntent;
605 final boolean taskIsDocument;
606 final Uri taskDocumentData;
607 if (taskIntent != null && taskIntent.isDocument()) {
608 taskIsDocument = true;
609 taskDocumentData = taskIntent.getData();
610 } else if (affinityIntent != null && affinityIntent.isDocument()) {
611 taskIsDocument = true;
612 taskDocumentData = affinityIntent.getData();
613 } else {
614 taskIsDocument = false;
615 taskDocumentData = null;
616 }
617
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700618 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700619 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700620 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700621 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700622 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
623 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700624 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700625 return r;
626 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700627 } else if (taskIntent != null && taskIntent.getComponent() != null &&
628 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700629 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700630 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800631 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700632 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
633 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800634 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700635 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
636 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700637 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800639 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700640 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
641 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800642 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700643 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700644 }
645
646 return null;
647 }
648
649 /**
650 * Returns the first activity (starting from the top of the stack) that
651 * is the same as the given activity. Returns null if no such activity
652 * is found.
653 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700654 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700655 ComponentName cls = intent.getComponent();
656 if (info.targetActivity != null) {
657 cls = new ComponentName(info.packageName, info.targetActivity);
658 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700659 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700660
Craig Mautner000f0022013-02-26 15:04:29 -0800661 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700662 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700663 final boolean notCurrentUserTask =
664 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700665 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700666
Craig Mautner000f0022013-02-26 15:04:29 -0800667 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
668 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700669 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700670 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700671 }
Craig Mautner000f0022013-02-26 15:04:29 -0800672 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 return r;
674 }
675 }
676 }
677
678 return null;
679 }
680
Amith Yamasani742a6712011-05-04 14:49:28 -0700681 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700682 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700683 */
Craig Mautner93529a42013-10-04 15:03:13 -0700684 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800685 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700686 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800687 }
688 mCurrentUser = userId;
689
690 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800691 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800692 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700693 final TaskRecord task = mTaskHistory.get(i);
694
695 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
696 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700697 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700698 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700699 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700700 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800701 mTaskHistory.remove(i);
702 mTaskHistory.add(task);
703 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800704 // Use same value for i.
705 } else {
706 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800707 }
708 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700709 if (VALIDATE_TOKENS) {
710 validateAppTokensLocked();
711 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700712 }
713
Craig Mautner2420ead2013-04-01 17:13:20 -0700714 void minimalResumeActivityLocked(ActivityRecord r) {
715 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700716 if (DEBUG_STATES) Slog.v(TAG_STATES,
717 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700718 r.stopped = false;
719 mResumedActivity = r;
720 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800721 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700722 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700723 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700724 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700725 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
726 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700727 }
728
Chong Zhang45c25ce2015-08-10 22:18:26 -0700729 private void addRecentActivityLocked(ActivityRecord r) {
730 if (r != null) {
731 mRecentTasks.addLocked(r.task);
732 r.task.touchActiveTime();
733 }
734 }
735
Narayan Kamath7829c812015-06-08 17:39:43 +0100736 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700737 if (mFullyDrawnStartTime != 0) {
738 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
739 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100740 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700741 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
742 }
743
744 private void stopFullyDrawnTraceIfNeeded() {
745 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
746 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
747 mFullyDrawnStartTime = 0;
748 }
749 }
750
Craig Mautnere79d42682013-04-01 19:01:53 -0700751 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700752 if (r.displayStartTime == 0) {
753 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
754 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100755 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700756 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700757 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700758 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100759 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700760 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700761 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800763
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700764 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700765 // Make sure that there is no activity waiting for this to launch.
766 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
767 r.displayStartTime = r.fullyDrawnStartTime = 0;
768 } else {
769 mStackSupervisor.removeTimeoutsForActivityLocked(r);
770 mStackSupervisor.scheduleIdleTimeoutLocked(r);
771 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700772 }
773
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800774 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800775 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800776 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
777 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
778 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
779 activities.get(activityNdx).setSleeping(false);
780 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800781 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800782 if (mPausingActivity != null) {
783 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
784 activityPausedLocked(mPausingActivity.appToken, true);
785 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800786 }
787
Craig Mautner0eea92c2013-05-16 13:35:39 -0700788 /**
789 * @return true if something must be done before going to sleep.
790 */
791 boolean checkReadyForSleepLocked() {
792 if (mResumedActivity != null) {
793 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700794 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
795 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
796 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700797 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700798 return true;
799 }
800 if (mPausingActivity != null) {
801 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700802 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700803 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800804 }
805
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700806 if (hasVisibleBehindActivity()) {
807 // Stop visible behind activity before going to sleep.
808 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
809 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700810 if (DEBUG_STATES) Slog.v(TAG_STATES,
811 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700812 return true;
813 }
814
Craig Mautner0eea92c2013-05-16 13:35:39 -0700815 return false;
816 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800817
Craig Mautner0eea92c2013-05-16 13:35:39 -0700818 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700819 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800820
Craig Mautner0eea92c2013-05-16 13:35:39 -0700821 // Make sure any stopped but visible activities are now sleeping.
822 // This ensures that the activity's onStop() is called.
823 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
824 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
825 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
826 final ActivityRecord r = activities.get(activityNdx);
827 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
828 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800829 }
830 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800831 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 }
Craig Mautner59c00972012-07-30 12:10:24 -0700833
Dianne Hackbornd2835932010-12-13 16:28:46 -0800834 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700835 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800836 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700837 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800838 return null;
839 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800840
Winson Chung083baf92014-07-11 10:32:42 -0700841 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700842 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800843 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700844 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700845 return null;
846 }
847
Winson Chung48a10a52014-08-27 14:36:51 -0700848 int w = mService.mThumbnailWidth;
849 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800850 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700851 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800852 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700853 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800854 }
Winson Chung376543b2014-05-21 17:43:28 -0700855 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800856 return null;
857 }
858
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700859 /**
860 * Start pausing the currently resumed activity. It is an error to call this if there
861 * is already an activity being paused or there is no resumed activity.
862 *
863 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
864 * @param uiSleeping True if this is happening with the user interface going to sleep (the
865 * screen turning off).
866 * @param resuming True if this is being called as part of resuming the top activity, so
867 * we shouldn't try to instigate a resume here.
868 * @param dontWait True if the caller does not want to wait for the pause to complete. If
869 * set to true, we will immediately complete the pause here before returning.
870 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
871 * it to tell us when it is done.
872 */
873 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
874 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800875 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800876 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
877 + " state=" + mPausingActivity.state);
878 if (!mService.isSleeping()) {
879 // Avoid recursion among check for sleep and complete pause during sleeping.
880 // Because activity will be paused immediately after resume, just let pause
881 // be completed by the order of activity paused from clients.
882 completePauseLocked(false);
883 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800884 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885 ActivityRecord prev = mResumedActivity;
886 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700887 if (!resuming) {
888 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
889 mStackSupervisor.resumeTopActivitiesLocked();
890 }
891 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800893
894 if (mActivityContainer.mParentActivity == null) {
895 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700896 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800897 }
898
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700899 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700900 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700901 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800902 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700903 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700904 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
905 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906 prev.state = ActivityState.PAUSING;
907 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700908 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700909 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700910 if (mService.mHasRecents
911 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800912 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700913 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700914 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915
916 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800917
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700919 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 try {
921 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700922 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700924 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800925 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700926 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 } catch (Exception e) {
928 // Ignore exception, if process died other code will cleanup.
929 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800930 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700932 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 }
934 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800935 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700937 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
939
940 // If we are not going to sleep, we want to ensure the device is
941 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700942 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700943 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 }
945
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800946 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947 // Have the window manager pause its key dispatching until the new
948 // activity has started. If we're pausing the activity just because
949 // the screen is being turned off and the UI is sleeping, don't interrupt
950 // key dispatch; the same activity will pick it up again on wakeup.
951 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800952 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700953 } else if (DEBUG_PAUSE) {
954 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955 }
956
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700957 if (dontWait) {
958 // If the caller said they don't want to wait for the pause, then complete
959 // the pause now.
960 completePauseLocked(false);
961 return false;
962
963 } else {
964 // Schedule a pause timeout in case the app doesn't respond.
965 // We don't give it much time because this directly impacts the
966 // responsiveness seen by the user.
967 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
968 msg.obj = prev;
969 prev.pauseTime = SystemClock.uptimeMillis();
970 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700971 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700972 return true;
973 }
974
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700975 } else {
976 // This activity failed to schedule the
977 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700978 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700979 if (!resuming) {
980 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
981 }
982 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983 }
984 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700985
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700986 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700987 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
988 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989
Craig Mautnerd2328952013-03-05 12:46:26 -0800990 final ActivityRecord r = isInStackLocked(token);
991 if (r != null) {
992 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
993 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700994 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -0800995 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700996 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800997 } else {
998 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
999 r.userId, System.identityHashCode(r), r.shortComponentName,
1000 mPausingActivity != null
1001 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001002 if (r.state == ActivityState.PAUSING) {
1003 r.state = ActivityState.PAUSED;
1004 if (r.finishing) {
1005 if (DEBUG_PAUSE) Slog.v(TAG,
1006 "Executing finish of failed to pause activity: " + r);
1007 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1008 }
louis_chang047dfd42015-04-08 16:35:55 +08001009 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001010 }
1011 }
1012 }
1013
Craig Mautnera0026042014-04-23 11:45:37 -07001014 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1015 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001016 if (r.state != ActivityState.STOPPING) {
1017 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1018 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1019 return;
1020 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001021 if (persistentState != null) {
1022 r.persistentState = persistentState;
1023 mService.notifyTaskPersisterLocked(r.task, false);
1024 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001025 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001026 if (icicle != null) {
1027 // If icicle is null, this is happening due to a timeout, so we
1028 // haven't really saved the state.
1029 r.icicle = icicle;
1030 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001031 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001032 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001033 }
1034 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001035 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001036 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1037 r.stopped = true;
1038 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001039 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1040 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001041 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001042 if (r.finishing) {
1043 r.clearOptionsLocked();
1044 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001045 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001046 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001047 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001048 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001049 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001050 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001051 }
1052 }
1053 }
1054
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001055 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001056 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001057 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001058
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001059 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001060 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001061 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001062 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001063 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001064 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001065 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001066 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001067 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1068 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001069 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001070 if (prev.configDestroy) {
1071 // The previous is being paused because the configuration
1072 // is changing, which means it is actually stopping...
1073 // To juggle the fact that we are also starting a new
1074 // instance right now, we need to first completely stop
1075 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001076 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001077 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001078 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001079 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001080 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001081 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001082 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1083 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001084 // If we already have a few activities waiting to stop,
1085 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001086 // them out. Or if r is the last of activity of the last task the stack
1087 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001088 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001089 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001090 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001091 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001092 }
1093 }
1094 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001095 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001096 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001097 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001098 // It is possible the activity was freezing the screen before it was paused.
1099 // In that case go ahead and remove the freeze this activity has on the screen
1100 // since it is no longer visible.
1101 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001102 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001103 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001104
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001105 if (resumeNext) {
1106 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1107 if (!mService.isSleepingOrShuttingDown()) {
1108 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1109 } else {
1110 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001111 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001112 if (top == null || (prev != null && top != prev)) {
1113 // If there are no more activities available to run,
1114 // do resume anyway to start something. Also if the top
1115 // activity on the stack is not the just paused activity,
1116 // we need to go ahead and resume it to ensure we complete
1117 // an in-flight app switch.
1118 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1119 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001120 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001121 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001122
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001123 if (prev != null) {
1124 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001125
Craig Mautner525f3d92013-05-07 14:01:50 -07001126 if (prev.app != null && prev.cpuTimeAtResume > 0
1127 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001128 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1129 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001130 if (diff > 0) {
1131 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1132 synchronized (bsi) {
1133 BatteryStatsImpl.Uid.Proc ps =
1134 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001135 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001136 if (ps != null) {
1137 ps.addForegroundTimeLocked(diff);
1138 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001139 }
1140 }
1141 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001142 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001143 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001144
1145 // Notfiy when the task stack has changed
1146 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001147 }
1148
1149 /**
1150 * Once we know that we have asked an application to put an activity in
1151 * the resumed state (either by launching it or explicitly telling it),
1152 * this function updates the rest of our state to match that fact.
1153 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001154 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001155 next.idle = false;
1156 next.results = null;
1157 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001158
Chong Zhang85ee6542015-10-02 13:36:38 -07001159 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001160 ProcessRecord app = next.task.mActivities.get(0).app;
1161 if (app != null && app != mService.mHomeProcess) {
1162 mService.mHomeProcess = app;
1163 }
1164 }
1165
Craig Mautner07566322013-09-26 16:42:55 -07001166 if (next.nowVisible) {
1167 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001168 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001169 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170
1171 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001172 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001174 mStackSupervisor.reportResumedActivityLocked(next);
1175
1176 next.resumeKeyDispatchingLocked();
1177 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001178
1179 // Mark the point when the activity is resuming
1180 // TODO: To be more accurate, the mark should be before the onCreate,
1181 // not after the onResume. But for subsequent starts, onResume is fine.
1182 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001183 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184 } else {
1185 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1186 }
Winson Chung376543b2014-05-21 17:43:28 -07001187
George Mount6ba042b2014-07-28 11:12:28 -07001188 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001189
1190 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1191 // When resuming an activity, require it to call requestVisibleBehind() again.
1192 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1193 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 }
1195
Craig Mautner2568c3a2015-03-26 14:22:34 -07001196 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001197 r.visible = visible;
1198 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001199 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001200 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001201 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001202 container.setVisible(visible);
1203 }
1204 }
1205
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001206 // Find the first visible activity above the passed activity and if it is translucent return it
1207 // otherwise return null;
1208 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1209 TaskRecord task = r.task;
1210 if (task == null) {
1211 return null;
1212 }
1213
1214 ActivityStack stack = task.stack;
1215 if (stack == null) {
1216 return null;
1217 }
1218
1219 int stackNdx = mStacks.indexOf(stack);
1220
1221 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1222 int taskNdx = tasks.indexOf(task);
1223
1224 ArrayList<ActivityRecord> activities = task.mActivities;
1225 int activityNdx = activities.indexOf(r) + 1;
1226
1227 final int numStacks = mStacks.size();
1228 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001229 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001230 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001231 final int numTasks = tasks.size();
1232 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001233 final TaskRecord currentTask = tasks.get(taskNdx);
1234 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001235 final int numActivities = activities.size();
1236 while (activityNdx < numActivities) {
1237 final ActivityRecord activity = activities.get(activityNdx);
1238 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001239 return historyStack.mFullscreen
1240 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001241 }
1242 ++activityNdx;
1243 }
1244 activityNdx = 0;
1245 ++taskNdx;
1246 }
1247 taskNdx = 0;
1248 ++stackNdx;
1249 }
1250
1251 return null;
1252 }
1253
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001254 private ActivityStack getNextVisibleStackLocked() {
1255 ArrayList<ActivityStack> stacks = mStacks;
1256 final ActivityRecord parent = mActivityContainer.mParentActivity;
1257 if (parent != null) {
1258 stacks = parent.task.stack.mStacks;
1259 }
1260 if (stacks != null) {
1261 for (int i = stacks.size() - 1; i >= 0; --i) {
1262 ActivityStack stack = stacks.get(i);
1263 if (stack != this && stack.isStackVisibleLocked()) {
1264 return stack;
1265 }
1266 }
1267 }
1268 return null;
1269 }
1270
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001271 /** Returns true if the stack contains a fullscreen task. */
1272 private boolean hasFullscreenTask() {
1273 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1274 final TaskRecord task = mTaskHistory.get(i);
1275 if (task.mFullscreen) {
1276 return true;
1277 }
1278 }
1279 return false;
1280 }
1281
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001282 private boolean hasTranslucentActivity(ActivityStack stack) {
1283 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1284 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1285 final TaskRecord task = tasks.get(taskNdx);
1286 final ArrayList<ActivityRecord> activities = task.mActivities;
1287 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1288 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001289
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001290 // Conditions for an activity to obscure the stack we're
1291 // examining:
1292 // 1. Not Finishing AND Visible AND:
1293 // 2. Either:
1294 // - Full Screen Activity OR
1295 // - On top of Home and our stack is NOT home
1296 if (!r.finishing && r.visible && (r.fullscreen ||
1297 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1298 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001299 }
1300 }
1301 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001302 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001303 }
1304
1305 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001306 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001307 if (!isAttached()) {
1308 return false;
1309 }
1310
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001311 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001312 return true;
1313 }
1314
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001315 final int stackIndex = mStacks.indexOf(this);
1316
1317 if (stackIndex == mStacks.size() - 1) {
1318 Slog.wtf(TAG,
1319 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1320 return false;
1321 }
1322
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001323 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1324 final int focusedStackId = focusedStack.mStackId;
1325
1326 if (mStackId == DOCKED_STACK_ID) {
1327 // Docked stack is always visible, except in the case where the home activity
1328 // is the top running activity in the focused home stack.
1329 if (focusedStackId != HOME_STACK_ID) {
1330 return true;
1331 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001332 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001333 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1334 }
1335
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001336 final int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001337 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1338 && stackIndex == belowFocusedIndex) {
1339 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001340 return true;
1341 }
1342
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001343 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1344 && hasTranslucentActivity(focusedStack)) {
1345 // Stacks behind the fullscreen stack with a translucent activity are always
1346 // visible so they can act as a backdrop to the translucent activity.
1347 // For example, dialog activities
1348 if (stackIndex == belowFocusedIndex) {
1349 return true;
1350 }
1351 if (belowFocusedIndex >= 0) {
1352 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001353 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1354 && stackIndex == (belowFocusedIndex - 1)) {
1355 // The stack behind the docked or pinned stack is also visible so we can have a
1356 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001357 return true;
1358 }
1359 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001360 }
1361
Wale Ogunwale3797c222015-10-27 14:21:58 -07001362 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001363 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001364 return false;
1365 }
1366
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001367 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001368 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001369
1370 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1371 continue;
1372 }
1373
Wale Ogunwale3797c222015-10-27 14:21:58 -07001374 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001375 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001376 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001377 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001378
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001379 if (!hasTranslucentActivity(stack)) {
1380 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001381 }
1382 }
1383
1384 return true;
1385 }
1386
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001387 final int rankTaskLayers(int baseLayer) {
1388 int layer = 0;
1389 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1390 final TaskRecord task = mTaskHistory.get(taskNdx);
1391 ActivityRecord r = task.topRunningActivityLocked();
1392 if (r == null || r.finishing || !r.visible) {
1393 task.mLayerRank = -1;
1394 } else {
1395 task.mLayerRank = baseLayer + layer++;
1396 }
1397 }
1398 return layer;
1399 }
1400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 /**
1402 * Make sure that all activities that need to be visible (that is, they
1403 * currently can be seen by the user) actually are.
1404 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001405 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1406 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001407 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001408 if (top == null) {
1409 return;
1410 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001411 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1412 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 + " configChanges=0x" + Integer.toHexString(configChanges));
1414
Craig Mautner5eda9b32013-07-02 11:58:16 -07001415 if (mTranslucentActivityWaiting != top) {
1416 mUndrawnActivitiesBelowTopTranslucent.clear();
1417 if (mTranslucentActivityWaiting != null) {
1418 // Call the callback with a timeout indication.
1419 notifyActivityDrawnLocked(null);
1420 mTranslucentActivityWaiting = null;
1421 }
1422 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1423 }
1424
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425 // If the top activity is not fullscreen, then we need to
1426 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001427 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001428 final boolean stackInvisible = !isStackVisibleLocked();
1429 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001430 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001431
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001433 final TaskRecord task = mTaskHistory.get(taskNdx);
1434 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001435
Craig Mautnerd44711d2013-02-23 11:24:36 -08001436 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1437 final ActivityRecord r = activities.get(activityNdx);
1438 if (r.finishing) {
1439 continue;
1440 }
1441 if (aboveTop && r != top) {
1442 continue;
1443 }
1444 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001445 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1446 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001447 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001448 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1449 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001450 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001451
Craig Mautnerd44711d2013-02-23 11:24:36 -08001452 // First: if this is not the current activity being started, make
1453 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001454 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001455 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001456 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001457
1458 if (r.app == null || r.app.thread == null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001459 // We need to make sure the app is running if it's the top, or it is
1460 // just made visible from invisible.
1461 // If the app is already visible, it must have died while it was visible.
1462 // In this case, we'll show the dead window but will not restart the app.
1463 // Otherwise we could end up thrashing.
1464 if (r == top || !r.visible) {
1465 // This activity needs to be visible, but isn't even running...
1466 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001467 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Chong Zhang112eb8c2015-11-02 11:17:00 -08001468 "Start and freeze screen for " + r);
1469 if (r != starting) {
1470 r.startFreezingScreenLocked(r.app, configChanges);
1471 }
1472 if (!r.visible || r.mLaunchTaskBehind) {
1473 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1474 "Starting and making visible: " + r);
1475 setVisible(r, true);
1476 }
1477 if (r != starting) {
1478 mStackSupervisor.startSpecificActivityLocked(
1479 r, noStackActivityResumed, false);
1480 if (activityNdx >= activities.size()) {
1481 // Record may be removed if its process needs to restart.
1482 activityNdx = activities.size() - 1;
1483 } else {
1484 noStackActivityResumed = false;
1485 }
riddle_hsu36ee73d2015-06-05 16:38:38 +08001486 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001487 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001488 } else if (r.visible) {
1489 // If this activity is already visible, then there is nothing
1490 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001491 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1492 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001494 try {
George Mount6ba042b2014-07-28 11:12:28 -07001495 if (r.returningOptions != null) {
1496 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1497 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001498 }
1499 } catch(RemoteException e) {
1500 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001501 if (r.state == ActivityState.RESUMED) {
1502 noStackActivityResumed = false;
1503 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001504 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001505 // This activity is not currently visible, but is running.
1506 // Tell it to become visible.
1507 r.visible = true;
1508 if (r.state != ActivityState.RESUMED && r != starting) {
1509 // If this activity is paused, tell it
1510 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001511 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1512 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001513 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001514 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001515 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001516 mUndrawnActivitiesBelowTopTranslucent.add(r);
1517 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001518 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001519 r.sleeping = false;
1520 r.app.pendingUiClean = true;
1521 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1522 r.stopFreezingScreenLocked(false);
1523 } catch (Exception e) {
1524 // Just skip on any failure; we'll make it
1525 // visible when it next restarts.
1526 Slog.w(TAG, "Exception thrown making visibile: "
1527 + r.intent.getComponent(), e);
1528 }
1529 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001531
Craig Mautnerd44711d2013-02-23 11:24:36 -08001532 // Aggregate current change flags.
1533 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534
Craig Mautnerd44711d2013-02-23 11:24:36 -08001535 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001536 // At this point, nothing else needs to be shown in this task.
1537 behindFullscreenActivity = true;
1538 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001539 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001540 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001541 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001542 behindFullscreenActivity = true;
1543 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001544 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001545 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001546 }
1547 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001548 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001549 "Make invisible? " + r + " finishing=" + r.finishing
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001550 + " state=" + r.state + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001551 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001552 // Now for any activities that aren't visible to the user, make
1553 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001554 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001555 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001557 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001558 switch (r.state) {
1559 case STOPPING:
1560 case STOPPED:
1561 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001562 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1563 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001564 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1565 }
1566 break;
1567
1568 case INITIALIZING:
1569 case RESUMED:
1570 case PAUSING:
1571 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001572 // This case created for transitioning activities from
1573 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001574 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001575 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001576 } else {
1577 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1578 mStackSupervisor.mStoppingActivities.add(r);
1579 }
1580 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001581 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001582 break;
1583
1584 default:
1585 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001586 }
1587 } catch (Exception e) {
1588 // Just skip on any failure; we'll make it
1589 // visible when it next restarts.
1590 Slog.w(TAG, "Exception thrown making hidden: "
1591 + r.intent.getComponent(), e);
1592 }
1593 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001594 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 }
1597 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001598 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1599 // The visibility of tasks and the activities they contain in freeform stack are
1600 // determined individually unlike other stacks where the visibility or fullscreen
1601 // status of an activity in a previous task affects other.
1602 behindFullscreenActivity = stackInvisible;
1603 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001604 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001605
1606 if (mTranslucentActivityWaiting != null &&
1607 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1608 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1609 notifyActivityDrawnLocked(null);
1610 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 }
Craig Mautner58547802013-03-05 08:23:53 -08001612
Todd Kennedyaab56db2015-01-30 09:39:53 -08001613 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001614 mTranslucentActivityWaiting = r;
1615 mUndrawnActivitiesBelowTopTranslucent.clear();
1616 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1617 }
1618
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001619 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1620 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1621 final TaskRecord task = mTaskHistory.get(taskNdx);
1622 final ArrayList<ActivityRecord> activities = task.mActivities;
1623 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1624 final ActivityRecord r = activities.get(activityNdx);
1625 if ( r.appTimeTracker != except) {
1626 r.appTimeTracker = null;
1627 }
1628 }
1629 }
1630 }
1631
Craig Mautner5eda9b32013-07-02 11:58:16 -07001632 /**
1633 * Called as activities below the top translucent activity are redrawn. When the last one is
1634 * redrawn notify the top activity by calling
1635 * {@link Activity#onTranslucentConversionComplete}.
1636 *
1637 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1638 * occurred and the activity will be notified immediately.
1639 */
1640 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001641 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001642 if ((r == null)
1643 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1644 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1645 // The last undrawn activity below the top has just been drawn. If there is an
1646 // opaque activity at the top, notify it that it can become translucent safely now.
1647 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1648 mTranslucentActivityWaiting = null;
1649 mUndrawnActivitiesBelowTopTranslucent.clear();
1650 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1651
Craig Mautner71dd1b62014-02-18 15:48:52 -08001652 if (waitingActivity != null) {
1653 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1654 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1655 try {
1656 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1657 waitingActivity.appToken, r != null);
1658 } catch (RemoteException e) {
1659 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001660 }
1661 }
1662 }
1663 }
1664
Craig Mautnera61bc652013-10-28 15:43:18 -07001665 /** If any activities below the top running one are in the INITIALIZING state and they have a
1666 * starting window displayed then remove that starting window. It is possible that the activity
1667 * in this state will never resumed in which case that starting window will be orphaned. */
1668 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001669 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001670 boolean aboveTop = true;
1671 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1672 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1673 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1674 final ActivityRecord r = activities.get(activityNdx);
1675 if (aboveTop) {
1676 if (r == topActivity) {
1677 aboveTop = false;
1678 }
1679 continue;
1680 }
1681
1682 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001683 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1684 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001685 r.mStartingWindowShown = false;
1686 mWindowManager.removeAppStartingWindow(r.appToken);
1687 }
1688 }
1689 }
1690 }
1691
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 /**
1693 * Ensure that the top activity in the stack is resumed.
1694 *
1695 * @param prev The previously resumed activity, for when in the process
1696 * of pausing; can be null to call from elsewhere.
1697 *
1698 * @return Returns true if something is being resumed, or false if
1699 * nothing happened.
1700 */
1701 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001702 return resumeTopActivityLocked(prev, null);
1703 }
1704
Chong Zhang280d3322015-11-03 17:27:26 -08001705 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001706 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001707 // Don't even start recursing.
1708 return false;
1709 }
1710
1711 boolean result = false;
1712 try {
1713 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001714 mStackSupervisor.inResumeTopActivity = true;
1715 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1716 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001717 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001718 }
Craig Mautner544efa72014-09-04 16:41:20 -07001719 result = resumeTopActivityInnerLocked(prev, options);
1720 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001721 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001722 }
1723 return result;
1724 }
1725
Chong Zhang280d3322015-11-03 17:27:26 -08001726 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001727 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001728
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001729 if (!mService.mBooting && !mService.mBooted) {
1730 // Not ready yet!
1731 return false;
1732 }
1733
Craig Mautnerdf88d732014-01-27 09:21:32 -08001734 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001735 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001736 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001737 // Do not resume this stack if its parent is not resumed.
1738 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1739 return false;
1740 }
1741
Craig Mautnera61bc652013-10-28 15:43:18 -07001742 cancelInitializingActivities();
1743
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001745 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746
1747 // Remember how we'll process this pause/resume situation, and ensure
1748 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001749 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1750 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751
Craig Mautner84984fa2014-06-19 11:19:20 -07001752 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001754 // There are no more activities!
1755 final String reason = "noMoreActivities";
1756 if (!mFullscreen) {
1757 // Try to move focus to the next visible stack with a running activity if this
1758 // stack is not covering the entire screen.
1759 final ActivityStack stack = getNextVisibleStackLocked();
1760 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1761 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1762 }
1763 }
1764 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001765 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001766 if (DEBUG_STATES) Slog.d(TAG_STATES,
1767 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001768 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001769 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001770 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1771 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1772 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001773 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 }
1775
1776 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001777
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001779 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1780 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001781 // Make sure we have executed any pending transitions, since there
1782 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001783 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001785 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001786 if (DEBUG_STATES) Slog.d(TAG_STATES,
1787 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001788 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 return false;
1790 }
1791
Craig Mautner525f3d92013-05-07 14:01:50 -07001792 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001793 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001794 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001795 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001796 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001797 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001798 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001799 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001800 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001801 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001802 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001803 } else if (!isOnHomeDisplay()) {
1804 return false;
1805 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001806 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001807 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001808 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1809 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1810 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001811 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001812 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001813 }
1814
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 // If we are sleeping, and there is no resumed activity, and the top
1816 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001817 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001818 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001819 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 // Make sure we have executed any pending transitions, since there
1821 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001822 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001824 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001825 if (DEBUG_STATES) Slog.d(TAG_STATES,
1826 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001827 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 return false;
1829 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001830
1831 // Make sure that the user who owns this activity is started. If not,
1832 // we will just leave it as is because someone should be bringing
1833 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001834 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001835 Slog.w(TAG, "Skipping resume of top activity " + next
1836 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001837 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001838 return false;
1839 }
1840
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001841 // The activity may be waiting for stop, but that is no longer
1842 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001843 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001844 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001845 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001846 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001847
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001848 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001850 // If we are currently pausing an activity, then don't do anything
1851 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001852 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001853 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001854 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001855 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001856 return false;
1857 }
1858
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001859 // Okay we are now going to start a switch, to 'next'. We may first
1860 // have to pause the current activity, but this is an important point
1861 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001862 // XXX "App Redirected" dialog is getting too many false positives
1863 // at this point, so turn off for now.
1864 if (false) {
1865 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1866 long now = SystemClock.uptimeMillis();
1867 final boolean inTime = mLastStartedActivity.startTime != 0
1868 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1869 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1870 final int nextUid = next.info.applicationInfo.uid;
1871 if (inTime && lastUid != nextUid
1872 && lastUid != next.launchedFromUid
1873 && mService.checkPermission(
1874 android.Manifest.permission.STOP_APP_SWITCHES,
1875 -1, next.launchedFromUid)
1876 != PackageManager.PERMISSION_GRANTED) {
1877 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1878 } else {
1879 next.startTime = now;
1880 mLastStartedActivity = next;
1881 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001882 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001883 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001884 mLastStartedActivity = next;
1885 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001886 }
Craig Mautner58547802013-03-05 08:23:53 -08001887
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001888 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1889
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001890 // We need to start pausing the current activity so the top one
1891 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001892 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1893 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001894 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001895 if (DEBUG_STATES) Slog.d(TAG_STATES,
1896 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001897 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001898 }
1899 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001900 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001901 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001902 // At this point we want to put the upcoming activity's process
1903 // at the top of the LRU list, since we know we will be needing it
1904 // very soon and it would be a waste to let it get killed if it
1905 // happens to be sitting towards the end.
1906 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001907 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001908 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001909 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001910 return true;
1911 }
1912
Christopher Tated3f175c2012-06-14 14:16:54 -07001913 // If the most recent activity was noHistory but was only stopped rather
1914 // than stopped+finished because the device went to sleep, we need to make
1915 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001916 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001917 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001918 if (DEBUG_STATES) Slog.d(TAG_STATES,
1919 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001920 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001921 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001922 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001923 }
1924
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001925 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001926 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1927 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001928 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001929 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1930 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001931 } else {
1932 // The next activity is already visible, so hide the previous
1933 // activity's windows right now so we can show the new one ASAP.
1934 // We only do this if the previous is finishing, which should mean
1935 // it is on top of the one being resumed so hiding it quickly
1936 // is good. Otherwise, we want to do the normal route of allowing
1937 // the resumed activity to be shown so we can decide if the
1938 // previous should actually be hidden depending on whether the
1939 // new one is found to be full-screen or not.
1940 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001941 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001942 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1943 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001944 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001945 + ", nowVisible=" + next.nowVisible);
1946 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001947 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001948 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001949 + ", waitingVisible="
1950 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1951 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001952 }
1953 }
1954 }
1955
Dianne Hackborne7f97212011-02-24 14:40:20 -08001956 // Launching this app's activity, make sure the app is no longer
1957 // considered stopped.
1958 try {
1959 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001960 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001961 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001962 } catch (IllegalArgumentException e) {
1963 Slog.w(TAG, "Failed trying to unstop package "
1964 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001965 }
1966
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 // We are starting up the next activity, so tell the window manager
1968 // that the previous one will be hidden soon. This way it can know
1969 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001970 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001971 if (prev != null) {
1972 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001973 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 "Prepare close transition: prev=" + prev);
1975 if (mNoAnimActivities.contains(prev)) {
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_CLOSE
1981 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001982 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001983 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001985 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1986 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001987 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001988 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001989 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001990 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001992 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001993 : next.mLaunchTaskBehind
1994 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1995 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001996 }
1997 }
Craig Mautner967212c2013-04-13 21:10:58 -07001998 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001999 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002001 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002002 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002004 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002005 }
2006 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002007
2008 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002009 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002010 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2011 if (opts != null) {
2012 resumeAnimOptions = opts.toBundle();
2013 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002014 next.applyOptionsLocked();
2015 } else {
2016 next.clearOptionsLocked();
2017 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018
Craig Mautnercf910b02013-04-23 11:23:27 -07002019 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002021 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002022
2023 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002024 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002025
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002026 // schedule launch ticks to collect information about slow apps.
2027 next.startLaunchTickingLocked();
2028
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002029 ActivityRecord lastResumedActivity =
2030 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002031 ActivityState lastState = next.state;
2032
2033 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002034
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002035 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002036 next.state = ActivityState.RESUMED;
2037 mResumedActivity = next;
2038 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002039 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002040 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002042 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043
2044 // Have the window manager re-evaluate the orientation of
2045 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002046 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002047 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002048 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002049 mService.mConfiguration,
2050 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2051 if (config != null) {
2052 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002053 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002054 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002055 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002056
Craig Mautner525f3d92013-05-07 14:01:50 -07002057 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 // The configuration update wasn't able to keep the existing
2059 // instance of the activity, and instead started a new one.
2060 // We should be all done, but let's just make sure our activity
2061 // is still at the top and schedule another run if something
2062 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002063 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002064 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 "Activity config changed during resume: " + next
2066 + ", new next: " + nextNext);
2067 if (nextNext != next) {
2068 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002069 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002071 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002072 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002073 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002074 return true;
2075 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002076 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002077 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002078 }
Craig Mautner58547802013-03-05 08:23:53 -08002079
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002080 try {
2081 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002082 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 if (a != null) {
2084 final int N = a.size();
2085 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002086 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2087 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002088 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 }
2090 }
2091
2092 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002093 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002094 }
2095
Craig Mautner299f9602015-01-26 09:47:33 -08002096 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2097 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002098
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002099 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002100 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002101 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002102 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002103 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002104 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002105 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002106
Craig Mautner0eea92c2013-05-16 13:35:39 -07002107 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002109 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 } catch (Exception e) {
2111 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002112 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002113 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002115 if (lastStack != null) {
2116 lastStack.mResumedActivity = lastResumedActivity;
2117 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 Slog.i(TAG, "Restarting because process died: " + next);
2119 if (!next.hasBeenLaunched) {
2120 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002121 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2122 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002123 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002124 next.appToken, next.packageName, next.theme,
2125 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002126 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2127 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 }
George Mount2c92c972014-03-20 09:38:23 -07002129 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002130 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131 return true;
2132 }
2133
2134 // From this point on, if something goes wrong there is no way
2135 // to recover the activity.
2136 try {
2137 next.visible = true;
2138 completeResumeLocked(next);
2139 } catch (Exception e) {
2140 // If any exception gets thrown, toss away this
2141 // activity and try the next one.
2142 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002143 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002144 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002145 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 return true;
2147 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 next.stopped = false;
2149
2150 } else {
2151 // Whoops, need to restart this activity!
2152 if (!next.hasBeenLaunched) {
2153 next.hasBeenLaunched = true;
2154 } else {
2155 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002156 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002157 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002158 mService.compatibilityInfoForPackageLocked(
2159 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002161 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002162 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002164 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002166 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002167 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002168 }
2169
Craig Mautnercf910b02013-04-23 11:23:27 -07002170 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 return true;
2172 }
2173
riddle_hsuc215a4f2014-12-27 12:10:45 +08002174 private TaskRecord getNextTask(TaskRecord targetTask) {
2175 final int index = mTaskHistory.indexOf(targetTask);
2176 if (index >= 0) {
2177 final int numTasks = mTaskHistory.size();
2178 for (int i = index + 1; i < numTasks; ++i) {
2179 TaskRecord task = mTaskHistory.get(i);
2180 if (task.userId == targetTask.userId) {
2181 return task;
2182 }
2183 }
2184 }
2185 return null;
2186 }
2187
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002188 private void insertTaskAtPosition(TaskRecord task, int position) {
2189 if (position >= mTaskHistory.size()) {
2190 insertTaskAtTop(task, null);
2191 return;
2192 }
2193 // Calculate maximum possible position for this task.
2194 int maxPosition = mTaskHistory.size();
2195 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002196 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002197 // Put non-current user tasks below current user tasks.
2198 while (maxPosition > 0) {
2199 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2200 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002201 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002202 break;
2203 }
2204 maxPosition--;
2205 }
2206 }
2207 position = Math.min(position, maxPosition);
2208 mTaskHistory.remove(task);
2209 mTaskHistory.add(position, task);
2210 updateTaskMovement(task, true);
2211 }
2212
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002213 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002214 // If the moving task is over home stack, transfer its return type to next task
2215 if (task.isOverHomeStack()) {
2216 final TaskRecord nextTask = getNextTask(task);
2217 if (nextTask != null) {
2218 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2219 }
2220 }
2221
Craig Mautner9c85c202013-10-04 20:11:26 -07002222 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002223 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002224 if (isOnHomeDisplay()) {
2225 ActivityStack lastStack = mStackSupervisor.getLastStack();
2226 final boolean fromHome = lastStack.isHomeStack();
2227 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002228 task.setTaskToReturnTo(fromHome
2229 ? lastStack.topTask() == null
2230 ? HOME_ACTIVITY_TYPE
2231 : lastStack.topTask().taskType
2232 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002233 }
2234 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002235 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002236 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002237
Craig Mautnerac6f8432013-07-17 13:24:59 -07002238 mTaskHistory.remove(task);
2239 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002240 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002241 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002242 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002243 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002244 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002245 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002246 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002247 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002248 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002249 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002250 break;
2251 }
2252 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002253 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002254 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002255 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002256 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002257 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002258
Craig Mautner8849a5e2013-04-02 16:41:03 -07002259 final void startActivityLocked(ActivityRecord r, boolean newTask,
Chong Zhang280d3322015-11-03 17:27:26 -08002260 boolean doResume, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002261 TaskRecord rTask = r.task;
2262 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002263 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2264 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002265 // Last activity in task had been removed or ActivityManagerService is reusing task.
2266 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002267 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002268 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002269 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002270 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002271 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 if (!newTask) {
2273 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002274 boolean startIt = true;
2275 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2276 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002277 if (task.getTopActivity() == null) {
2278 // All activities in task are finishing.
2279 continue;
2280 }
Craig Mautner70a86932013-02-28 22:37:44 -08002281 if (task == r.task) {
2282 // Here it is! Now, if this is not yet visible to the
2283 // user, then just add it without starting; it will
2284 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002285 if (!startIt) {
2286 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2287 + task, new RuntimeException("here").fillInStackTrace());
2288 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002289 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002290 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002291 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002292 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002294 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 return;
2296 }
2297 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002298 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002299 startIt = false;
2300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 }
2302 }
2303
2304 // Place a new activity at top of stack, so it is next to interact
2305 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002306
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002307 // If we are not placing the new activity frontmost, we do not want
2308 // to deliver the onUserLeaving callback to the actual frontmost
2309 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002310 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002311 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002312 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002313 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 }
Craig Mautner70a86932013-02-28 22:37:44 -08002315
2316 task = r.task;
2317
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002318 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002319 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002320 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002321 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002322 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002323
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002324 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002325 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 // We want to show the starting preview window if we are
2327 // switching to a new task, or the next activity's process is
2328 // not currently running.
2329 boolean showStartingIcon = newTask;
2330 ProcessRecord proc = r.app;
2331 if (proc == null) {
2332 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2333 }
2334 if (proc == null || proc.thread == null) {
2335 showStartingIcon = true;
2336 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002337 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002339 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002340 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002341 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002343 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002344 ? r.mLaunchTaskBehind
2345 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2346 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002347 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 mNoAnimActivities.remove(r);
2349 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002350 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 boolean doShow = true;
2352 if (newTask) {
2353 // Even though this activity is starting fresh, we still need
2354 // to reset it to make sure we apply affinities to move any
2355 // existing activities from other tasks in to it.
2356 // If the caller has requested that the target task be
2357 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002358 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 resetTaskIfNeededLocked(r, r);
2360 doShow = topRunningNonDelayedActivityLocked(null) == r;
2361 }
Chong Zhang280d3322015-11-03 17:27:26 -08002362 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002363 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2364 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002366 if (r.mLaunchTaskBehind) {
2367 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2368 // tell WindowManager that r is visible even though it is at the back of the stack.
2369 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002370 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002371 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 // Figure out if we are transitioning from another activity that is
2373 // "has the same starting icon" as the next one. This allows the
2374 // window manager to keep the previous window it had previously
2375 // created, if it still had one.
2376 ActivityRecord prev = mResumedActivity;
2377 if (prev != null) {
2378 // We don't want to reuse the previous starting preview if:
2379 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002380 if (prev.task != r.task) {
2381 prev = null;
2382 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002384 else if (prev.nowVisible) {
2385 prev = null;
2386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002388 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002389 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002390 mService.compatibilityInfoForPackageLocked(
2391 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002392 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002393 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002394 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002395 }
2396 } else {
2397 // If this is the first activity, don't do any fancy animations,
2398 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002399 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002400 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002401 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002402 }
2403 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002404 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 }
2406
2407 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002408 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002409 } else {
2410 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 }
2412 }
2413
Dianne Hackbornbe707852011-11-11 14:32:10 -08002414 final void validateAppTokensLocked() {
2415 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002416 mValidateAppTokens.ensureCapacity(numActivities());
2417 final int numTasks = mTaskHistory.size();
2418 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2419 TaskRecord task = mTaskHistory.get(taskNdx);
2420 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002421 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002422 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002423 }
Craig Mautner000f0022013-02-26 15:04:29 -08002424 TaskGroup group = new TaskGroup();
2425 group.taskId = task.taskId;
2426 mValidateAppTokens.add(group);
2427 final int numActivities = activities.size();
2428 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2429 final ActivityRecord r = activities.get(activityNdx);
2430 group.tokens.add(r.appToken);
2431 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002432 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002433 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002434 }
2435
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 /**
2437 * Perform a reset of the given task, if needed as part of launching it.
2438 * Returns the new HistoryRecord at the top of the task.
2439 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002440 /**
2441 * Helper method for #resetTaskIfNeededLocked.
2442 * We are inside of the task being reset... we'll either finish this activity, push it out
2443 * for another task, or leave it as-is.
2444 * @param task The task containing the Activity (taskTop) that might be reset.
2445 * @param forceReset
2446 * @return An ActivityOptions that needs to be processed.
2447 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002448 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002449 ActivityOptions topOptions = null;
2450
2451 int replyChainEnd = -1;
2452 boolean canMoveOptions = true;
2453
2454 // We only do this for activities that are not the root of the task (since if we finish
2455 // the root, we may no longer have the task!).
2456 final ArrayList<ActivityRecord> activities = task.mActivities;
2457 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002458 final int rootActivityNdx = task.findEffectiveRootIndex();
2459 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002460 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002461 if (target.frontOfTask)
2462 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002463
2464 final int flags = target.info.flags;
2465 final boolean finishOnTaskLaunch =
2466 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2467 final boolean allowTaskReparenting =
2468 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2469 final boolean clearWhenTaskReset =
2470 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2471
2472 if (!finishOnTaskLaunch
2473 && !clearWhenTaskReset
2474 && target.resultTo != null) {
2475 // If this activity is sending a reply to a previous
2476 // activity, we can't do anything with it now until
2477 // we reach the start of the reply chain.
2478 // XXX note that we are assuming the result is always
2479 // to the previous activity, which is almost always
2480 // the case but we really shouldn't count on.
2481 if (replyChainEnd < 0) {
2482 replyChainEnd = i;
2483 }
2484 } else if (!finishOnTaskLaunch
2485 && !clearWhenTaskReset
2486 && allowTaskReparenting
2487 && target.taskAffinity != null
2488 && !target.taskAffinity.equals(task.affinity)) {
2489 // If this activity has an affinity for another
2490 // task, then we need to move it out of here. We will
2491 // move it as far out of the way as possible, to the
2492 // bottom of the activity stack. This also keeps it
2493 // correctly ordered with any activities we previously
2494 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002495 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002496 final ActivityRecord bottom =
2497 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002498 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002499 if (bottom != null && target.taskAffinity != null
2500 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002501 // If the activity currently at the bottom has the
2502 // same task affinity as the one we are moving,
2503 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002504 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002505 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002506 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002507 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002508 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002509 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002510 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002511 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002512 + " out to new task " + target.task);
2513 }
2514
Wale Ogunwale706ed792015-08-02 10:29:44 -07002515 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002516
Craig Mautner525f3d92013-05-07 14:01:50 -07002517 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002518 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2519 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002520 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002521 if (p.finishing) {
2522 continue;
2523 }
2524
Craig Mautnere3a74d52013-02-22 14:14:58 -08002525 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002526 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002527 topOptions = p.takeOptionsLocked();
2528 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002529 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002530 }
2531 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002532 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2533 "Removing activity " + p + " from task=" + task + " adding to task="
2534 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002535 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2536 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002537 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002538 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002539
Wale Ogunwale706ed792015-08-02 10:29:44 -07002540 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002541 }
2542
Wale Ogunwale706ed792015-08-02 10:29:44 -07002543 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002544 if (VALIDATE_TOKENS) {
2545 validateAppTokensLocked();
2546 }
2547
2548 replyChainEnd = -1;
2549 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2550 // If the activity should just be removed -- either
2551 // because it asks for it, or the task should be
2552 // cleared -- then finish it and anything that is
2553 // part of its reply chain.
2554 int end;
2555 if (clearWhenTaskReset) {
2556 // In this case, we want to finish this activity
2557 // and everything above it, so be sneaky and pretend
2558 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002559 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002560 } else if (replyChainEnd < 0) {
2561 end = i;
2562 } else {
2563 end = replyChainEnd;
2564 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002565 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002566 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002567 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002568 if (p.finishing) {
2569 continue;
2570 }
2571 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002572 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002573 topOptions = p.takeOptionsLocked();
2574 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002575 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002576 }
2577 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002578 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002579 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002580 if (finishActivityLocked(
2581 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002582 end--;
2583 srcPos--;
2584 }
2585 }
2586 replyChainEnd = -1;
2587 } else {
2588 // If we were in the middle of a chain, well the
2589 // activity that started it all doesn't want anything
2590 // special, so leave it all as-is.
2591 replyChainEnd = -1;
2592 }
2593 }
2594
2595 return topOptions;
2596 }
2597
2598 /**
2599 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2600 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2601 * @param affinityTask The task we are looking for an affinity to.
2602 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2603 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2604 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2605 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002606 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002607 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002608 int replyChainEnd = -1;
2609 final int taskId = task.taskId;
2610 final String taskAffinity = task.affinity;
2611
2612 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2613 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002614 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2615
2616 // Do not operate on or below the effective root Activity.
2617 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002618 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002619 if (target.frontOfTask)
2620 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002621
2622 final int flags = target.info.flags;
2623 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2624 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2625
2626 if (target.resultTo != null) {
2627 // If this activity is sending a reply to a previous
2628 // activity, we can't do anything with it now until
2629 // we reach the start of the reply chain.
2630 // XXX note that we are assuming the result is always
2631 // to the previous activity, which is almost always
2632 // the case but we really shouldn't count on.
2633 if (replyChainEnd < 0) {
2634 replyChainEnd = i;
2635 }
2636 } else if (topTaskIsHigher
2637 && allowTaskReparenting
2638 && taskAffinity != null
2639 && taskAffinity.equals(target.taskAffinity)) {
2640 // This activity has an affinity for our task. Either remove it if we are
2641 // clearing or move it over to our task. Note that
2642 // we currently punt on the case where we are resetting a
2643 // task that is not at the top but who has activities above
2644 // with an affinity to it... this is really not a normal
2645 // case, and we will need to later pull that task to the front
2646 // and usually at that point we will do the reset and pick
2647 // up those remaining activities. (This only happens if
2648 // someone starts an activity in a new task from an activity
2649 // in a task that is not currently on top.)
2650 if (forceReset || finishOnTaskLaunch) {
2651 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002652 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2653 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002654 for (int srcPos = start; srcPos >= i; --srcPos) {
2655 final ActivityRecord p = activities.get(srcPos);
2656 if (p.finishing) {
2657 continue;
2658 }
Todd Kennedy539db512014-12-15 09:57:55 -08002659 finishActivityLocked(
2660 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002661 }
2662 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002663 if (taskInsertionPoint < 0) {
2664 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002665
Craig Mautner77878772013-03-04 19:46:24 -08002666 }
Craig Mautner77878772013-03-04 19:46:24 -08002667
2668 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002669 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2670 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2671 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002672 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002673 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002674 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002675 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002676
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002677 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2678 "Removing and adding activity " + p + " to stack at " + task
2679 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002680 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2681 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002682 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002683 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002684 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002685 if (VALIDATE_TOKENS) {
2686 validateAppTokensLocked();
2687 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002688
2689 // Now we've moved it in to place... but what if this is
2690 // a singleTop activity and we have put it on top of another
2691 // instance of the same activity? Then we drop the instance
2692 // below so it remains singleTop.
2693 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2694 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002695 int targetNdx = taskActivities.indexOf(target);
2696 if (targetNdx > 0) {
2697 ActivityRecord p = taskActivities.get(targetNdx - 1);
2698 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002699 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2700 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002701 }
2702 }
2703 }
2704 }
2705
2706 replyChainEnd = -1;
2707 }
2708 }
Craig Mautner77878772013-03-04 19:46:24 -08002709 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002710 }
2711
Craig Mautner8849a5e2013-04-02 16:41:03 -07002712 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002713 ActivityRecord newActivity) {
2714 boolean forceReset =
2715 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2716 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2717 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2718 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2719 forceReset = true;
2720 }
2721 }
2722
2723 final TaskRecord task = taskTop.task;
2724
2725 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2726 * for remaining tasks. Used for later tasks to reparent to task. */
2727 boolean taskFound = false;
2728
2729 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2730 ActivityOptions topOptions = null;
2731
Craig Mautner77878772013-03-04 19:46:24 -08002732 // Preserve the location for reparenting in the new task.
2733 int reparentInsertionPoint = -1;
2734
Craig Mautnere3a74d52013-02-22 14:14:58 -08002735 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2736 final TaskRecord targetTask = mTaskHistory.get(i);
2737
2738 if (targetTask == task) {
2739 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2740 taskFound = true;
2741 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002742 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2743 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002744 }
2745 }
2746
Craig Mautner70a86932013-02-28 22:37:44 -08002747 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002748 if (taskNdx >= 0) {
2749 do {
2750 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2751 } while (taskTop == null && taskNdx >= 0);
2752 }
Craig Mautner70a86932013-02-28 22:37:44 -08002753
Craig Mautnere3a74d52013-02-22 14:14:58 -08002754 if (topOptions != null) {
2755 // If we got some ActivityOptions from an activity on top that
2756 // was removed from the task, propagate them to the new real top.
2757 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002758 taskTop.updateOptionsLocked(topOptions);
2759 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002760 topOptions.abort();
2761 }
2762 }
2763
2764 return taskTop;
2765 }
2766
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2768 String resultWho, int requestCode, int resultCode, Intent data) {
2769
2770 if (callingUid > 0) {
2771 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002772 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002773 }
2774
2775 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2776 + " : who=" + resultWho + " req=" + requestCode
2777 + " res=" + resultCode + " data=" + data);
2778 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2779 try {
2780 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2781 list.add(new ResultInfo(resultWho, requestCode,
2782 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002783 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 return;
2785 } catch (Exception e) {
2786 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2787 }
2788 }
2789
2790 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2791 }
2792
Craig Mautner299f9602015-01-26 09:47:33 -08002793 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002794 if (mStackSupervisor.isFocusedStack(this) && mService.mFocusedActivity == r) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002795 ActivityRecord next = topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002796 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002797 if (next != r) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002798 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07002799 // For freeform, docked, and pinned stacks we always keep the focus within the
2800 // stack as long as there is a running activity in the stack that we can adjust
2801 // focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002802 mService.setFocusedActivityLocked(next, myReason);
2803 return;
2804 } else {
2805 final TaskRecord task = r.task;
2806 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2807 // For non-fullscreen stack, we want to move the focus to the next visible
2808 // stack to prevent the home screen from moving to the top and obscuring
2809 // other visible stacks.
2810 if (!mFullscreen
2811 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2812 return;
2813 }
2814 // Move the home stack to the top if this stack is fullscreen or there is no
2815 // other visible stack.
2816 if (mStackSupervisor.moveHomeStackTaskToTop(
2817 task.getTaskToReturnTo(), myReason)) {
2818 // Activity focus was already adjusted. Nothing else to do...
2819 return;
2820 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002821 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002822 }
2823 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002824
2825 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002826 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002827 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002828 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002829 }
2830 }
2831
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002832 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2833 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2834 final String myReason = reason + " adjustFocusToNextVisibleStack";
2835 if (stack == null) {
2836 return false;
2837 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002838 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002839 if (top == null) {
2840 return false;
2841 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002842 mService.setFocusedActivityLocked(top, myReason);
2843 return true;
2844 }
2845
Craig Mautnerf3333272013-04-22 10:55:53 -07002846 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002847 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2849 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2850 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002851 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002852 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002853 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2854 "stop-no-history", false)) {
2855 // Activity was finished, no need to continue trying to schedule stop.
2856 adjustFocusedActivityLocked(r, "stopActivityFinished");
2857 r.resumeKeyDispatchingLocked();
2858 return;
2859 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002860 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002861 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002862 + " on stop because we're just sleeping");
2863 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002864 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002865 }
2866
2867 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002868 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002869 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002870 try {
2871 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002872 if (DEBUG_STATES) Slog.v(TAG_STATES,
2873 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002874 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002875 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2876 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002877 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002878 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002879 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002880 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002881 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002882 r.setSleeping(true);
2883 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002884 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002885 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002886 } catch (Exception e) {
2887 // Maybe just ignore exceptions here... if the process
2888 // has crashed, our death notification will clean things
2889 // up.
2890 Slog.w(TAG, "Exception thrown during pause", e);
2891 // Just in case, assume it to be stopped.
2892 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002893 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 r.state = ActivityState.STOPPED;
2895 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002896 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 }
2898 }
2899 }
2900 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002901
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 /**
2903 * @return Returns true if the activity is being finished, false if for
2904 * some reason it is being left as-is.
2905 */
2906 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002907 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002908 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002909 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2910 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002911 + ", result=" + resultCode + ", data=" + resultData
2912 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002913 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914 return false;
2915 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002916
Craig Mautnerd44711d2013-02-23 11:24:36 -08002917 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002918 return true;
2919 }
2920
Craig Mautnerd2328952013-03-05 12:46:26 -08002921 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002922 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2923 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2924 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2925 ActivityRecord r = activities.get(activityNdx);
2926 if (r.resultTo == self && r.requestCode == requestCode) {
2927 if ((r.resultWho == null && resultWho == null) ||
2928 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2929 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2930 false);
2931 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002932 }
2933 }
2934 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002935 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002936 }
2937
Todd Kennedy539db512014-12-15 09:57:55 -08002938 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002939 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002940 if (r != null && r.app == app) {
2941 // If the top running activity is from this crashing
2942 // process, then terminate it to avoid getting in a loop.
2943 Slog.w(TAG, " Force finishing activity "
2944 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002945 int taskNdx = mTaskHistory.indexOf(r.task);
2946 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002947 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002948 // Also terminate any activities below it that aren't yet
2949 // stopped, to avoid a situation where one will get
2950 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002951 --activityNdx;
2952 if (activityNdx < 0) {
2953 do {
2954 --taskNdx;
2955 if (taskNdx < 0) {
2956 break;
2957 }
2958 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2959 } while (activityNdx < 0);
2960 }
2961 if (activityNdx >= 0) {
2962 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002963 if (r.state == ActivityState.RESUMED
2964 || r.state == ActivityState.PAUSING
2965 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002966 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002967 Slog.w(TAG, " Force finishing activity "
2968 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002969 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002970 }
2971 }
2972 }
2973 }
2974 }
2975
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002976 final void finishVoiceTask(IVoiceInteractionSession session) {
2977 IBinder sessionBinder = session.asBinder();
2978 boolean didOne = false;
2979 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2980 TaskRecord tr = mTaskHistory.get(taskNdx);
2981 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2982 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2983 ActivityRecord r = tr.mActivities.get(activityNdx);
2984 if (!r.finishing) {
2985 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2986 false);
2987 didOne = true;
2988 }
2989 }
2990 }
2991 }
2992 if (didOne) {
2993 mService.updateOomAdjLocked();
2994 }
2995 }
2996
Craig Mautnerd2328952013-03-05 12:46:26 -08002997 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002998 ArrayList<ActivityRecord> activities = r.task.mActivities;
2999 for (int index = activities.indexOf(r); index >= 0; --index) {
3000 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003001 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003002 break;
3003 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003004 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003005 }
3006 return true;
3007 }
3008
Dianne Hackborn5c607432012-02-28 14:44:19 -08003009 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3010 // send the result
3011 ActivityRecord resultTo = r.resultTo;
3012 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003013 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003014 + " who=" + r.resultWho + " req=" + r.requestCode
3015 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003016 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003017 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003018 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003019 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003020 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003021 if (r.info.applicationInfo.uid > 0) {
3022 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3023 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003024 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003025 }
3026 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3027 resultData);
3028 r.resultTo = null;
3029 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003030 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003031
3032 // Make sure this HistoryRecord is not holding on to other resources,
3033 // because clients have remote IPC references to this object so we
3034 // can't assume that will go away and want to avoid circular IPC refs.
3035 r.results = null;
3036 r.pendingResults = null;
3037 r.newIntents = null;
3038 r.icicle = null;
3039 }
3040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003041 /**
3042 * @return Returns true if this activity has been removed from the history
3043 * list, or false if it is still in the list and will be removed later.
3044 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003045 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3046 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 if (r.finishing) {
3048 Slog.w(TAG, "Duplicate finish request for " + r);
3049 return false;
3050 }
3051
Wale Ogunwale7d701172015-03-11 15:36:30 -07003052 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003053 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003055 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003056 task.taskId, r.shortComponentName, reason);
3057 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003058 final int index = activities.indexOf(r);
3059 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003060 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003061 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003062 // If the caller asked that this activity (and all above it)
3063 // be cleared when the task is reset, don't lose that information,
3064 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003065 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003066 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 }
3068 }
3069
3070 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003071
Craig Mautner299f9602015-01-26 09:47:33 -08003072 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003073
Dianne Hackborn5c607432012-02-28 14:44:19 -08003074 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003075
Craig Mautnerde4ef022013-04-07 19:01:33 -07003076 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003077 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003078 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003080 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003081 ? AppTransition.TRANSIT_TASK_CLOSE
3082 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003083
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003084 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003085 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003086
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003087 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003088 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3089 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3090 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003091 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003092 }
3093
Craig Mautneraea74a52014-03-08 14:23:10 -08003094 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003095 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003096 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003097 } else if (r.state != ActivityState.PAUSING) {
3098 // If the activity is PAUSING, we will complete the finish once
3099 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003100 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003101 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003103 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 }
3105
3106 return false;
3107 }
3108
Craig Mautnerf3333272013-04-22 10:55:53 -07003109 static final int FINISH_IMMEDIATELY = 0;
3110 static final int FINISH_AFTER_PAUSE = 1;
3111 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112
Craig Mautnerf3333272013-04-22 10:55:53 -07003113 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114 // First things first: if this activity is currently visible,
3115 // and the resumed activity is not yet visible, then hold off on
3116 // finishing until the resumed one becomes visible.
3117 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003118 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3119 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003120 if (mStackSupervisor.mStoppingActivities.size() > 3
3121 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003122 // If we already have a few activities waiting to stop,
3123 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003124 // them out. Or if r is the last of activity of the last task the stack
3125 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003126 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003127 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003128 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 }
3130 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003131 if (DEBUG_STATES) Slog.v(TAG_STATES,
3132 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003134 if (oomAdj) {
3135 mService.updateOomAdjLocked();
3136 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 return r;
3138 }
3139
3140 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003141 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003142 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003143 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144 if (mResumedActivity == r) {
3145 mResumedActivity = null;
3146 }
3147 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003148 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 r.state = ActivityState.FINISHING;
3150
3151 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003152 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 || prevState == ActivityState.STOPPED
3154 || prevState == ActivityState.INITIALIZING) {
3155 // If this activity is already stopped, we can just finish
3156 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003157 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003158 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003159 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003160 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003161 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003162 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003163 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3164 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003165 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003167
3168 // Need to go through the full pause cycle to get this
3169 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003170 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003171 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003172 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003173 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 return r;
3175 }
3176
Craig Mautneree36c772014-07-16 14:56:05 -07003177 void finishAllActivitiesLocked(boolean immediately) {
3178 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003179 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3180 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3181 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3182 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003183 noActivitiesInStack = false;
3184 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003185 continue;
3186 }
Craig Mautneree36c772014-07-16 14:56:05 -07003187 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003188 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3189 }
3190 }
Craig Mautneree36c772014-07-16 14:56:05 -07003191 if (noActivitiesInStack) {
3192 mActivityContainer.onTaskListEmptyLocked();
3193 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003194 }
3195
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003196 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3197 // Basic case: for simple app-centric recents, we need to recreate
3198 // the task if the affinity has changed.
3199 if (srec == null || srec.task.affinity == null ||
3200 !srec.task.affinity.equals(destAffinity)) {
3201 return true;
3202 }
3203 // Document-centric case: an app may be split in to multiple documents;
3204 // they need to re-create their task if this current activity is the root
3205 // of a document, unless simply finishing it will return them to the the
3206 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003207 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3208 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003209 // Okay, this activity is at the root of its task. What to do, what to do...
3210 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3211 // Finishing won't return to an application, so we need to recreate.
3212 return true;
3213 }
3214 // We now need to get the task below it to determine what to do.
3215 int taskIdx = mTaskHistory.indexOf(srec.task);
3216 if (taskIdx <= 0) {
3217 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3218 return false;
3219 }
3220 if (taskIdx == 0) {
3221 // At the bottom of the stack, nothing to go back to.
3222 return true;
3223 }
3224 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3225 if (!srec.task.affinity.equals(prevTask.affinity)) {
3226 // These are different apps, so need to recreate.
3227 return true;
3228 }
3229 }
3230 return false;
3231 }
3232
Wale Ogunwale7d701172015-03-11 15:36:30 -07003233 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003234 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003235 final TaskRecord task = srec.task;
3236 final ArrayList<ActivityRecord> activities = task.mActivities;
3237 final int start = activities.indexOf(srec);
3238 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003239 return false;
3240 }
3241 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003242 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003243 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003244 final ComponentName dest = destIntent.getComponent();
3245 if (start > 0 && dest != null) {
3246 for (int i = finishTo; i >= 0; i--) {
3247 ActivityRecord r = activities.get(i);
3248 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003249 r.info.name.equals(dest.getClassName())) {
3250 finishTo = i;
3251 parent = r;
3252 foundParentInTask = true;
3253 break;
3254 }
3255 }
3256 }
3257
3258 IActivityController controller = mService.mController;
3259 if (controller != null) {
3260 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3261 if (next != null) {
3262 // ask watcher if this is allowed
3263 boolean resumeOK = true;
3264 try {
3265 resumeOK = controller.activityResuming(next.packageName);
3266 } catch (RemoteException e) {
3267 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003268 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003269 }
3270
3271 if (!resumeOK) {
3272 return false;
3273 }
3274 }
3275 }
3276 final long origId = Binder.clearCallingIdentity();
3277 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003278 ActivityRecord r = activities.get(i);
3279 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003280 // Only return the supplied result for the first activity finished
3281 resultCode = Activity.RESULT_CANCELED;
3282 resultData = null;
3283 }
3284
3285 if (parent != null && foundParentInTask) {
3286 final int parentLaunchMode = parent.info.launchMode;
3287 final int destIntentFlags = destIntent.getFlags();
3288 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3289 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3290 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3291 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003292 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3293 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003294 } else {
3295 try {
3296 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3297 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003298 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003299 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003300 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003301 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003302 foundParentInTask = res == ActivityManager.START_SUCCESS;
3303 } catch (RemoteException e) {
3304 foundParentInTask = false;
3305 }
3306 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003307 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003308 }
3309 }
3310 Binder.restoreCallingIdentity(origId);
3311 return foundParentInTask;
3312 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003313 /**
3314 * Perform the common clean-up of an activity record. This is called both
3315 * as part of destroyActivityLocked() (when destroying the client-side
3316 * representation) and cleaning things up as a result of its hosting
3317 * processing going away, in which case there is no remaining client-side
3318 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003319 *
3320 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003322 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3323 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 if (mResumedActivity == r) {
3325 mResumedActivity = null;
3326 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003327 if (mPausingActivity == r) {
3328 mPausingActivity = null;
3329 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003330 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331
3332 r.configDestroy = false;
3333 r.frozenBeforeDestroy = false;
3334
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003335 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003336 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003337 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003338 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003339 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003340 }
3341
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003342 // Make sure this record is no longer in the pending finishes list.
3343 // This could happen, for example, if we are trimming activities
3344 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003345 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003346 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003347
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003348 // Remove any pending results.
3349 if (r.finishing && r.pendingResults != null) {
3350 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3351 PendingIntentRecord rec = apr.get();
3352 if (rec != null) {
3353 mService.cancelIntentSenderLocked(rec, false);
3354 }
3355 }
3356 r.pendingResults = null;
3357 }
3358
3359 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003360 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 }
3362
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003363 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003364 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003365 if (getVisibleBehindActivity() == r) {
3366 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003367 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003368 }
3369
3370 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003371 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003372 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003373 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003374 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003375 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003376 }
3377
Craig Mautner299f9602015-01-26 09:47:33 -08003378 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003379 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003380 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003381 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003382 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3383 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3384
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003385 r.takeFromHistory();
3386 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003387 if (DEBUG_STATES) Slog.v(TAG_STATES,
3388 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003389 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003390 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003391 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003392 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003393 if (VALIDATE_TOKENS) {
3394 validateAppTokensLocked();
3395 }
Craig Mautner312ba862014-02-10 17:55:01 -08003396 final TaskRecord task = r.task;
3397 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003398 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003399 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003400 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003401 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003402 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003403 }
Craig Mautner299f9602015-01-26 09:47:33 -08003404 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003405 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003406 cleanUpActivityServicesLocked(r);
3407 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003408 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003409
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003410 /**
3411 * Perform clean-up of service connections in an activity record.
3412 */
3413 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3414 // Throw away any services that have been bound by this activity.
3415 if (r.connections != null) {
3416 Iterator<ConnectionRecord> it = r.connections.iterator();
3417 while (it.hasNext()) {
3418 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003419 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003420 }
3421 r.connections = null;
3422 }
3423 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003424
Craig Mautneree2e45a2014-06-27 12:10:03 -07003425 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003426 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003427 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003428 mHandler.sendMessage(msg);
3429 }
3430
Craig Mautneree2e45a2014-06-27 12:10:03 -07003431 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003432 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003433 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003434 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3435 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3436 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3437 final ActivityRecord r = activities.get(activityNdx);
3438 if (r.finishing) {
3439 continue;
3440 }
3441 if (r.fullscreen) {
3442 lastIsOpaque = true;
3443 }
3444 if (owner != null && r.app != owner) {
3445 continue;
3446 }
3447 if (!lastIsOpaque) {
3448 continue;
3449 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003450 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003451 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003452 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003453 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003454 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003455 activityRemoved = true;
3456 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003457 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003458 }
3459 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003460 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003461 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003462 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003463 }
3464
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003465 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3466 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003467 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3468 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003469 + " pausing=" + mPausingActivity + " for reason " + reason);
3470 return destroyActivityLocked(r, true, reason);
3471 }
3472 return false;
3473 }
3474
3475 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3476 String reason) {
3477 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003478 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003479 int maxTasks = tasks.size() / 4;
3480 if (maxTasks < 1) {
3481 maxTasks = 1;
3482 }
3483 int numReleased = 0;
3484 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3485 final TaskRecord task = mTaskHistory.get(taskNdx);
3486 if (!tasks.contains(task)) {
3487 continue;
3488 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003489 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003490 int curNum = 0;
3491 final ArrayList<ActivityRecord> activities = task.mActivities;
3492 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3493 final ActivityRecord activity = activities.get(actNdx);
3494 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003495 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003496 + " in state " + activity.state + " resumed=" + mResumedActivity
3497 + " pausing=" + mPausingActivity + " for reason " + reason);
3498 destroyActivityLocked(activity, true, reason);
3499 if (activities.get(actNdx) != activity) {
3500 // Was removed from list, back up so we don't miss the next one.
3501 actNdx--;
3502 }
3503 curNum++;
3504 }
3505 }
3506 if (curNum > 0) {
3507 numReleased += curNum;
3508 maxTasks--;
3509 if (mTaskHistory.get(taskNdx) != task) {
3510 // The entire task got removed, back up so we don't miss the next one.
3511 taskNdx--;
3512 }
3513 }
3514 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003515 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3516 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003517 return numReleased;
3518 }
3519
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520 /**
3521 * Destroy the current CLIENT SIDE instance of an activity. This may be
3522 * called both when actually finishing an activity, or when performing
3523 * a configuration switch where we destroy the current client-side object
3524 * but then create a new client-side object for this same HistoryRecord.
3525 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003526 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003527 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3528 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003529 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003531 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003532 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533
3534 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003535
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003536 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003537
3538 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003539
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003540 if (hadApp) {
3541 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003542 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3544 mService.mHeavyWeightProcess = null;
3545 mService.mHandler.sendEmptyMessage(
3546 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3547 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003548 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003549 // Update any services we are bound to that might care about whether
3550 // their client may have activities.
3551 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003552 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003553 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003554 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003555 }
3556 }
3557
3558 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003559
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003560 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003561 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003562 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003563 r.configChangeFlags);
3564 } catch (Exception e) {
3565 // We can just ignore exceptions here... if the process
3566 // has crashed, our death notification will clean things
3567 // up.
3568 //Slog.w(TAG, "Exception thrown during finish", e);
3569 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003570 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003571 removedFromHistory = true;
3572 skipDestroy = true;
3573 }
3574 }
3575
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003577
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003578 // If the activity is finishing, we need to wait on removing it
3579 // from the list to give it a chance to do its cleanup. During
3580 // that time it may make calls back with its token so we need to
3581 // be able to find it on the list and so we don't want to remove
3582 // it from the list yet. Otherwise, we can just immediately put
3583 // it in the destroyed state since we are not removing it from the
3584 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003586 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003587 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003589 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003590 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3591 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003592 if (DEBUG_STATES) Slog.v(TAG_STATES,
3593 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003595 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003596 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003597 }
3598 } else {
3599 // remove this record from the history.
3600 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003601 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003602 removedFromHistory = true;
3603 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003604 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003605 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003606 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003607 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 }
3609 }
3610
3611 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003612
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003613 if (!mLRUActivities.remove(r) && hadApp) {
3614 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3615 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003616
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 return removedFromHistory;
3618 }
3619
Craig Mautner299f9602015-01-26 09:47:33 -08003620 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003621 final long origId = Binder.clearCallingIdentity();
3622 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003623 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003624 if (r != null) {
3625 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003626 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003627 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003628
Wale Ogunwale60454db2015-01-23 16:05:07 -08003629 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003630 if (r.state == ActivityState.DESTROYING) {
3631 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003632 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003633 }
3634 }
Craig Mautner05d29032013-05-03 13:40:13 -07003635 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003636 } finally {
3637 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 }
3639 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003640
Todd Kennedyaab56db2015-01-30 09:39:53 -08003641 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003642 if (hasVisibleBehindActivity() &&
3643 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003644 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003645 // Don't release the top activity if it has requested to run behind the next
3646 // activity.
3647 return;
3648 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003649 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003650 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003651 " thread=" + r.app.thread);
3652 if (r != null && r.app != null && r.app.thread != null) {
3653 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003654 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003655 } catch (RemoteException e) {
3656 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003657 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003658 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003659 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003660 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003661 }
3662 }
3663 }
3664
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003665 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003666 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3667 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003668 if (r != null) {
3669 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003670 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003671 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003672 }
3673 mStackSupervisor.resumeTopActivitiesLocked();
3674 }
3675
Jose Lima4b6c6692014-08-12 17:41:12 -07003676 boolean hasVisibleBehindActivity() {
3677 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003678 }
3679
Jose Lima4b6c6692014-08-12 17:41:12 -07003680 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003681 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003682 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003683 }
3684 }
3685
Jose Lima4b6c6692014-08-12 17:41:12 -07003686 ActivityRecord getVisibleBehindActivity() {
3687 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003688 }
3689
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003690 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3691 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003693 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3694 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003695 while (i > 0) {
3696 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003697 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003698 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003699 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003700 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003701 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003702 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703 }
3704 }
3705 }
3706
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003707 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3708 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003709 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3710 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003711 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3712 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003713 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003714 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003715 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3716 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003717
3718 boolean hasVisibleActivities = false;
3719
3720 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003721 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003722 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3723 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003724 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3725 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3726 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3727 final ActivityRecord r = activities.get(activityNdx);
3728 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003729 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3730 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003731 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003732 if (r.visible) {
3733 hasVisibleActivities = true;
3734 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003735 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003736 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3737 // Don't currently have state for the activity, or
3738 // it is finishing -- always remove it.
3739 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003740 } else if (!r.visible && r.launchCount > 2 &&
3741 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003742 // We have launched this activity too many times since it was
3743 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003744 // (Note if the activity is visible, we don't remove the record.
3745 // We leave the dead window on the screen but the process will
3746 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003747 remove = true;
3748 } else {
3749 // The process may be gone, but the activity lives on!
3750 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003751 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003752 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003753 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3754 "Removing activity " + r + " from stack at " + i
3755 + ": haveState=" + r.haveState
3756 + " stateNotNeeded=" + r.stateNotNeeded
3757 + " finishing=" + r.finishing
3758 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003759 if (!r.finishing) {
3760 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3761 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3762 r.userId, System.identityHashCode(r),
3763 r.task.taskId, r.shortComponentName,
3764 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003765 if (r.state == ActivityState.RESUMED) {
3766 mService.updateUsageStats(r, false);
3767 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003768 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003769 } else {
3770 // We have the current state for this activity, so
3771 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003772 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3773 if (DEBUG_APP) Slog.v(TAG_APP,
3774 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003775 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003776 // Set nowVisible to previous visible state. If the app was visible while
3777 // it died, we leave the dead window on screen so it's basically visible.
3778 // This is needed when user later tap on the dead window, we need to stop
3779 // other apps when user transfers focus to the restarted activity.
3780 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003781 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003782 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003783 "App died, clearing saved state of " + r);
3784 r.icicle = null;
3785 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003786 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003787 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003788 if (remove) {
3789 removeActivityFromHistoryLocked(r, "appDied");
3790 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003791 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003792 }
3793 }
3794
3795 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003797
Chong Zhang280d3322015-11-03 17:27:26 -08003798 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003799 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003800 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003801 if (r != null && r.state != ActivityState.RESUMED) {
3802 r.updateOptionsLocked(options);
3803 } else {
3804 ActivityOptions.abort(options);
3805 }
3806 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003807 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003808 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003809
Craig Mautner21d24a22014-04-23 11:45:37 -07003810 void updateTaskMovement(TaskRecord task, boolean toFront) {
3811 if (task.isPersistable) {
3812 task.mLastTimeMoved = System.currentTimeMillis();
3813 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3814 // recently will be most negative, tasks sent to the bottom before that will be less
3815 // negative. Similarly for recent tasks moved to the top which will be most positive.
3816 if (!toFront) {
3817 task.mLastTimeMoved *= -1;
3818 }
3819 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003820 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003821 }
3822
Craig Mautner84984fa2014-06-19 11:19:20 -07003823 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003824 final int top = mTaskHistory.size() - 1;
3825 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3826 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003827 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003828 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3829 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003830 mTaskHistory.remove(taskNdx);
3831 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003832 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003833 return;
3834 }
3835 }
3836 }
3837
Chong Zhang280d3322015-11-03 17:27:26 -08003838 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003839 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003840 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003841
Craig Mautner11bf9a52013-02-19 14:08:51 -08003842 final int numTasks = mTaskHistory.size();
3843 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003844 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003845 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003846 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003847 ActivityOptions.abort(options);
3848 } else {
3849 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3850 }
3851 return;
3852 }
3853
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003854 if (timeTracker != null) {
3855 // The caller wants a time tracker associated with this task.
3856 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3857 tr.mActivities.get(i).appTimeTracker = timeTracker;
3858 }
3859 }
3860
Craig Mautner11bf9a52013-02-19 14:08:51 -08003861 // Shift all activities with this task up to the top
3862 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003863 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003864
Chong Zhang45c25ce2015-08-10 22:18:26 -07003865 // Don't refocus if invisible to current user
3866 ActivityRecord top = tr.getTopActivity();
3867 if (!okToShowLocked(top)) {
3868 addRecentActivityLocked(top);
3869 ActivityOptions.abort(options);
3870 return;
3871 }
3872
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003873 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003874 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003875 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003876
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003877 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003878 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003879 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 if (r != null) {
3881 mNoAnimActivities.add(r);
3882 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003883 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003884 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003885 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003886 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003887
Craig Mautner05d29032013-05-03 13:40:13 -07003888 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003889 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003890
3891 if (VALIDATE_TOKENS) {
3892 validateAppTokensLocked();
3893 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003894 }
3895
3896 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003897 * Worker method for rearranging history stack. Implements the function of moving all
3898 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003899 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003900 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003901 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3902 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003903 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003904 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003905 * @return Returns true if the move completed, false if not.
3906 */
Craig Mautner299f9602015-01-26 09:47:33 -08003907 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003908 final TaskRecord tr = taskForIdLocked(taskId);
3909 if (tr == null) {
3910 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3911 return false;
3912 }
3913
3914 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003915 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003917 // If we have a watcher, preflight the move before committing to it. First check
3918 // for *other* available tasks, but if none are available, then try again allowing the
3919 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003920 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003921 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003922 if (next == null) {
3923 next = topRunningActivityLocked(null, 0);
3924 }
3925 if (next != null) {
3926 // ask watcher if this is allowed
3927 boolean moveOK = true;
3928 try {
3929 moveOK = mService.mController.activityResuming(next.packageName);
3930 } catch (RemoteException e) {
3931 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003932 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003933 }
3934 if (!moveOK) {
3935 return false;
3936 }
3937 }
3938 }
3939
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003940 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003941
riddle_hsuc215a4f2014-12-27 12:10:45 +08003942 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003943
3944 // If true, we should resume the home activity next if the task we are moving to the
3945 // back is over the home stack. We force to false if the task we are moving to back
3946 // is the home task and we don't want it resumed after moving to the back.
3947 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3948 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003949 final TaskRecord nextTask = getNextTask(tr);
3950 if (nextTask != null) {
3951 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3952 } else {
3953 prevIsHome = true;
3954 }
3955 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003956 mTaskHistory.remove(tr);
3957 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003958 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003959
Craig Mautnerc8143c62013-09-03 12:15:57 -07003960 // There is an assumption that moving a task to the back moves it behind the home activity.
3961 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003962 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003963 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3964 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003965 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003966 break;
3967 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003968 if (taskNdx == 1) {
3969 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003970 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003971 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003972 }
3973
Craig Mautner299f9602015-01-26 09:47:33 -08003974 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003975 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003976
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003977 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003978 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 }
3980
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003981 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003982 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003983 if (!mService.mBooting && !mService.mBooted) {
3984 // Not ready yet!
3985 return false;
3986 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003987 final int taskToReturnTo = tr.getTaskToReturnTo();
3988 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003989 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003990 }
3991
Craig Mautner05d29032013-05-03 13:40:13 -07003992 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003993 return true;
3994 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003995
Craig Mautner8849a5e2013-04-02 16:41:03 -07003996 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003997 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003998 final Uri data = r.intent.getData();
3999 final String strData = data != null ? data.toSafeString() : null;
4000
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004001 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004002 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004003 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004004 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004005 }
4006
4007 /**
4008 * Make sure the given activity matches the current configuration. Returns
4009 * false if the activity had to be destroyed. Returns true if the
4010 * configuration is the same, or the activity will remain running as-is
4011 * for whatever reason. Ensures the HistoryRecord is updated with the
4012 * correct configuration and all other bookkeeping is handled.
4013 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004014 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4015 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004016 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004017 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004018 "Skipping config check (will change): " + r);
4019 return true;
4020 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004021
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004022 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004023 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004024
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004025 // Short circuit: if the two configurations are the exact same
4026 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004027 final Configuration newConfig = mService.mConfiguration;
4028 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004029 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004030 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004031 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004032 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004033 "Configuration unchanged in " + r);
4034 return true;
4035 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004036
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004037 // We don't worry about activities that are finishing.
4038 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004039 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004040 "Configuration doesn't matter in finishing " + r);
4041 r.stopFreezingScreenLocked(false);
4042 return true;
4043 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004044
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004045 // Okay we now are going to make this activity have the new config.
4046 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004047 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004048 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004049 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004050 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004051
Filip Gruszczynski23493322015-07-29 17:02:59 -07004052 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004053 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004054 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004055 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004056 "Configuration no differences in " + r);
4057 return true;
4058 }
4059
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004060 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4061 "Configuration changes for " + r + " ; taskChanges="
4062 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4063 + Configuration.configurationDiffToString(changes));
4064
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 // If the activity isn't currently running, just leave the new
4066 // configuration and it will pick that up next time it starts.
4067 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 Hackborn50dc3bc2010-06-25 10:05:59 -07004069 "Configuration doesn't matter not running " + r);
4070 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004071 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004072 return true;
4073 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004074
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004075 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004076 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4077 "Checking to restart " + r.info.name + ": changed=0x"
4078 + Integer.toHexString(changes) + ", handles=0x"
4079 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4080
Dianne Hackborne6676352011-06-01 16:51:20 -07004081 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004082 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4083 r.configChangeFlags |= changes;
4084 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004085 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004086 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004087 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004088 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004089 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004090 } else if (r.state == ActivityState.PAUSING) {
4091 // A little annoying: we are waiting for this activity to
4092 // finish pausing. Let's not do anything now, but just
4093 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004094 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004095 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004096 r.configDestroy = true;
4097 return true;
4098 } else if (r.state == ActivityState.RESUMED) {
4099 // Try to optimize this case: the configuration is changing
4100 // and we need to restart the top, resumed activity.
4101 // Instead of doing the normal handshaking, just say
4102 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004103 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004104 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004105 relaunchActivityLocked(r, r.configChangeFlags, true,
4106 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004107 r.configChangeFlags = 0;
4108 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004109 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004110 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004111 relaunchActivityLocked(r, r.configChangeFlags, false,
4112 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004113 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004114 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004115
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004116 // All done... tell the caller we weren't able to keep this
4117 // activity around.
4118 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004119 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004120
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004121 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004122 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004123 // changes is always sent to all processes when they happen so it can just use whatever
4124 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004125 if (r.app != null && r.app.thread != null) {
4126 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004127 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004128 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004129 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004130 } catch (RemoteException e) {
4131 // If process died, whatever.
4132 }
4133 }
4134 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004136 return true;
4137 }
4138
Filip Gruszczynski23493322015-07-29 17:02:59 -07004139 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4140 Configuration oldTaskOverride) {
4141 // Determine what has changed. May be nothing, if this is a config
4142 // that has come back from the app after going idle. In that case
4143 // we just want to leave the official config object now in the
4144 // activity and do nothing else.
4145 int taskChanges = oldTaskOverride.diff(taskConfig);
4146 // We don't want to use size changes if they don't cross boundaries that are important to
4147 // the app.
4148 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4149 final boolean crosses = record.crossesHorizontalSizeThreshold(
4150 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4151 || record.crossesVerticalSizeThreshold(
4152 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4153 if (!crosses) {
4154 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4155 }
4156 }
4157 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4158 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4159 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004160 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004161 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4162 }
4163 }
4164 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4165 }
4166
4167 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4168 Configuration oldTaskOverride, int taskChanges) {
4169 if (taskChanges == 0) {
4170 // {@link Configuration#diff} doesn't catch changes from unset values.
4171 // Check for changes we care about.
4172 if (oldTaskOverride.orientation != taskConfig.orientation) {
4173 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4174 }
4175 // We want to explicitly track situations where the size configuration goes from
4176 // undefined to defined. We don't care about crossing the threshold in that case,
4177 // because there is no threshold.
4178 final int oldHeight = oldTaskOverride.screenHeightDp;
4179 final int newHeight = taskConfig.screenHeightDp;
4180 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4181 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4182 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4183 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4184 }
4185 final int oldWidth = oldTaskOverride.screenWidthDp;
4186 final int newWidth = taskConfig.screenWidthDp;
4187 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4188 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4189 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4190 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4191 }
4192 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4193 final int newSmallest = taskConfig.smallestScreenWidthDp;
4194 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4195 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4196 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4197 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4198 }
4199 }
4200 return taskChanges;
4201 }
4202
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004203 private static boolean isResizeOnlyChange(int change) {
4204 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4205 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4206 }
4207
Wale Ogunwale83301a92015-09-24 15:54:08 -07004208 private void relaunchActivityLocked(
4209 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4210 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4211 return;
4212 }
4213
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004214 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004215 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004216 if (andResume) {
4217 results = r.results;
4218 newIntents = r.newIntents;
4219 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004220 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4221 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004222 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004223 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004224 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004225 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004226
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004227 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004228
Craig Mautner34b73df2014-01-12 21:11:08 -08004229 mStackSupervisor.removeChildActivityContainers(r);
4230
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004231 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004232 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4233 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004234 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004235 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4236 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004237 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004238 // Note: don't need to call pauseIfSleepingLocked() here, because
4239 // the caller will only pass in 'andResume' if this activity is
4240 // currently resumed, which implies we aren't sleeping.
4241 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004242 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004243 }
4244
4245 if (andResume) {
4246 r.results = null;
4247 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004248 r.state = ActivityState.RESUMED;
4249 } else {
4250 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4251 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004252 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004253 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004254
4255 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004256 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4257 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4258 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4259 final ActivityRecord r = activities.get(activityNdx);
4260 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004261 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004262 }
4263 if (r.fullscreen && !r.finishing) {
4264 return false;
4265 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004266 }
4267 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004268 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004269 if (r == null) {
4270 return false;
4271 }
4272 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4273 + " would have returned true for r=" + r);
4274 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004275 }
4276
4277 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004278 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4279 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4280 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4281 final ActivityRecord r = activities.get(activityNdx);
4282 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004283 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004284 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004285 }
4286 }
4287 }
4288
Wale Ogunwale540e1232015-05-01 15:35:39 -07004289 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4290 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004291 boolean didSomething = false;
4292 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004293 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004294 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4295 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4296 int numActivities = activities.size();
4297 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4298 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004299 final boolean sameComponent =
4300 (r.packageName.equals(packageName) && (filterByClasses == null
4301 || filterByClasses.contains(r.realActivity.getClassName())))
4302 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004303 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004304 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004305 && (r.app == null || evenPersistent || !r.app.persistent)) {
4306 if (!doit) {
4307 if (r.finishing) {
4308 // If this activity is just finishing, then it is not
4309 // interesting as far as something to stop.
4310 continue;
4311 }
4312 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004313 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004314 if (r.isHomeActivity()) {
4315 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4316 Slog.i(TAG, "Skip force-stop again " + r);
4317 continue;
4318 } else {
4319 homeActivity = r.realActivity;
4320 }
4321 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004322 didSomething = true;
4323 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004324 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004325 if (r.app != null) {
4326 r.app.removed = true;
4327 }
4328 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004329 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004330 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004331 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4332 true)) {
4333 // r has been deleted from mActivities, accommodate.
4334 --numActivities;
4335 --activityNdx;
4336 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004337 }
4338 }
4339 }
4340 return didSomething;
4341 }
4342
Dianne Hackborn09233282014-04-30 11:33:59 -07004343 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004344 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4345 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004346 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004347 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004348 if (task.getTopActivity() == null) {
4349 continue;
4350 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004351 ActivityRecord r = null;
4352 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004353 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004354 int numActivities = 0;
4355 int numRunning = 0;
4356 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004357 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004358 continue;
4359 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004360 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004361 tmp = activities.get(activityNdx);
4362 if (tmp.finishing) {
4363 continue;
4364 }
4365 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004366
Craig Mautneraab647e2013-02-28 16:31:36 -08004367 // Initialize state for next task if needed.
4368 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4369 top = r;
4370 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004371 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004372
4373 // Add 'r' into the current task.
4374 numActivities++;
4375 if (r.app != null && r.app.thread != null) {
4376 numRunning++;
4377 }
4378
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004379 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004380 TAG, r.intent.getComponent().flattenToShortString()
4381 + ": task=" + r.task);
4382 }
4383
4384 RunningTaskInfo ci = new RunningTaskInfo();
4385 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004386 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004387 ci.baseActivity = r.intent.getComponent();
4388 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004389 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004390 if (focusedStack && topTask) {
4391 // Give the latest time to ensure foreground task can be sorted
4392 // at the first, because lastActiveTime of creating task is 0.
4393 ci.lastActiveTime = System.currentTimeMillis();
4394 topTask = false;
4395 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004396
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004397 if (top.task != null) {
4398 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004399 }
4400 ci.numActivities = numActivities;
4401 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004402 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004403 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004404 }
4405
4406 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004407 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004408 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004409 if (top >= 0) {
4410 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4411 int activityTop = activities.size() - 1;
4412 if (activityTop > 0) {
4413 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4414 "unhandled-back", true);
4415 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004416 }
4417 }
4418
Craig Mautner6b74cb52013-09-27 17:02:21 -07004419 /**
4420 * Reset local parameters because an app's activity died.
4421 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004422 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004423 */
4424 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004425 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004426 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004427 "App died while pausing: " + mPausingActivity);
4428 mPausingActivity = null;
4429 }
4430 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4431 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004432 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004433 }
4434
Craig Mautner19091252013-10-05 00:03:53 -07004435 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004436 }
4437
Craig Mautnercae015f2013-02-08 14:31:27 -08004438 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004439 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4440 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4441 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4442 final ActivityRecord r = activities.get(activityNdx);
4443 if (r.app == app) {
4444 Slog.w(TAG, " Force finishing activity "
4445 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004446 // Force the destroy to skip right to removal.
4447 r.app = null;
4448 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004449 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004450 }
4451 }
4452 }
4453
Dianne Hackborn390517b2013-05-30 15:03:32 -07004454 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004455 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004456 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004457 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4458 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004459 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4460 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004461 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004462 " Task id #" + task.taskId + "\n" +
4463 " mFullscreen=" + task.mFullscreen + "\n" +
4464 " mBounds=" + task.mBounds + "\n" +
4465 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004466 if (printed) {
4467 header = null;
4468 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004469 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004470 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004471 }
4472
4473 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4474 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4475
4476 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004477 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4478 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004479 }
4480 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004481 final int top = mTaskHistory.size() - 1;
4482 if (top >= 0) {
4483 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4484 int listTop = list.size() - 1;
4485 if (listTop >= 0) {
4486 activities.add(list.get(listTop));
4487 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004488 }
4489 } else {
4490 ItemMatcher matcher = new ItemMatcher();
4491 matcher.build(name);
4492
Craig Mautneraab647e2013-02-28 16:31:36 -08004493 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4494 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4495 if (matcher.match(r1, r1.intent.getComponent())) {
4496 activities.add(r1);
4497 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004498 }
4499 }
4500 }
4501
4502 return activities;
4503 }
4504
4505 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004506 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004507
4508 // All activities that came from the package must be
4509 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004510 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4511 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4512 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4513 final ActivityRecord a = activities.get(activityNdx);
4514 if (a.info.packageName.equals(packageName)) {
4515 a.forceNewConfig = true;
4516 if (starting != null && a == starting && a.visible) {
4517 a.startFreezingScreenLocked(starting.app,
4518 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4519 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004520 }
4521 }
4522 }
4523
4524 return starting;
4525 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004526
Craig Mautner299f9602015-01-26 09:47:33 -08004527 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004528 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004529 }
4530
Wale Ogunwale000957c2015-04-03 08:19:12 -07004531 /**
4532 * Removes the input task from this stack.
4533 * @param task to remove.
4534 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004535 * @param moving task to another stack. In the case we are moving we don't want to perform
4536 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004537 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004538 void removeTask(TaskRecord task, String reason, boolean moving) {
4539 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004540 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004541 mWindowManager.removeTask(task.taskId);
4542 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004543
Craig Mautner04a0ea62014-01-13 12:51:26 -08004544 final ActivityRecord r = mResumedActivity;
4545 if (r != null && r.task == task) {
4546 mResumedActivity = null;
4547 }
4548
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004549 final int taskNdx = mTaskHistory.indexOf(task);
4550 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004551 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4552 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4553 if (!nextTask.isOverHomeStack()) {
4554 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4555 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004556 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004557 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004558 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004559
Wale Ogunwale040b4702015-08-06 18:10:50 -07004560 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004561 final boolean isVoiceSession = task.voiceSession != null;
4562 if (isVoiceSession) {
4563 try {
4564 task.voiceSession.taskFinished(task.intent, task.taskId);
4565 } catch (RemoteException e) {
4566 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004567 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004568 if (task.autoRemoveFromRecents() || isVoiceSession) {
4569 // Task creator asked to remove this when done, or this task was a voice
4570 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004571 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004572 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004573 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004574 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004575
4576 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004577 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004578 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004579 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004580 String myReason = reason + " leftTaskHistoryEmpty";
4581 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004582 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004583 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004584 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004585 if (mStacks != null) {
4586 mStacks.remove(this);
4587 mStacks.add(0, this);
4588 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004589 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004590 mActivityContainer.onTaskListEmptyLocked();
4591 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004592 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004593
4594 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004595 }
4596
Dianne Hackborn91097de2014-04-04 18:02:06 -07004597 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4598 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4599 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004600 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4601 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004602 // add the task to stack first, mTaskPositioner might need the stack association
4603 addTask(task, toTop, false);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004604 if (mTaskPositioner != null) {
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004605 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, info.layout);
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004606 } else if (mBounds != null && task.mResizeable) {
4607 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004608 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004609 return task;
4610 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004611
4612 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004613 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004614 }
4615
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004616 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004617 task.stack = this;
4618 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004619 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004620 } else {
4621 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004622 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004623 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004624 if (!moving && task.voiceSession != null) {
4625 try {
4626 task.voiceSession.taskStarted(task.intent, task.taskId);
4627 } catch (RemoteException e) {
4628 }
4629 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004630 }
4631
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004632 void positionTask(final TaskRecord task, int position, boolean moving) {
4633 task.stack = this;
4634 insertTaskAtPosition(task, position);
4635 if (!moving && task.voiceSession != null) {
4636 try {
4637 task.voiceSession.taskStarted(task.intent, task.taskId);
4638 } catch (RemoteException e) {
4639 }
4640 }
4641 }
4642
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004643 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004644 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004645 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004646 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4647 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4648 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4649 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004650 bounds, task.mOverrideConfig, !r.isHomeActivity());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004651 r.taskConfigOverride = task.mOverrideConfig;
4652 }
4653
Wale Ogunwale079a0042015-10-24 11:44:07 -07004654 void setFocusAndResumeStateIfNeeded(
4655 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4656 // If the activity had focus before move focus to this stack.
4657 if (setFocus) {
4658 // If the activity owns the last resumed activity, transfer that together,
4659 // so that we don't resume the same activity again in the new stack.
4660 // Apps may depend on onResume()/onPause() being called in pairs.
4661 if (setResume) {
4662 mResumedActivity = r;
4663 // Move the stack in which we are placing the activity to the front. We don't use
4664 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4665 // already focused, the call will short-circuit and do nothing.
4666 moveToFront(reason);
4667 } else {
4668 // We need to not only move the stack to the front, but also have the activity
4669 // focused. This will achieve both goals.
4670 mService.setFocusedActivityLocked(r, reason);
4671 }
4672 }
4673 }
4674
4675 /**
4676 * Moves the input activity from its current stack to this one.
4677 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4678 * created on this stack which the activity is added to.
4679 * */
4680 void moveActivityToStack(ActivityRecord r) {
4681 final ActivityStack prevStack = r.task.stack;
4682 if (prevStack.mStackId == mStackId) {
4683 // You are already in the right stack silly...
4684 return;
4685 }
4686
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004687 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004688 && (mStackSupervisor.topRunningActivityLocked() == r);
4689 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4690
4691 final TaskRecord task = createTaskRecord(
4692 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4693 r.setTask(task, null);
4694 task.addActivityToTop(r);
4695 setAppTask(r, task);
4696 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4697 }
4698
Wale Ogunwale706ed792015-08-02 10:29:44 -07004699 private void setAppTask(ActivityRecord r, TaskRecord task) {
4700 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004701 task.updateOverrideConfiguration(bounds);
4702 mWindowManager.setAppTask(
4703 r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004704 r.taskConfigOverride = task.mOverrideConfig;
4705 }
4706
Craig Mautnerc00204b2013-03-05 15:02:14 -08004707 public int getStackId() {
4708 return mStackId;
4709 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004710
4711 @Override
4712 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004713 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4714 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004715 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004716
Craig Mautner15df08a2015-04-01 12:17:18 -07004717 void onLockTaskPackagesUpdatedLocked() {
4718 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4719 mTaskHistory.get(taskNdx).setLockTaskAuth();
4720 }
4721 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004722}