blob: b5f424d34adfa36cdddb9177b4104e589d62cb30 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -070019import static android.app.ActivityManager.DOCKED_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070020import static android.app.ActivityManager.FIRST_STATIC_STACK_ID;
Wale Ogunwale61803ee2015-08-07 19:59:47 -070021import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070022import static android.app.ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -070023import static android.app.ActivityManager.HOME_STACK_ID;
Winson5510f6c2015-10-27 12:11:26 -070024import static android.app.ActivityManager.INVALID_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070025import static android.app.ActivityManager.LAST_STATIC_STACK_ID;
Wale Ogunwale99db1862015-10-23 20:08:22 -070026import static android.app.ActivityManager.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070028
Wale Ogunwalee23149f2015-03-06 15:39:44 -080029import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070030
Craig Mautner84984fa2014-06-19 11:19:20 -070031import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070033
Wale Ogunwale040b4702015-08-06 18:10:50 -070034import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070035import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070036
Wale Ogunwale706ed792015-08-02 10:29:44 -070037import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070038import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070039
Dianne Hackborn91097de2014-04-04 18:02:06 -070040import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080041import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070043import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080044import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080045import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080046import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080047import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049
50import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070051import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070052import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080054import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080056import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080063import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070065import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080066import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.Handler;
68import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090069import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070071import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.RemoteException;
73import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080074import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070075import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070076import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070079import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080082import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import java.lang.ref.WeakReference;
84import java.util.ArrayList;
85import java.util.Iterator;
86import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080087import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070088import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089
90/**
91 * State and management of a single stack of activities.
92 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070093final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080094
Wale Ogunwalee23149f2015-03-06 15:39:44 -080095 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070096 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
97 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080098 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070099 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700100 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700101 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700102 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700103 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700104 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
105 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700106 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700107 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700108 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
109 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
110 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
111 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
112 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700113
114 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800115
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700116 // Ticks during which we check progress while waiting for an app to launch.
117 static final int LAUNCH_TICK = 500;
118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119 // How long we wait until giving up on the last activity to pause. This
120 // is short because it directly impacts the responsiveness of starting the
121 // next activity.
122 static final int PAUSE_TIMEOUT = 500;
123
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700124 // How long we wait for the activity to tell us it has stopped before
125 // giving up. This is a good amount of time because we really need this
126 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700127 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 // How long we wait until giving up on an activity telling us it has
130 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700131 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800134 // disabled.
135 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800136
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700137 // How long between activity launches that we consider safe to not warn
138 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700139 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800140
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700141 // Set to false to disable the preview that is shown while a new activity
142 // is being started.
143 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144
Craig Mautner5eda9b32013-07-02 11:58:16 -0700145 // How long to wait for all background Activities to redraw following a call to
146 // convertToTranslucent().
147 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 enum ActivityState {
150 INITIALIZING,
151 RESUMED,
152 PAUSING,
153 PAUSED,
154 STOPPING,
155 STOPPED,
156 FINISHING,
157 DESTROYING,
158 DESTROYED
159 }
160
161 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700162 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800163 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800164
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165 /**
166 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800167 * running) activities. It contains #TaskRecord objects.
168 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800169 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800170
171 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800172 * Used for validating app tokens with window manager.
173 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800174 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800175
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700176 /**
177 * List of running activities, sorted by recent usage.
178 * The first entry in the list is the least recently used.
179 * It contains HistoryRecord objects.
180 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800181 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700182
183 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 * Animations that for the current transition have requested not to
185 * be considered for the transition animation.
186 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800187 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700188
189 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700190 * When we are in the process of pausing an activity, before starting the
191 * next one, this variable holds the activity that is currently being paused.
192 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800193 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194
195 /**
196 * This is the last activity that we put into the paused state. This is
197 * used to determine if we need to do an activity transition while sleeping,
198 * when we normally hold the top activity paused.
199 */
200 ActivityRecord mLastPausedActivity = null;
201
202 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700203 * Activities that specify No History must be removed once the user navigates away from them.
204 * If the device goes to sleep with such an activity in the paused state then we save it here
205 * and finish it later if another activity replaces it on wakeup.
206 */
207 ActivityRecord mLastNoHistoryActivity = null;
208
209 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210 * Current activity that is resumed, or null if there is none.
211 */
212 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800213
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700214 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700215 * This is the last activity that has been started. It is only used to
216 * identify when multiple activities are started at once so that the user
217 * can be warned they may not be in the activity they think they are.
218 */
219 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800220
Craig Mautner5eda9b32013-07-02 11:58:16 -0700221 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
222 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
223 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
224 // Activity in mTranslucentActivityWaiting is notified via
225 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
226 // background activity being drawn then the same call will be made with a true value.
227 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700228 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700229
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700230 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700231 * Set when we know we are going to be calling updateConfiguration()
232 * soon, so want to skip intermediate config checks.
233 */
234 boolean mConfigWillChange;
235
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700236 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800237 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700238 // Current bounds of the stack or null if fullscreen.
239 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800240
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700241 long mLaunchStartTime = 0;
242 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800243
Craig Mautner858d8a62013-04-23 17:08:34 -0700244 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700245
Craig Mautnerc00204b2013-03-05 15:02:14 -0800246 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800247 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800248 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
249 ArrayList<ActivityStack> mStacks;
250 /** The attached Display's unique identifier, or -1 if detached */
251 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800252
Craig Mautner27084302013-03-25 08:05:25 -0700253 /** Run all ActivityStacks through this */
254 final ActivityStackSupervisor mStackSupervisor;
255
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700256 private final LaunchingTaskPositioner mTaskPositioner;
257
Wale Ogunwale99db1862015-10-23 20:08:22 -0700258 // If the bounds of task contained in this stack should be persisted across power cycles.
259 final boolean mPersistTaskBounds;
260
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700261 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700262 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
263 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
264 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
265 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700266 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700267 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700268 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700269
270 static class ScheduleDestroyArgs {
271 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700272 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700273 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700274 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700275 mReason = reason;
276 }
277 }
278
Zoran Marcetaf958b322012-08-09 20:27:12 +0900279 final Handler mHandler;
280
281 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800282
Craig Mautnerc8143c62013-09-03 12:15:57 -0700283 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900284 super(looper);
285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286
Zoran Marcetaf958b322012-08-09 20:27:12 +0900287 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 public void handleMessage(Message msg) {
289 switch (msg.what) {
290 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800291 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 // We don't at this point know if the activity is fullscreen,
293 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800294 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700295 synchronized (mService) {
296 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700297 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700298 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700299 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700301 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700302 case LAUNCH_TICK_MSG: {
303 ActivityRecord r = (ActivityRecord)msg.obj;
304 synchronized (mService) {
305 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700306 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700307 }
308 }
309 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700310 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800311 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700312 // We don't at this point know if the activity is fullscreen,
313 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800314 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800315 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800316 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800317 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700319 case STOP_TIMEOUT_MSG: {
320 ActivityRecord r = (ActivityRecord)msg.obj;
321 // We don't at this point know if the activity is fullscreen,
322 // so we need to be conservative and assume it isn't.
323 Slog.w(TAG, "Activity stop timeout for " + r);
324 synchronized (mService) {
325 if (r.isInHistory()) {
326 activityStoppedLocked(r, null, null, null);
327 }
328 }
329 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700330 case DESTROY_ACTIVITIES_MSG: {
331 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
332 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700333 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700334 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700335 } break;
336 case TRANSLUCENT_TIMEOUT_MSG: {
337 synchronized (mService) {
338 notifyActivityDrawnLocked(null);
339 }
340 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700341 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700342 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700343 final ActivityRecord r = getVisibleBehindActivity();
344 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700345 if (r != null) {
346 mService.killAppAtUsersRequest(r.app, null);
347 }
348 }
349 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700350 }
351 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800352 }
353
Craig Mautner34b73df2014-01-12 21:11:08 -0800354 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800355 int count = 0;
356 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
357 count += mTaskHistory.get(taskNdx).mActivities.size();
358 }
359 return count;
360 }
361
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800362 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
363 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800364 mActivityContainer = activityContainer;
365 mStackSupervisor = activityContainer.getOuter();
366 mService = mStackSupervisor.mService;
367 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
368 mWindowManager = mService.mWindowManager;
369 mStackId = activityContainer.mStackId;
Fyodor Kupolov610acda2015-10-19 18:44:07 -0700370 mCurrentUser = mService.mUserController.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800371 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700372 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
373 ? new LaunchingTaskPositioner() : null;
Wale Ogunwale99db1862015-10-23 20:08:22 -0700374 mPersistTaskBounds = mStackId != DOCKED_STACK_ID && mStackId != PINNED_STACK_ID;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700375 }
376
377 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
378 mDisplayId = activityDisplay.mDisplayId;
379 mStacks = activityDisplay.mStacks;
380 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
381 mFullscreen = mBounds == null;
382 if (mTaskPositioner != null) {
383 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
384 mTaskPositioner.configure(mBounds);
385 }
386 }
387
388 void detachDisplay() {
389 mDisplayId = Display.INVALID_DISPLAY;
390 mStacks = null;
391 if (mTaskPositioner != null) {
392 mTaskPositioner.reset();
393 }
394 mWindowManager.detachStack(mStackId);
395 }
396
397 void setBounds(Rect bounds) {
398 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700399 if (mTaskPositioner != null) {
400 mTaskPositioner.configure(bounds);
401 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700402 }
Craig Mautner5962b122012-10-05 14:45:52 -0700403
Amith Yamasani734983f2014-03-04 16:48:05 -0800404 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700405 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700406 }
407
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700408 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800409 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700410 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700411 if (r != null) {
412 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800413 }
414 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700415 return null;
416 }
417
418 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800419 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
420 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800421 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800422 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
423 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800424 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800425 return r;
426 }
427 }
428 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700429 return null;
430 }
431
432 /**
433 * This is a simplified version of topRunningActivityLocked that provides a number of
434 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800435 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700436 * @param token If non-null, any history records matching this token will be skipped.
437 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800438 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700439 * @return Returns the HistoryRecord of the next activity on the stack.
440 */
441 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
443 TaskRecord task = mTaskHistory.get(taskNdx);
444 if (task.taskId == taskId) {
445 continue;
446 }
447 ArrayList<ActivityRecord> activities = task.mActivities;
448 for (int i = activities.size() - 1; i >= 0; --i) {
449 final ActivityRecord r = activities.get(i);
450 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800451 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800452 return r;
453 }
454 }
455 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 return null;
457 }
458
Craig Mautner8849a5e2013-04-02 16:41:03 -0700459 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700460 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
461 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700462 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
463 final ActivityRecord r = activities.get(activityNdx);
464 if (!r.finishing) {
465 return r;
466 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700467 }
468 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700469 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700470 }
471
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700472 final TaskRecord topTask() {
473 final int size = mTaskHistory.size();
474 if (size > 0) {
475 return mTaskHistory.get(size - 1);
476 }
477 return null;
478 }
479
Craig Mautnerd2328952013-03-05 12:46:26 -0800480 TaskRecord taskForIdLocked(int id) {
481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final TaskRecord task = mTaskHistory.get(taskNdx);
483 if (task.taskId == id) {
484 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800485 }
486 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700487 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700488 }
489
Craig Mautnerd2328952013-03-05 12:46:26 -0800490 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700491 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800492 return isInStackLocked(r);
493 }
494
495 ActivityRecord isInStackLocked(ActivityRecord r) {
496 if (r == null) {
497 return null;
498 }
499 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700500 if (task != null && task.stack != null
501 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800502 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800503 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800504 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800505 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800506 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800507 }
508
Craig Mautner2420ead2013-04-01 17:13:20 -0700509 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700510 final boolean hadit = mLRUActivities.remove(r);
511 mLRUActivities.add(r);
512 return hadit;
513 }
514
Craig Mautnerde4ef022013-04-07 19:01:33 -0700515 final boolean isHomeStack() {
516 return mStackId == HOME_STACK_ID;
517 }
518
Craig Mautnere0a38842013-12-16 16:14:02 -0800519 final boolean isOnHomeDisplay() {
520 return isAttached() &&
521 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
522 }
523
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700524 void moveToFront(String reason) {
525 moveToFront(reason, null);
526 }
527
528 /**
529 * @param reason The reason for moving the stack to the front.
530 * @param task If non-null, the task will be moved to the top of the stack.
531 * */
532 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700533 if (!isAttached()) {
534 return;
535 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700536
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700537 mStacks.remove(this);
538 int addIndex = mStacks.size();
539
540 if (addIndex > 0) {
541 final ActivityStack topStack = mStacks.get(addIndex - 1);
542 if (topStack.mStackId == PINNED_STACK_ID && topStack != this) {
543 // The pinned stack is always the top most stack (always-on-top).
544 // So, stack is moved just below the pinned stack.
545 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800546 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700547 }
548
549 mStacks.add(addIndex, this);
550
551 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
552 if (isOnHomeDisplay()) {
553 mStackSupervisor.setFocusStack(reason, this);
554 }
555 if (task != null) {
556 insertTaskAtTop(task, null);
557 } else {
558 task = topTask();
559 }
560 if (task != null) {
561 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800563 }
564
565 final boolean isAttached() {
566 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567 }
568
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700569 /**
570 * Returns the top activity in any existing task matching the given
571 * Intent. Returns null if no such task is found.
572 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700573 ActivityRecord findTaskLocked(ActivityRecord target) {
574 Intent intent = target.intent;
575 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700576 ComponentName cls = intent.getComponent();
577 if (info.targetActivity != null) {
578 cls = new ComponentName(info.packageName, info.targetActivity);
579 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700580 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700581 boolean isDocument = intent != null & intent.isDocument();
582 // If documentData is non-null then it must match the existing task data.
583 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800584
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700585 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800586 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
587 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700588 if (task.voiceSession != null) {
589 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700591 continue;
592 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700593 if (task.userId != userId) {
594 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700595 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700596 continue;
597 }
Craig Mautner000f0022013-02-26 15:04:29 -0800598 final ActivityRecord r = task.getTopActivity();
599 if (r == null || r.finishing || r.userId != userId ||
600 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700601 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800602 continue;
603 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700604 if (r.mActivityType != target.mActivityType) {
605 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
606 continue;
607 }
Craig Mautner000f0022013-02-26 15:04:29 -0800608
Craig Mautnerd00f4742014-03-12 14:17:26 -0700609 final Intent taskIntent = task.intent;
610 final Intent affinityIntent = task.affinityIntent;
611 final boolean taskIsDocument;
612 final Uri taskDocumentData;
613 if (taskIntent != null && taskIntent.isDocument()) {
614 taskIsDocument = true;
615 taskDocumentData = taskIntent.getData();
616 } else if (affinityIntent != null && affinityIntent.isDocument()) {
617 taskIsDocument = true;
618 taskDocumentData = affinityIntent.getData();
619 } else {
620 taskIsDocument = false;
621 taskDocumentData = null;
622 }
623
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700624 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700625 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700626 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700627 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700628 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
629 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700630 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 return r;
632 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700633 } else if (taskIntent != null && taskIntent.getComponent() != null &&
634 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700635 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700636 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800637 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
639 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800640 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700641 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
642 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700643 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800645 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700646 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
647 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800648 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700649 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700650 }
651
652 return null;
653 }
654
655 /**
656 * Returns the first activity (starting from the top of the stack) that
657 * is the same as the given activity. Returns null if no such activity
658 * is found.
659 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700660 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700661 ComponentName cls = intent.getComponent();
662 if (info.targetActivity != null) {
663 cls = new ComponentName(info.packageName, info.targetActivity);
664 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700665 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700666
Craig Mautner000f0022013-02-26 15:04:29 -0800667 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700668 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700669 final boolean notCurrentUserTask =
670 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700671 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700672
Craig Mautner000f0022013-02-26 15:04:29 -0800673 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
674 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700675 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700676 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700677 }
Craig Mautner000f0022013-02-26 15:04:29 -0800678 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 return r;
680 }
681 }
682 }
683
684 return null;
685 }
686
Amith Yamasani742a6712011-05-04 14:49:28 -0700687 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700688 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700689 */
Craig Mautner93529a42013-10-04 15:03:13 -0700690 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800691 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700692 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800693 }
694 mCurrentUser = userId;
695
696 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800697 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800698 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700699 final TaskRecord task = mTaskHistory.get(i);
700
701 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
702 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700703 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700704 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700705 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700706 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800707 mTaskHistory.remove(i);
708 mTaskHistory.add(task);
709 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800710 // Use same value for i.
711 } else {
712 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800713 }
714 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700715 if (VALIDATE_TOKENS) {
716 validateAppTokensLocked();
717 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700718 }
719
Craig Mautner2420ead2013-04-01 17:13:20 -0700720 void minimalResumeActivityLocked(ActivityRecord r) {
721 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700722 if (DEBUG_STATES) Slog.v(TAG_STATES,
723 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700724 r.stopped = false;
725 mResumedActivity = r;
726 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800727 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700728 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700729 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700730 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700731 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
732 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700733 }
734
Chong Zhang45c25ce2015-08-10 22:18:26 -0700735 private void addRecentActivityLocked(ActivityRecord r) {
736 if (r != null) {
737 mRecentTasks.addLocked(r.task);
738 r.task.touchActiveTime();
739 }
740 }
741
Narayan Kamath7829c812015-06-08 17:39:43 +0100742 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700743 if (mFullyDrawnStartTime != 0) {
744 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
745 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100746 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700747 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
748 }
749
750 private void stopFullyDrawnTraceIfNeeded() {
751 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
752 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
753 mFullyDrawnStartTime = 0;
754 }
755 }
756
Craig Mautnere79d42682013-04-01 19:01:53 -0700757 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700758 if (r.displayStartTime == 0) {
759 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
760 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100761 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700762 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700763 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700764 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100765 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700766 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700767 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800769
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700770 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700771 // Make sure that there is no activity waiting for this to launch.
772 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
773 r.displayStartTime = r.fullyDrawnStartTime = 0;
774 } else {
775 mStackSupervisor.removeTimeoutsForActivityLocked(r);
776 mStackSupervisor.scheduleIdleTimeoutLocked(r);
777 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700778 }
779
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800780 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800781 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800782 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
783 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
784 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
785 activities.get(activityNdx).setSleeping(false);
786 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800787 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800788 if (mPausingActivity != null) {
789 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
790 activityPausedLocked(mPausingActivity.appToken, true);
791 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800792 }
793
Craig Mautner0eea92c2013-05-16 13:35:39 -0700794 /**
795 * @return true if something must be done before going to sleep.
796 */
797 boolean checkReadyForSleepLocked() {
798 if (mResumedActivity != null) {
799 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700800 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
801 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
802 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700803 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700804 return true;
805 }
806 if (mPausingActivity != null) {
807 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700808 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700809 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800810 }
811
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700812 if (hasVisibleBehindActivity()) {
813 // Stop visible behind activity before going to sleep.
814 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
815 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700816 if (DEBUG_STATES) Slog.v(TAG_STATES,
817 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700818 return true;
819 }
820
Craig Mautner0eea92c2013-05-16 13:35:39 -0700821 return false;
822 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800823
Craig Mautner0eea92c2013-05-16 13:35:39 -0700824 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700825 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800826
Craig Mautner0eea92c2013-05-16 13:35:39 -0700827 // Make sure any stopped but visible activities are now sleeping.
828 // This ensures that the activity's onStop() is called.
829 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
830 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
831 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
832 final ActivityRecord r = activities.get(activityNdx);
833 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
834 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800835 }
836 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800837 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 }
Craig Mautner59c00972012-07-30 12:10:24 -0700839
Dianne Hackbornd2835932010-12-13 16:28:46 -0800840 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700841 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800842 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700843 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800844 return null;
845 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800846
Winson Chung083baf92014-07-11 10:32:42 -0700847 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700848 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800849 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700850 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700851 return null;
852 }
853
Winson Chung48a10a52014-08-27 14:36:51 -0700854 int w = mService.mThumbnailWidth;
855 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800856 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700857 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800858 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700859 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800860 }
Winson Chung376543b2014-05-21 17:43:28 -0700861 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800862 return null;
863 }
864
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700865 /**
866 * Start pausing the currently resumed activity. It is an error to call this if there
867 * is already an activity being paused or there is no resumed activity.
868 *
869 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
870 * @param uiSleeping True if this is happening with the user interface going to sleep (the
871 * screen turning off).
872 * @param resuming True if this is being called as part of resuming the top activity, so
873 * we shouldn't try to instigate a resume here.
874 * @param dontWait True if the caller does not want to wait for the pause to complete. If
875 * set to true, we will immediately complete the pause here before returning.
876 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
877 * it to tell us when it is done.
878 */
879 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
880 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800881 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800882 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
883 + " state=" + mPausingActivity.state);
884 if (!mService.isSleeping()) {
885 // Avoid recursion among check for sleep and complete pause during sleeping.
886 // Because activity will be paused immediately after resume, just let pause
887 // be completed by the order of activity paused from clients.
888 completePauseLocked(false);
889 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800890 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700891 ActivityRecord prev = mResumedActivity;
892 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700893 if (!resuming) {
894 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
895 mStackSupervisor.resumeTopActivitiesLocked();
896 }
897 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800899
900 if (mActivityContainer.mParentActivity == null) {
901 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700902 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800903 }
904
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700905 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700906 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700907 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800908 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700910 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
911 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 prev.state = ActivityState.PAUSING;
913 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700914 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700915 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700916 if (mService.mHasRecents
917 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800918 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700919 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700920 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921
922 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800923
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700924 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700925 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926 try {
927 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700928 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700929 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700930 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800931 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700932 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 } catch (Exception e) {
934 // Ignore exception, if process died other code will cleanup.
935 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800936 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700938 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700939 }
940 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800941 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700942 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700943 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 }
945
946 // If we are not going to sleep, we want to ensure the device is
947 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700948 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700949 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950 }
951
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800952 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700953 // Have the window manager pause its key dispatching until the new
954 // activity has started. If we're pausing the activity just because
955 // the screen is being turned off and the UI is sleeping, don't interrupt
956 // key dispatch; the same activity will pick it up again on wakeup.
957 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800958 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700959 } else if (DEBUG_PAUSE) {
960 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700961 }
962
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700963 if (dontWait) {
964 // If the caller said they don't want to wait for the pause, then complete
965 // the pause now.
966 completePauseLocked(false);
967 return false;
968
969 } else {
970 // Schedule a pause timeout in case the app doesn't respond.
971 // We don't give it much time because this directly impacts the
972 // responsiveness seen by the user.
973 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
974 msg.obj = prev;
975 prev.pauseTime = SystemClock.uptimeMillis();
976 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700977 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700978 return true;
979 }
980
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700981 } else {
982 // This activity failed to schedule the
983 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700984 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700985 if (!resuming) {
986 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
987 }
988 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 }
990 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700991
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700992 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700993 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
994 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700995
Craig Mautnerd2328952013-03-05 12:46:26 -0800996 final ActivityRecord r = isInStackLocked(token);
997 if (r != null) {
998 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
999 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001000 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001001 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001002 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001003 } else {
1004 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1005 r.userId, System.identityHashCode(r), r.shortComponentName,
1006 mPausingActivity != null
1007 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001008 if (r.state == ActivityState.PAUSING) {
1009 r.state = ActivityState.PAUSED;
1010 if (r.finishing) {
1011 if (DEBUG_PAUSE) Slog.v(TAG,
1012 "Executing finish of failed to pause activity: " + r);
1013 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1014 }
louis_chang047dfd42015-04-08 16:35:55 +08001015 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001016 }
1017 }
1018 }
1019
Craig Mautnera0026042014-04-23 11:45:37 -07001020 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1021 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001022 if (r.state != ActivityState.STOPPING) {
1023 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1024 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1025 return;
1026 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001027 if (persistentState != null) {
1028 r.persistentState = persistentState;
1029 mService.notifyTaskPersisterLocked(r.task, false);
1030 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001031 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001032 if (icicle != null) {
1033 // If icicle is null, this is happening due to a timeout, so we
1034 // haven't really saved the state.
1035 r.icicle = icicle;
1036 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001037 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001038 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001039 }
1040 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001041 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001042 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1043 r.stopped = true;
1044 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001045 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1046 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001047 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001048 if (r.finishing) {
1049 r.clearOptionsLocked();
1050 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001051 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001052 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001053 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001054 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001055 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001056 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001057 }
1058 }
1059 }
1060
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001061 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001062 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001063 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001064
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001065 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001066 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001067 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001068 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001069 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001070 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001071 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001072 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001073 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1074 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001075 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001076 if (prev.configDestroy) {
1077 // The previous is being paused because the configuration
1078 // is changing, which means it is actually stopping...
1079 // To juggle the fact that we are also starting a new
1080 // instance right now, we need to first completely stop
1081 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001082 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001083 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001084 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001085 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001086 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001087 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001088 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1089 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001090 // If we already have a few activities waiting to stop,
1091 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001092 // them out. Or if r is the last of activity of the last task the stack
1093 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001094 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001095 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001096 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001097 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001098 }
1099 }
1100 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001101 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001102 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001103 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001104 // It is possible the activity was freezing the screen before it was paused.
1105 // In that case go ahead and remove the freeze this activity has on the screen
1106 // since it is no longer visible.
1107 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001108 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001109 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001110
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001111 if (resumeNext) {
1112 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1113 if (!mService.isSleepingOrShuttingDown()) {
1114 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1115 } else {
1116 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001117 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001118 if (top == null || (prev != null && top != prev)) {
1119 // If there are no more activities available to run,
1120 // do resume anyway to start something. Also if the top
1121 // activity on the stack is not the just paused activity,
1122 // we need to go ahead and resume it to ensure we complete
1123 // an in-flight app switch.
1124 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1125 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001126 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001128
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001129 if (prev != null) {
1130 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131
Craig Mautner525f3d92013-05-07 14:01:50 -07001132 if (prev.app != null && prev.cpuTimeAtResume > 0
1133 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001134 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1135 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001136 if (diff > 0) {
1137 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1138 synchronized (bsi) {
1139 BatteryStatsImpl.Uid.Proc ps =
1140 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001141 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001142 if (ps != null) {
1143 ps.addForegroundTimeLocked(diff);
1144 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145 }
1146 }
1147 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001148 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001149 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001150
1151 // Notfiy when the task stack has changed
1152 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 }
1154
1155 /**
1156 * Once we know that we have asked an application to put an activity in
1157 * the resumed state (either by launching it or explicitly telling it),
1158 * this function updates the rest of our state to match that fact.
1159 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001160 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001161 next.idle = false;
1162 next.results = null;
1163 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001164
Chong Zhang85ee6542015-10-02 13:36:38 -07001165 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001166 ProcessRecord app = next.task.mActivities.get(0).app;
1167 if (app != null && app != mService.mHomeProcess) {
1168 mService.mHomeProcess = app;
1169 }
1170 }
1171
Craig Mautner07566322013-09-26 16:42:55 -07001172 if (next.nowVisible) {
1173 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001174 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001175 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176
1177 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001178 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001179
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001180 mStackSupervisor.reportResumedActivityLocked(next);
1181
1182 next.resumeKeyDispatchingLocked();
1183 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184
1185 // Mark the point when the activity is resuming
1186 // TODO: To be more accurate, the mark should be before the onCreate,
1187 // not after the onResume. But for subsequent starts, onResume is fine.
1188 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001189 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001190 } else {
1191 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1192 }
Winson Chung376543b2014-05-21 17:43:28 -07001193
George Mount6ba042b2014-07-28 11:12:28 -07001194 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001195
1196 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1197 // When resuming an activity, require it to call requestVisibleBehind() again.
1198 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1199 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001200 }
1201
Craig Mautner2568c3a2015-03-26 14:22:34 -07001202 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001203 r.visible = visible;
1204 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001205 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001206 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001207 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001208 container.setVisible(visible);
1209 }
1210 }
1211
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001212 // Find the first visible activity above the passed activity and if it is translucent return it
1213 // otherwise return null;
1214 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1215 TaskRecord task = r.task;
1216 if (task == null) {
1217 return null;
1218 }
1219
1220 ActivityStack stack = task.stack;
1221 if (stack == null) {
1222 return null;
1223 }
1224
1225 int stackNdx = mStacks.indexOf(stack);
1226
1227 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1228 int taskNdx = tasks.indexOf(task);
1229
1230 ArrayList<ActivityRecord> activities = task.mActivities;
1231 int activityNdx = activities.indexOf(r) + 1;
1232
1233 final int numStacks = mStacks.size();
1234 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001235 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001236 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001237 final int numTasks = tasks.size();
1238 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001239 final TaskRecord currentTask = tasks.get(taskNdx);
1240 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001241 final int numActivities = activities.size();
1242 while (activityNdx < numActivities) {
1243 final ActivityRecord activity = activities.get(activityNdx);
1244 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001245 return historyStack.mFullscreen
1246 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001247 }
1248 ++activityNdx;
1249 }
1250 activityNdx = 0;
1251 ++taskNdx;
1252 }
1253 taskNdx = 0;
1254 ++stackNdx;
1255 }
1256
1257 return null;
1258 }
1259
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001260 private ActivityStack getNextVisibleStackLocked() {
1261 ArrayList<ActivityStack> stacks = mStacks;
1262 final ActivityRecord parent = mActivityContainer.mParentActivity;
1263 if (parent != null) {
1264 stacks = parent.task.stack.mStacks;
1265 }
1266 if (stacks != null) {
1267 for (int i = stacks.size() - 1; i >= 0; --i) {
1268 ActivityStack stack = stacks.get(i);
1269 if (stack != this && stack.isStackVisibleLocked()) {
1270 return stack;
1271 }
1272 }
1273 }
1274 return null;
1275 }
1276
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001277 /** Returns true if the stack contains a fullscreen task. */
1278 private boolean hasFullscreenTask() {
1279 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1280 final TaskRecord task = mTaskHistory.get(i);
1281 if (task.mFullscreen) {
1282 return true;
1283 }
1284 }
1285 return false;
1286 }
1287
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001288 private boolean hasTranslucentActivity(ActivityStack stack) {
1289 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1290 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1291 final TaskRecord task = tasks.get(taskNdx);
1292 final ArrayList<ActivityRecord> activities = task.mActivities;
1293 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1294 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001295
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001296 // Conditions for an activity to obscure the stack we're
1297 // examining:
1298 // 1. Not Finishing AND Visible AND:
1299 // 2. Either:
1300 // - Full Screen Activity OR
1301 // - On top of Home and our stack is NOT home
1302 if (!r.finishing && r.visible && (r.fullscreen ||
1303 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1304 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001305 }
1306 }
1307 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001308 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001309 }
1310
1311 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001312 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001313 if (!isAttached()) {
1314 return false;
1315 }
1316
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001317 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001318 return true;
1319 }
1320
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001321 final int stackIndex = mStacks.indexOf(this);
1322
1323 if (stackIndex == mStacks.size() - 1) {
1324 Slog.wtf(TAG,
1325 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1326 return false;
1327 }
1328
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001329 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1330 final int focusedStackId = focusedStack.mStackId;
1331
1332 if (mStackId == DOCKED_STACK_ID) {
1333 // Docked stack is always visible, except in the case where the home activity
1334 // is the top running activity in the focused home stack.
1335 if (focusedStackId != HOME_STACK_ID) {
1336 return true;
1337 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001338 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001339 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1340 }
1341
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001342 final int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001343 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1344 && stackIndex == belowFocusedIndex) {
1345 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001346 return true;
1347 }
1348
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001349 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1350 && hasTranslucentActivity(focusedStack)) {
1351 // Stacks behind the fullscreen stack with a translucent activity are always
1352 // visible so they can act as a backdrop to the translucent activity.
1353 // For example, dialog activities
1354 if (stackIndex == belowFocusedIndex) {
1355 return true;
1356 }
1357 if (belowFocusedIndex >= 0) {
1358 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001359 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1360 && stackIndex == (belowFocusedIndex - 1)) {
1361 // The stack behind the docked or pinned stack is also visible so we can have a
1362 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001363 return true;
1364 }
1365 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001366 }
1367
1368 if (mStackId >= FIRST_STATIC_STACK_ID && mStackId <= LAST_STATIC_STACK_ID) {
1369 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001370 return false;
1371 }
1372
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001373 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001374 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001375
1376 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1377 continue;
1378 }
1379
1380 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001381 || stack.mStackId == HOME_STACK_ID
1382 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
1383 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001384 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001385 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001386
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001387 if (!hasTranslucentActivity(stack)) {
1388 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001389 }
1390 }
1391
1392 return true;
1393 }
1394
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001395 final int rankTaskLayers(int baseLayer) {
1396 int layer = 0;
1397 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1398 final TaskRecord task = mTaskHistory.get(taskNdx);
1399 ActivityRecord r = task.topRunningActivityLocked();
1400 if (r == null || r.finishing || !r.visible) {
1401 task.mLayerRank = -1;
1402 } else {
1403 task.mLayerRank = baseLayer + layer++;
1404 }
1405 }
1406 return layer;
1407 }
1408
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 /**
1410 * Make sure that all activities that need to be visible (that is, they
1411 * currently can be seen by the user) actually are.
1412 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001413 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1414 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001415 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001416 if (top == null) {
1417 return;
1418 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001419 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1420 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 + " configChanges=0x" + Integer.toHexString(configChanges));
1422
Craig Mautner5eda9b32013-07-02 11:58:16 -07001423 if (mTranslucentActivityWaiting != top) {
1424 mUndrawnActivitiesBelowTopTranslucent.clear();
1425 if (mTranslucentActivityWaiting != null) {
1426 // Call the callback with a timeout indication.
1427 notifyActivityDrawnLocked(null);
1428 mTranslucentActivityWaiting = null;
1429 }
1430 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1431 }
1432
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 // If the top activity is not fullscreen, then we need to
1434 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001435 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001436 final boolean stackInvisible = !isStackVisibleLocked();
1437 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001438 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001439
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001440 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001441 final TaskRecord task = mTaskHistory.get(taskNdx);
1442 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001443
Craig Mautnerd44711d2013-02-23 11:24:36 -08001444 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1445 final ActivityRecord r = activities.get(activityNdx);
1446 if (r.finishing) {
1447 continue;
1448 }
1449 if (aboveTop && r != top) {
1450 continue;
1451 }
1452 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001453 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1454 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001455 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001456 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1457 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001458 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001459
Craig Mautnerd44711d2013-02-23 11:24:36 -08001460 // First: if this is not the current activity being started, make
1461 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001462 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001463 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001465
1466 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001467 // This activity needs to be visible, but isn't even running...
1468 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001469 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1470 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001471 if (r != starting) {
1472 r.startFreezingScreenLocked(r.app, configChanges);
1473 }
1474 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001475 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1476 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001477 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001478 }
1479 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001480 mStackSupervisor.startSpecificActivityLocked(
1481 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001482 if (activityNdx >= activities.size()) {
1483 // Record may be removed if its process needs to restart.
1484 activityNdx = activities.size() - 1;
1485 } else {
1486 noStackActivityResumed = false;
1487 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001488 }
1489
1490 } else if (r.visible) {
1491 // If this activity is already visible, then there is nothing
1492 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001493 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1494 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001496 try {
George Mount6ba042b2014-07-28 11:12:28 -07001497 if (r.returningOptions != null) {
1498 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1499 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001500 }
1501 } catch(RemoteException e) {
1502 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001503 if (r.state == ActivityState.RESUMED) {
1504 noStackActivityResumed = false;
1505 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001506 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001507 // This activity is not currently visible, but is running.
1508 // Tell it to become visible.
1509 r.visible = true;
1510 if (r.state != ActivityState.RESUMED && r != starting) {
1511 // If this activity is paused, tell it
1512 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001513 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1514 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001515 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001516 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001517 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001518 mUndrawnActivitiesBelowTopTranslucent.add(r);
1519 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001520 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001521 r.sleeping = false;
1522 r.app.pendingUiClean = true;
1523 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1524 r.stopFreezingScreenLocked(false);
1525 } catch (Exception e) {
1526 // Just skip on any failure; we'll make it
1527 // visible when it next restarts.
1528 Slog.w(TAG, "Exception thrown making visibile: "
1529 + r.intent.getComponent(), e);
1530 }
1531 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001532 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533
Craig Mautnerd44711d2013-02-23 11:24:36 -08001534 // Aggregate current change flags.
1535 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536
Craig Mautnerd44711d2013-02-23 11:24:36 -08001537 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001538 // At this point, nothing else needs to be shown in this task.
1539 behindFullscreenActivity = true;
1540 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001541 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001542 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001543 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001544 behindFullscreenActivity = true;
1545 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001546 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001547 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001548 }
1549 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001550 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001551 "Make invisible? " + r + " finishing=" + r.finishing
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001552 + " state=" + r.state + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001553 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001554 // Now for any activities that aren't visible to the user, make
1555 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001557 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001559 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001560 switch (r.state) {
1561 case STOPPING:
1562 case STOPPED:
1563 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001564 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1565 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001566 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1567 }
1568 break;
1569
1570 case INITIALIZING:
1571 case RESUMED:
1572 case PAUSING:
1573 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001574 // This case created for transitioning activities from
1575 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001576 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001577 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001578 } else {
1579 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1580 mStackSupervisor.mStoppingActivities.add(r);
1581 }
1582 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001583 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001584 break;
1585
1586 default:
1587 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 }
1589 } catch (Exception e) {
1590 // Just skip on any failure; we'll make it
1591 // visible when it next restarts.
1592 Slog.w(TAG, "Exception thrown making hidden: "
1593 + r.intent.getComponent(), e);
1594 }
1595 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001596 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 }
1599 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001600 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1601 // The visibility of tasks and the activities they contain in freeform stack are
1602 // determined individually unlike other stacks where the visibility or fullscreen
1603 // status of an activity in a previous task affects other.
1604 behindFullscreenActivity = stackInvisible;
1605 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001606 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001607
1608 if (mTranslucentActivityWaiting != null &&
1609 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1610 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1611 notifyActivityDrawnLocked(null);
1612 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 }
Craig Mautner58547802013-03-05 08:23:53 -08001614
Todd Kennedyaab56db2015-01-30 09:39:53 -08001615 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001616 mTranslucentActivityWaiting = r;
1617 mUndrawnActivitiesBelowTopTranslucent.clear();
1618 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1619 }
1620
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001621 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1622 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1623 final TaskRecord task = mTaskHistory.get(taskNdx);
1624 final ArrayList<ActivityRecord> activities = task.mActivities;
1625 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1626 final ActivityRecord r = activities.get(activityNdx);
1627 if ( r.appTimeTracker != except) {
1628 r.appTimeTracker = null;
1629 }
1630 }
1631 }
1632 }
1633
Craig Mautner5eda9b32013-07-02 11:58:16 -07001634 /**
1635 * Called as activities below the top translucent activity are redrawn. When the last one is
1636 * redrawn notify the top activity by calling
1637 * {@link Activity#onTranslucentConversionComplete}.
1638 *
1639 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1640 * occurred and the activity will be notified immediately.
1641 */
1642 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001643 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001644 if ((r == null)
1645 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1646 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1647 // The last undrawn activity below the top has just been drawn. If there is an
1648 // opaque activity at the top, notify it that it can become translucent safely now.
1649 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1650 mTranslucentActivityWaiting = null;
1651 mUndrawnActivitiesBelowTopTranslucent.clear();
1652 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1653
Craig Mautner71dd1b62014-02-18 15:48:52 -08001654 if (waitingActivity != null) {
1655 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1656 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1657 try {
1658 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1659 waitingActivity.appToken, r != null);
1660 } catch (RemoteException e) {
1661 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001662 }
1663 }
1664 }
1665 }
1666
Craig Mautnera61bc652013-10-28 15:43:18 -07001667 /** If any activities below the top running one are in the INITIALIZING state and they have a
1668 * starting window displayed then remove that starting window. It is possible that the activity
1669 * in this state will never resumed in which case that starting window will be orphaned. */
1670 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001671 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001672 boolean aboveTop = true;
1673 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1674 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1675 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1676 final ActivityRecord r = activities.get(activityNdx);
1677 if (aboveTop) {
1678 if (r == topActivity) {
1679 aboveTop = false;
1680 }
1681 continue;
1682 }
1683
1684 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001685 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1686 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001687 r.mStartingWindowShown = false;
1688 mWindowManager.removeAppStartingWindow(r.appToken);
1689 }
1690 }
1691 }
1692 }
1693
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 /**
1695 * Ensure that the top activity in the stack is resumed.
1696 *
1697 * @param prev The previously resumed activity, for when in the process
1698 * of pausing; can be null to call from elsewhere.
1699 *
1700 * @return Returns true if something is being resumed, or false if
1701 * nothing happened.
1702 */
1703 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001704 return resumeTopActivityLocked(prev, null);
1705 }
1706
1707 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001708 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001709 // Don't even start recursing.
1710 return false;
1711 }
1712
1713 boolean result = false;
1714 try {
1715 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001716 mStackSupervisor.inResumeTopActivity = true;
1717 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1718 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001719 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001720 }
Craig Mautner544efa72014-09-04 16:41:20 -07001721 result = resumeTopActivityInnerLocked(prev, options);
1722 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001723 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001724 }
1725 return result;
1726 }
1727
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001728 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001729 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001730
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001731 if (!mService.mBooting && !mService.mBooted) {
1732 // Not ready yet!
1733 return false;
1734 }
1735
Craig Mautnerdf88d732014-01-27 09:21:32 -08001736 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001737 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001738 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001739 // Do not resume this stack if its parent is not resumed.
1740 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1741 return false;
1742 }
1743
Craig Mautnera61bc652013-10-28 15:43:18 -07001744 cancelInitializingActivities();
1745
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001747 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748
1749 // Remember how we'll process this pause/resume situation, and ensure
1750 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001751 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1752 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753
Craig Mautner84984fa2014-06-19 11:19:20 -07001754 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001756 // There are no more activities!
1757 final String reason = "noMoreActivities";
1758 if (!mFullscreen) {
1759 // Try to move focus to the next visible stack with a running activity if this
1760 // stack is not covering the entire screen.
1761 final ActivityStack stack = getNextVisibleStackLocked();
1762 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1763 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1764 }
1765 }
1766 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001767 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001768 if (DEBUG_STATES) Slog.d(TAG_STATES,
1769 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001770 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001771 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001772 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1773 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1774 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001775 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 }
1777
1778 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001779
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001781 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1782 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 // Make sure we have executed any pending transitions, since there
1784 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001785 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001787 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001788 if (DEBUG_STATES) Slog.d(TAG_STATES,
1789 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001790 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 return false;
1792 }
1793
Craig Mautner525f3d92013-05-07 14:01:50 -07001794 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001795 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001796 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001797 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001798 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001799 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001800 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001801 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001802 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001803 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001804 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001805 } else if (!isOnHomeDisplay()) {
1806 return false;
1807 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001808 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001809 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001810 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1811 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1812 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001813 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001814 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001815 }
1816
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 // If we are sleeping, and there is no resumed activity, and the top
1818 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001819 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001820 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001821 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 // Make sure we have executed any pending transitions, since there
1823 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001824 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001826 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001827 if (DEBUG_STATES) Slog.d(TAG_STATES,
1828 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001829 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 return false;
1831 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001832
1833 // Make sure that the user who owns this activity is started. If not,
1834 // we will just leave it as is because someone should be bringing
1835 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001836 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001837 Slog.w(TAG, "Skipping resume of top activity " + next
1838 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001839 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001840 return false;
1841 }
1842
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 // The activity may be waiting for stop, but that is no longer
1844 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001845 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001846 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001847 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001848 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001850 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001851
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001852 // If we are currently pausing an activity, then don't do anything
1853 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001854 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001855 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001856 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001857 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001858 return false;
1859 }
1860
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001861 // Okay we are now going to start a switch, to 'next'. We may first
1862 // have to pause the current activity, but this is an important point
1863 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001864 // XXX "App Redirected" dialog is getting too many false positives
1865 // at this point, so turn off for now.
1866 if (false) {
1867 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1868 long now = SystemClock.uptimeMillis();
1869 final boolean inTime = mLastStartedActivity.startTime != 0
1870 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1871 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1872 final int nextUid = next.info.applicationInfo.uid;
1873 if (inTime && lastUid != nextUid
1874 && lastUid != next.launchedFromUid
1875 && mService.checkPermission(
1876 android.Manifest.permission.STOP_APP_SWITCHES,
1877 -1, next.launchedFromUid)
1878 != PackageManager.PERMISSION_GRANTED) {
1879 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1880 } else {
1881 next.startTime = now;
1882 mLastStartedActivity = next;
1883 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001884 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001885 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001886 mLastStartedActivity = next;
1887 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001888 }
Craig Mautner58547802013-03-05 08:23:53 -08001889
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001890 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1891
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001892 // We need to start pausing the current activity so the top one
1893 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001894 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1895 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001896 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001897 if (DEBUG_STATES) Slog.d(TAG_STATES,
1898 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001899 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001900 }
1901 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001902 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001903 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001904 // At this point we want to put the upcoming activity's process
1905 // at the top of the LRU list, since we know we will be needing it
1906 // very soon and it would be a waste to let it get killed if it
1907 // happens to be sitting towards the end.
1908 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001909 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001910 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001911 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 return true;
1913 }
1914
Christopher Tated3f175c2012-06-14 14:16:54 -07001915 // If the most recent activity was noHistory but was only stopped rather
1916 // than stopped+finished because the device went to sleep, we need to make
1917 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001918 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001919 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001920 if (DEBUG_STATES) Slog.d(TAG_STATES,
1921 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001922 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001923 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001924 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001925 }
1926
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001927 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001928 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1929 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001930 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001931 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1932 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001933 } else {
1934 // The next activity is already visible, so hide the previous
1935 // activity's windows right now so we can show the new one ASAP.
1936 // We only do this if the previous is finishing, which should mean
1937 // it is on top of the one being resumed so hiding it quickly
1938 // is good. Otherwise, we want to do the normal route of allowing
1939 // the resumed activity to be shown so we can decide if the
1940 // previous should actually be hidden depending on whether the
1941 // new one is found to be full-screen or not.
1942 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001943 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001944 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1945 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001946 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947 + ", nowVisible=" + next.nowVisible);
1948 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001949 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001950 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001951 + ", waitingVisible="
1952 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1953 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001954 }
1955 }
1956 }
1957
Dianne Hackborne7f97212011-02-24 14:40:20 -08001958 // Launching this app's activity, make sure the app is no longer
1959 // considered stopped.
1960 try {
1961 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001962 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001963 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001964 } catch (IllegalArgumentException e) {
1965 Slog.w(TAG, "Failed trying to unstop package "
1966 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001967 }
1968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001969 // We are starting up the next activity, so tell the window manager
1970 // that the previous one will be hidden soon. This way it can know
1971 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001972 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001973 if (prev != null) {
1974 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001975 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001976 "Prepare close transition: prev=" + prev);
1977 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001978 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001979 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001981 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001982 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1983 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001985 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001986 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001987 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1988 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001990 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001992 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001993 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001994 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001995 : next.mLaunchTaskBehind
1996 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1997 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 }
1999 }
Craig Mautner967212c2013-04-13 21:10:58 -07002000 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002001 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002002 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002003 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002004 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002005 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002006 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002007 }
2008 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002009
2010 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002011 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002012 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2013 if (opts != null) {
2014 resumeAnimOptions = opts.toBundle();
2015 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002016 next.applyOptionsLocked();
2017 } else {
2018 next.clearOptionsLocked();
2019 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020
Craig Mautnercf910b02013-04-23 11:23:27 -07002021 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002022 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002023 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024
2025 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002026 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002028 // schedule launch ticks to collect information about slow apps.
2029 next.startLaunchTickingLocked();
2030
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002031 ActivityRecord lastResumedActivity =
2032 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002033 ActivityState lastState = next.state;
2034
2035 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002036
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002037 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002038 next.state = ActivityState.RESUMED;
2039 mResumedActivity = next;
2040 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002041 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002042 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002044 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002045
2046 // Have the window manager re-evaluate the orientation of
2047 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002048 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002049 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002050 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002051 mService.mConfiguration,
2052 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2053 if (config != null) {
2054 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002055 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002056 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002057 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002058
Craig Mautner525f3d92013-05-07 14:01:50 -07002059 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 // The configuration update wasn't able to keep the existing
2061 // instance of the activity, and instead started a new one.
2062 // We should be all done, but let's just make sure our activity
2063 // is still at the top and schedule another run if something
2064 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002065 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002066 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 "Activity config changed during resume: " + next
2068 + ", new next: " + nextNext);
2069 if (nextNext != next) {
2070 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002071 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002072 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002073 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002074 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002075 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002076 return true;
2077 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002078 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002079 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002080 }
Craig Mautner58547802013-03-05 08:23:53 -08002081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 try {
2083 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002084 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002085 if (a != null) {
2086 final int N = a.size();
2087 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002088 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2089 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002090 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002091 }
2092 }
2093
2094 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002095 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 }
2097
Craig Mautner299f9602015-01-26 09:47:33 -08002098 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2099 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002100
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002101 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002102 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002103 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002104 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002105 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002106 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002107 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002108
Craig Mautner0eea92c2013-05-16 13:35:39 -07002109 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002111 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 } catch (Exception e) {
2113 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002114 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002115 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002117 if (lastStack != null) {
2118 lastStack.mResumedActivity = lastResumedActivity;
2119 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 Slog.i(TAG, "Restarting because process died: " + next);
2121 if (!next.hasBeenLaunched) {
2122 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002123 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2124 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002125 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002126 next.appToken, next.packageName, next.theme,
2127 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002128 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2129 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002130 }
George Mount2c92c972014-03-20 09:38:23 -07002131 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002132 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133 return true;
2134 }
2135
2136 // From this point on, if something goes wrong there is no way
2137 // to recover the activity.
2138 try {
2139 next.visible = true;
2140 completeResumeLocked(next);
2141 } catch (Exception e) {
2142 // If any exception gets thrown, toss away this
2143 // activity and try the next one.
2144 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002145 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002146 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002147 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 return true;
2149 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150 next.stopped = false;
2151
2152 } else {
2153 // Whoops, need to restart this activity!
2154 if (!next.hasBeenLaunched) {
2155 next.hasBeenLaunched = true;
2156 } else {
2157 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002158 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002159 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002160 mService.compatibilityInfoForPackageLocked(
2161 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002163 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002164 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002166 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002168 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002169 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 }
2171
Craig Mautnercf910b02013-04-23 11:23:27 -07002172 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002173 return true;
2174 }
2175
riddle_hsuc215a4f2014-12-27 12:10:45 +08002176 private TaskRecord getNextTask(TaskRecord targetTask) {
2177 final int index = mTaskHistory.indexOf(targetTask);
2178 if (index >= 0) {
2179 final int numTasks = mTaskHistory.size();
2180 for (int i = index + 1; i < numTasks; ++i) {
2181 TaskRecord task = mTaskHistory.get(i);
2182 if (task.userId == targetTask.userId) {
2183 return task;
2184 }
2185 }
2186 }
2187 return null;
2188 }
2189
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002190 private void insertTaskAtPosition(TaskRecord task, int position) {
2191 if (position >= mTaskHistory.size()) {
2192 insertTaskAtTop(task, null);
2193 return;
2194 }
2195 // Calculate maximum possible position for this task.
2196 int maxPosition = mTaskHistory.size();
2197 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002198 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002199 // Put non-current user tasks below current user tasks.
2200 while (maxPosition > 0) {
2201 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2202 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002203 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002204 break;
2205 }
2206 maxPosition--;
2207 }
2208 }
2209 position = Math.min(position, maxPosition);
2210 mTaskHistory.remove(task);
2211 mTaskHistory.add(position, task);
2212 updateTaskMovement(task, true);
2213 }
2214
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002215 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002216 // If the moving task is over home stack, transfer its return type to next task
2217 if (task.isOverHomeStack()) {
2218 final TaskRecord nextTask = getNextTask(task);
2219 if (nextTask != null) {
2220 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2221 }
2222 }
2223
Craig Mautner9c85c202013-10-04 20:11:26 -07002224 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002225 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002226 if (isOnHomeDisplay()) {
2227 ActivityStack lastStack = mStackSupervisor.getLastStack();
2228 final boolean fromHome = lastStack.isHomeStack();
2229 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002230 task.setTaskToReturnTo(fromHome
2231 ? lastStack.topTask() == null
2232 ? HOME_ACTIVITY_TYPE
2233 : lastStack.topTask().taskType
2234 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002235 }
2236 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002237 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002238 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002239
Craig Mautnerac6f8432013-07-17 13:24:59 -07002240 mTaskHistory.remove(task);
2241 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002242 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002243 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002244 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002245 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002246 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002247 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002248 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002249 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002250 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002251 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002252 break;
2253 }
2254 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002255 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002256 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002257 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002258 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002259 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002260
Craig Mautner8849a5e2013-04-02 16:41:03 -07002261 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002262 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002263 TaskRecord rTask = r.task;
2264 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002265 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2266 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002267 // Last activity in task had been removed or ActivityManagerService is reusing task.
2268 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002269 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002270 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002271 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002272 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002273 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 if (!newTask) {
2275 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002276 boolean startIt = true;
2277 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2278 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002279 if (task.getTopActivity() == null) {
2280 // All activities in task are finishing.
2281 continue;
2282 }
Craig Mautner70a86932013-02-28 22:37:44 -08002283 if (task == r.task) {
2284 // Here it is! Now, if this is not yet visible to the
2285 // user, then just add it without starting; it will
2286 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002287 if (!startIt) {
2288 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2289 + task, new RuntimeException("here").fillInStackTrace());
2290 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002291 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002292 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002294 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002296 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002297 return;
2298 }
2299 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002300 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 startIt = false;
2302 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 }
2304 }
2305
2306 // Place a new activity at top of stack, so it is next to interact
2307 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002308
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 // If we are not placing the new activity frontmost, we do not want
2310 // to deliver the onUserLeaving callback to the actual frontmost
2311 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002312 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002313 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002314 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002315 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 }
Craig Mautner70a86932013-02-28 22:37:44 -08002317
2318 task = r.task;
2319
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002321 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002322 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002323 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002324 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002325
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002326 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002327 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002328 // We want to show the starting preview window if we are
2329 // switching to a new task, or the next activity's process is
2330 // not currently running.
2331 boolean showStartingIcon = newTask;
2332 ProcessRecord proc = r.app;
2333 if (proc == null) {
2334 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2335 }
2336 if (proc == null || proc.thread == null) {
2337 showStartingIcon = true;
2338 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002339 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002341 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002342 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002344 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002345 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002346 ? r.mLaunchTaskBehind
2347 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2348 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002349 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002350 mNoAnimActivities.remove(r);
2351 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002352 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002353 boolean doShow = true;
2354 if (newTask) {
2355 // Even though this activity is starting fresh, we still need
2356 // to reset it to make sure we apply affinities to move any
2357 // existing activities from other tasks in to it.
2358 // If the caller has requested that the target task be
2359 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002360 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002361 resetTaskIfNeededLocked(r, r);
2362 doShow = topRunningNonDelayedActivityLocked(null) == r;
2363 }
George Mount70778d72014-07-01 16:33:45 -07002364 } else if (options != null && new ActivityOptions(options).getAnimationType()
2365 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2366 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002367 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002368 if (r.mLaunchTaskBehind) {
2369 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2370 // tell WindowManager that r is visible even though it is at the back of the stack.
2371 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002372 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002373 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 // Figure out if we are transitioning from another activity that is
2375 // "has the same starting icon" as the next one. This allows the
2376 // window manager to keep the previous window it had previously
2377 // created, if it still had one.
2378 ActivityRecord prev = mResumedActivity;
2379 if (prev != null) {
2380 // We don't want to reuse the previous starting preview if:
2381 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002382 if (prev.task != r.task) {
2383 prev = null;
2384 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002386 else if (prev.nowVisible) {
2387 prev = null;
2388 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002390 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002391 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002392 mService.compatibilityInfoForPackageLocked(
2393 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002394 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002395 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002396 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 }
2398 } else {
2399 // If this is the first activity, don't do any fancy animations,
2400 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002401 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002402 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002403 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 }
2405 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002406 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002407 }
2408
2409 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002410 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002411 } else {
2412 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002413 }
2414 }
2415
Dianne Hackbornbe707852011-11-11 14:32:10 -08002416 final void validateAppTokensLocked() {
2417 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002418 mValidateAppTokens.ensureCapacity(numActivities());
2419 final int numTasks = mTaskHistory.size();
2420 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2421 TaskRecord task = mTaskHistory.get(taskNdx);
2422 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002423 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002424 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002425 }
Craig Mautner000f0022013-02-26 15:04:29 -08002426 TaskGroup group = new TaskGroup();
2427 group.taskId = task.taskId;
2428 mValidateAppTokens.add(group);
2429 final int numActivities = activities.size();
2430 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2431 final ActivityRecord r = activities.get(activityNdx);
2432 group.tokens.add(r.appToken);
2433 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002434 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002435 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002436 }
2437
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 /**
2439 * Perform a reset of the given task, if needed as part of launching it.
2440 * Returns the new HistoryRecord at the top of the task.
2441 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002442 /**
2443 * Helper method for #resetTaskIfNeededLocked.
2444 * We are inside of the task being reset... we'll either finish this activity, push it out
2445 * for another task, or leave it as-is.
2446 * @param task The task containing the Activity (taskTop) that might be reset.
2447 * @param forceReset
2448 * @return An ActivityOptions that needs to be processed.
2449 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002450 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002451 ActivityOptions topOptions = null;
2452
2453 int replyChainEnd = -1;
2454 boolean canMoveOptions = true;
2455
2456 // We only do this for activities that are not the root of the task (since if we finish
2457 // the root, we may no longer have the task!).
2458 final ArrayList<ActivityRecord> activities = task.mActivities;
2459 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002460 final int rootActivityNdx = task.findEffectiveRootIndex();
2461 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002462 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002463 if (target.frontOfTask)
2464 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002465
2466 final int flags = target.info.flags;
2467 final boolean finishOnTaskLaunch =
2468 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2469 final boolean allowTaskReparenting =
2470 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2471 final boolean clearWhenTaskReset =
2472 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2473
2474 if (!finishOnTaskLaunch
2475 && !clearWhenTaskReset
2476 && target.resultTo != null) {
2477 // If this activity is sending a reply to a previous
2478 // activity, we can't do anything with it now until
2479 // we reach the start of the reply chain.
2480 // XXX note that we are assuming the result is always
2481 // to the previous activity, which is almost always
2482 // the case but we really shouldn't count on.
2483 if (replyChainEnd < 0) {
2484 replyChainEnd = i;
2485 }
2486 } else if (!finishOnTaskLaunch
2487 && !clearWhenTaskReset
2488 && allowTaskReparenting
2489 && target.taskAffinity != null
2490 && !target.taskAffinity.equals(task.affinity)) {
2491 // If this activity has an affinity for another
2492 // task, then we need to move it out of here. We will
2493 // move it as far out of the way as possible, to the
2494 // bottom of the activity stack. This also keeps it
2495 // correctly ordered with any activities we previously
2496 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002497 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002498 final ActivityRecord bottom =
2499 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002500 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002501 if (bottom != null && target.taskAffinity != null
2502 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002503 // If the activity currently at the bottom has the
2504 // same task affinity as the one we are moving,
2505 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002506 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002507 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002508 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002510 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002511 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002512 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002513 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002514 + " out to new task " + target.task);
2515 }
2516
Wale Ogunwale706ed792015-08-02 10:29:44 -07002517 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002518
Craig Mautner525f3d92013-05-07 14:01:50 -07002519 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002520 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2521 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002522 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002523 if (p.finishing) {
2524 continue;
2525 }
2526
Craig Mautnere3a74d52013-02-22 14:14:58 -08002527 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002528 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002529 topOptions = p.takeOptionsLocked();
2530 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002531 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002532 }
2533 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002534 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2535 "Removing activity " + p + " from task=" + task + " adding to task="
2536 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002537 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2538 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002539 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002540 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002541
Wale Ogunwale706ed792015-08-02 10:29:44 -07002542 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002543 }
2544
Wale Ogunwale706ed792015-08-02 10:29:44 -07002545 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002546 if (VALIDATE_TOKENS) {
2547 validateAppTokensLocked();
2548 }
2549
2550 replyChainEnd = -1;
2551 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2552 // If the activity should just be removed -- either
2553 // because it asks for it, or the task should be
2554 // cleared -- then finish it and anything that is
2555 // part of its reply chain.
2556 int end;
2557 if (clearWhenTaskReset) {
2558 // In this case, we want to finish this activity
2559 // and everything above it, so be sneaky and pretend
2560 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002561 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002562 } else if (replyChainEnd < 0) {
2563 end = i;
2564 } else {
2565 end = replyChainEnd;
2566 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002567 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002568 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002569 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002570 if (p.finishing) {
2571 continue;
2572 }
2573 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002574 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002575 topOptions = p.takeOptionsLocked();
2576 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002577 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002578 }
2579 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002580 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002581 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002582 if (finishActivityLocked(
2583 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002584 end--;
2585 srcPos--;
2586 }
2587 }
2588 replyChainEnd = -1;
2589 } else {
2590 // If we were in the middle of a chain, well the
2591 // activity that started it all doesn't want anything
2592 // special, so leave it all as-is.
2593 replyChainEnd = -1;
2594 }
2595 }
2596
2597 return topOptions;
2598 }
2599
2600 /**
2601 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2602 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2603 * @param affinityTask The task we are looking for an affinity to.
2604 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2605 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2606 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2607 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002608 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002609 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002610 int replyChainEnd = -1;
2611 final int taskId = task.taskId;
2612 final String taskAffinity = task.affinity;
2613
2614 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2615 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002616 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2617
2618 // Do not operate on or below the effective root Activity.
2619 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002620 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002621 if (target.frontOfTask)
2622 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002623
2624 final int flags = target.info.flags;
2625 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2626 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2627
2628 if (target.resultTo != null) {
2629 // If this activity is sending a reply to a previous
2630 // activity, we can't do anything with it now until
2631 // we reach the start of the reply chain.
2632 // XXX note that we are assuming the result is always
2633 // to the previous activity, which is almost always
2634 // the case but we really shouldn't count on.
2635 if (replyChainEnd < 0) {
2636 replyChainEnd = i;
2637 }
2638 } else if (topTaskIsHigher
2639 && allowTaskReparenting
2640 && taskAffinity != null
2641 && taskAffinity.equals(target.taskAffinity)) {
2642 // This activity has an affinity for our task. Either remove it if we are
2643 // clearing or move it over to our task. Note that
2644 // we currently punt on the case where we are resetting a
2645 // task that is not at the top but who has activities above
2646 // with an affinity to it... this is really not a normal
2647 // case, and we will need to later pull that task to the front
2648 // and usually at that point we will do the reset and pick
2649 // up those remaining activities. (This only happens if
2650 // someone starts an activity in a new task from an activity
2651 // in a task that is not currently on top.)
2652 if (forceReset || finishOnTaskLaunch) {
2653 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002654 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2655 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002656 for (int srcPos = start; srcPos >= i; --srcPos) {
2657 final ActivityRecord p = activities.get(srcPos);
2658 if (p.finishing) {
2659 continue;
2660 }
Todd Kennedy539db512014-12-15 09:57:55 -08002661 finishActivityLocked(
2662 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002663 }
2664 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002665 if (taskInsertionPoint < 0) {
2666 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002667
Craig Mautner77878772013-03-04 19:46:24 -08002668 }
Craig Mautner77878772013-03-04 19:46:24 -08002669
2670 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002671 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2672 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2673 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002674 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002675 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002676 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002677 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002678
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002679 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2680 "Removing and adding activity " + p + " to stack at " + task
2681 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002682 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2683 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002684 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002685 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002686 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002687 if (VALIDATE_TOKENS) {
2688 validateAppTokensLocked();
2689 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002690
2691 // Now we've moved it in to place... but what if this is
2692 // a singleTop activity and we have put it on top of another
2693 // instance of the same activity? Then we drop the instance
2694 // below so it remains singleTop.
2695 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2696 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002697 int targetNdx = taskActivities.indexOf(target);
2698 if (targetNdx > 0) {
2699 ActivityRecord p = taskActivities.get(targetNdx - 1);
2700 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002701 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2702 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002703 }
2704 }
2705 }
2706 }
2707
2708 replyChainEnd = -1;
2709 }
2710 }
Craig Mautner77878772013-03-04 19:46:24 -08002711 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002712 }
2713
Craig Mautner8849a5e2013-04-02 16:41:03 -07002714 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002715 ActivityRecord newActivity) {
2716 boolean forceReset =
2717 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2718 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2719 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2720 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2721 forceReset = true;
2722 }
2723 }
2724
2725 final TaskRecord task = taskTop.task;
2726
2727 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2728 * for remaining tasks. Used for later tasks to reparent to task. */
2729 boolean taskFound = false;
2730
2731 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2732 ActivityOptions topOptions = null;
2733
Craig Mautner77878772013-03-04 19:46:24 -08002734 // Preserve the location for reparenting in the new task.
2735 int reparentInsertionPoint = -1;
2736
Craig Mautnere3a74d52013-02-22 14:14:58 -08002737 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2738 final TaskRecord targetTask = mTaskHistory.get(i);
2739
2740 if (targetTask == task) {
2741 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2742 taskFound = true;
2743 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002744 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2745 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002746 }
2747 }
2748
Craig Mautner70a86932013-02-28 22:37:44 -08002749 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002750 if (taskNdx >= 0) {
2751 do {
2752 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2753 } while (taskTop == null && taskNdx >= 0);
2754 }
Craig Mautner70a86932013-02-28 22:37:44 -08002755
Craig Mautnere3a74d52013-02-22 14:14:58 -08002756 if (topOptions != null) {
2757 // If we got some ActivityOptions from an activity on top that
2758 // was removed from the task, propagate them to the new real top.
2759 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002760 taskTop.updateOptionsLocked(topOptions);
2761 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002762 topOptions.abort();
2763 }
2764 }
2765
2766 return taskTop;
2767 }
2768
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2770 String resultWho, int requestCode, int resultCode, Intent data) {
2771
2772 if (callingUid > 0) {
2773 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002774 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002775 }
2776
2777 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2778 + " : who=" + resultWho + " req=" + requestCode
2779 + " res=" + resultCode + " data=" + data);
2780 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2781 try {
2782 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2783 list.add(new ResultInfo(resultWho, requestCode,
2784 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002785 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786 return;
2787 } catch (Exception e) {
2788 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2789 }
2790 }
2791
2792 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2793 }
2794
Craig Mautner299f9602015-01-26 09:47:33 -08002795 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002796 if (mStackSupervisor.isFocusedStack(this) && mService.mFocusedActivity == r) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002797 ActivityRecord next = topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002798 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002799 if (next != r) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002800 if (next != null && (mStackId == FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale99db1862015-10-23 20:08:22 -07002801 || mStackId == DOCKED_STACK_ID || mStackId == PINNED_STACK_ID)) {
2802 // For freeform, docked, and pinned stacks we always keep the focus within the
2803 // stack as long as there is a running activity in the stack that we can adjust
2804 // focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002805 mService.setFocusedActivityLocked(next, myReason);
2806 return;
2807 } else {
2808 final TaskRecord task = r.task;
2809 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2810 // For non-fullscreen stack, we want to move the focus to the next visible
2811 // stack to prevent the home screen from moving to the top and obscuring
2812 // other visible stacks.
2813 if (!mFullscreen
2814 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2815 return;
2816 }
2817 // Move the home stack to the top if this stack is fullscreen or there is no
2818 // other visible stack.
2819 if (mStackSupervisor.moveHomeStackTaskToTop(
2820 task.getTaskToReturnTo(), myReason)) {
2821 // Activity focus was already adjusted. Nothing else to do...
2822 return;
2823 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002824 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002825 }
2826 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002827
2828 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002829 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002830 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002831 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002832 }
2833 }
2834
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002835 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2836 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2837 final String myReason = reason + " adjustFocusToNextVisibleStack";
2838 if (stack == null) {
2839 return false;
2840 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002841 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002842 if (top == null) {
2843 return false;
2844 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002845 mService.setFocusedActivityLocked(top, myReason);
2846 return true;
2847 }
2848
Craig Mautnerf3333272013-04-22 10:55:53 -07002849 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002850 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002851 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2852 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2853 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002854 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002855 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002856 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2857 "stop-no-history", false)) {
2858 // Activity was finished, no need to continue trying to schedule stop.
2859 adjustFocusedActivityLocked(r, "stopActivityFinished");
2860 r.resumeKeyDispatchingLocked();
2861 return;
2862 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002863 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002864 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002865 + " on stop because we're just sleeping");
2866 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002868 }
2869
2870 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002871 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002872 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 try {
2874 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002875 if (DEBUG_STATES) Slog.v(TAG_STATES,
2876 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002877 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002878 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2879 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002880 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002881 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002882 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002883 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002884 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002885 r.setSleeping(true);
2886 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002887 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002888 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 } catch (Exception e) {
2890 // Maybe just ignore exceptions here... if the process
2891 // has crashed, our death notification will clean things
2892 // up.
2893 Slog.w(TAG, "Exception thrown during pause", e);
2894 // Just in case, assume it to be stopped.
2895 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002896 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 r.state = ActivityState.STOPPED;
2898 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002899 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002900 }
2901 }
2902 }
2903 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002904
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 /**
2906 * @return Returns true if the activity is being finished, false if for
2907 * some reason it is being left as-is.
2908 */
2909 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002910 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002911 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002912 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2913 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002914 + ", result=" + resultCode + ", data=" + resultData
2915 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002916 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 return false;
2918 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919
Craig Mautnerd44711d2013-02-23 11:24:36 -08002920 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002921 return true;
2922 }
2923
Craig Mautnerd2328952013-03-05 12:46:26 -08002924 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002925 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2926 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2927 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2928 ActivityRecord r = activities.get(activityNdx);
2929 if (r.resultTo == self && r.requestCode == requestCode) {
2930 if ((r.resultWho == null && resultWho == null) ||
2931 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2932 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2933 false);
2934 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002935 }
2936 }
2937 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002938 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002939 }
2940
Todd Kennedy539db512014-12-15 09:57:55 -08002941 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002942 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002943 if (r != null && r.app == app) {
2944 // If the top running activity is from this crashing
2945 // process, then terminate it to avoid getting in a loop.
2946 Slog.w(TAG, " Force finishing activity "
2947 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002948 int taskNdx = mTaskHistory.indexOf(r.task);
2949 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002950 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002951 // Also terminate any activities below it that aren't yet
2952 // stopped, to avoid a situation where one will get
2953 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002954 --activityNdx;
2955 if (activityNdx < 0) {
2956 do {
2957 --taskNdx;
2958 if (taskNdx < 0) {
2959 break;
2960 }
2961 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2962 } while (activityNdx < 0);
2963 }
2964 if (activityNdx >= 0) {
2965 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002966 if (r.state == ActivityState.RESUMED
2967 || r.state == ActivityState.PAUSING
2968 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002969 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002970 Slog.w(TAG, " Force finishing activity "
2971 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002972 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002973 }
2974 }
2975 }
2976 }
2977 }
2978
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002979 final void finishVoiceTask(IVoiceInteractionSession session) {
2980 IBinder sessionBinder = session.asBinder();
2981 boolean didOne = false;
2982 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2983 TaskRecord tr = mTaskHistory.get(taskNdx);
2984 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2985 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2986 ActivityRecord r = tr.mActivities.get(activityNdx);
2987 if (!r.finishing) {
2988 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2989 false);
2990 didOne = true;
2991 }
2992 }
2993 }
2994 }
2995 if (didOne) {
2996 mService.updateOomAdjLocked();
2997 }
2998 }
2999
Craig Mautnerd2328952013-03-05 12:46:26 -08003000 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003001 ArrayList<ActivityRecord> activities = r.task.mActivities;
3002 for (int index = activities.indexOf(r); index >= 0; --index) {
3003 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003004 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003005 break;
3006 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003007 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003008 }
3009 return true;
3010 }
3011
Dianne Hackborn5c607432012-02-28 14:44:19 -08003012 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3013 // send the result
3014 ActivityRecord resultTo = r.resultTo;
3015 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003016 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003017 + " who=" + r.resultWho + " req=" + r.requestCode
3018 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003019 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003020 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003021 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003022 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003023 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003024 if (r.info.applicationInfo.uid > 0) {
3025 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3026 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003027 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003028 }
3029 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3030 resultData);
3031 r.resultTo = null;
3032 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003033 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003034
3035 // Make sure this HistoryRecord is not holding on to other resources,
3036 // because clients have remote IPC references to this object so we
3037 // can't assume that will go away and want to avoid circular IPC refs.
3038 r.results = null;
3039 r.pendingResults = null;
3040 r.newIntents = null;
3041 r.icicle = null;
3042 }
3043
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044 /**
3045 * @return Returns true if this activity has been removed from the history
3046 * list, or false if it is still in the list and will be removed later.
3047 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003048 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3049 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 if (r.finishing) {
3051 Slog.w(TAG, "Duplicate finish request for " + r);
3052 return false;
3053 }
3054
Wale Ogunwale7d701172015-03-11 15:36:30 -07003055 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003056 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003057 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003058 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003059 task.taskId, r.shortComponentName, reason);
3060 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003061 final int index = activities.indexOf(r);
3062 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003063 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003064 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003065 // If the caller asked that this activity (and all above it)
3066 // be cleared when the task is reset, don't lose that information,
3067 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003068 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003069 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 }
3071 }
3072
3073 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003074
Craig Mautner299f9602015-01-26 09:47:33 -08003075 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076
Dianne Hackborn5c607432012-02-28 14:44:19 -08003077 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003078
Craig Mautnerde4ef022013-04-07 19:01:33 -07003079 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003080 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003081 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003083 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003084 ? AppTransition.TRANSIT_TASK_CLOSE
3085 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003088 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003089
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003090 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003091 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3092 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3093 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003094 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 }
3096
Craig Mautneraea74a52014-03-08 14:23:10 -08003097 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003098 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003099 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 } else if (r.state != ActivityState.PAUSING) {
3101 // If the activity is PAUSING, we will complete the finish once
3102 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003103 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003104 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003105 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003106 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 }
3108
3109 return false;
3110 }
3111
Craig Mautnerf3333272013-04-22 10:55:53 -07003112 static final int FINISH_IMMEDIATELY = 0;
3113 static final int FINISH_AFTER_PAUSE = 1;
3114 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003115
Craig Mautnerf3333272013-04-22 10:55:53 -07003116 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003117 // First things first: if this activity is currently visible,
3118 // and the resumed activity is not yet visible, then hold off on
3119 // finishing until the resumed one becomes visible.
3120 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003121 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3122 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003123 if (mStackSupervisor.mStoppingActivities.size() > 3
3124 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 // If we already have a few activities waiting to stop,
3126 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003127 // them out. Or if r is the last of activity of the last task the stack
3128 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003129 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003130 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003131 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 }
3133 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003134 if (DEBUG_STATES) Slog.v(TAG_STATES,
3135 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003136 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003137 if (oomAdj) {
3138 mService.updateOomAdjLocked();
3139 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 return r;
3141 }
3142
3143 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003144 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003145 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003146 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 if (mResumedActivity == r) {
3148 mResumedActivity = null;
3149 }
3150 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003151 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 r.state = ActivityState.FINISHING;
3153
3154 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003155 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003156 || prevState == ActivityState.STOPPED
3157 || prevState == ActivityState.INITIALIZING) {
3158 // If this activity is already stopped, we can just finish
3159 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003160 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003161 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003162 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003163 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003164 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003165 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003166 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3167 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003168 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003170
3171 // Need to go through the full pause cycle to get this
3172 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003173 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003174 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003175 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003176 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003177 return r;
3178 }
3179
Craig Mautneree36c772014-07-16 14:56:05 -07003180 void finishAllActivitiesLocked(boolean immediately) {
3181 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003182 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3183 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3184 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3185 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003186 noActivitiesInStack = false;
3187 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003188 continue;
3189 }
Craig Mautneree36c772014-07-16 14:56:05 -07003190 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003191 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3192 }
3193 }
Craig Mautneree36c772014-07-16 14:56:05 -07003194 if (noActivitiesInStack) {
3195 mActivityContainer.onTaskListEmptyLocked();
3196 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003197 }
3198
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003199 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3200 // Basic case: for simple app-centric recents, we need to recreate
3201 // the task if the affinity has changed.
3202 if (srec == null || srec.task.affinity == null ||
3203 !srec.task.affinity.equals(destAffinity)) {
3204 return true;
3205 }
3206 // Document-centric case: an app may be split in to multiple documents;
3207 // they need to re-create their task if this current activity is the root
3208 // of a document, unless simply finishing it will return them to the the
3209 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003210 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3211 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003212 // Okay, this activity is at the root of its task. What to do, what to do...
3213 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3214 // Finishing won't return to an application, so we need to recreate.
3215 return true;
3216 }
3217 // We now need to get the task below it to determine what to do.
3218 int taskIdx = mTaskHistory.indexOf(srec.task);
3219 if (taskIdx <= 0) {
3220 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3221 return false;
3222 }
3223 if (taskIdx == 0) {
3224 // At the bottom of the stack, nothing to go back to.
3225 return true;
3226 }
3227 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3228 if (!srec.task.affinity.equals(prevTask.affinity)) {
3229 // These are different apps, so need to recreate.
3230 return true;
3231 }
3232 }
3233 return false;
3234 }
3235
Wale Ogunwale7d701172015-03-11 15:36:30 -07003236 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003237 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003238 final TaskRecord task = srec.task;
3239 final ArrayList<ActivityRecord> activities = task.mActivities;
3240 final int start = activities.indexOf(srec);
3241 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003242 return false;
3243 }
3244 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003245 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003246 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003247 final ComponentName dest = destIntent.getComponent();
3248 if (start > 0 && dest != null) {
3249 for (int i = finishTo; i >= 0; i--) {
3250 ActivityRecord r = activities.get(i);
3251 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003252 r.info.name.equals(dest.getClassName())) {
3253 finishTo = i;
3254 parent = r;
3255 foundParentInTask = true;
3256 break;
3257 }
3258 }
3259 }
3260
3261 IActivityController controller = mService.mController;
3262 if (controller != null) {
3263 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3264 if (next != null) {
3265 // ask watcher if this is allowed
3266 boolean resumeOK = true;
3267 try {
3268 resumeOK = controller.activityResuming(next.packageName);
3269 } catch (RemoteException e) {
3270 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003271 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003272 }
3273
3274 if (!resumeOK) {
3275 return false;
3276 }
3277 }
3278 }
3279 final long origId = Binder.clearCallingIdentity();
3280 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003281 ActivityRecord r = activities.get(i);
3282 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003283 // Only return the supplied result for the first activity finished
3284 resultCode = Activity.RESULT_CANCELED;
3285 resultData = null;
3286 }
3287
3288 if (parent != null && foundParentInTask) {
3289 final int parentLaunchMode = parent.info.launchMode;
3290 final int destIntentFlags = destIntent.getFlags();
3291 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3292 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3293 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3294 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003295 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3296 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003297 } else {
3298 try {
3299 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3300 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003301 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003302 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003303 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003304 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003305 foundParentInTask = res == ActivityManager.START_SUCCESS;
3306 } catch (RemoteException e) {
3307 foundParentInTask = false;
3308 }
3309 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003310 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003311 }
3312 }
3313 Binder.restoreCallingIdentity(origId);
3314 return foundParentInTask;
3315 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003316 /**
3317 * Perform the common clean-up of an activity record. This is called both
3318 * as part of destroyActivityLocked() (when destroying the client-side
3319 * representation) and cleaning things up as a result of its hosting
3320 * processing going away, in which case there is no remaining client-side
3321 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003322 *
3323 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003325 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3326 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003327 if (mResumedActivity == r) {
3328 mResumedActivity = null;
3329 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003330 if (mPausingActivity == r) {
3331 mPausingActivity = null;
3332 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003333 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334
3335 r.configDestroy = false;
3336 r.frozenBeforeDestroy = false;
3337
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003338 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003339 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003340 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003341 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003342 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003343 }
3344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 // Make sure this record is no longer in the pending finishes list.
3346 // This could happen, for example, if we are trimming activities
3347 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003348 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003349 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003350
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003351 // Remove any pending results.
3352 if (r.finishing && r.pendingResults != null) {
3353 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3354 PendingIntentRecord rec = apr.get();
3355 if (rec != null) {
3356 mService.cancelIntentSenderLocked(rec, false);
3357 }
3358 }
3359 r.pendingResults = null;
3360 }
3361
3362 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003363 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 }
3365
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003366 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003367 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003368 if (getVisibleBehindActivity() == r) {
3369 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003370 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003371 }
3372
3373 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003374 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003376 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003377 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003378 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003379 }
3380
Craig Mautner299f9602015-01-26 09:47:33 -08003381 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003382 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003383 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003384 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003385 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3386 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3387
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003388 r.takeFromHistory();
3389 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003390 if (DEBUG_STATES) Slog.v(TAG_STATES,
3391 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003392 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003393 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003394 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003395 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003396 if (VALIDATE_TOKENS) {
3397 validateAppTokensLocked();
3398 }
Craig Mautner312ba862014-02-10 17:55:01 -08003399 final TaskRecord task = r.task;
3400 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003401 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003402 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003403 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003404 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003405 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003406 }
Craig Mautner299f9602015-01-26 09:47:33 -08003407 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003408 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003409 cleanUpActivityServicesLocked(r);
3410 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003411 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003412
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003413 /**
3414 * Perform clean-up of service connections in an activity record.
3415 */
3416 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3417 // Throw away any services that have been bound by this activity.
3418 if (r.connections != null) {
3419 Iterator<ConnectionRecord> it = r.connections.iterator();
3420 while (it.hasNext()) {
3421 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003422 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003423 }
3424 r.connections = null;
3425 }
3426 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003427
Craig Mautneree2e45a2014-06-27 12:10:03 -07003428 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003429 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003430 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003431 mHandler.sendMessage(msg);
3432 }
3433
Craig Mautneree2e45a2014-06-27 12:10:03 -07003434 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003435 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003436 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003437 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3438 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3439 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3440 final ActivityRecord r = activities.get(activityNdx);
3441 if (r.finishing) {
3442 continue;
3443 }
3444 if (r.fullscreen) {
3445 lastIsOpaque = true;
3446 }
3447 if (owner != null && r.app != owner) {
3448 continue;
3449 }
3450 if (!lastIsOpaque) {
3451 continue;
3452 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003453 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003454 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003455 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003456 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003457 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003458 activityRemoved = true;
3459 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003460 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003461 }
3462 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003463 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003464 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003465 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003466 }
3467
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003468 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3469 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003470 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3471 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003472 + " pausing=" + mPausingActivity + " for reason " + reason);
3473 return destroyActivityLocked(r, true, reason);
3474 }
3475 return false;
3476 }
3477
3478 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3479 String reason) {
3480 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003481 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003482 int maxTasks = tasks.size() / 4;
3483 if (maxTasks < 1) {
3484 maxTasks = 1;
3485 }
3486 int numReleased = 0;
3487 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3488 final TaskRecord task = mTaskHistory.get(taskNdx);
3489 if (!tasks.contains(task)) {
3490 continue;
3491 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003492 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003493 int curNum = 0;
3494 final ArrayList<ActivityRecord> activities = task.mActivities;
3495 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3496 final ActivityRecord activity = activities.get(actNdx);
3497 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003498 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003499 + " in state " + activity.state + " resumed=" + mResumedActivity
3500 + " pausing=" + mPausingActivity + " for reason " + reason);
3501 destroyActivityLocked(activity, true, reason);
3502 if (activities.get(actNdx) != activity) {
3503 // Was removed from list, back up so we don't miss the next one.
3504 actNdx--;
3505 }
3506 curNum++;
3507 }
3508 }
3509 if (curNum > 0) {
3510 numReleased += curNum;
3511 maxTasks--;
3512 if (mTaskHistory.get(taskNdx) != task) {
3513 // The entire task got removed, back up so we don't miss the next one.
3514 taskNdx--;
3515 }
3516 }
3517 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003518 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3519 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003520 return numReleased;
3521 }
3522
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 /**
3524 * Destroy the current CLIENT SIDE instance of an activity. This may be
3525 * called both when actually finishing an activity, or when performing
3526 * a configuration switch where we destroy the current client-side object
3527 * but then create a new client-side object for this same HistoryRecord.
3528 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003529 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003530 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3531 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003532 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003534 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003535 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003536
3537 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003538
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003539 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003540
3541 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003542
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 if (hadApp) {
3544 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003545 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003546 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3547 mService.mHeavyWeightProcess = null;
3548 mService.mHandler.sendEmptyMessage(
3549 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3550 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003551 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003552 // Update any services we are bound to that might care about whether
3553 // their client may have activities.
3554 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003555 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003556 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003557 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003558 }
3559 }
3560
3561 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003562
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003563 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003564 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003565 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003566 r.configChangeFlags);
3567 } catch (Exception e) {
3568 // We can just ignore exceptions here... if the process
3569 // has crashed, our death notification will clean things
3570 // up.
3571 //Slog.w(TAG, "Exception thrown during finish", e);
3572 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003573 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 removedFromHistory = true;
3575 skipDestroy = true;
3576 }
3577 }
3578
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003579 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003580
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003581 // If the activity is finishing, we need to wait on removing it
3582 // from the list to give it a chance to do its cleanup. During
3583 // that time it may make calls back with its token so we need to
3584 // be able to find it on the list and so we don't want to remove
3585 // it from the list yet. Otherwise, we can just immediately put
3586 // it in the destroyed state since we are not removing it from the
3587 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003589 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003590 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003591 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003592 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003593 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3594 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003595 if (DEBUG_STATES) Slog.v(TAG_STATES,
3596 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003597 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003598 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003599 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003600 }
3601 } else {
3602 // remove this record from the history.
3603 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003604 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003605 removedFromHistory = true;
3606 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003607 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003609 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003610 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003611 }
3612 }
3613
3614 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003615
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003616 if (!mLRUActivities.remove(r) && hadApp) {
3617 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3618 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003619
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003620 return removedFromHistory;
3621 }
3622
Craig Mautner299f9602015-01-26 09:47:33 -08003623 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003624 final long origId = Binder.clearCallingIdentity();
3625 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003626 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003627 if (r != null) {
3628 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003629 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003630 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003631
Wale Ogunwale60454db2015-01-23 16:05:07 -08003632 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003633 if (r.state == ActivityState.DESTROYING) {
3634 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003635 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003636 }
3637 }
Craig Mautner05d29032013-05-03 13:40:13 -07003638 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003639 } finally {
3640 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 }
3642 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003643
Todd Kennedyaab56db2015-01-30 09:39:53 -08003644 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003645 if (hasVisibleBehindActivity() &&
3646 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003647 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003648 // Don't release the top activity if it has requested to run behind the next
3649 // activity.
3650 return;
3651 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003652 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003653 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003654 " thread=" + r.app.thread);
3655 if (r != null && r.app != null && r.app.thread != null) {
3656 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003657 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003658 } catch (RemoteException e) {
3659 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003660 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003661 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003662 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003663 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003664 }
3665 }
3666 }
3667
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003668 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003669 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3670 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003671 if (r != null) {
3672 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003673 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003674 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003675 }
3676 mStackSupervisor.resumeTopActivitiesLocked();
3677 }
3678
Jose Lima4b6c6692014-08-12 17:41:12 -07003679 boolean hasVisibleBehindActivity() {
3680 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003681 }
3682
Jose Lima4b6c6692014-08-12 17:41:12 -07003683 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003684 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003685 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003686 }
3687 }
3688
Jose Lima4b6c6692014-08-12 17:41:12 -07003689 ActivityRecord getVisibleBehindActivity() {
3690 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003691 }
3692
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003693 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3694 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003695 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003696 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3697 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003698 while (i > 0) {
3699 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003700 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003701 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003703 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003704 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003705 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003706 }
3707 }
3708 }
3709
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003710 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3711 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003712 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3713 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003714 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3715 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003716 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003717 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003718 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3719 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003720
3721 boolean hasVisibleActivities = false;
3722
3723 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003724 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003725 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3726 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003727 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3728 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3729 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3730 final ActivityRecord r = activities.get(activityNdx);
3731 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003732 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3733 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003734 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003735 if (r.visible) {
3736 hasVisibleActivities = true;
3737 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003738 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003739 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3740 // Don't currently have state for the activity, or
3741 // it is finishing -- always remove it.
3742 remove = true;
3743 } else if (r.launchCount > 2 &&
3744 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3745 // We have launched this activity too many times since it was
3746 // able to run, so give up and remove it.
3747 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;
3776 r.nowVisible = false;
3777 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003778 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003779 "App died, clearing saved state of " + r);
3780 r.icicle = null;
3781 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003782 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003783 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003784 if (remove) {
3785 removeActivityFromHistoryLocked(r, "appDied");
3786 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003787 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003788 }
3789 }
3790
3791 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003792 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003793
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003794 final void updateTransitLocked(int transit, Bundle options) {
3795 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003796 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003797 if (r != null && r.state != ActivityState.RESUMED) {
3798 r.updateOptionsLocked(options);
3799 } else {
3800 ActivityOptions.abort(options);
3801 }
3802 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003803 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003804 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003805
Craig Mautner21d24a22014-04-23 11:45:37 -07003806 void updateTaskMovement(TaskRecord task, boolean toFront) {
3807 if (task.isPersistable) {
3808 task.mLastTimeMoved = System.currentTimeMillis();
3809 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3810 // recently will be most negative, tasks sent to the bottom before that will be less
3811 // negative. Similarly for recent tasks moved to the top which will be most positive.
3812 if (!toFront) {
3813 task.mLastTimeMoved *= -1;
3814 }
3815 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003816 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003817 }
3818
Craig Mautner84984fa2014-06-19 11:19:20 -07003819 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003820 final int top = mTaskHistory.size() - 1;
3821 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3822 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003823 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003824 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3825 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003826 mTaskHistory.remove(taskNdx);
3827 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003828 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003829 return;
3830 }
3831 }
3832 }
3833
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003834 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003835 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003836 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003837
Craig Mautner11bf9a52013-02-19 14:08:51 -08003838 final int numTasks = mTaskHistory.size();
3839 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003840 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003841 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003842 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003843 ActivityOptions.abort(options);
3844 } else {
3845 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3846 }
3847 return;
3848 }
3849
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003850 if (timeTracker != null) {
3851 // The caller wants a time tracker associated with this task.
3852 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3853 tr.mActivities.get(i).appTimeTracker = timeTracker;
3854 }
3855 }
3856
Craig Mautner11bf9a52013-02-19 14:08:51 -08003857 // Shift all activities with this task up to the top
3858 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003859 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003860
Chong Zhang45c25ce2015-08-10 22:18:26 -07003861 // Don't refocus if invisible to current user
3862 ActivityRecord top = tr.getTopActivity();
3863 if (!okToShowLocked(top)) {
3864 addRecentActivityLocked(top);
3865 ActivityOptions.abort(options);
3866 return;
3867 }
3868
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003869 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003870 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003871 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003872
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003873 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003874 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003875 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003876 if (r != null) {
3877 mNoAnimActivities.add(r);
3878 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003879 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003881 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003882 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003883
Craig Mautner05d29032013-05-03 13:40:13 -07003884 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003885 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003886
3887 if (VALIDATE_TOKENS) {
3888 validateAppTokensLocked();
3889 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003890 }
3891
3892 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003893 * Worker method for rearranging history stack. Implements the function of moving all
3894 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003895 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003896 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003897 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3898 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003899 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003900 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003901 * @return Returns true if the move completed, false if not.
3902 */
Craig Mautner299f9602015-01-26 09:47:33 -08003903 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003904 final TaskRecord tr = taskForIdLocked(taskId);
3905 if (tr == null) {
3906 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3907 return false;
3908 }
3909
3910 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003911 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003912
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 // If we have a watcher, preflight the move before committing to it. First check
3914 // for *other* available tasks, but if none are available, then try again allowing the
3915 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003916 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003917 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003918 if (next == null) {
3919 next = topRunningActivityLocked(null, 0);
3920 }
3921 if (next != null) {
3922 // ask watcher if this is allowed
3923 boolean moveOK = true;
3924 try {
3925 moveOK = mService.mController.activityResuming(next.packageName);
3926 } catch (RemoteException e) {
3927 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003928 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003929 }
3930 if (!moveOK) {
3931 return false;
3932 }
3933 }
3934 }
3935
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003936 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003937
riddle_hsuc215a4f2014-12-27 12:10:45 +08003938 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003939
3940 // If true, we should resume the home activity next if the task we are moving to the
3941 // back is over the home stack. We force to false if the task we are moving to back
3942 // is the home task and we don't want it resumed after moving to the back.
3943 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3944 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003945 final TaskRecord nextTask = getNextTask(tr);
3946 if (nextTask != null) {
3947 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3948 } else {
3949 prevIsHome = true;
3950 }
3951 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003952 mTaskHistory.remove(tr);
3953 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003954 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003955
Craig Mautnerc8143c62013-09-03 12:15:57 -07003956 // There is an assumption that moving a task to the back moves it behind the home activity.
3957 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003958 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003959 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3960 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003961 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003962 break;
3963 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003964 if (taskNdx == 1) {
3965 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003966 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003967 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003968 }
3969
Craig Mautner299f9602015-01-26 09:47:33 -08003970 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003971 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003972
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003973 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003974 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003975 }
3976
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003977 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003978 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003979 if (!mService.mBooting && !mService.mBooted) {
3980 // Not ready yet!
3981 return false;
3982 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003983 final int taskToReturnTo = tr.getTaskToReturnTo();
3984 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003985 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003986 }
3987
Craig Mautner05d29032013-05-03 13:40:13 -07003988 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003989 return true;
3990 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003991
Craig Mautner8849a5e2013-04-02 16:41:03 -07003992 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003993 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003994 final Uri data = r.intent.getData();
3995 final String strData = data != null ? data.toSafeString() : null;
3996
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003997 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003998 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003999 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004000 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004001 }
4002
4003 /**
4004 * Make sure the given activity matches the current configuration. Returns
4005 * false if the activity had to be destroyed. Returns true if the
4006 * configuration is the same, or the activity will remain running as-is
4007 * for whatever reason. Ensures the HistoryRecord is updated with the
4008 * correct configuration and all other bookkeeping is handled.
4009 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004010 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4011 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004012 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004013 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004014 "Skipping config check (will change): " + r);
4015 return true;
4016 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004017
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004018 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004019 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004021 // Short circuit: if the two configurations are the exact same
4022 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004023 final Configuration newConfig = mService.mConfiguration;
4024 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004025 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004026 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004027 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004028 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004029 "Configuration unchanged in " + r);
4030 return true;
4031 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004032
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004033 // We don't worry about activities that are finishing.
4034 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004035 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004036 "Configuration doesn't matter in finishing " + r);
4037 r.stopFreezingScreenLocked(false);
4038 return true;
4039 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004041 // Okay we now are going to make this activity have the new config.
4042 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004043 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004044 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004045 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004046 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004047
Filip Gruszczynski23493322015-07-29 17:02:59 -07004048 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004049 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004050 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004051 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004052 "Configuration no differences in " + r);
4053 return true;
4054 }
4055
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004056 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4057 "Configuration changes for " + r + " ; taskChanges="
4058 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4059 + Configuration.configurationDiffToString(changes));
4060
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004061 // If the activity isn't currently running, just leave the new
4062 // configuration and it will pick that up next time it starts.
4063 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004064 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 "Configuration doesn't matter not running " + r);
4066 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004067 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004068 return true;
4069 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004070
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004071 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004072 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4073 "Checking to restart " + r.info.name + ": changed=0x"
4074 + Integer.toHexString(changes) + ", handles=0x"
4075 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4076
Dianne Hackborne6676352011-06-01 16:51:20 -07004077 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004078 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4079 r.configChangeFlags |= changes;
4080 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004081 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004082 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004083 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004084 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004085 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004086 } else if (r.state == ActivityState.PAUSING) {
4087 // A little annoying: we are waiting for this activity to
4088 // finish pausing. Let's not do anything now, but just
4089 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004090 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004091 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004092 r.configDestroy = true;
4093 return true;
4094 } else if (r.state == ActivityState.RESUMED) {
4095 // Try to optimize this case: the configuration is changing
4096 // and we need to restart the top, resumed activity.
4097 // Instead of doing the normal handshaking, just say
4098 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004099 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004100 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004101 relaunchActivityLocked(r, r.configChangeFlags, true,
4102 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004103 r.configChangeFlags = 0;
4104 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004105 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004106 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004107 relaunchActivityLocked(r, r.configChangeFlags, false,
4108 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004109 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004110 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004111
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004112 // All done... tell the caller we weren't able to keep this
4113 // activity around.
4114 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004115 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004116
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004117 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004118 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004119 // changes is always sent to all processes when they happen so it can just use whatever
4120 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004121 if (r.app != null && r.app.thread != null) {
4122 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004123 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004124 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004125 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004126 } catch (RemoteException e) {
4127 // If process died, whatever.
4128 }
4129 }
4130 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004132 return true;
4133 }
4134
Filip Gruszczynski23493322015-07-29 17:02:59 -07004135 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4136 Configuration oldTaskOverride) {
4137 // Determine what has changed. May be nothing, if this is a config
4138 // that has come back from the app after going idle. In that case
4139 // we just want to leave the official config object now in the
4140 // activity and do nothing else.
4141 int taskChanges = oldTaskOverride.diff(taskConfig);
4142 // We don't want to use size changes if they don't cross boundaries that are important to
4143 // the app.
4144 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4145 final boolean crosses = record.crossesHorizontalSizeThreshold(
4146 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4147 || record.crossesVerticalSizeThreshold(
4148 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4149 if (!crosses) {
4150 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4151 }
4152 }
4153 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4154 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4155 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004156 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004157 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4158 }
4159 }
4160 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4161 }
4162
4163 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4164 Configuration oldTaskOverride, int taskChanges) {
4165 if (taskChanges == 0) {
4166 // {@link Configuration#diff} doesn't catch changes from unset values.
4167 // Check for changes we care about.
4168 if (oldTaskOverride.orientation != taskConfig.orientation) {
4169 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4170 }
4171 // We want to explicitly track situations where the size configuration goes from
4172 // undefined to defined. We don't care about crossing the threshold in that case,
4173 // because there is no threshold.
4174 final int oldHeight = oldTaskOverride.screenHeightDp;
4175 final int newHeight = taskConfig.screenHeightDp;
4176 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4177 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4178 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4179 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4180 }
4181 final int oldWidth = oldTaskOverride.screenWidthDp;
4182 final int newWidth = taskConfig.screenWidthDp;
4183 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4184 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4185 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4186 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4187 }
4188 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4189 final int newSmallest = taskConfig.smallestScreenWidthDp;
4190 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4191 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4192 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4193 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4194 }
4195 }
4196 return taskChanges;
4197 }
4198
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004199 private static boolean isResizeOnlyChange(int change) {
4200 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4201 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4202 }
4203
Wale Ogunwale83301a92015-09-24 15:54:08 -07004204 private void relaunchActivityLocked(
4205 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4206 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4207 return;
4208 }
4209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004211 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004212 if (andResume) {
4213 results = r.results;
4214 newIntents = r.newIntents;
4215 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004216 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4217 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004218 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004219 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004220 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004221 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004222
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004223 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004224
Craig Mautner34b73df2014-01-12 21:11:08 -08004225 mStackSupervisor.removeChildActivityContainers(r);
4226
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004227 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004228 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4229 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004230 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004231 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4232 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004233 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004234 // Note: don't need to call pauseIfSleepingLocked() here, because
4235 // the caller will only pass in 'andResume' if this activity is
4236 // currently resumed, which implies we aren't sleeping.
4237 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004238 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004239 }
4240
4241 if (andResume) {
4242 r.results = null;
4243 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004244 r.state = ActivityState.RESUMED;
4245 } else {
4246 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4247 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004248 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004249 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004250
4251 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004252 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4253 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4254 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4255 final ActivityRecord r = activities.get(activityNdx);
4256 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004257 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004258 }
4259 if (r.fullscreen && !r.finishing) {
4260 return false;
4261 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004262 }
4263 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004264 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004265 if (r == null) {
4266 return false;
4267 }
4268 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4269 + " would have returned true for r=" + r);
4270 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004271 }
4272
4273 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004274 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4275 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4276 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4277 final ActivityRecord r = activities.get(activityNdx);
4278 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004279 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004280 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004281 }
4282 }
4283 }
4284
Wale Ogunwale540e1232015-05-01 15:35:39 -07004285 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4286 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004287 boolean didSomething = false;
4288 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004289 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004290 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4291 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4292 int numActivities = activities.size();
4293 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4294 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004295 final boolean sameComponent =
4296 (r.packageName.equals(packageName) && (filterByClasses == null
4297 || filterByClasses.contains(r.realActivity.getClassName())))
4298 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004299 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004300 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004301 && (r.app == null || evenPersistent || !r.app.persistent)) {
4302 if (!doit) {
4303 if (r.finishing) {
4304 // If this activity is just finishing, then it is not
4305 // interesting as far as something to stop.
4306 continue;
4307 }
4308 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004309 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004310 if (r.isHomeActivity()) {
4311 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4312 Slog.i(TAG, "Skip force-stop again " + r);
4313 continue;
4314 } else {
4315 homeActivity = r.realActivity;
4316 }
4317 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004318 didSomething = true;
4319 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004320 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004321 if (r.app != null) {
4322 r.app.removed = true;
4323 }
4324 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004325 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004326 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004327 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4328 true)) {
4329 // r has been deleted from mActivities, accommodate.
4330 --numActivities;
4331 --activityNdx;
4332 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004333 }
4334 }
4335 }
4336 return didSomething;
4337 }
4338
Dianne Hackborn09233282014-04-30 11:33:59 -07004339 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004340 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4341 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004342 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004343 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004344 if (task.getTopActivity() == null) {
4345 continue;
4346 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004347 ActivityRecord r = null;
4348 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004349 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004350 int numActivities = 0;
4351 int numRunning = 0;
4352 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004353 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004354 continue;
4355 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004356 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004357 tmp = activities.get(activityNdx);
4358 if (tmp.finishing) {
4359 continue;
4360 }
4361 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004362
Craig Mautneraab647e2013-02-28 16:31:36 -08004363 // Initialize state for next task if needed.
4364 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4365 top = r;
4366 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004367 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004368
4369 // Add 'r' into the current task.
4370 numActivities++;
4371 if (r.app != null && r.app.thread != null) {
4372 numRunning++;
4373 }
4374
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004375 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004376 TAG, r.intent.getComponent().flattenToShortString()
4377 + ": task=" + r.task);
4378 }
4379
4380 RunningTaskInfo ci = new RunningTaskInfo();
4381 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004382 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004383 ci.baseActivity = r.intent.getComponent();
4384 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004385 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004386 if (focusedStack && topTask) {
4387 // Give the latest time to ensure foreground task can be sorted
4388 // at the first, because lastActiveTime of creating task is 0.
4389 ci.lastActiveTime = System.currentTimeMillis();
4390 topTask = false;
4391 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004392
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004393 if (top.task != null) {
4394 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004395 }
4396 ci.numActivities = numActivities;
4397 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004398 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004399 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004400 }
4401
4402 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004403 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004404 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004405 if (top >= 0) {
4406 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4407 int activityTop = activities.size() - 1;
4408 if (activityTop > 0) {
4409 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4410 "unhandled-back", true);
4411 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004412 }
4413 }
4414
Craig Mautner6b74cb52013-09-27 17:02:21 -07004415 /**
4416 * Reset local parameters because an app's activity died.
4417 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004418 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004419 */
4420 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004421 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004422 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004423 "App died while pausing: " + mPausingActivity);
4424 mPausingActivity = null;
4425 }
4426 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4427 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004428 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004429 }
4430
Craig Mautner19091252013-10-05 00:03:53 -07004431 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004432 }
4433
Craig Mautnercae015f2013-02-08 14:31:27 -08004434 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004435 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4436 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4437 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4438 final ActivityRecord r = activities.get(activityNdx);
4439 if (r.app == app) {
4440 Slog.w(TAG, " Force finishing activity "
4441 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004442 // Force the destroy to skip right to removal.
4443 r.app = null;
4444 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004445 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004446 }
4447 }
4448 }
4449
Dianne Hackborn390517b2013-05-30 15:03:32 -07004450 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004451 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004452 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004453 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4454 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004455 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4456 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004457 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004458 " Task id #" + task.taskId + "\n" +
4459 " mFullscreen=" + task.mFullscreen + "\n" +
4460 " mBounds=" + task.mBounds + "\n" +
4461 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004462 if (printed) {
4463 header = null;
4464 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004465 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004466 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004467 }
4468
4469 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4470 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4471
4472 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004473 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4474 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004475 }
4476 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004477 final int top = mTaskHistory.size() - 1;
4478 if (top >= 0) {
4479 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4480 int listTop = list.size() - 1;
4481 if (listTop >= 0) {
4482 activities.add(list.get(listTop));
4483 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004484 }
4485 } else {
4486 ItemMatcher matcher = new ItemMatcher();
4487 matcher.build(name);
4488
Craig Mautneraab647e2013-02-28 16:31:36 -08004489 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4490 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4491 if (matcher.match(r1, r1.intent.getComponent())) {
4492 activities.add(r1);
4493 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004494 }
4495 }
4496 }
4497
4498 return activities;
4499 }
4500
4501 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004502 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004503
4504 // All activities that came from the package must be
4505 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004506 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4507 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4508 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4509 final ActivityRecord a = activities.get(activityNdx);
4510 if (a.info.packageName.equals(packageName)) {
4511 a.forceNewConfig = true;
4512 if (starting != null && a == starting && a.visible) {
4513 a.startFreezingScreenLocked(starting.app,
4514 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4515 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004516 }
4517 }
4518 }
4519
4520 return starting;
4521 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004522
Craig Mautner299f9602015-01-26 09:47:33 -08004523 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004524 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004525 }
4526
Wale Ogunwale000957c2015-04-03 08:19:12 -07004527 /**
4528 * Removes the input task from this stack.
4529 * @param task to remove.
4530 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004531 * @param moving task to another stack. In the case we are moving we don't want to perform
4532 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004533 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004534 void removeTask(TaskRecord task, String reason, boolean moving) {
4535 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004536 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004537 mWindowManager.removeTask(task.taskId);
4538 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004539
Craig Mautner04a0ea62014-01-13 12:51:26 -08004540 final ActivityRecord r = mResumedActivity;
4541 if (r != null && r.task == task) {
4542 mResumedActivity = null;
4543 }
4544
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004545 final int taskNdx = mTaskHistory.indexOf(task);
4546 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004547 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4548 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4549 if (!nextTask.isOverHomeStack()) {
4550 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4551 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004552 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004553 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004554 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004555
Wale Ogunwale040b4702015-08-06 18:10:50 -07004556 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004557 final boolean isVoiceSession = task.voiceSession != null;
4558 if (isVoiceSession) {
4559 try {
4560 task.voiceSession.taskFinished(task.intent, task.taskId);
4561 } catch (RemoteException e) {
4562 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004563 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004564 if (task.autoRemoveFromRecents() || isVoiceSession) {
4565 // Task creator asked to remove this when done, or this task was a voice
4566 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004567 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004568 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004569 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004570 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004571
4572 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004573 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004574 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004575 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004576 String myReason = reason + " leftTaskHistoryEmpty";
4577 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004578 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004579 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004580 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004581 if (mStacks != null) {
4582 mStacks.remove(this);
4583 mStacks.add(0, this);
4584 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004585 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004586 mActivityContainer.onTaskListEmptyLocked();
4587 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004588 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004589
4590 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004591 }
4592
Dianne Hackborn91097de2014-04-04 18:02:06 -07004593 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4594 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4595 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004596 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4597 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004598 // add the task to stack first, mTaskPositioner might need the stack association
4599 addTask(task, toTop, false);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004600 if (mTaskPositioner != null) {
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004601 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, info.layout);
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004602 } else if (mBounds != null && task.mResizeable) {
4603 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004604 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004605 return task;
4606 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004607
4608 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004609 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004610 }
4611
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004612 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004613 task.stack = this;
4614 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004615 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004616 } else {
4617 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004618 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004619 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004620 if (!moving && task.voiceSession != null) {
4621 try {
4622 task.voiceSession.taskStarted(task.intent, task.taskId);
4623 } catch (RemoteException e) {
4624 }
4625 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004626 }
4627
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004628 void positionTask(final TaskRecord task, int position, boolean moving) {
4629 task.stack = this;
4630 insertTaskAtPosition(task, position);
4631 if (!moving && task.voiceSession != null) {
4632 try {
4633 task.voiceSession.taskStarted(task.intent, task.taskId);
4634 } catch (RemoteException e) {
4635 }
4636 }
4637 }
4638
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004639 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004640 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004641 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004642 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4643 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4644 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4645 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004646 bounds, task.mOverrideConfig, !r.isHomeActivity());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004647 r.taskConfigOverride = task.mOverrideConfig;
4648 }
4649
Wale Ogunwale079a0042015-10-24 11:44:07 -07004650 void setFocusAndResumeStateIfNeeded(
4651 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4652 // If the activity had focus before move focus to this stack.
4653 if (setFocus) {
4654 // If the activity owns the last resumed activity, transfer that together,
4655 // so that we don't resume the same activity again in the new stack.
4656 // Apps may depend on onResume()/onPause() being called in pairs.
4657 if (setResume) {
4658 mResumedActivity = r;
4659 // Move the stack in which we are placing the activity to the front. We don't use
4660 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4661 // already focused, the call will short-circuit and do nothing.
4662 moveToFront(reason);
4663 } else {
4664 // We need to not only move the stack to the front, but also have the activity
4665 // focused. This will achieve both goals.
4666 mService.setFocusedActivityLocked(r, reason);
4667 }
4668 }
4669 }
4670
4671 /**
4672 * Moves the input activity from its current stack to this one.
4673 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4674 * created on this stack which the activity is added to.
4675 * */
4676 void moveActivityToStack(ActivityRecord r) {
4677 final ActivityStack prevStack = r.task.stack;
4678 if (prevStack.mStackId == mStackId) {
4679 // You are already in the right stack silly...
4680 return;
4681 }
4682
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004683 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004684 && (mStackSupervisor.topRunningActivityLocked() == r);
4685 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4686
4687 final TaskRecord task = createTaskRecord(
4688 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4689 r.setTask(task, null);
4690 task.addActivityToTop(r);
4691 setAppTask(r, task);
4692 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4693 }
4694
Wale Ogunwale706ed792015-08-02 10:29:44 -07004695 private void setAppTask(ActivityRecord r, TaskRecord task) {
4696 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004697 task.updateOverrideConfiguration(bounds);
4698 mWindowManager.setAppTask(
4699 r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004700 r.taskConfigOverride = task.mOverrideConfig;
4701 }
4702
Craig Mautnerc00204b2013-03-05 15:02:14 -08004703 public int getStackId() {
4704 return mStackId;
4705 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004706
4707 @Override
4708 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004709 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4710 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004711 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004712
Craig Mautner15df08a2015-04-01 12:17:18 -07004713 void onLockTaskPackagesUpdatedLocked() {
4714 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4715 mTaskHistory.get(taskNdx).setLockTaskAuth();
4716 }
4717 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004718}