blob: 4bb265deacedc50c19805b1a35c48ef6ace90cc0 [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 Ogunwalee23149f2015-03-06 15:39:44 -080019import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070020import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
21import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
22import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
23import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
24import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
25import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
26import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
27import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070031
Craig Mautner0eea92c2013-05-16 13:35:39 -070032import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070034import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070035import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070037import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070039import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
40
Dianne Hackborn89ad4562014-08-24 16:45:38 -070041import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070042import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080043import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070045import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080046import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080047import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080048import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080049import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070050import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051
52import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070053import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070054import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080056import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080058import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080064import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080065import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070067import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080068import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Handler;
70import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090071import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070073import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.os.RemoteException;
75import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080076import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070077import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070078import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070081import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082
Craig Mautnercae015f2013-02-08 14:31:27 -080083import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080084import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085import java.lang.ref.WeakReference;
86import java.util.ArrayList;
87import java.util.Iterator;
88import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080089import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070090
91/**
92 * State and management of a single stack of activities.
93 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070094final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080095
Wale Ogunwalee23149f2015-03-06 15:39:44 -080096 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
97 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070098 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
99
100 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800101
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700102 // Ticks during which we check progress while waiting for an app to launch.
103 static final int LAUNCH_TICK = 500;
104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105 // How long we wait until giving up on the last activity to pause. This
106 // is short because it directly impacts the responsiveness of starting the
107 // next activity.
108 static final int PAUSE_TIMEOUT = 500;
109
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700110 // How long we wait for the activity to tell us it has stopped before
111 // giving up. This is a good amount of time because we really need this
112 // from the application in order to get its saved state.
113 static final int STOP_TIMEOUT = 10*1000;
114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700115 // How long we wait until giving up on an activity telling us it has
116 // finished destroying itself.
117 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800120 // disabled.
121 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800122
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700123 // How long between activity launches that we consider safe to not warn
124 // the user about an unexpected activity being launched on top.
125 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 // Set to false to disable the preview that is shown while a new activity
128 // is being started.
129 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800130
Craig Mautner5eda9b32013-07-02 11:58:16 -0700131 // How long to wait for all background Activities to redraw following a call to
132 // convertToTranslucent().
133 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
134
Craig Mautnera61bc652013-10-28 15:43:18 -0700135 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700136
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700137 enum ActivityState {
138 INITIALIZING,
139 RESUMED,
140 PAUSING,
141 PAUSED,
142 STOPPING,
143 STOPPED,
144 FINISHING,
145 DESTROYING,
146 DESTROYED
147 }
148
149 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700150 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800151 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700153 /**
154 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800155 * running) activities. It contains #TaskRecord objects.
156 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800157 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800158
159 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800160 * Used for validating app tokens with window manager.
161 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800162 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 /**
165 * List of running activities, sorted by recent usage.
166 * The first entry in the list is the least recently used.
167 * It contains HistoryRecord objects.
168 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800169 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170
171 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172 * Animations that for the current transition have requested not to
173 * be considered for the transition animation.
174 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800175 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700176
177 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700178 * When we are in the process of pausing an activity, before starting the
179 * next one, this variable holds the activity that is currently being paused.
180 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800181 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700182
183 /**
184 * This is the last activity that we put into the paused state. This is
185 * used to determine if we need to do an activity transition while sleeping,
186 * when we normally hold the top activity paused.
187 */
188 ActivityRecord mLastPausedActivity = null;
189
190 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700191 * Activities that specify No History must be removed once the user navigates away from them.
192 * If the device goes to sleep with such an activity in the paused state then we save it here
193 * and finish it later if another activity replaces it on wakeup.
194 */
195 ActivityRecord mLastNoHistoryActivity = null;
196
197 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198 * Current activity that is resumed, or null if there is none.
199 */
200 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700203 * This is the last activity that has been started. It is only used to
204 * identify when multiple activities are started at once so that the user
205 * can be warned they may not be in the activity they think they are.
206 */
207 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800208
Craig Mautner5eda9b32013-07-02 11:58:16 -0700209 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
210 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
211 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
212 // Activity in mTranslucentActivityWaiting is notified via
213 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
214 // background activity being drawn then the same call will be made with a true value.
215 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700216 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700217 new ArrayList<ActivityRecord>();
218
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700219 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700220 * Set when we know we are going to be calling updateConfiguration()
221 * soon, so want to skip intermediate config checks.
222 */
223 boolean mConfigWillChange;
224
Todd Kennedyaab56db2015-01-30 09:39:53 -0800225 // Whether or not this stack covers the entire screen; by default stacks are full screen
226 boolean mFullscreen = true;
227
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700228 long mLaunchStartTime = 0;
229 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800230
Craig Mautner858d8a62013-04-23 17:08:34 -0700231 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700232
Craig Mautnerc00204b2013-03-05 15:02:14 -0800233 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800234 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800235 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
236 ArrayList<ActivityStack> mStacks;
237 /** The attached Display's unique identifier, or -1 if detached */
238 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800239
Craig Mautner27084302013-03-25 08:05:25 -0700240 /** Run all ActivityStacks through this */
241 final ActivityStackSupervisor mStackSupervisor;
242
Wale Ogunwale60454db2015-01-23 16:05:07 -0800243 Configuration mOverrideConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800244 /** True if the stack was forced to full screen because {@link TaskRecord#mResizeable} is false
245 * and the stack was previously resized. */
246 private boolean mForcedFullscreen = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800247
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700248 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700249 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
250 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
251 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
252 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700253 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700254 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700255 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700256
257 static class ScheduleDestroyArgs {
258 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700259 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700260 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700261 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700262 mReason = reason;
263 }
264 }
265
Zoran Marcetaf958b322012-08-09 20:27:12 +0900266 final Handler mHandler;
267
268 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800269
Craig Mautnerc8143c62013-09-03 12:15:57 -0700270 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900271 super(looper);
272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 public void handleMessage(Message msg) {
276 switch (msg.what) {
277 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800278 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700279 // We don't at this point know if the activity is fullscreen,
280 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800281 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700282 synchronized (mService) {
283 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700284 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700286 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700289 case LAUNCH_TICK_MSG: {
290 ActivityRecord r = (ActivityRecord)msg.obj;
291 synchronized (mService) {
292 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700293 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 }
295 }
296 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800298 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700299 // We don't at this point know if the activity is fullscreen,
300 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800301 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800302 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800303 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800304 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700306 case STOP_TIMEOUT_MSG: {
307 ActivityRecord r = (ActivityRecord)msg.obj;
308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
310 Slog.w(TAG, "Activity stop timeout for " + r);
311 synchronized (mService) {
312 if (r.isInHistory()) {
313 activityStoppedLocked(r, null, null, null);
314 }
315 }
316 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700317 case DESTROY_ACTIVITIES_MSG: {
318 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
319 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700320 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700321 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700322 } break;
323 case TRANSLUCENT_TIMEOUT_MSG: {
324 synchronized (mService) {
325 notifyActivityDrawnLocked(null);
326 }
327 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700328 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700329 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700330 final ActivityRecord r = getVisibleBehindActivity();
331 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700332 if (r != null) {
333 mService.killAppAtUsersRequest(r.app, null);
334 }
335 }
336 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700337 }
338 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800339 }
340
Craig Mautner34b73df2014-01-12 21:11:08 -0800341 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800342 int count = 0;
343 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
344 count += mTaskHistory.get(taskNdx).mActivities.size();
345 }
346 return count;
347 }
348
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800349 int numTasks() {
350 return mTaskHistory.size();
351 }
352
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800353 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
354 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800355 mActivityContainer = activityContainer;
356 mStackSupervisor = activityContainer.getOuter();
357 mService = mStackSupervisor.mService;
358 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
359 mWindowManager = mService.mWindowManager;
360 mStackId = activityContainer.mStackId;
361 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800362 mRecentTasks = recentTasks;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800363 mOverrideConfig = Configuration.EMPTY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700364 }
Craig Mautner5962b122012-10-05 14:45:52 -0700365
Amith Yamasani734983f2014-03-04 16:48:05 -0800366 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100367 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800368 */
Kenny Guy2a764942014-04-02 13:29:20 +0100369 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100370 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100371 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
372 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800373 }
374 return false;
375 }
376
377 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100378 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700379 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
380 }
381
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700382 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700384 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
385 if (r != null) {
386 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800387 }
388 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700389 return null;
390 }
391
392 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
394 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800395 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800396 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
397 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800398 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800399 return r;
400 }
401 }
402 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700403 return null;
404 }
405
406 /**
407 * This is a simplified version of topRunningActivityLocked that provides a number of
408 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800409 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700410 * @param token If non-null, any history records matching this token will be skipped.
411 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800412 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700413 * @return Returns the HistoryRecord of the next activity on the stack.
414 */
415 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800416 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
417 TaskRecord task = mTaskHistory.get(taskNdx);
418 if (task.taskId == taskId) {
419 continue;
420 }
421 ArrayList<ActivityRecord> activities = task.mActivities;
422 for (int i = activities.size() - 1; i >= 0; --i) {
423 final ActivityRecord r = activities.get(i);
424 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800425 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800426 return r;
427 }
428 }
429 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700430 return null;
431 }
432
Craig Mautner8849a5e2013-04-02 16:41:03 -0700433 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700434 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
435 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700436 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
437 final ActivityRecord r = activities.get(activityNdx);
438 if (!r.finishing) {
439 return r;
440 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700441 }
442 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700443 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700444 }
445
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700446 final TaskRecord topTask() {
447 final int size = mTaskHistory.size();
448 if (size > 0) {
449 return mTaskHistory.get(size - 1);
450 }
451 return null;
452 }
453
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 TaskRecord taskForIdLocked(int id) {
455 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
456 final TaskRecord task = mTaskHistory.get(taskNdx);
457 if (task.taskId == id) {
458 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800459 }
460 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700461 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 }
463
Craig Mautnerd2328952013-03-05 12:46:26 -0800464 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700465 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800466 return isInStackLocked(r);
467 }
468
469 ActivityRecord isInStackLocked(ActivityRecord r) {
470 if (r == null) {
471 return null;
472 }
473 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700474 if (task != null && task.stack != null
475 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800476 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800477 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800478 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800479 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800480 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800481 }
482
Craig Mautner2420ead2013-04-01 17:13:20 -0700483 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700484 final boolean hadit = mLRUActivities.remove(r);
485 mLRUActivities.add(r);
486 return hadit;
487 }
488
Craig Mautnerde4ef022013-04-07 19:01:33 -0700489 final boolean isHomeStack() {
490 return mStackId == HOME_STACK_ID;
491 }
492
Craig Mautnere0a38842013-12-16 16:14:02 -0800493 final boolean isOnHomeDisplay() {
494 return isAttached() &&
495 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
496 }
497
Craig Mautner299f9602015-01-26 09:47:33 -0800498 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800499 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800500 final boolean homeStack = isHomeStack()
501 || (mActivityContainer.mParentActivity != null
502 && mActivityContainer.mParentActivity.isHomeActivity());
503 ActivityStack lastFocusStack = null;
504 if (!homeStack) {
505 // Need to move this stack to the front before calling
506 // {@link ActivityStackSupervisor#moveHomeStack} below.
507 lastFocusStack = mStacks.get(mStacks.size() - 1);
508 mStacks.remove(this);
509 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800510 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700511 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
512 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800513 if (isOnHomeDisplay()) {
514 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
515 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800516 final TaskRecord task = topTask();
517 if (task != null) {
518 mWindowManager.moveTaskToTop(task.taskId);
519 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800520 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800521 }
522
523 final boolean isAttached() {
524 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800525 }
526
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700527 /**
528 * Returns the top activity in any existing task matching the given
529 * Intent. Returns null if no such task is found.
530 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700531 ActivityRecord findTaskLocked(ActivityRecord target) {
532 Intent intent = target.intent;
533 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700534 ComponentName cls = intent.getComponent();
535 if (info.targetActivity != null) {
536 cls = new ComponentName(info.packageName, info.targetActivity);
537 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700538 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700539 boolean isDocument = intent != null & intent.isDocument();
540 // If documentData is non-null then it must match the existing task data.
541 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800542
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700543 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800544 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
545 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700546 if (task.voiceSession != null) {
547 // We never match voice sessions; those always run independently.
548 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
549 continue;
550 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700551 if (task.userId != userId) {
552 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700553 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700554 continue;
555 }
Craig Mautner000f0022013-02-26 15:04:29 -0800556 final ActivityRecord r = task.getTopActivity();
557 if (r == null || r.finishing || r.userId != userId ||
558 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700559 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800560 continue;
561 }
562
Craig Mautnerd00f4742014-03-12 14:17:26 -0700563 final Intent taskIntent = task.intent;
564 final Intent affinityIntent = task.affinityIntent;
565 final boolean taskIsDocument;
566 final Uri taskDocumentData;
567 if (taskIntent != null && taskIntent.isDocument()) {
568 taskIsDocument = true;
569 taskDocumentData = taskIntent.getData();
570 } else if (affinityIntent != null && affinityIntent.isDocument()) {
571 taskIsDocument = true;
572 taskDocumentData = affinityIntent.getData();
573 } else {
574 taskIsDocument = false;
575 taskDocumentData = null;
576 }
577
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700578 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700579 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700580 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700581 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700582 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
583 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700584 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700585 return r;
586 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700587 } else if (taskIntent != null && taskIntent.getComponent() != null &&
588 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700589 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700590 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800591 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700592 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
593 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800594 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700595 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
596 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700597 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700598 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800599 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700600 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
601 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800602 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700603 } else if (DEBUG_TASKS) {
604 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 }
606 }
607
608 return null;
609 }
610
611 /**
612 * Returns the first activity (starting from the top of the stack) that
613 * is the same as the given activity. Returns null if no such activity
614 * is found.
615 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700616 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700617 ComponentName cls = intent.getComponent();
618 if (info.targetActivity != null) {
619 cls = new ComponentName(info.packageName, info.targetActivity);
620 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700621 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700622
Craig Mautner000f0022013-02-26 15:04:29 -0800623 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700624 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100625 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700626 return null;
627 }
628 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800629 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
630 ActivityRecord r = activities.get(activityNdx);
631 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700632 //Slog.i(TAG, "Found matching class!");
633 //dump();
634 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
635 return r;
636 }
637 }
638 }
639
640 return null;
641 }
642
Amith Yamasani742a6712011-05-04 14:49:28 -0700643 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700644 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700645 */
Craig Mautner93529a42013-10-04 15:03:13 -0700646 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800647 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700648 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800649 }
650 mCurrentUser = userId;
651
652 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800653 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800654 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700655 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100656 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700657 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
658 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800659 mTaskHistory.remove(i);
660 mTaskHistory.add(task);
661 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800662 // Use same value for i.
663 } else {
664 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800665 }
666 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700667 if (VALIDATE_TOKENS) {
668 validateAppTokensLocked();
669 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700670 }
671
Craig Mautner2420ead2013-04-01 17:13:20 -0700672 void minimalResumeActivityLocked(ActivityRecord r) {
673 r.state = ActivityState.RESUMED;
674 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
675 + " (starting new instance)");
676 r.stopped = false;
677 mResumedActivity = r;
678 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800679 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700680 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700681 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700682 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700683 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700684 }
685
Dianne Hackborncee04b52013-07-03 17:01:28 -0700686 private void startLaunchTraces() {
687 if (mFullyDrawnStartTime != 0) {
688 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
689 }
690 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
691 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
692 }
693
694 private void stopFullyDrawnTraceIfNeeded() {
695 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
696 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
697 mFullyDrawnStartTime = 0;
698 }
699 }
700
Craig Mautnere79d42682013-04-01 19:01:53 -0700701 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700702 if (r.displayStartTime == 0) {
703 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
704 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700705 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700706 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700707 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700708 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700709 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700710 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700711 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700712 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800713
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700714 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700715 // Make sure that there is no activity waiting for this to launch.
716 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
717 r.displayStartTime = r.fullyDrawnStartTime = 0;
718 } else {
719 mStackSupervisor.removeTimeoutsForActivityLocked(r);
720 mStackSupervisor.scheduleIdleTimeoutLocked(r);
721 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700722 }
723
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800724 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800725 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800726 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
727 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
728 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
729 activities.get(activityNdx).setSleeping(false);
730 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800731 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800732 if (mPausingActivity != null) {
733 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
734 activityPausedLocked(mPausingActivity.appToken, true);
735 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800736 }
737
Craig Mautner0eea92c2013-05-16 13:35:39 -0700738 /**
739 * @return true if something must be done before going to sleep.
740 */
741 boolean checkReadyForSleepLocked() {
742 if (mResumedActivity != null) {
743 // Still have something resumed; can't sleep until it is paused.
744 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
745 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700746 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700747 return true;
748 }
749 if (mPausingActivity != null) {
750 // Still waiting for something to pause; can't sleep yet.
751 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
752 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800753 }
754
Craig Mautner0eea92c2013-05-16 13:35:39 -0700755 return false;
756 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800757
Craig Mautner0eea92c2013-05-16 13:35:39 -0700758 void goToSleep() {
759 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800760
Craig Mautner0eea92c2013-05-16 13:35:39 -0700761 // Make sure any stopped but visible activities are now sleeping.
762 // This ensures that the activity's onStop() is called.
763 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
764 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
765 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
766 final ActivityRecord r = activities.get(activityNdx);
767 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
768 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800769 }
770 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800771 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700772 }
Craig Mautner59c00972012-07-30 12:10:24 -0700773
Dianne Hackbornd2835932010-12-13 16:28:46 -0800774 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700775 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800776 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700777 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800778 return null;
779 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800780
Winson Chung083baf92014-07-11 10:32:42 -0700781 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700782 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800783 // we can just go ahead and skip taking the screenshot if this is the home stack.
784 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700785 return null;
786 }
787
Winson Chung48a10a52014-08-27 14:36:51 -0700788 int w = mService.mThumbnailWidth;
789 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800790 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800791 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
792 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
793 w, h, SCREENSHOT_FORCE_565);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800794 }
Winson Chung376543b2014-05-21 17:43:28 -0700795 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800796 return null;
797 }
798
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700799 /**
800 * Start pausing the currently resumed activity. It is an error to call this if there
801 * is already an activity being paused or there is no resumed activity.
802 *
803 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
804 * @param uiSleeping True if this is happening with the user interface going to sleep (the
805 * screen turning off).
806 * @param resuming True if this is being called as part of resuming the top activity, so
807 * we shouldn't try to instigate a resume here.
808 * @param dontWait True if the caller does not want to wait for the pause to complete. If
809 * set to true, we will immediately complete the pause here before returning.
810 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
811 * it to tell us when it is done.
812 */
813 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
814 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800815 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700816 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
817 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800818 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 ActivityRecord prev = mResumedActivity;
820 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700821 if (!resuming) {
822 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
823 mStackSupervisor.resumeTopActivitiesLocked();
824 }
825 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800827
828 if (mActivityContainer.mParentActivity == null) {
829 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700830 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800831 }
832
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700833 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
834 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800836 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700838 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
839 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 prev.state = ActivityState.PAUSING;
841 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700842 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700843 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800844 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800845 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700846 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700847 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848
849 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800850
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 if (prev.app != null && prev.app.thread != null) {
852 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
853 try {
854 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700855 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700856 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700857 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800858 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700859 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 } catch (Exception e) {
861 // Ignore exception, if process died other code will cleanup.
862 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800863 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700865 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 }
867 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700870 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700871 }
872
873 // If we are not going to sleep, we want to ensure the device is
874 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700875 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700876 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700877 }
878
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 // Have the window manager pause its key dispatching until the new
881 // activity has started. If we're pausing the activity just because
882 // the screen is being turned off and the UI is sleeping, don't interrupt
883 // key dispatch; the same activity will pick it up again on wakeup.
884 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800885 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886 } else {
887 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
888 }
889
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700890 if (dontWait) {
891 // If the caller said they don't want to wait for the pause, then complete
892 // the pause now.
893 completePauseLocked(false);
894 return false;
895
896 } else {
897 // Schedule a pause timeout in case the app doesn't respond.
898 // We don't give it much time because this directly impacts the
899 // responsiveness seen by the user.
900 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
901 msg.obj = prev;
902 prev.pauseTime = SystemClock.uptimeMillis();
903 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
904 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
905 return true;
906 }
907
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700908 } else {
909 // This activity failed to schedule the
910 // pause, so just treat it as being paused now.
911 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700912 if (!resuming) {
913 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
914 }
915 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916 }
917 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700918
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700919 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800921 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700922
Craig Mautnerd2328952013-03-05 12:46:26 -0800923 final ActivityRecord r = isInStackLocked(token);
924 if (r != null) {
925 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
926 if (mPausingActivity == r) {
927 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
928 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700929 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800930 } else {
931 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
932 r.userId, System.identityHashCode(r), r.shortComponentName,
933 mPausingActivity != null
934 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700935 }
936 }
937 }
938
Craig Mautnera0026042014-04-23 11:45:37 -0700939 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
940 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700941 if (r.state != ActivityState.STOPPING) {
942 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
943 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
944 return;
945 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700946 if (persistentState != null) {
947 r.persistentState = persistentState;
948 mService.notifyTaskPersisterLocked(r.task, false);
949 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700950 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700951 if (icicle != null) {
952 // If icicle is null, this is happening due to a timeout, so we
953 // haven't really saved the state.
954 r.icicle = icicle;
955 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800956 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800957 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700958 }
959 if (!r.stopped) {
960 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
961 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
962 r.stopped = true;
963 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700964 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
965 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700966 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700967 if (r.finishing) {
968 r.clearOptionsLocked();
969 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700970 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700971 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700972 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700973 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700974 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800975 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700976 }
977 }
978 }
979
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700980 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800983
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700985 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800986 if (prev.finishing) {
987 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700988 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 } else if (prev.app != null) {
990 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800991 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800992 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
993 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800994 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800995 if (prev.configDestroy) {
996 // The previous is being paused because the configuration
997 // is changing, which means it is actually stopping...
998 // To juggle the fact that we are also starting a new
999 // instance right now, we need to first completely stop
1000 // the current instance before starting the new one.
1001 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001002 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -07001003 } else if (!hasVisibleBehindActivity()) {
1004 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001005 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001006 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001007 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1008 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001009 // If we already have a few activities waiting to stop,
1010 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001011 // them out. Or if r is the last of activity of the last task the stack
1012 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001014 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001015 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001016 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001017 }
1018 }
1019 } else {
1020 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1021 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001022 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001025
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001026 if (resumeNext) {
1027 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1028 if (!mService.isSleepingOrShuttingDown()) {
1029 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1030 } else {
1031 mStackSupervisor.checkReadyForSleepLocked();
1032 ActivityRecord top = topStack.topRunningActivityLocked(null);
1033 if (top == null || (prev != null && top != prev)) {
1034 // If there are no more activities available to run,
1035 // do resume anyway to start something. Also if the top
1036 // activity on the stack is not the just paused activity,
1037 // we need to go ahead and resume it to ensure we complete
1038 // an in-flight app switch.
1039 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1040 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001041 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001043
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001044 if (prev != null) {
1045 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001046
Craig Mautner525f3d92013-05-07 14:01:50 -07001047 if (prev.app != null && prev.cpuTimeAtResume > 0
1048 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001049 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1050 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001051 if (diff > 0) {
1052 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1053 synchronized (bsi) {
1054 BatteryStatsImpl.Uid.Proc ps =
1055 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001056 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001057 if (ps != null) {
1058 ps.addForegroundTimeLocked(diff);
1059 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001060 }
1061 }
1062 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001063 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001065
1066 // Notfiy when the task stack has changed
1067 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068 }
1069
1070 /**
1071 * Once we know that we have asked an application to put an activity in
1072 * the resumed state (either by launching it or explicitly telling it),
1073 * this function updates the rest of our state to match that fact.
1074 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001075 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001076 next.idle = false;
1077 next.results = null;
1078 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001079
1080 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1081 ProcessRecord app = next.task.mActivities.get(0).app;
1082 if (app != null && app != mService.mHomeProcess) {
1083 mService.mHomeProcess = app;
1084 }
1085 }
1086
Craig Mautner07566322013-09-26 16:42:55 -07001087 if (next.nowVisible) {
1088 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001089 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001090 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001091
1092 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001093 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 mStackSupervisor.reportResumedActivityLocked(next);
1096
1097 next.resumeKeyDispatchingLocked();
1098 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001099
1100 // Mark the point when the activity is resuming
1101 // TODO: To be more accurate, the mark should be before the onCreate,
1102 // not after the onResume. But for subsequent starts, onResume is fine.
1103 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001104 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001105 } else {
1106 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1107 }
Winson Chung376543b2014-05-21 17:43:28 -07001108
George Mount6ba042b2014-07-28 11:12:28 -07001109 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001110
1111 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1112 // When resuming an activity, require it to call requestVisibleBehind() again.
1113 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1114 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115 }
1116
Craig Mautnere3a00d72014-04-16 08:31:19 -07001117 private void setVisibile(ActivityRecord r, boolean visible) {
1118 r.visible = visible;
1119 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001120 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001121 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001122 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001123 container.setVisible(visible);
1124 }
1125 }
1126
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001127 // Find the first visible activity above the passed activity and if it is translucent return it
1128 // otherwise return null;
1129 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1130 TaskRecord task = r.task;
1131 if (task == null) {
1132 return null;
1133 }
1134
1135 ActivityStack stack = task.stack;
1136 if (stack == null) {
1137 return null;
1138 }
1139
1140 int stackNdx = mStacks.indexOf(stack);
1141
1142 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1143 int taskNdx = tasks.indexOf(task);
1144
1145 ArrayList<ActivityRecord> activities = task.mActivities;
1146 int activityNdx = activities.indexOf(r) + 1;
1147
1148 final int numStacks = mStacks.size();
1149 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001150 ActivityStack historyStack = mStacks.get(stackNdx);
1151 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001152 final int numTasks = tasks.size();
1153 while (taskNdx < numTasks) {
1154 activities = tasks.get(taskNdx).mActivities;
1155 final int numActivities = activities.size();
1156 while (activityNdx < numActivities) {
1157 final ActivityRecord activity = activities.get(activityNdx);
1158 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001159 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001160 }
1161 ++activityNdx;
1162 }
1163 activityNdx = 0;
1164 ++taskNdx;
1165 }
1166 taskNdx = 0;
1167 ++stackNdx;
1168 }
1169
1170 return null;
1171 }
1172
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001173 private ActivityStack getNextVisibleStackLocked() {
1174 ArrayList<ActivityStack> stacks = mStacks;
1175 final ActivityRecord parent = mActivityContainer.mParentActivity;
1176 if (parent != null) {
1177 stacks = parent.task.stack.mStacks;
1178 }
1179 if (stacks != null) {
1180 for (int i = stacks.size() - 1; i >= 0; --i) {
1181 ActivityStack stack = stacks.get(i);
1182 if (stack != this && stack.isStackVisibleLocked()) {
1183 return stack;
1184 }
1185 }
1186 }
1187 return null;
1188 }
1189
Jose Lima7ba71252014-04-30 12:59:27 -07001190 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1191 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001192 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001193 if (!isAttached()) {
1194 return false;
1195 }
1196
1197 if (mStackSupervisor.isFrontStack(this)) {
1198 return true;
1199 }
1200
Jose Lima729cb232014-05-05 15:59:40 -07001201 /**
1202 * Start at the task above this one and go up, looking for a visible
1203 * fullscreen activity, or a translucent activity that requested the
1204 * wallpaper to be shown behind it.
1205 */
Jose Lima7ba71252014-04-30 12:59:27 -07001206 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001207 ActivityStack stack = mStacks.get(i);
1208 // stack above isn't full screen, so, we assume we're still visible. at some point
1209 // we should look at the stack bounds to see if we're occluded even if the stack
1210 // isn't fullscreen
1211 if (!stack.mFullscreen) {
1212 continue;
1213 }
1214 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1215 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001216 final TaskRecord task = tasks.get(taskNdx);
1217 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001218 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001219 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001220
1221 // Conditions for an activity to obscure the stack we're
1222 // examining:
1223 // 1. Not Finishing AND Visible AND:
1224 // 2. Either:
1225 // - Full Screen Activity OR
1226 // - On top of Home and our stack is NOT home
1227 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001228 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001229 return false;
1230 }
1231 }
1232 }
1233 }
1234
1235 return true;
1236 }
1237
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001238 /**
1239 * Make sure that all activities that need to be visible (that is, they
1240 * currently can be seen by the user) actually are.
1241 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001242 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1243 ActivityRecord top = topRunningActivityLocked(null);
1244 if (top == null) {
1245 return;
1246 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001247 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248 TAG, "ensureActivitiesVisible behind " + top
1249 + " configChanges=0x" + Integer.toHexString(configChanges));
1250
Craig Mautner5eda9b32013-07-02 11:58:16 -07001251 if (mTranslucentActivityWaiting != top) {
1252 mUndrawnActivitiesBelowTopTranslucent.clear();
1253 if (mTranslucentActivityWaiting != null) {
1254 // Call the callback with a timeout indication.
1255 notifyActivityDrawnLocked(null);
1256 mTranslucentActivityWaiting = null;
1257 }
1258 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1259 }
1260
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261 // If the top activity is not fullscreen, then we need to
1262 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001263 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001264 boolean behindFullscreen = !isStackVisibleLocked();
Jose Lima7ba71252014-04-30 12:59:27 -07001265
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001266 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001267 final TaskRecord task = mTaskHistory.get(taskNdx);
1268 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001269 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1270 final ActivityRecord r = activities.get(activityNdx);
1271 if (r.finishing) {
1272 continue;
1273 }
1274 if (aboveTop && r != top) {
1275 continue;
1276 }
1277 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001278 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1279 // but must be drawn initially for the animation as though they were visible.
1280 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001281 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001282 TAG, "Make visible? " + r + " finishing=" + r.finishing
1283 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001284
Craig Mautnerd44711d2013-02-23 11:24:36 -08001285 // First: if this is not the current activity being started, make
1286 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001287 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001288 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001289 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001290
1291 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001292 // This activity needs to be visible, but isn't even
1293 // running... get it started, but don't resume it
1294 // at this point.
1295 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1296 if (r != starting) {
1297 r.startFreezingScreenLocked(r.app, configChanges);
1298 }
1299 if (!r.visible || r.mLaunchTaskBehind) {
1300 if (DEBUG_VISBILITY) Slog.v(
1301 TAG, "Starting and making visible: " + r);
1302 setVisibile(r, true);
1303 }
1304 if (r != starting) {
1305 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001306 }
1307
1308 } else if (r.visible) {
1309 // If this activity is already visible, then there is nothing
1310 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001311 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001313 try {
George Mount6ba042b2014-07-28 11:12:28 -07001314 if (r.returningOptions != null) {
1315 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1316 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001317 }
1318 } catch(RemoteException e) {
1319 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001320 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001321 // This activity is not currently visible, but is running.
1322 // Tell it to become visible.
1323 r.visible = true;
1324 if (r.state != ActivityState.RESUMED && r != starting) {
1325 // If this activity is paused, tell it
1326 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001327 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001328 TAG, "Making visible and scheduling visibility: " + r);
1329 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001330 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001331 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001332 mUndrawnActivitiesBelowTopTranslucent.add(r);
1333 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001334 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001335 r.sleeping = false;
1336 r.app.pendingUiClean = true;
1337 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1338 r.stopFreezingScreenLocked(false);
1339 } catch (Exception e) {
1340 // Just skip on any failure; we'll make it
1341 // visible when it next restarts.
1342 Slog.w(TAG, "Exception thrown making visibile: "
1343 + r.intent.getComponent(), e);
1344 }
1345 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001346 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001347
Craig Mautnerd44711d2013-02-23 11:24:36 -08001348 // Aggregate current change flags.
1349 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350
Craig Mautnerd44711d2013-02-23 11:24:36 -08001351 if (r.fullscreen) {
1352 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001353 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1354 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001355 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001356 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001357 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001358 }
1359 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001360 if (DEBUG_VISBILITY) Slog.v(
1361 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1362 + " state=" + r.state
1363 + " behindFullscreen=" + behindFullscreen);
1364 // Now for any activities that aren't visible to the user, make
1365 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001367 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001368 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001369 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001370 switch (r.state) {
1371 case STOPPING:
1372 case STOPPED:
1373 if (r.app != null && r.app.thread != null) {
1374 if (DEBUG_VISBILITY) Slog.v(
1375 TAG, "Scheduling invisibility: " + r);
1376 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1377 }
1378 break;
1379
1380 case INITIALIZING:
1381 case RESUMED:
1382 case PAUSING:
1383 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001384 // This case created for transitioning activities from
1385 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001386 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001387 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001388 } else {
1389 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1390 mStackSupervisor.mStoppingActivities.add(r);
1391 }
1392 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001393 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001394 break;
1395
1396 default:
1397 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 }
1399 } catch (Exception e) {
1400 // Just skip on any failure; we'll make it
1401 // visible when it next restarts.
1402 Slog.w(TAG, "Exception thrown making hidden: "
1403 + r.intent.getComponent(), e);
1404 }
1405 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001406 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 }
1409 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001410 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001411
1412 if (mTranslucentActivityWaiting != null &&
1413 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1414 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1415 notifyActivityDrawnLocked(null);
1416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417 }
Craig Mautner58547802013-03-05 08:23:53 -08001418
Todd Kennedyaab56db2015-01-30 09:39:53 -08001419 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001420 mTranslucentActivityWaiting = r;
1421 mUndrawnActivitiesBelowTopTranslucent.clear();
1422 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1423 }
1424
1425 /**
1426 * Called as activities below the top translucent activity are redrawn. When the last one is
1427 * redrawn notify the top activity by calling
1428 * {@link Activity#onTranslucentConversionComplete}.
1429 *
1430 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1431 * occurred and the activity will be notified immediately.
1432 */
1433 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001434 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001435 if ((r == null)
1436 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1437 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1438 // The last undrawn activity below the top has just been drawn. If there is an
1439 // opaque activity at the top, notify it that it can become translucent safely now.
1440 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1441 mTranslucentActivityWaiting = null;
1442 mUndrawnActivitiesBelowTopTranslucent.clear();
1443 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1444
Craig Mautner71dd1b62014-02-18 15:48:52 -08001445 if (waitingActivity != null) {
1446 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1447 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1448 try {
1449 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1450 waitingActivity.appToken, r != null);
1451 } catch (RemoteException e) {
1452 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001453 }
1454 }
1455 }
1456 }
1457
Craig Mautnera61bc652013-10-28 15:43:18 -07001458 /** If any activities below the top running one are in the INITIALIZING state and they have a
1459 * starting window displayed then remove that starting window. It is possible that the activity
1460 * in this state will never resumed in which case that starting window will be orphaned. */
1461 void cancelInitializingActivities() {
1462 final ActivityRecord topActivity = topRunningActivityLocked(null);
1463 boolean aboveTop = true;
1464 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1465 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1466 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1467 final ActivityRecord r = activities.get(activityNdx);
1468 if (aboveTop) {
1469 if (r == topActivity) {
1470 aboveTop = false;
1471 }
1472 continue;
1473 }
1474
1475 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1476 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1477 r.mStartingWindowShown = false;
1478 mWindowManager.removeAppStartingWindow(r.appToken);
1479 }
1480 }
1481 }
1482 }
1483
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484 /**
1485 * Ensure that the top activity in the stack is resumed.
1486 *
1487 * @param prev The previously resumed activity, for when in the process
1488 * of pausing; can be null to call from elsewhere.
1489 *
1490 * @return Returns true if something is being resumed, or false if
1491 * nothing happened.
1492 */
1493 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001494 return resumeTopActivityLocked(prev, null);
1495 }
1496
1497 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001498 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001499 // Don't even start recursing.
1500 return false;
1501 }
1502
1503 boolean result = false;
1504 try {
1505 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001506 mStackSupervisor.inResumeTopActivity = true;
1507 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1508 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001509 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001510 }
Craig Mautner544efa72014-09-04 16:41:20 -07001511 result = resumeTopActivityInnerLocked(prev, options);
1512 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001513 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001514 }
1515 return result;
1516 }
1517
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001518 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001519 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1520
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001521 if (!mService.mBooting && !mService.mBooted) {
1522 // Not ready yet!
1523 return false;
1524 }
1525
Craig Mautnerdf88d732014-01-27 09:21:32 -08001526 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001527 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001528 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001529 // Do not resume this stack if its parent is not resumed.
1530 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1531 return false;
1532 }
1533
Craig Mautnera61bc652013-10-28 15:43:18 -07001534 cancelInitializingActivities();
1535
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001537 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001538
1539 // Remember how we'll process this pause/resume situation, and ensure
1540 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001541 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1542 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543
Craig Mautner84984fa2014-06-19 11:19:20 -07001544 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001546 // There are no more activities!
1547 final String reason = "noMoreActivities";
1548 if (!mFullscreen) {
1549 // Try to move focus to the next visible stack with a running activity if this
1550 // stack is not covering the entire screen.
1551 final ActivityStack stack = getNextVisibleStackLocked();
1552 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1553 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1554 }
1555 }
1556 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001557 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001558 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001559 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001560 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001561 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1562 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1563 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001564 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 }
1566
1567 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001568
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001570 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1571 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 // Make sure we have executed any pending transitions, since there
1573 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001574 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001576 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001577 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001578 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 return false;
1580 }
1581
Craig Mautner525f3d92013-05-07 14:01:50 -07001582 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001583 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001584 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001585 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001586 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001587 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001588 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001589 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001590 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001591 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001592 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001593 } else if (!isOnHomeDisplay()) {
1594 return false;
1595 } else if (!isHomeStack()){
1596 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001597 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001598 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1599 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1600 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001601 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001602 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001603 }
1604
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001605 // If we are sleeping, and there is no resumed activity, and the top
1606 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001607 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001608 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001609 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001610 // Make sure we have executed any pending transitions, since there
1611 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001612 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001614 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001615 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001616 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001617 return false;
1618 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001619
1620 // Make sure that the user who owns this activity is started. If not,
1621 // we will just leave it as is because someone should be bringing
1622 // another user's activities to the top of the stack.
1623 if (mService.mStartedUsers.get(next.userId) == null) {
1624 Slog.w(TAG, "Skipping resume of top activity " + next
1625 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001626 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001627 return false;
1628 }
1629
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 // The activity may be waiting for stop, but that is no longer
1631 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001632 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001633 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001634 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001635 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636
1637 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1638
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001639 // If we are currently pausing an activity, then don't do anything
1640 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001641 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001642 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1643 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001644 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001645 return false;
1646 }
1647
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001648 // Okay we are now going to start a switch, to 'next'. We may first
1649 // have to pause the current activity, but this is an important point
1650 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001651 // XXX "App Redirected" dialog is getting too many false positives
1652 // at this point, so turn off for now.
1653 if (false) {
1654 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1655 long now = SystemClock.uptimeMillis();
1656 final boolean inTime = mLastStartedActivity.startTime != 0
1657 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1658 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1659 final int nextUid = next.info.applicationInfo.uid;
1660 if (inTime && lastUid != nextUid
1661 && lastUid != next.launchedFromUid
1662 && mService.checkPermission(
1663 android.Manifest.permission.STOP_APP_SWITCHES,
1664 -1, next.launchedFromUid)
1665 != PackageManager.PERMISSION_GRANTED) {
1666 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1667 } else {
1668 next.startTime = now;
1669 mLastStartedActivity = next;
1670 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001671 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001672 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001673 mLastStartedActivity = next;
1674 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001675 }
Craig Mautner58547802013-03-05 08:23:53 -08001676
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001677 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1678
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 // We need to start pausing the current activity so the top one
1680 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001681 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1682 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001683 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001684 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001685 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001686 }
1687 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001688 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1689 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001690 // At this point we want to put the upcoming activity's process
1691 // at the top of the LRU list, since we know we will be needing it
1692 // very soon and it would be a waste to let it get killed if it
1693 // happens to be sitting towards the end.
1694 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001695 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001696 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001697 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 return true;
1699 }
1700
Christopher Tated3f175c2012-06-14 14:16:54 -07001701 // If the most recent activity was noHistory but was only stopped rather
1702 // than stopped+finished because the device went to sleep, we need to make
1703 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001704 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001705 !mLastNoHistoryActivity.finishing) {
1706 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1707 " on new resume");
1708 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001709 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001710 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001711 }
1712
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001714 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1715 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001716 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001717 if (DEBUG_SWITCH) Slog.v(
1718 TAG, "Resuming top, waiting visible to hide: " + prev);
1719 } else {
1720 // The next activity is already visible, so hide the previous
1721 // activity's windows right now so we can show the new one ASAP.
1722 // We only do this if the previous is finishing, which should mean
1723 // it is on top of the one being resumed so hiding it quickly
1724 // is good. Otherwise, we want to do the normal route of allowing
1725 // the resumed activity to be shown so we can decide if the
1726 // previous should actually be hidden depending on whether the
1727 // new one is found to be full-screen or not.
1728 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001729 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1731 + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001732 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 + ", nowVisible=" + next.nowVisible);
1734 } else {
Craig Mautner8c14c152015-01-15 17:32:07 -08001735 if (DEBUG_SWITCH) Slog.v(TAG,
1736 "Previous already visible but still waiting to hide: " + prev
1737 + ", waitingVisible="
1738 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1739 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 }
1741 }
1742 }
1743
Dianne Hackborne7f97212011-02-24 14:40:20 -08001744 // Launching this app's activity, make sure the app is no longer
1745 // considered stopped.
1746 try {
1747 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001748 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001749 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001750 } catch (IllegalArgumentException e) {
1751 Slog.w(TAG, "Failed trying to unstop package "
1752 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001753 }
1754
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 // We are starting up the next activity, so tell the window manager
1756 // that the previous one will be hidden soon. This way it can know
1757 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001758 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001759 if (prev != null) {
1760 if (prev.finishing) {
1761 if (DEBUG_TRANSITION) Slog.v(TAG,
1762 "Prepare close transition: prev=" + prev);
1763 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001764 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001765 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001766 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001767 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001768 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1769 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001771 mWindowManager.setAppWillBeHidden(prev.appToken);
1772 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001774 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001776 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001777 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001780 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001781 : next.mLaunchTaskBehind
1782 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1783 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 }
1785 }
1786 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001787 mWindowManager.setAppWillBeHidden(prev.appToken);
1788 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 }
Craig Mautner967212c2013-04-13 21:10:58 -07001790 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001791 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001793 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001794 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001796 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
1798 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001799
1800 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001801 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001802 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1803 if (opts != null) {
1804 resumeAnimOptions = opts.toBundle();
1805 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001806 next.applyOptionsLocked();
1807 } else {
1808 next.clearOptionsLocked();
1809 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810
Craig Mautnercf910b02013-04-23 11:23:27 -07001811 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 if (next.app != null && next.app.thread != null) {
1813 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1814
1815 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001816 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001818 // schedule launch ticks to collect information about slow apps.
1819 next.startLaunchTickingLocked();
1820
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001821 ActivityRecord lastResumedActivity =
1822 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 ActivityState lastState = next.state;
1824
1825 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001826
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001827 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 next.state = ActivityState.RESUMED;
1829 mResumedActivity = next;
1830 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001831 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001832 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001834 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835
1836 // Have the window manager re-evaluate the orientation of
1837 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001838 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001839 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001840 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001841 mService.mConfiguration,
1842 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1843 if (config != null) {
1844 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001846 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001847 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001848
Craig Mautner525f3d92013-05-07 14:01:50 -07001849 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001850 // The configuration update wasn't able to keep the existing
1851 // instance of the activity, and instead started a new one.
1852 // We should be all done, but let's just make sure our activity
1853 // is still at the top and schedule another run if something
1854 // weird happened.
1855 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001856 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 "Activity config changed during resume: " + next
1858 + ", new next: " + nextNext);
1859 if (nextNext != next) {
1860 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001861 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001863 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001864 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001865 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001866 return true;
1867 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001868 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001869 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001870 }
Craig Mautner58547802013-03-05 08:23:53 -08001871
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 try {
1873 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001874 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001875 if (a != null) {
1876 final int N = a.size();
1877 if (!next.finishing && N > 0) {
1878 if (DEBUG_RESULTS) Slog.v(
1879 TAG, "Delivering results to " + next
1880 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001881 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001882 }
1883 }
1884
1885 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001886 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001887 }
1888
Craig Mautner299f9602015-01-26 09:47:33 -08001889 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1890 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001891
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001892 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001893 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001894 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001895 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001896 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001897 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001898 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001899
Craig Mautner0eea92c2013-05-16 13:35:39 -07001900 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901
Craig Mautnerac6f8432013-07-17 13:24:59 -07001902 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903 } catch (Exception e) {
1904 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001905 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1906 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001907 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001908 if (lastStack != null) {
1909 lastStack.mResumedActivity = lastResumedActivity;
1910 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001911 Slog.i(TAG, "Restarting because process died: " + next);
1912 if (!next.hasBeenLaunched) {
1913 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001914 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1915 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001916 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001917 next.appToken, next.packageName, next.theme,
1918 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001919 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1920 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921 }
George Mount2c92c972014-03-20 09:38:23 -07001922 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001923 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001924 return true;
1925 }
1926
1927 // From this point on, if something goes wrong there is no way
1928 // to recover the activity.
1929 try {
1930 next.visible = true;
1931 completeResumeLocked(next);
1932 } catch (Exception e) {
1933 // If any exception gets thrown, toss away this
1934 // activity and try the next one.
1935 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001936 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001937 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001938 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001939 return true;
1940 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 next.stopped = false;
1942
1943 } else {
1944 // Whoops, need to restart this activity!
1945 if (!next.hasBeenLaunched) {
1946 next.hasBeenLaunched = true;
1947 } else {
1948 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001949 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001950 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001951 mService.compatibilityInfoForPackageLocked(
1952 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001953 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001954 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001955 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001956 }
1957 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1958 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001959 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001960 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001961 }
1962
Craig Mautnercf910b02013-04-23 11:23:27 -07001963 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001964 return true;
1965 }
1966
riddle_hsuc215a4f2014-12-27 12:10:45 +08001967 private TaskRecord getNextTask(TaskRecord targetTask) {
1968 final int index = mTaskHistory.indexOf(targetTask);
1969 if (index >= 0) {
1970 final int numTasks = mTaskHistory.size();
1971 for (int i = index + 1; i < numTasks; ++i) {
1972 TaskRecord task = mTaskHistory.get(i);
1973 if (task.userId == targetTask.userId) {
1974 return task;
1975 }
1976 }
1977 }
1978 return null;
1979 }
1980
Craig Mautnerac6f8432013-07-17 13:24:59 -07001981 private void insertTaskAtTop(TaskRecord task) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001982 // If the moving task is over home stack, transfer its return type to next task
1983 if (task.isOverHomeStack()) {
1984 final TaskRecord nextTask = getNextTask(task);
1985 if (nextTask != null) {
1986 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1987 }
1988 }
1989
Craig Mautner9c85c202013-10-04 20:11:26 -07001990 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08001991 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001992 if (isOnHomeDisplay()) {
1993 ActivityStack lastStack = mStackSupervisor.getLastStack();
1994 final boolean fromHome = lastStack.isHomeStack();
1995 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001996 task.setTaskToReturnTo(fromHome
1997 ? lastStack.topTask() == null
1998 ? HOME_ACTIVITY_TYPE
1999 : lastStack.topTask().taskType
2000 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002001 }
2002 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002003 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002004 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002005
Craig Mautnerac6f8432013-07-17 13:24:59 -07002006 mTaskHistory.remove(task);
2007 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002008 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01002009 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002010 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002011 while (--taskNdx >= 0) {
2012 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002013 break;
2014 }
2015 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002016 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002017 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002018 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002019 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002020 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002021
Craig Mautner8849a5e2013-04-02 16:41:03 -07002022 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002023 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002024 TaskRecord rTask = r.task;
2025 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002026 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2027 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002028 // Last activity in task had been removed or ActivityManagerService is reusing task.
2029 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002030 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002031 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002032 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002033 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002034 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 if (!newTask) {
2036 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002037 boolean startIt = true;
2038 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2039 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002040 if (task.getTopActivity() == null) {
2041 // All activities in task are finishing.
2042 continue;
2043 }
Craig Mautner70a86932013-02-28 22:37:44 -08002044 if (task == r.task) {
2045 // Here it is! Now, if this is not yet visible to the
2046 // user, then just add it without starting; it will
2047 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002048 if (!startIt) {
2049 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2050 + task, new RuntimeException("here").fillInStackTrace());
2051 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002052 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002053 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2054 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002055 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002056 r.userId, r.info.configChanges, task.voiceSession != null,
2057 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002059 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002061 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 return;
2063 }
2064 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002065 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 startIt = false;
2067 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 }
2069 }
2070
2071 // Place a new activity at top of stack, so it is next to interact
2072 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002073
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 // If we are not placing the new activity frontmost, we do not want
2075 // to deliver the onUserLeaving callback to the actual frontmost
2076 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002077 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002078 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002079 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2080 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 }
Craig Mautner70a86932013-02-28 22:37:44 -08002082
2083 task = r.task;
2084
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002085 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002086 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002087 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002088 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002089 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002090
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002091 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002092 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 // We want to show the starting preview window if we are
2094 // switching to a new task, or the next activity's process is
2095 // not currently running.
2096 boolean showStartingIcon = newTask;
2097 ProcessRecord proc = r.app;
2098 if (proc == null) {
2099 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2100 }
2101 if (proc == null || proc.thread == null) {
2102 showStartingIcon = true;
2103 }
2104 if (DEBUG_TRANSITION) Slog.v(TAG,
2105 "Prepare open transition: starting " + r);
2106 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002107 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002110 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002111 ? r.mLaunchTaskBehind
2112 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2113 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002114 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 mNoAnimActivities.remove(r);
2116 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002117 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002118 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002119 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002120 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 boolean doShow = true;
2122 if (newTask) {
2123 // Even though this activity is starting fresh, we still need
2124 // to reset it to make sure we apply affinities to move any
2125 // existing activities from other tasks in to it.
2126 // If the caller has requested that the target task be
2127 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002128 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129 resetTaskIfNeededLocked(r, r);
2130 doShow = topRunningNonDelayedActivityLocked(null) == r;
2131 }
George Mount70778d72014-07-01 16:33:45 -07002132 } else if (options != null && new ActivityOptions(options).getAnimationType()
2133 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2134 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002136 if (r.mLaunchTaskBehind) {
2137 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2138 // tell WindowManager that r is visible even though it is at the back of the stack.
2139 mWindowManager.setAppVisibility(r.appToken, true);
2140 ensureActivitiesVisibleLocked(null, 0);
2141 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 // Figure out if we are transitioning from another activity that is
2143 // "has the same starting icon" as the next one. This allows the
2144 // window manager to keep the previous window it had previously
2145 // created, if it still had one.
2146 ActivityRecord prev = mResumedActivity;
2147 if (prev != null) {
2148 // We don't want to reuse the previous starting preview if:
2149 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002150 if (prev.task != r.task) {
2151 prev = null;
2152 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002154 else if (prev.nowVisible) {
2155 prev = null;
2156 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002158 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002159 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002160 mService.compatibilityInfoForPackageLocked(
2161 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002162 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002163 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002164 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 }
2166 } else {
2167 // If this is the first activity, don't do any fancy animations,
2168 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002169 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002170 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002171 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002172 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002173 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002174 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002175 }
2176 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002177 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002178 }
2179
2180 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002181 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 }
2183 }
2184
Dianne Hackbornbe707852011-11-11 14:32:10 -08002185 final void validateAppTokensLocked() {
2186 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002187 mValidateAppTokens.ensureCapacity(numActivities());
2188 final int numTasks = mTaskHistory.size();
2189 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2190 TaskRecord task = mTaskHistory.get(taskNdx);
2191 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002192 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002193 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002194 }
Craig Mautner000f0022013-02-26 15:04:29 -08002195 TaskGroup group = new TaskGroup();
2196 group.taskId = task.taskId;
2197 mValidateAppTokens.add(group);
2198 final int numActivities = activities.size();
2199 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2200 final ActivityRecord r = activities.get(activityNdx);
2201 group.tokens.add(r.appToken);
2202 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002203 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002204 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002205 }
2206
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207 /**
2208 * Perform a reset of the given task, if needed as part of launching it.
2209 * Returns the new HistoryRecord at the top of the task.
2210 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002211 /**
2212 * Helper method for #resetTaskIfNeededLocked.
2213 * We are inside of the task being reset... we'll either finish this activity, push it out
2214 * for another task, or leave it as-is.
2215 * @param task The task containing the Activity (taskTop) that might be reset.
2216 * @param forceReset
2217 * @return An ActivityOptions that needs to be processed.
2218 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002219 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002220 ActivityOptions topOptions = null;
2221
2222 int replyChainEnd = -1;
2223 boolean canMoveOptions = true;
2224
2225 // We only do this for activities that are not the root of the task (since if we finish
2226 // the root, we may no longer have the task!).
2227 final ArrayList<ActivityRecord> activities = task.mActivities;
2228 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002229 final int rootActivityNdx = task.findEffectiveRootIndex();
2230 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002231 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002232 if (target.frontOfTask)
2233 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002234
2235 final int flags = target.info.flags;
2236 final boolean finishOnTaskLaunch =
2237 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2238 final boolean allowTaskReparenting =
2239 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2240 final boolean clearWhenTaskReset =
2241 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2242
2243 if (!finishOnTaskLaunch
2244 && !clearWhenTaskReset
2245 && target.resultTo != null) {
2246 // If this activity is sending a reply to a previous
2247 // activity, we can't do anything with it now until
2248 // we reach the start of the reply chain.
2249 // XXX note that we are assuming the result is always
2250 // to the previous activity, which is almost always
2251 // the case but we really shouldn't count on.
2252 if (replyChainEnd < 0) {
2253 replyChainEnd = i;
2254 }
2255 } else if (!finishOnTaskLaunch
2256 && !clearWhenTaskReset
2257 && allowTaskReparenting
2258 && target.taskAffinity != null
2259 && !target.taskAffinity.equals(task.affinity)) {
2260 // If this activity has an affinity for another
2261 // task, then we need to move it out of here. We will
2262 // move it as far out of the way as possible, to the
2263 // bottom of the activity stack. This also keeps it
2264 // correctly ordered with any activities we previously
2265 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002266 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002267 final ActivityRecord bottom =
2268 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002269 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002270 if (bottom != null && target.taskAffinity != null
2271 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002272 // If the activity currently at the bottom has the
2273 // same task affinity as the one we are moving,
2274 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002275 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002276 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002277 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002278 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002279 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002280 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002281 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2283 + " out to new task " + target.task);
2284 }
2285
Craig Mautnere3a74d52013-02-22 14:14:58 -08002286 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002287 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002288
Craig Mautner525f3d92013-05-07 14:01:50 -07002289 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002290 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2291 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002292 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002293 if (p.finishing) {
2294 continue;
2295 }
2296
Craig Mautnere3a74d52013-02-22 14:14:58 -08002297 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002298 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002299 topOptions = p.takeOptionsLocked();
2300 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002301 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002302 }
2303 }
2304 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002305 + task + " adding to task=" + targetTask
2306 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002307 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2308 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002309 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002310 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002311
Craig Mautner83162a92015-01-26 14:43:30 -08002312 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002313 }
2314
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002315 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 if (VALIDATE_TOKENS) {
2317 validateAppTokensLocked();
2318 }
2319
2320 replyChainEnd = -1;
2321 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2322 // If the activity should just be removed -- either
2323 // because it asks for it, or the task should be
2324 // cleared -- then finish it and anything that is
2325 // part of its reply chain.
2326 int end;
2327 if (clearWhenTaskReset) {
2328 // In this case, we want to finish this activity
2329 // and everything above it, so be sneaky and pretend
2330 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002331 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002332 } else if (replyChainEnd < 0) {
2333 end = i;
2334 } else {
2335 end = replyChainEnd;
2336 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002337 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002338 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002339 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002340 if (p.finishing) {
2341 continue;
2342 }
2343 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002344 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002345 topOptions = p.takeOptionsLocked();
2346 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002347 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002348 }
2349 }
Craig Mautner58547802013-03-05 08:23:53 -08002350 if (DEBUG_TASKS) Slog.w(TAG,
2351 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002352 if (finishActivityLocked(
2353 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002354 end--;
2355 srcPos--;
2356 }
2357 }
2358 replyChainEnd = -1;
2359 } else {
2360 // If we were in the middle of a chain, well the
2361 // activity that started it all doesn't want anything
2362 // special, so leave it all as-is.
2363 replyChainEnd = -1;
2364 }
2365 }
2366
2367 return topOptions;
2368 }
2369
2370 /**
2371 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2372 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2373 * @param affinityTask The task we are looking for an affinity to.
2374 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2375 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2376 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2377 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002378 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002379 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002380 int replyChainEnd = -1;
2381 final int taskId = task.taskId;
2382 final String taskAffinity = task.affinity;
2383
2384 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2385 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002386 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2387
2388 // Do not operate on or below the effective root Activity.
2389 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002390 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002391 if (target.frontOfTask)
2392 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002393
2394 final int flags = target.info.flags;
2395 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2396 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2397
2398 if (target.resultTo != null) {
2399 // If this activity is sending a reply to a previous
2400 // activity, we can't do anything with it now until
2401 // we reach the start of the reply chain.
2402 // XXX note that we are assuming the result is always
2403 // to the previous activity, which is almost always
2404 // the case but we really shouldn't count on.
2405 if (replyChainEnd < 0) {
2406 replyChainEnd = i;
2407 }
2408 } else if (topTaskIsHigher
2409 && allowTaskReparenting
2410 && taskAffinity != null
2411 && taskAffinity.equals(target.taskAffinity)) {
2412 // This activity has an affinity for our task. Either remove it if we are
2413 // clearing or move it over to our task. Note that
2414 // we currently punt on the case where we are resetting a
2415 // task that is not at the top but who has activities above
2416 // with an affinity to it... this is really not a normal
2417 // case, and we will need to later pull that task to the front
2418 // and usually at that point we will do the reset and pick
2419 // up those remaining activities. (This only happens if
2420 // someone starts an activity in a new task from an activity
2421 // in a task that is not currently on top.)
2422 if (forceReset || finishOnTaskLaunch) {
2423 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2424 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2425 for (int srcPos = start; srcPos >= i; --srcPos) {
2426 final ActivityRecord p = activities.get(srcPos);
2427 if (p.finishing) {
2428 continue;
2429 }
Todd Kennedy539db512014-12-15 09:57:55 -08002430 finishActivityLocked(
2431 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002432 }
2433 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002434 if (taskInsertionPoint < 0) {
2435 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002436
Craig Mautner77878772013-03-04 19:46:24 -08002437 }
Craig Mautner77878772013-03-04 19:46:24 -08002438
2439 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2440 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2441 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2442 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002443 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002444 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002445 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002446
Craig Mautnere3a74d52013-02-22 14:14:58 -08002447 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2448 + " to stack at " + task,
2449 new RuntimeException("here").fillInStackTrace());
2450 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2451 + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002452 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002453 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002454 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002455 if (VALIDATE_TOKENS) {
2456 validateAppTokensLocked();
2457 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002458
2459 // Now we've moved it in to place... but what if this is
2460 // a singleTop activity and we have put it on top of another
2461 // instance of the same activity? Then we drop the instance
2462 // below so it remains singleTop.
2463 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2464 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002465 int targetNdx = taskActivities.indexOf(target);
2466 if (targetNdx > 0) {
2467 ActivityRecord p = taskActivities.get(targetNdx - 1);
2468 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002469 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2470 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002471 }
2472 }
2473 }
2474 }
2475
2476 replyChainEnd = -1;
2477 }
2478 }
Craig Mautner77878772013-03-04 19:46:24 -08002479 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002480 }
2481
Craig Mautner8849a5e2013-04-02 16:41:03 -07002482 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002483 ActivityRecord newActivity) {
2484 boolean forceReset =
2485 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2486 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2487 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2488 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2489 forceReset = true;
2490 }
2491 }
2492
2493 final TaskRecord task = taskTop.task;
2494
2495 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2496 * for remaining tasks. Used for later tasks to reparent to task. */
2497 boolean taskFound = false;
2498
2499 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2500 ActivityOptions topOptions = null;
2501
Craig Mautner77878772013-03-04 19:46:24 -08002502 // Preserve the location for reparenting in the new task.
2503 int reparentInsertionPoint = -1;
2504
Craig Mautnere3a74d52013-02-22 14:14:58 -08002505 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2506 final TaskRecord targetTask = mTaskHistory.get(i);
2507
2508 if (targetTask == task) {
2509 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2510 taskFound = true;
2511 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002512 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2513 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002514 }
2515 }
2516
Craig Mautner70a86932013-02-28 22:37:44 -08002517 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002518 if (taskNdx >= 0) {
2519 do {
2520 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2521 } while (taskTop == null && taskNdx >= 0);
2522 }
Craig Mautner70a86932013-02-28 22:37:44 -08002523
Craig Mautnere3a74d52013-02-22 14:14:58 -08002524 if (topOptions != null) {
2525 // If we got some ActivityOptions from an activity on top that
2526 // was removed from the task, propagate them to the new real top.
2527 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002528 taskTop.updateOptionsLocked(topOptions);
2529 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002530 topOptions.abort();
2531 }
2532 }
2533
2534 return taskTop;
2535 }
2536
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002537 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2538 String resultWho, int requestCode, int resultCode, Intent data) {
2539
2540 if (callingUid > 0) {
2541 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002542 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 }
2544
2545 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2546 + " : who=" + resultWho + " req=" + requestCode
2547 + " res=" + resultCode + " data=" + data);
2548 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2549 try {
2550 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2551 list.add(new ResultInfo(resultWho, requestCode,
2552 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002553 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002554 return;
2555 } catch (Exception e) {
2556 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2557 }
2558 }
2559
2560 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2561 }
2562
Craig Mautner299f9602015-01-26 09:47:33 -08002563 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002564 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2565 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002566 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002567 if (next != r) {
2568 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002569 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002570 // For non-fullscreen stack, we want to move the focus to the next visible
2571 // stack to prevent the home screen from moving to the top and obscuring
2572 // other visible stacks.
2573 if (!mFullscreen
2574 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2575 return;
2576 }
2577 // Move the home stack to the top if this stack is fullscreen or there is no
2578 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002579 if (mStackSupervisor.moveHomeStackTaskToTop(
2580 task.getTaskToReturnTo(), myReason)) {
2581 // Activity focus was already adjusted. Nothing else to do...
2582 return;
2583 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002584 }
2585 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002586
2587 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002588 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002589 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002590 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002591 }
2592 }
2593
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002594 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2595 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2596 final String myReason = reason + " adjustFocusToNextVisibleStack";
2597 if (stack == null) {
2598 return false;
2599 }
2600 final ActivityRecord top = stack.topRunningActivityLocked(null);
2601 if (top == null) {
2602 return false;
2603 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002604 mService.setFocusedActivityLocked(top, myReason);
2605 return true;
2606 }
2607
Craig Mautnerf3333272013-04-22 10:55:53 -07002608 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002609 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2610 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2611 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2612 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002613 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002614 if (DEBUG_STATES) {
2615 Slog.d(TAG, "no-history finish of " + r);
2616 }
2617 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002618 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002619 } else {
2620 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2621 + " on stop because we're just sleeping");
2622 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002623 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002624 }
2625
2626 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002627 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002628 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002629 try {
2630 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002631 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2632 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 r.state = ActivityState.STOPPING;
2634 if (DEBUG_VISBILITY) Slog.v(
2635 TAG, "Stopping visible=" + r.visible + " for " + r);
2636 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002637 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002638 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002639 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002640 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002641 r.setSleeping(true);
2642 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002643 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002644 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002645 } catch (Exception e) {
2646 // Maybe just ignore exceptions here... if the process
2647 // has crashed, our death notification will clean things
2648 // up.
2649 Slog.w(TAG, "Exception thrown during pause", e);
2650 // Just in case, assume it to be stopped.
2651 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002652 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 r.state = ActivityState.STOPPED;
2654 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002655 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 }
2657 }
2658 }
2659 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002660
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 /**
2662 * @return Returns true if the activity is being finished, false if for
2663 * some reason it is being left as-is.
2664 */
2665 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002666 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002667 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002668 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002669 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002670 + ", result=" + resultCode + ", data=" + resultData
2671 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002672 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 return false;
2674 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675
Craig Mautnerd44711d2013-02-23 11:24:36 -08002676 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677 return true;
2678 }
2679
Craig Mautnerd2328952013-03-05 12:46:26 -08002680 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002681 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2682 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2683 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2684 ActivityRecord r = activities.get(activityNdx);
2685 if (r.resultTo == self && r.requestCode == requestCode) {
2686 if ((r.resultWho == null && resultWho == null) ||
2687 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2688 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2689 false);
2690 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002691 }
2692 }
2693 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002694 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002695 }
2696
Todd Kennedy539db512014-12-15 09:57:55 -08002697 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002698 ActivityRecord r = topRunningActivityLocked(null);
2699 if (r != null && r.app == app) {
2700 // If the top running activity is from this crashing
2701 // process, then terminate it to avoid getting in a loop.
2702 Slog.w(TAG, " Force finishing activity "
2703 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002704 int taskNdx = mTaskHistory.indexOf(r.task);
2705 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002706 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002707 // Also terminate any activities below it that aren't yet
2708 // stopped, to avoid a situation where one will get
2709 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002710 --activityNdx;
2711 if (activityNdx < 0) {
2712 do {
2713 --taskNdx;
2714 if (taskNdx < 0) {
2715 break;
2716 }
2717 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2718 } while (activityNdx < 0);
2719 }
2720 if (activityNdx >= 0) {
2721 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002722 if (r.state == ActivityState.RESUMED
2723 || r.state == ActivityState.PAUSING
2724 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002725 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002726 Slog.w(TAG, " Force finishing activity "
2727 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002728 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002729 }
2730 }
2731 }
2732 }
2733 }
2734
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002735 final void finishVoiceTask(IVoiceInteractionSession session) {
2736 IBinder sessionBinder = session.asBinder();
2737 boolean didOne = false;
2738 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2739 TaskRecord tr = mTaskHistory.get(taskNdx);
2740 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2741 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2742 ActivityRecord r = tr.mActivities.get(activityNdx);
2743 if (!r.finishing) {
2744 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2745 false);
2746 didOne = true;
2747 }
2748 }
2749 }
2750 }
2751 if (didOne) {
2752 mService.updateOomAdjLocked();
2753 }
2754 }
2755
Craig Mautnerd2328952013-03-05 12:46:26 -08002756 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002757 ArrayList<ActivityRecord> activities = r.task.mActivities;
2758 for (int index = activities.indexOf(r); index >= 0; --index) {
2759 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002760 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002761 break;
2762 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002763 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002764 }
2765 return true;
2766 }
2767
Dianne Hackborn5c607432012-02-28 14:44:19 -08002768 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2769 // send the result
2770 ActivityRecord resultTo = r.resultTo;
2771 if (resultTo != null) {
2772 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2773 + " who=" + r.resultWho + " req=" + r.requestCode
2774 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002775 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002776 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002777 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002778 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002779 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002780 if (r.info.applicationInfo.uid > 0) {
2781 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2782 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002783 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002784 }
2785 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2786 resultData);
2787 r.resultTo = null;
2788 }
2789 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2790
2791 // Make sure this HistoryRecord is not holding on to other resources,
2792 // because clients have remote IPC references to this object so we
2793 // can't assume that will go away and want to avoid circular IPC refs.
2794 r.results = null;
2795 r.pendingResults = null;
2796 r.newIntents = null;
2797 r.icicle = null;
2798 }
2799
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 /**
2801 * @return Returns true if this activity has been removed from the history
2802 * list, or false if it is still in the list and will be removed later.
2803 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002804 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2805 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 if (r.finishing) {
2807 Slog.w(TAG, "Duplicate finish request for " + r);
2808 return false;
2809 }
2810
Wale Ogunwale7d701172015-03-11 15:36:30 -07002811 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002812 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002814 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002815 task.taskId, r.shortComponentName, reason);
2816 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002817 final int index = activities.indexOf(r);
2818 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002819 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002820 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002821 // If the caller asked that this activity (and all above it)
2822 // be cleared when the task is reset, don't lose that information,
2823 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002824 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002825 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 }
2827 }
2828
2829 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002830
Craig Mautner299f9602015-01-26 09:47:33 -08002831 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002832
Dianne Hackborn5c607432012-02-28 14:44:19 -08002833 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002834
Craig Mautnerde4ef022013-04-07 19:01:33 -07002835 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002836 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002837 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002839 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002840 ? AppTransition.TRANSIT_TASK_CLOSE
2841 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002842
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002844 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002845
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002846 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2848 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002849 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002850 }
2851
Craig Mautneraea74a52014-03-08 14:23:10 -08002852 if (endTask) {
2853 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2854 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002855 } else if (r.state != ActivityState.PAUSING) {
2856 // If the activity is PAUSING, we will complete the finish once
2857 // it is done pausing; else we can just directly finish it here.
2858 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002859 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 } else {
2861 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2862 }
2863
2864 return false;
2865 }
2866
Craig Mautnerf3333272013-04-22 10:55:53 -07002867 static final int FINISH_IMMEDIATELY = 0;
2868 static final int FINISH_AFTER_PAUSE = 1;
2869 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002870
Craig Mautnerf3333272013-04-22 10:55:53 -07002871 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872 // First things first: if this activity is currently visible,
2873 // and the resumed activity is not yet visible, then hold off on
2874 // finishing until the resumed one becomes visible.
2875 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002876 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2877 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002878 if (mStackSupervisor.mStoppingActivities.size() > 3
2879 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002880 // If we already have a few activities waiting to stop,
2881 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002882 // them out. Or if r is the last of activity of the last task the stack
2883 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002884 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002885 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002886 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887 }
2888 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002889 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2890 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002891 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002892 if (oomAdj) {
2893 mService.updateOomAdjLocked();
2894 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895 return r;
2896 }
2897
2898 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002899 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002900 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002901 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 if (mResumedActivity == r) {
2903 mResumedActivity = null;
2904 }
2905 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002906 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002907 r.state = ActivityState.FINISHING;
2908
2909 if (mode == FINISH_IMMEDIATELY
2910 || prevState == ActivityState.STOPPED
2911 || prevState == ActivityState.INITIALIZING) {
2912 // If this activity is already stopped, we can just finish
2913 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002914 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002915 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002916 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002917 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002918 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002919 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002920 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2921 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002922 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002924
2925 // Need to go through the full pause cycle to get this
2926 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002927 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002928 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002929 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002930 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 return r;
2932 }
2933
Craig Mautneree36c772014-07-16 14:56:05 -07002934 void finishAllActivitiesLocked(boolean immediately) {
2935 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002936 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2937 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2938 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2939 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002940 noActivitiesInStack = false;
2941 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002942 continue;
2943 }
Craig Mautneree36c772014-07-16 14:56:05 -07002944 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002945 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2946 }
2947 }
Craig Mautneree36c772014-07-16 14:56:05 -07002948 if (noActivitiesInStack) {
2949 mActivityContainer.onTaskListEmptyLocked();
2950 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002951 }
2952
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002953 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2954 // Basic case: for simple app-centric recents, we need to recreate
2955 // the task if the affinity has changed.
2956 if (srec == null || srec.task.affinity == null ||
2957 !srec.task.affinity.equals(destAffinity)) {
2958 return true;
2959 }
2960 // Document-centric case: an app may be split in to multiple documents;
2961 // they need to re-create their task if this current activity is the root
2962 // of a document, unless simply finishing it will return them to the the
2963 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002964 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2965 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002966 // Okay, this activity is at the root of its task. What to do, what to do...
2967 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2968 // Finishing won't return to an application, so we need to recreate.
2969 return true;
2970 }
2971 // We now need to get the task below it to determine what to do.
2972 int taskIdx = mTaskHistory.indexOf(srec.task);
2973 if (taskIdx <= 0) {
2974 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2975 return false;
2976 }
2977 if (taskIdx == 0) {
2978 // At the bottom of the stack, nothing to go back to.
2979 return true;
2980 }
2981 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2982 if (!srec.task.affinity.equals(prevTask.affinity)) {
2983 // These are different apps, so need to recreate.
2984 return true;
2985 }
2986 }
2987 return false;
2988 }
2989
Wale Ogunwale7d701172015-03-11 15:36:30 -07002990 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002991 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002992 final TaskRecord task = srec.task;
2993 final ArrayList<ActivityRecord> activities = task.mActivities;
2994 final int start = activities.indexOf(srec);
2995 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002996 return false;
2997 }
2998 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002999 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003000 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003001 final ComponentName dest = destIntent.getComponent();
3002 if (start > 0 && dest != null) {
3003 for (int i = finishTo; i >= 0; i--) {
3004 ActivityRecord r = activities.get(i);
3005 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003006 r.info.name.equals(dest.getClassName())) {
3007 finishTo = i;
3008 parent = r;
3009 foundParentInTask = true;
3010 break;
3011 }
3012 }
3013 }
3014
3015 IActivityController controller = mService.mController;
3016 if (controller != null) {
3017 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3018 if (next != null) {
3019 // ask watcher if this is allowed
3020 boolean resumeOK = true;
3021 try {
3022 resumeOK = controller.activityResuming(next.packageName);
3023 } catch (RemoteException e) {
3024 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003025 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003026 }
3027
3028 if (!resumeOK) {
3029 return false;
3030 }
3031 }
3032 }
3033 final long origId = Binder.clearCallingIdentity();
3034 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003035 ActivityRecord r = activities.get(i);
3036 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003037 // Only return the supplied result for the first activity finished
3038 resultCode = Activity.RESULT_CANCELED;
3039 resultData = null;
3040 }
3041
3042 if (parent != null && foundParentInTask) {
3043 final int parentLaunchMode = parent.info.launchMode;
3044 final int destIntentFlags = destIntent.getFlags();
3045 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3046 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3047 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3048 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003049 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3050 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003051 } else {
3052 try {
3053 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3054 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003055 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003056 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003057 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003058 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003059 foundParentInTask = res == ActivityManager.START_SUCCESS;
3060 } catch (RemoteException e) {
3061 foundParentInTask = false;
3062 }
3063 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003064 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003065 }
3066 }
3067 Binder.restoreCallingIdentity(origId);
3068 return foundParentInTask;
3069 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 /**
3071 * Perform the common clean-up of an activity record. This is called both
3072 * as part of destroyActivityLocked() (when destroying the client-side
3073 * representation) and cleaning things up as a result of its hosting
3074 * processing going away, in which case there is no remaining client-side
3075 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003076 *
3077 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003079 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3080 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081 if (mResumedActivity == r) {
3082 mResumedActivity = null;
3083 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003084 if (mPausingActivity == r) {
3085 mPausingActivity = null;
3086 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003087 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003088
3089 r.configDestroy = false;
3090 r.frozenBeforeDestroy = false;
3091
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003092 if (setState) {
3093 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3094 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003095 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003096 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003097 }
3098
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 // Make sure this record is no longer in the pending finishes list.
3100 // This could happen, for example, if we are trimming activities
3101 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003102 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003103 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003105 // Remove any pending results.
3106 if (r.finishing && r.pendingResults != null) {
3107 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3108 PendingIntentRecord rec = apr.get();
3109 if (rec != null) {
3110 mService.cancelIntentSenderLocked(rec, false);
3111 }
3112 }
3113 r.pendingResults = null;
3114 }
3115
3116 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003117 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 }
3119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003121 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003122 if (getVisibleBehindActivity() == r) {
3123 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003124 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003125 }
3126
3127 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003128 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003130 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003131 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003132 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 }
3134
Craig Mautner299f9602015-01-26 09:47:33 -08003135 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003136 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003137 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003138 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003139 if (DEBUG_ADD_REMOVE) {
3140 RuntimeException here = new RuntimeException("here");
3141 here.fillInStackTrace();
3142 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003144 r.takeFromHistory();
3145 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003146 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003147 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003148 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003149 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003150 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003151 if (VALIDATE_TOKENS) {
3152 validateAppTokensLocked();
3153 }
Craig Mautner312ba862014-02-10 17:55:01 -08003154 final TaskRecord task = r.task;
3155 if (task != null && task.removeActivity(r)) {
3156 if (DEBUG_STACK) Slog.i(TAG,
3157 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003158 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3159 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003160 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003161 }
Craig Mautner299f9602015-01-26 09:47:33 -08003162 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003163 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003164 cleanUpActivityServicesLocked(r);
3165 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003167
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 /**
3169 * Perform clean-up of service connections in an activity record.
3170 */
3171 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3172 // Throw away any services that have been bound by this activity.
3173 if (r.connections != null) {
3174 Iterator<ConnectionRecord> it = r.connections.iterator();
3175 while (it.hasNext()) {
3176 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003177 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003178 }
3179 r.connections = null;
3180 }
3181 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003182
Craig Mautneree2e45a2014-06-27 12:10:03 -07003183 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003184 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003185 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003186 mHandler.sendMessage(msg);
3187 }
3188
Craig Mautneree2e45a2014-06-27 12:10:03 -07003189 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003190 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003191 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003192 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3193 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3194 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3195 final ActivityRecord r = activities.get(activityNdx);
3196 if (r.finishing) {
3197 continue;
3198 }
3199 if (r.fullscreen) {
3200 lastIsOpaque = true;
3201 }
3202 if (owner != null && r.app != owner) {
3203 continue;
3204 }
3205 if (!lastIsOpaque) {
3206 continue;
3207 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003208 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003209 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3210 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003211 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003212 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003213 activityRemoved = true;
3214 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003215 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003216 }
3217 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003218 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003219 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003220 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003221 }
3222
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003223 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3224 if (r.isDestroyable()) {
3225 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3226 + " resumed=" + mResumedActivity
3227 + " pausing=" + mPausingActivity + " for reason " + reason);
3228 return destroyActivityLocked(r, true, reason);
3229 }
3230 return false;
3231 }
3232
3233 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3234 String reason) {
3235 // Iterate over tasks starting at the back (oldest) first.
3236 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3237 int maxTasks = tasks.size() / 4;
3238 if (maxTasks < 1) {
3239 maxTasks = 1;
3240 }
3241 int numReleased = 0;
3242 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3243 final TaskRecord task = mTaskHistory.get(taskNdx);
3244 if (!tasks.contains(task)) {
3245 continue;
3246 }
3247 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3248 int curNum = 0;
3249 final ArrayList<ActivityRecord> activities = task.mActivities;
3250 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3251 final ActivityRecord activity = activities.get(actNdx);
3252 if (activity.app == app && activity.isDestroyable()) {
3253 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3254 + " in state " + activity.state + " resumed=" + mResumedActivity
3255 + " pausing=" + mPausingActivity + " for reason " + reason);
3256 destroyActivityLocked(activity, true, reason);
3257 if (activities.get(actNdx) != activity) {
3258 // Was removed from list, back up so we don't miss the next one.
3259 actNdx--;
3260 }
3261 curNum++;
3262 }
3263 }
3264 if (curNum > 0) {
3265 numReleased += curNum;
3266 maxTasks--;
3267 if (mTaskHistory.get(taskNdx) != task) {
3268 // The entire task got removed, back up so we don't miss the next one.
3269 taskNdx--;
3270 }
3271 }
3272 }
3273 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3274 return numReleased;
3275 }
3276
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003277 /**
3278 * Destroy the current CLIENT SIDE instance of an activity. This may be
3279 * called both when actually finishing an activity, or when performing
3280 * a configuration switch where we destroy the current client-side object
3281 * but then create a new client-side object for this same HistoryRecord.
3282 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003283 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003284 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003285 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3287 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003288 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003289 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003290
3291 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003292
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003293 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003294
3295 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003296
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297 if (hadApp) {
3298 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003299 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3301 mService.mHeavyWeightProcess = null;
3302 mService.mHandler.sendEmptyMessage(
3303 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3304 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003305 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003306 // Update any services we are bound to that might care about whether
3307 // their client may have activities.
3308 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003309 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003310 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003311 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 }
3313 }
3314
3315 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003316
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003317 try {
3318 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003319 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 r.configChangeFlags);
3321 } catch (Exception e) {
3322 // We can just ignore exceptions here... if the process
3323 // has crashed, our death notification will clean things
3324 // up.
3325 //Slog.w(TAG, "Exception thrown during finish", e);
3326 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003327 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328 removedFromHistory = true;
3329 skipDestroy = true;
3330 }
3331 }
3332
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003334
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003335 // If the activity is finishing, we need to wait on removing it
3336 // from the list to give it a chance to do its cleanup. During
3337 // that time it may make calls back with its token so we need to
3338 // be able to find it on the list and so we don't want to remove
3339 // it from the list yet. Otherwise, we can just immediately put
3340 // it in the destroyed state since we are not removing it from the
3341 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003342 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003343 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3344 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003346 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3348 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003349 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003350 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003351 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003352 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 }
3354 } else {
3355 // remove this record from the history.
3356 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003357 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 removedFromHistory = true;
3359 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003360 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003362 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003363 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 }
3365 }
3366
3367 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 if (!mLRUActivities.remove(r) && hadApp) {
3370 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3371 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003373 return removedFromHistory;
3374 }
3375
Craig Mautner299f9602015-01-26 09:47:33 -08003376 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003377 final long origId = Binder.clearCallingIdentity();
3378 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003379 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003380 if (r != null) {
3381 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003383 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003384
Wale Ogunwale60454db2015-01-23 16:05:07 -08003385 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003386 if (r.state == ActivityState.DESTROYING) {
3387 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003388 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003389 }
3390 }
Craig Mautner05d29032013-05-03 13:40:13 -07003391 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003392 } finally {
3393 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003394 }
3395 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003396
Todd Kennedyaab56db2015-01-30 09:39:53 -08003397 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003398 if (hasVisibleBehindActivity() &&
3399 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003400 if (r == topRunningActivityLocked(null)) {
3401 // Don't release the top activity if it has requested to run behind the next
3402 // activity.
3403 return;
3404 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003405 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3406 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003407 " thread=" + r.app.thread);
3408 if (r != null && r.app != null && r.app.thread != null) {
3409 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003410 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003411 } catch (RemoteException e) {
3412 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003413 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003414 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003415 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003416 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003417 }
3418 }
3419 }
3420
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003421 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003422 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3423 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003424 if (r != null) {
3425 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003426 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003427 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003428 }
3429 mStackSupervisor.resumeTopActivitiesLocked();
3430 }
3431
Jose Lima4b6c6692014-08-12 17:41:12 -07003432 boolean hasVisibleBehindActivity() {
3433 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003434 }
3435
Jose Lima4b6c6692014-08-12 17:41:12 -07003436 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003437 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003438 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003439 }
3440 }
3441
Jose Lima4b6c6692014-08-12 17:41:12 -07003442 ActivityRecord getVisibleBehindActivity() {
3443 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003444 }
3445
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003446 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3447 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003449 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3450 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 while (i > 0) {
3452 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003453 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003454 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003455 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003456 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003458 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 }
3460 }
3461 }
3462
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003463 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3464 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003465 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3466 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003467 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3468 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003469 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003470 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003471 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3472 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003473
3474 boolean hasVisibleActivities = false;
3475
3476 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003477 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003478 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3479 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3481 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3482 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3483 final ActivityRecord r = activities.get(activityNdx);
3484 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003485 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3486 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003487 if (r.app == app) {
Craig Mautneracebdc82015-02-24 10:53:03 -08003488 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003489 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3490 // Don't currently have state for the activity, or
3491 // it is finishing -- always remove it.
3492 remove = true;
3493 } else if (r.launchCount > 2 &&
3494 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3495 // We have launched this activity too many times since it was
3496 // able to run, so give up and remove it.
3497 remove = true;
3498 } else {
3499 // The process may be gone, but the activity lives on!
3500 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003501 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003502 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003503 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003504 RuntimeException here = new RuntimeException("here");
3505 here.fillInStackTrace();
3506 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3507 + ": haveState=" + r.haveState
3508 + " stateNotNeeded=" + r.stateNotNeeded
3509 + " finishing=" + r.finishing
3510 + " state=" + r.state, here);
3511 }
3512 if (!r.finishing) {
3513 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3514 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3515 r.userId, System.identityHashCode(r),
3516 r.task.taskId, r.shortComponentName,
3517 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003518 if (r.state == ActivityState.RESUMED) {
3519 mService.updateUsageStats(r, false);
3520 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003521 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003522 } else {
3523 // We have the current state for this activity, so
3524 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003525 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003526 TAG, "Keeping entry, setting app to null");
3527 if (r.visible) {
3528 hasVisibleActivities = true;
3529 }
3530 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3531 + r);
3532 r.app = null;
3533 r.nowVisible = false;
3534 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003535 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003536 "App died, clearing saved state of " + r);
3537 r.icicle = null;
3538 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003539 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003540 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003541 if (remove) {
3542 removeActivityFromHistoryLocked(r, "appDied");
3543 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003544 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003545 }
3546 }
3547
3548 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003549 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003550
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003551 final void updateTransitLocked(int transit, Bundle options) {
3552 if (options != null) {
3553 ActivityRecord r = topRunningActivityLocked(null);
3554 if (r != null && r.state != ActivityState.RESUMED) {
3555 r.updateOptionsLocked(options);
3556 } else {
3557 ActivityOptions.abort(options);
3558 }
3559 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003560 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003561 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003562
Craig Mautner21d24a22014-04-23 11:45:37 -07003563 void updateTaskMovement(TaskRecord task, boolean toFront) {
3564 if (task.isPersistable) {
3565 task.mLastTimeMoved = System.currentTimeMillis();
3566 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3567 // recently will be most negative, tasks sent to the bottom before that will be less
3568 // negative. Similarly for recent tasks moved to the top which will be most positive.
3569 if (!toFront) {
3570 task.mLastTimeMoved *= -1;
3571 }
3572 }
3573 }
3574
Craig Mautner84984fa2014-06-19 11:19:20 -07003575 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003576 final int top = mTaskHistory.size() - 1;
3577 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3578 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003579 if (task.taskType == homeStackTaskType) {
3580 if (DEBUG_TASKS || DEBUG_STACK)
3581 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003582 mTaskHistory.remove(taskNdx);
3583 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003584 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003585 return;
3586 }
3587 }
3588 }
3589
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003590 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003591 String reason) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003592 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003593
Craig Mautner11bf9a52013-02-19 14:08:51 -08003594 final int numTasks = mTaskHistory.size();
3595 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003596 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003597 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003598 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003599 ActivityOptions.abort(options);
3600 } else {
3601 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3602 }
3603 return;
3604 }
3605
3606 // Shift all activities with this task up to the top
3607 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003608 insertTaskAtTop(tr);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003609
3610 // Set focus to the top running activity of this stack.
3611 ActivityRecord r = topRunningActivityLocked(null);
3612 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003613
3614 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003615 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003616 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 if (r != null) {
3618 mNoAnimActivities.add(r);
3619 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003620 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003621 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003622 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003624
Craig Mautner05d29032013-05-03 13:40:13 -07003625 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003626 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003627
3628 if (VALIDATE_TOKENS) {
3629 validateAppTokensLocked();
3630 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003631 }
3632
3633 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003634 * Worker method for rearranging history stack. Implements the function of moving all
3635 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003637 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3639 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003640 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003641 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003642 * @return Returns true if the move completed, false if not.
3643 */
Craig Mautner299f9602015-01-26 09:47:33 -08003644 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003645 final TaskRecord tr = taskForIdLocked(taskId);
3646 if (tr == null) {
3647 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3648 return false;
3649 }
3650
3651 Slog.i(TAG, "moveTaskToBack: " + tr);
3652
3653 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003654
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003655 // If we have a watcher, preflight the move before committing to it. First check
3656 // for *other* available tasks, but if none are available, then try again allowing the
3657 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003658 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003659 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003660 if (next == null) {
3661 next = topRunningActivityLocked(null, 0);
3662 }
3663 if (next != null) {
3664 // ask watcher if this is allowed
3665 boolean moveOK = true;
3666 try {
3667 moveOK = mService.mController.activityResuming(next.packageName);
3668 } catch (RemoteException e) {
3669 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003670 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003671 }
3672 if (!moveOK) {
3673 return false;
3674 }
3675 }
3676 }
3677
Wale Ogunwalecb82f302015-02-25 07:53:40 -08003678 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003679
riddle_hsuc215a4f2014-12-27 12:10:45 +08003680 boolean prevIsHome = false;
3681 if (tr.isOverHomeStack()) {
3682 final TaskRecord nextTask = getNextTask(tr);
3683 if (nextTask != null) {
3684 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3685 } else {
3686 prevIsHome = true;
3687 }
3688 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003689 mTaskHistory.remove(tr);
3690 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003691 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692
Craig Mautnerc8143c62013-09-03 12:15:57 -07003693 // There is an assumption that moving a task to the back moves it behind the home activity.
3694 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003695 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003696 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3697 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003698 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003699 break;
3700 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003701 if (taskNdx == 1) {
3702 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003703 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003704 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003705 }
3706
Craig Mautner299f9602015-01-26 09:47:33 -08003707 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003708 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003709
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003711 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003712 }
3713
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003714 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003715 if (prevIsHome || task == tr && tr.isOverHomeStack()
3716 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003717 if (!mService.mBooting && !mService.mBooted) {
3718 // Not ready yet!
3719 return false;
3720 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003721 final int taskToReturnTo = tr.getTaskToReturnTo();
3722 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003723 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003724 }
3725
Craig Mautner05d29032013-05-03 13:40:13 -07003726 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003727 return true;
3728 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003729
Craig Mautner8849a5e2013-04-02 16:41:03 -07003730 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003732 final Uri data = r.intent.getData();
3733 final String strData = data != null ? data.toSafeString() : null;
3734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003735 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003736 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003737 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003738 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739 }
3740
3741 /**
3742 * Make sure the given activity matches the current configuration. Returns
3743 * false if the activity had to be destroyed. Returns true if the
3744 * configuration is the same, or the activity will remain running as-is
3745 * for whatever reason. Ensures the HistoryRecord is updated with the
3746 * correct configuration and all other bookkeeping is handled.
3747 */
3748 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3749 int globalChanges) {
3750 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003751 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 "Skipping config check (will change): " + r);
3753 return true;
3754 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003755
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003756 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003757 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003758
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003759 // Make sure the current stack override configuration is supported by the top task
3760 // before continuing.
3761 final TaskRecord topTask = topTask();
3762 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3763 || (!topTask.mResizeable && !mFullscreen))) {
3764 final boolean prevFullscreen = mFullscreen;
3765 final Configuration newOverrideConfig =
3766 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3767 updateOverrideConfiguration(newOverrideConfig);
3768 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003769 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003770 "Updated stack config to support task=" + topTask
3771 + " resizeable=" + topTask.mResizeable
3772 + " mForcedFullscreen=" + mForcedFullscreen
3773 + " prevFullscreen=" + prevFullscreen
3774 + " mFullscreen=" + mFullscreen);
3775 }
3776
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003777 // Short circuit: if the two configurations are the exact same
3778 // object (the common case), then there is nothing to do.
3779 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003780 if (r.configuration == newConfig
3781 && r.stackConfigOverride == mOverrideConfig
3782 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003783 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 "Configuration unchanged in " + r);
3785 return true;
3786 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003787
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003788 // We don't worry about activities that are finishing.
3789 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003790 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003791 "Configuration doesn't matter in finishing " + r);
3792 r.stopFreezingScreenLocked(false);
3793 return true;
3794 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003795
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796 // Okay we now are going to make this activity have the new config.
3797 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003798 final Configuration oldConfig = r.configuration;
3799 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003800 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003801 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003802
3803 // Determine what has changed. May be nothing, if this is a config
3804 // that has come back from the app after going idle. In that case
3805 // we just want to leave the official config object now in the
3806 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003807 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003808 if (stackChanges == 0) {
3809 // {@link Configuration#diff} doesn't catch changes from unset values.
3810 // Check for changes we care about.
3811 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3812 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3813 }
3814 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3815 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3816 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3817 }
3818 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3819 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3820 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003821 }
3822 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003823 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003824 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003825 "Configuration no differences in " + r);
3826 return true;
3827 }
3828
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003829 // If the activity isn't currently running, just leave the new
3830 // configuration and it will pick that up next time it starts.
3831 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003832 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003833 "Configuration doesn't matter not running " + r);
3834 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003835 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003836 return true;
3837 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003838
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003839 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003840 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3841 "Checking to restart " + r.info.name + ": changed=0x"
3842 + Integer.toHexString(changes) + ", handles=0x"
3843 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3844
Dianne Hackborne6676352011-06-01 16:51:20 -07003845 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003846 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3847 r.configChangeFlags |= changes;
3848 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003849 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003850 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003851 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003852 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003853 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003854 } else if (r.state == ActivityState.PAUSING) {
3855 // A little annoying: we are waiting for this activity to
3856 // finish pausing. Let's not do anything now, but just
3857 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003858 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003859 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003860 r.configDestroy = true;
3861 return true;
3862 } else if (r.state == ActivityState.RESUMED) {
3863 // Try to optimize this case: the configuration is changing
3864 // and we need to restart the top, resumed activity.
3865 // Instead of doing the normal handshaking, just say
3866 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003867 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003868 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003869 relaunchActivityLocked(r, r.configChangeFlags, true);
3870 r.configChangeFlags = 0;
3871 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003872 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003873 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003874 relaunchActivityLocked(r, r.configChangeFlags, false);
3875 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003876 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003877
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003878 // All done... tell the caller we weren't able to keep this
3879 // activity around.
3880 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003881 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003882
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003883 // Default case: the activity can handle this new configuration, so hand it over.
3884 // NOTE: We only forward the stack override configuration as the system level configuration
3885 // changes is always sent to all processes when they happen so it can just use whatever
3886 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003887 if (r.app != null && r.app.thread != null) {
3888 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003889 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003890 r.app.thread.scheduleActivityConfigurationChanged(
3891 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003892 } catch (RemoteException e) {
3893 // If process died, whatever.
3894 }
3895 }
3896 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003898 return true;
3899 }
3900
Craig Mautnerc8143c62013-09-03 12:15:57 -07003901 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 int changes, boolean andResume) {
3903 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003904 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003905 if (andResume) {
3906 results = r.results;
3907 newIntents = r.newIntents;
3908 }
3909 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3910 + " with results=" + results + " newIntents=" + newIntents
3911 + " andResume=" + andResume);
3912 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003913 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003914 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003915
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003916 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003917
Craig Mautner34b73df2014-01-12 21:11:08 -08003918 mStackSupervisor.removeChildActivityContainers(r);
3919
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003920 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003921 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3922 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3923 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003924 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003925 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3926 !andResume, new Configuration(mService.mConfiguration),
3927 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003928 // Note: don't need to call pauseIfSleepingLocked() here, because
3929 // the caller will only pass in 'andResume' if this activity is
3930 // currently resumed, which implies we aren't sleeping.
3931 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003932 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003933 }
3934
3935 if (andResume) {
3936 r.results = null;
3937 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003938 r.state = ActivityState.RESUMED;
3939 } else {
3940 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3941 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003942 }
3943
3944 return true;
3945 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003946
3947 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003948 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3949 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3950 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3951 final ActivityRecord r = activities.get(activityNdx);
3952 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003953 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003954 }
3955 if (r.fullscreen && !r.finishing) {
3956 return false;
3957 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003958 }
3959 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003960 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003961 if (r == null) {
3962 return false;
3963 }
3964 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3965 + " would have returned true for r=" + r);
3966 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003967 }
3968
3969 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003970 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3971 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3972 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3973 final ActivityRecord r = activities.get(activityNdx);
3974 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003975 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003976 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003977 }
3978 }
3979 }
3980
3981 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3982 boolean didSomething = false;
3983 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003984 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003985 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3986 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3987 int numActivities = activities.size();
3988 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3989 ActivityRecord r = activities.get(activityNdx);
3990 final boolean samePackage = r.packageName.equals(name)
3991 || (name == null && r.userId == userId);
3992 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3993 && (samePackage || r.task == lastTask)
3994 && (r.app == null || evenPersistent || !r.app.persistent)) {
3995 if (!doit) {
3996 if (r.finishing) {
3997 // If this activity is just finishing, then it is not
3998 // interesting as far as something to stop.
3999 continue;
4000 }
4001 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004002 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004003 if (r.isHomeActivity()) {
4004 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4005 Slog.i(TAG, "Skip force-stop again " + r);
4006 continue;
4007 } else {
4008 homeActivity = r.realActivity;
4009 }
4010 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004011 didSomething = true;
4012 Slog.i(TAG, " Force finishing activity " + r);
4013 if (samePackage) {
4014 if (r.app != null) {
4015 r.app.removed = true;
4016 }
4017 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004018 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004019 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004020 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4021 true)) {
4022 // r has been deleted from mActivities, accommodate.
4023 --numActivities;
4024 --activityNdx;
4025 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004026 }
4027 }
4028 }
4029 return didSomething;
4030 }
4031
Dianne Hackborn09233282014-04-30 11:33:59 -07004032 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004033 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004034 final TaskRecord task = mTaskHistory.get(taskNdx);
4035 ActivityRecord r = null;
4036 ActivityRecord top = null;
4037 int numActivities = 0;
4038 int numRunning = 0;
4039 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07004040 if (activities.isEmpty()) {
4041 continue;
4042 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004043 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004044 continue;
4045 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004046 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4047 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004048
Craig Mautneraab647e2013-02-28 16:31:36 -08004049 // Initialize state for next task if needed.
4050 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4051 top = r;
4052 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004053 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004054
4055 // Add 'r' into the current task.
4056 numActivities++;
4057 if (r.app != null && r.app.thread != null) {
4058 numRunning++;
4059 }
4060
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004061 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004062 TAG, r.intent.getComponent().flattenToShortString()
4063 + ": task=" + r.task);
4064 }
4065
4066 RunningTaskInfo ci = new RunningTaskInfo();
4067 ci.id = task.taskId;
4068 ci.baseActivity = r.intent.getComponent();
4069 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004070 ci.lastActiveTime = task.lastActiveTime;
4071
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004072 if (top.task != null) {
4073 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004074 }
4075 ci.numActivities = numActivities;
4076 ci.numRunning = numRunning;
4077 //System.out.println(
4078 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08004079 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004080 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004081 }
4082
4083 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004084 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004085 if (DEBUG_SWITCH) Slog.d(
4086 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004087 if (top >= 0) {
4088 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4089 int activityTop = activities.size() - 1;
4090 if (activityTop > 0) {
4091 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4092 "unhandled-back", true);
4093 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004094 }
4095 }
4096
Craig Mautner6b74cb52013-09-27 17:02:21 -07004097 /**
4098 * Reset local parameters because an app's activity died.
4099 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004100 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004101 */
4102 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004103 if (mPausingActivity != null && mPausingActivity.app == app) {
4104 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
4105 "App died while pausing: " + mPausingActivity);
4106 mPausingActivity = null;
4107 }
4108 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4109 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004110 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004111 }
4112
Craig Mautner19091252013-10-05 00:03:53 -07004113 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004114 }
4115
Craig Mautnercae015f2013-02-08 14:31:27 -08004116 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004117 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4118 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4119 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4120 final ActivityRecord r = activities.get(activityNdx);
4121 if (r.app == app) {
4122 Slog.w(TAG, " Force finishing activity "
4123 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004124 // Force the destroy to skip right to removal.
4125 r.app = null;
4126 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004127 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004128 }
4129 }
4130 }
4131
Dianne Hackborn390517b2013-05-30 15:03:32 -07004132 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004133 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004134 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004135 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4136 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004137 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4138 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004139 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004140 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004141 if (printed) {
4142 header = null;
4143 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004144 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004145 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004146 }
4147
4148 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4149 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4150
4151 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004152 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4153 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004154 }
4155 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004156 final int top = mTaskHistory.size() - 1;
4157 if (top >= 0) {
4158 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4159 int listTop = list.size() - 1;
4160 if (listTop >= 0) {
4161 activities.add(list.get(listTop));
4162 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004163 }
4164 } else {
4165 ItemMatcher matcher = new ItemMatcher();
4166 matcher.build(name);
4167
Craig Mautneraab647e2013-02-28 16:31:36 -08004168 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4169 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4170 if (matcher.match(r1, r1.intent.getComponent())) {
4171 activities.add(r1);
4172 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004173 }
4174 }
4175 }
4176
4177 return activities;
4178 }
4179
4180 ActivityRecord restartPackage(String packageName) {
4181 ActivityRecord starting = topRunningActivityLocked(null);
4182
4183 // All activities that came from the package must be
4184 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004185 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4186 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4187 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4188 final ActivityRecord a = activities.get(activityNdx);
4189 if (a.info.packageName.equals(packageName)) {
4190 a.forceNewConfig = true;
4191 if (starting != null && a == starting && a.visible) {
4192 a.startFreezingScreenLocked(starting.app,
4193 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4194 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004195 }
4196 }
4197 }
4198
4199 return starting;
4200 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004201
Craig Mautner299f9602015-01-26 09:47:33 -08004202 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004203 removeTask(task, reason, true);
4204 }
4205
4206 void removeTask(TaskRecord task, String reason, boolean removeFromWindowManager) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004207 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004208 if (removeFromWindowManager) {
4209 mWindowManager.removeTask(task.taskId);
4210 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004211 final ActivityRecord r = mResumedActivity;
4212 if (r != null && r.task == task) {
4213 mResumedActivity = null;
4214 }
4215
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004216 final int taskNdx = mTaskHistory.indexOf(task);
4217 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004218 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4219 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4220 if (!nextTask.isOverHomeStack()) {
4221 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4222 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004223 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004224 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004225 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004226
4227 if (task.mActivities.isEmpty()) {
4228 final boolean isVoiceSession = task.voiceSession != null;
4229 if (isVoiceSession) {
4230 try {
4231 task.voiceSession.taskFinished(task.intent, task.taskId);
4232 } catch (RemoteException e) {
4233 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004234 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004235 if (task.autoRemoveFromRecents() || isVoiceSession) {
4236 // Task creator asked to remove this when done, or this task was a voice
4237 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004238 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004239 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004240 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004241 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004242
4243 if (mTaskHistory.isEmpty()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004244 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004245 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004246 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004247 String myReason = reason + " leftTaskHistoryEmpty";
4248 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4249 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4250 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004251 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004252 if (mStacks != null) {
4253 mStacks.remove(this);
4254 mStacks.add(0, this);
4255 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004256 if (notHomeStack) {
4257 mActivityContainer.onTaskListEmptyLocked();
4258 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004259 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004260
4261 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004262 }
4263
Dianne Hackborn91097de2014-04-04 18:02:06 -07004264 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4265 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4266 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004267 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4268 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004269 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004270 return task;
4271 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004272
4273 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004274 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004275 }
4276
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004277 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004278 task.stack = this;
4279 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004280 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004281 } else {
4282 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004283 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004284 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004285 if (!moving && task.voiceSession != null) {
4286 try {
4287 task.voiceSession.taskStarted(task.intent, task.taskId);
4288 } catch (RemoteException e) {
4289 }
4290 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004291 }
4292
4293 public int getStackId() {
4294 return mStackId;
4295 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004296
4297 @Override
4298 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004299 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4300 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004301 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004302
4303 boolean updateOverrideConfiguration(Configuration newConfig) {
4304 Configuration oldConfig = mOverrideConfig;
4305 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004306 // We override the configuration only when the stack's dimensions are different from
4307 // the display. In this manner, we know that if the override configuration is empty,
4308 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004309 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004310 return !mOverrideConfig.equals(oldConfig);
4311 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004312}