blob: 1fee1aedbf0fbe282e893a3248099d59fc9b2620 [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 Ogunwale3fcb4a82015-04-06 14:00:13 -070019import static android.content.pm.ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN;
20
Wale Ogunwalee23149f2015-03-06 15:39:44 -080021import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070022
Craig Mautner84984fa2014-06-19 11:19:20 -070023import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
24import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070025
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
27import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070028import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070029import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070030import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070031import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070032import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070033import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
34
Dianne Hackborn89ad4562014-08-24 16:45:38 -070035import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070036import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080037import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070039import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080040import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080041import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080042import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080043import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070044import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070045
46import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070047import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070048import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080050import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080058import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080059import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070061import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080062import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Handler;
64import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090065import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070067import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.RemoteException;
69import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080070import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070071import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070072import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070075import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076
Craig Mautnercae015f2013-02-08 14:31:27 -080077import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080078import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import java.lang.ref.WeakReference;
80import java.util.ArrayList;
81import java.util.Iterator;
82import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080083import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084
85/**
86 * State and management of a single stack of activities.
87 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070088final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080089
Wale Ogunwalee23149f2015-03-06 15:39:44 -080090 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
91 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070092 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070093 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
94 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
95 private static final String TAG_STACK = TAG + POSTFIX_STACK;
96 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
97 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
98 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
99 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
100 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700101
102 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800103
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700104 // Ticks during which we check progress while waiting for an app to launch.
105 static final int LAUNCH_TICK = 500;
106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700107 // How long we wait until giving up on the last activity to pause. This
108 // is short because it directly impacts the responsiveness of starting the
109 // next activity.
110 static final int PAUSE_TIMEOUT = 500;
111
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700112 // How long we wait for the activity to tell us it has stopped before
113 // giving up. This is a good amount of time because we really need this
114 // from the application in order to get its saved state.
115 static final int STOP_TIMEOUT = 10*1000;
116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // How long we wait until giving up on an activity telling us it has
118 // finished destroying itself.
119 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800122 // disabled.
123 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700125 // How long between activity launches that we consider safe to not warn
126 // the user about an unexpected activity being launched on top.
127 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 // Set to false to disable the preview that is shown while a new activity
130 // is being started.
131 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800132
Craig Mautner5eda9b32013-07-02 11:58:16 -0700133 // How long to wait for all background Activities to redraw following a call to
134 // convertToTranslucent().
135 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
136
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) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700378 return isCurrentProfileLocked(r.userId) || (r.info.flags & FLAG_SHOW_ON_LOCK_SCREEN) != 0;
Craig Mautner5962b122012-10-05 14:45:52 -0700379 }
380
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700381 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800382 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700383 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
384 if (r != null) {
385 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800386 }
387 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 return null;
389 }
390
391 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800392 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
393 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800394 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800395 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
396 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800397 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800398 return r;
399 }
400 }
401 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700402 return null;
403 }
404
405 /**
406 * This is a simplified version of topRunningActivityLocked that provides a number of
407 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800408 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700409 * @param token If non-null, any history records matching this token will be skipped.
410 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800411 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 * @return Returns the HistoryRecord of the next activity on the stack.
413 */
414 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800415 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
416 TaskRecord task = mTaskHistory.get(taskNdx);
417 if (task.taskId == taskId) {
418 continue;
419 }
420 ArrayList<ActivityRecord> activities = task.mActivities;
421 for (int i = activities.size() - 1; i >= 0; --i) {
422 final ActivityRecord r = activities.get(i);
423 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800424 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800425 return r;
426 }
427 }
428 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700429 return null;
430 }
431
Craig Mautner8849a5e2013-04-02 16:41:03 -0700432 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700433 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
434 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700435 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
436 final ActivityRecord r = activities.get(activityNdx);
437 if (!r.finishing) {
438 return r;
439 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700440 }
441 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700442 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700443 }
444
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700445 final TaskRecord topTask() {
446 final int size = mTaskHistory.size();
447 if (size > 0) {
448 return mTaskHistory.get(size - 1);
449 }
450 return null;
451 }
452
Craig Mautnerd2328952013-03-05 12:46:26 -0800453 TaskRecord taskForIdLocked(int id) {
454 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
455 final TaskRecord task = mTaskHistory.get(taskNdx);
456 if (task.taskId == id) {
457 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800458 }
459 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700460 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700461 }
462
Craig Mautnerd2328952013-03-05 12:46:26 -0800463 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700464 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800465 return isInStackLocked(r);
466 }
467
468 ActivityRecord isInStackLocked(ActivityRecord r) {
469 if (r == null) {
470 return null;
471 }
472 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700473 if (task != null && task.stack != null
474 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800475 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800476 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800477 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800478 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800479 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800480 }
481
Craig Mautner2420ead2013-04-01 17:13:20 -0700482 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700483 final boolean hadit = mLRUActivities.remove(r);
484 mLRUActivities.add(r);
485 return hadit;
486 }
487
Craig Mautnerde4ef022013-04-07 19:01:33 -0700488 final boolean isHomeStack() {
489 return mStackId == HOME_STACK_ID;
490 }
491
Craig Mautnere0a38842013-12-16 16:14:02 -0800492 final boolean isOnHomeDisplay() {
493 return isAttached() &&
494 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
495 }
496
Craig Mautner299f9602015-01-26 09:47:33 -0800497 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800498 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800499 final boolean homeStack = isHomeStack()
500 || (mActivityContainer.mParentActivity != null
501 && mActivityContainer.mParentActivity.isHomeActivity());
502 ActivityStack lastFocusStack = null;
503 if (!homeStack) {
504 // Need to move this stack to the front before calling
505 // {@link ActivityStackSupervisor#moveHomeStack} below.
506 lastFocusStack = mStacks.get(mStacks.size() - 1);
507 mStacks.remove(this);
508 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800509 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700510 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
511 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800512 if (isOnHomeDisplay()) {
513 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
514 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800515 final TaskRecord task = topTask();
516 if (task != null) {
517 mWindowManager.moveTaskToTop(task.taskId);
518 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800519 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800520 }
521
522 final boolean isAttached() {
523 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800524 }
525
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700526 /**
527 * Returns the top activity in any existing task matching the given
528 * Intent. Returns null if no such task is found.
529 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700530 ActivityRecord findTaskLocked(ActivityRecord target) {
531 Intent intent = target.intent;
532 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700533 ComponentName cls = intent.getComponent();
534 if (info.targetActivity != null) {
535 cls = new ComponentName(info.packageName, info.targetActivity);
536 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700537 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700538 boolean isDocument = intent != null & intent.isDocument();
539 // If documentData is non-null then it must match the existing task data.
540 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800541
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700542 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800543 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
544 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700545 if (task.voiceSession != null) {
546 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700547 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700548 continue;
549 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700550 if (task.userId != userId) {
551 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700552 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700553 continue;
554 }
Craig Mautner000f0022013-02-26 15:04:29 -0800555 final ActivityRecord r = task.getTopActivity();
556 if (r == null || r.finishing || r.userId != userId ||
557 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700558 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800559 continue;
560 }
561
Craig Mautnerd00f4742014-03-12 14:17:26 -0700562 final Intent taskIntent = task.intent;
563 final Intent affinityIntent = task.affinityIntent;
564 final boolean taskIsDocument;
565 final Uri taskDocumentData;
566 if (taskIntent != null && taskIntent.isDocument()) {
567 taskIsDocument = true;
568 taskDocumentData = taskIntent.getData();
569 } else if (affinityIntent != null && affinityIntent.isDocument()) {
570 taskIsDocument = true;
571 taskDocumentData = affinityIntent.getData();
572 } else {
573 taskIsDocument = false;
574 taskDocumentData = null;
575 }
576
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700577 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700578 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700579 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700580 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700581 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
582 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700584 return r;
585 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700586 } else if (taskIntent != null && taskIntent.getComponent() != null &&
587 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700588 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800590 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700591 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
592 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800593 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700594 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
595 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700596 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700597 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800598 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700599 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
600 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800601 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700602 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700603 }
604
605 return null;
606 }
607
608 /**
609 * Returns the first activity (starting from the top of the stack) that
610 * is the same as the given activity. Returns null if no such activity
611 * is found.
612 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700613 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700614 ComponentName cls = intent.getComponent();
615 if (info.targetActivity != null) {
616 cls = new ComponentName(info.packageName, info.targetActivity);
617 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700618 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619
Craig Mautner000f0022013-02-26 15:04:29 -0800620 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700621 final TaskRecord task = mTaskHistory.get(taskNdx);
622 final boolean notCurrentUserTask = !isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700623 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700624
Craig Mautner000f0022013-02-26 15:04:29 -0800625 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
626 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700627 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_ON_LOCK_SCREEN) == 0) {
628 return null;
629 }
Craig Mautner000f0022013-02-26 15:04:29 -0800630 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 //Slog.i(TAG, "Found matching class!");
632 //dump();
633 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
634 return r;
635 }
636 }
637 }
638
639 return null;
640 }
641
Amith Yamasani742a6712011-05-04 14:49:28 -0700642 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700643 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700644 */
Craig Mautner93529a42013-10-04 15:03:13 -0700645 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800646 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700647 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800648 }
649 mCurrentUser = userId;
650
651 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800652 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800653 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700654 final TaskRecord task = mTaskHistory.get(i);
655
656 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
657 // okay to show the activity when locked.
658 if (isCurrentProfileLocked(task.userId)
659 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700660 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700661 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800662 mTaskHistory.remove(i);
663 mTaskHistory.add(task);
664 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800665 // Use same value for i.
666 } else {
667 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800668 }
669 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700670 if (VALIDATE_TOKENS) {
671 validateAppTokensLocked();
672 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700673 }
674
Craig Mautner2420ead2013-04-01 17:13:20 -0700675 void minimalResumeActivityLocked(ActivityRecord r) {
676 r.state = ActivityState.RESUMED;
677 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
678 + " (starting new instance)");
679 r.stopped = false;
680 mResumedActivity = r;
681 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800682 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700683 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700684 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700685 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700686 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700687 }
688
Dianne Hackborncee04b52013-07-03 17:01:28 -0700689 private void startLaunchTraces() {
690 if (mFullyDrawnStartTime != 0) {
691 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
692 }
693 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
694 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
695 }
696
697 private void stopFullyDrawnTraceIfNeeded() {
698 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
699 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
700 mFullyDrawnStartTime = 0;
701 }
702 }
703
Craig Mautnere79d42682013-04-01 19:01:53 -0700704 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700705 if (r.displayStartTime == 0) {
706 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
707 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700708 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700709 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700710 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700711 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700712 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700713 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700714 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700715 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800716
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700717 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700718 // Make sure that there is no activity waiting for this to launch.
719 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
720 r.displayStartTime = r.fullyDrawnStartTime = 0;
721 } else {
722 mStackSupervisor.removeTimeoutsForActivityLocked(r);
723 mStackSupervisor.scheduleIdleTimeoutLocked(r);
724 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700725 }
726
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800728 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800729 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
730 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
731 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
732 activities.get(activityNdx).setSleeping(false);
733 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800734 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800735 if (mPausingActivity != null) {
736 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
737 activityPausedLocked(mPausingActivity.appToken, true);
738 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800739 }
740
Craig Mautner0eea92c2013-05-16 13:35:39 -0700741 /**
742 * @return true if something must be done before going to sleep.
743 */
744 boolean checkReadyForSleepLocked() {
745 if (mResumedActivity != null) {
746 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700747 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
748 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
749 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700750 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700751 return true;
752 }
753 if (mPausingActivity != null) {
754 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700755 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700756 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800757 }
758
Craig Mautner0eea92c2013-05-16 13:35:39 -0700759 return false;
760 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800761
Craig Mautner0eea92c2013-05-16 13:35:39 -0700762 void goToSleep() {
763 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800764
Craig Mautner0eea92c2013-05-16 13:35:39 -0700765 // Make sure any stopped but visible activities are now sleeping.
766 // This ensures that the activity's onStop() is called.
767 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
768 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
769 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
770 final ActivityRecord r = activities.get(activityNdx);
771 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
772 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800773 }
774 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800775 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 }
Craig Mautner59c00972012-07-30 12:10:24 -0700777
Dianne Hackbornd2835932010-12-13 16:28:46 -0800778 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700779 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800780 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700781 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800782 return null;
783 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800784
Winson Chung083baf92014-07-11 10:32:42 -0700785 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700786 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800787 // we can just go ahead and skip taking the screenshot if this is the home stack.
788 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700789 return null;
790 }
791
Winson Chung48a10a52014-08-27 14:36:51 -0700792 int w = mService.mThumbnailWidth;
793 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800794 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800795 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
796 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700797 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800798 }
Winson Chung376543b2014-05-21 17:43:28 -0700799 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800800 return null;
801 }
802
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700803 /**
804 * Start pausing the currently resumed activity. It is an error to call this if there
805 * is already an activity being paused or there is no resumed activity.
806 *
807 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
808 * @param uiSleeping True if this is happening with the user interface going to sleep (the
809 * screen turning off).
810 * @param resuming True if this is being called as part of resuming the top activity, so
811 * we shouldn't try to instigate a resume here.
812 * @param dontWait True if the caller does not want to wait for the pause to complete. If
813 * set to true, we will immediately complete the pause here before returning.
814 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
815 * it to tell us when it is done.
816 */
817 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
818 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800819 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700820 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
821 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800822 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 ActivityRecord prev = mResumedActivity;
824 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700825 if (!resuming) {
826 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
827 mStackSupervisor.resumeTopActivitiesLocked();
828 }
829 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700830 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800831
832 if (mActivityContainer.mParentActivity == null) {
833 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700834 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800835 }
836
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700837 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700838 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800840 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700841 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700842 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
843 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 prev.state = ActivityState.PAUSING;
845 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700846 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700847 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800848 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800849 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700850 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700851 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700852
853 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800854
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700855 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700856 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 try {
858 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700859 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700861 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800862 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700863 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 } catch (Exception e) {
865 // Ignore exception, if process died other code will cleanup.
866 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800867 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700869 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 }
871 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800872 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700873 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700874 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 }
876
877 // If we are not going to sleep, we want to ensure the device is
878 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700879 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700880 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700881 }
882
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800883 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700884 // Have the window manager pause its key dispatching until the new
885 // activity has started. If we're pausing the activity just because
886 // the screen is being turned off and the UI is sleeping, don't interrupt
887 // key dispatch; the same activity will pick it up again on wakeup.
888 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800889 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700890 } else if (DEBUG_PAUSE) {
891 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892 }
893
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700894 if (dontWait) {
895 // If the caller said they don't want to wait for the pause, then complete
896 // the pause now.
897 completePauseLocked(false);
898 return false;
899
900 } else {
901 // Schedule a pause timeout in case the app doesn't respond.
902 // We don't give it much time because this directly impacts the
903 // responsiveness seen by the user.
904 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
905 msg.obj = prev;
906 prev.pauseTime = SystemClock.uptimeMillis();
907 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700908 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700909 return true;
910 }
911
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 } else {
913 // This activity failed to schedule the
914 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700915 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700916 if (!resuming) {
917 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
918 }
919 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 }
921 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700922
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700923 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700924 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
925 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926
Craig Mautnerd2328952013-03-05 12:46:26 -0800927 final ActivityRecord r = isInStackLocked(token);
928 if (r != null) {
929 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
930 if (mPausingActivity == r) {
931 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
932 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700933 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800934 } else {
935 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
936 r.userId, System.identityHashCode(r), r.shortComponentName,
937 mPausingActivity != null
938 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700939 }
940 }
941 }
942
Craig Mautnera0026042014-04-23 11:45:37 -0700943 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
944 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700945 if (r.state != ActivityState.STOPPING) {
946 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
947 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
948 return;
949 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700950 if (persistentState != null) {
951 r.persistentState = persistentState;
952 mService.notifyTaskPersisterLocked(r.task, false);
953 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700954 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700955 if (icicle != null) {
956 // If icicle is null, this is happening due to a timeout, so we
957 // haven't really saved the state.
958 r.icicle = icicle;
959 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800960 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800961 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700962 }
963 if (!r.stopped) {
964 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
965 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
966 r.stopped = true;
967 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700968 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
969 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700970 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700971 if (r.finishing) {
972 r.clearOptionsLocked();
973 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700974 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700975 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700976 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700977 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700978 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800979 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700980 }
981 }
982 }
983
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700984 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800985 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700986 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800987
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700989 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800990 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700991 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700992 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700994 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800995 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700996 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
997 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800998 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800999 if (prev.configDestroy) {
1000 // The previous is being paused because the configuration
1001 // is changing, which means it is actually stopping...
1002 // To juggle the fact that we are also starting a new
1003 // instance right now, we need to first completely stop
1004 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001005 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001006 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -07001007 } else if (!hasVisibleBehindActivity()) {
1008 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001009 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001010 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001011 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1012 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 // If we already have a few activities waiting to stop,
1014 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001015 // them out. Or if r is the last of activity of the last task the stack
1016 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001017 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001018 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001019 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001020 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001021 }
1022 }
1023 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001024 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001025 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001026 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001027 // It is possible the activity was freezing the screen before it was paused.
1028 // In that case go ahead and remove the freeze this activity has on the screen
1029 // since it is no longer visible.
1030 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001031 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001032 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001033
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001034 if (resumeNext) {
1035 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1036 if (!mService.isSleepingOrShuttingDown()) {
1037 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1038 } else {
1039 mStackSupervisor.checkReadyForSleepLocked();
1040 ActivityRecord top = topStack.topRunningActivityLocked(null);
1041 if (top == null || (prev != null && top != prev)) {
1042 // If there are no more activities available to run,
1043 // do resume anyway to start something. Also if the top
1044 // activity on the stack is not the just paused activity,
1045 // we need to go ahead and resume it to ensure we complete
1046 // an in-flight app switch.
1047 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1048 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001049 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001051
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001052 if (prev != null) {
1053 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001054
Craig Mautner525f3d92013-05-07 14:01:50 -07001055 if (prev.app != null && prev.cpuTimeAtResume > 0
1056 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001057 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1058 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001059 if (diff > 0) {
1060 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1061 synchronized (bsi) {
1062 BatteryStatsImpl.Uid.Proc ps =
1063 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001064 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001065 if (ps != null) {
1066 ps.addForegroundTimeLocked(diff);
1067 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068 }
1069 }
1070 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001071 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001073
1074 // Notfiy when the task stack has changed
1075 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001076 }
1077
1078 /**
1079 * Once we know that we have asked an application to put an activity in
1080 * the resumed state (either by launching it or explicitly telling it),
1081 * this function updates the rest of our state to match that fact.
1082 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001083 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084 next.idle = false;
1085 next.results = null;
1086 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001087
1088 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1089 ProcessRecord app = next.task.mActivities.get(0).app;
1090 if (app != null && app != mService.mHomeProcess) {
1091 mService.mHomeProcess = app;
1092 }
1093 }
1094
Craig Mautner07566322013-09-26 16:42:55 -07001095 if (next.nowVisible) {
1096 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001097 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001098 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001099
1100 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001101 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001102
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001103 mStackSupervisor.reportResumedActivityLocked(next);
1104
1105 next.resumeKeyDispatchingLocked();
1106 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107
1108 // Mark the point when the activity is resuming
1109 // TODO: To be more accurate, the mark should be before the onCreate,
1110 // not after the onResume. But for subsequent starts, onResume is fine.
1111 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001112 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001113 } else {
1114 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1115 }
Winson Chung376543b2014-05-21 17:43:28 -07001116
George Mount6ba042b2014-07-28 11:12:28 -07001117 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001118
1119 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1120 // When resuming an activity, require it to call requestVisibleBehind() again.
1121 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1122 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 }
1124
Craig Mautner2568c3a2015-03-26 14:22:34 -07001125 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001126 r.visible = visible;
1127 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001128 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001129 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001130 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001131 container.setVisible(visible);
1132 }
1133 }
1134
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001135 // Find the first visible activity above the passed activity and if it is translucent return it
1136 // otherwise return null;
1137 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1138 TaskRecord task = r.task;
1139 if (task == null) {
1140 return null;
1141 }
1142
1143 ActivityStack stack = task.stack;
1144 if (stack == null) {
1145 return null;
1146 }
1147
1148 int stackNdx = mStacks.indexOf(stack);
1149
1150 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1151 int taskNdx = tasks.indexOf(task);
1152
1153 ArrayList<ActivityRecord> activities = task.mActivities;
1154 int activityNdx = activities.indexOf(r) + 1;
1155
1156 final int numStacks = mStacks.size();
1157 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001158 ActivityStack historyStack = mStacks.get(stackNdx);
1159 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001160 final int numTasks = tasks.size();
1161 while (taskNdx < numTasks) {
1162 activities = tasks.get(taskNdx).mActivities;
1163 final int numActivities = activities.size();
1164 while (activityNdx < numActivities) {
1165 final ActivityRecord activity = activities.get(activityNdx);
1166 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001167 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001168 }
1169 ++activityNdx;
1170 }
1171 activityNdx = 0;
1172 ++taskNdx;
1173 }
1174 taskNdx = 0;
1175 ++stackNdx;
1176 }
1177
1178 return null;
1179 }
1180
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001181 private ActivityStack getNextVisibleStackLocked() {
1182 ArrayList<ActivityStack> stacks = mStacks;
1183 final ActivityRecord parent = mActivityContainer.mParentActivity;
1184 if (parent != null) {
1185 stacks = parent.task.stack.mStacks;
1186 }
1187 if (stacks != null) {
1188 for (int i = stacks.size() - 1; i >= 0; --i) {
1189 ActivityStack stack = stacks.get(i);
1190 if (stack != this && stack.isStackVisibleLocked()) {
1191 return stack;
1192 }
1193 }
1194 }
1195 return null;
1196 }
1197
Jose Lima7ba71252014-04-30 12:59:27 -07001198 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1199 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001200 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001201 if (!isAttached()) {
1202 return false;
1203 }
1204
1205 if (mStackSupervisor.isFrontStack(this)) {
1206 return true;
1207 }
1208
Jose Lima729cb232014-05-05 15:59:40 -07001209 /**
1210 * Start at the task above this one and go up, looking for a visible
1211 * fullscreen activity, or a translucent activity that requested the
1212 * wallpaper to be shown behind it.
1213 */
Jose Lima7ba71252014-04-30 12:59:27 -07001214 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001215 ActivityStack stack = mStacks.get(i);
1216 // stack above isn't full screen, so, we assume we're still visible. at some point
1217 // we should look at the stack bounds to see if we're occluded even if the stack
1218 // isn't fullscreen
1219 if (!stack.mFullscreen) {
1220 continue;
1221 }
1222 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1223 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001224 final TaskRecord task = tasks.get(taskNdx);
1225 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001226 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001227 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001228
1229 // Conditions for an activity to obscure the stack we're
1230 // examining:
1231 // 1. Not Finishing AND Visible AND:
1232 // 2. Either:
1233 // - Full Screen Activity OR
1234 // - On top of Home and our stack is NOT home
1235 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001236 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001237 return false;
1238 }
1239 }
1240 }
1241 }
1242
1243 return true;
1244 }
1245
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001246 /**
1247 * Make sure that all activities that need to be visible (that is, they
1248 * currently can be seen by the user) actually are.
1249 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001250 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1251 ActivityRecord top = topRunningActivityLocked(null);
1252 if (top == null) {
1253 return;
1254 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001255 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1256 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001257 + " configChanges=0x" + Integer.toHexString(configChanges));
1258
Craig Mautner5eda9b32013-07-02 11:58:16 -07001259 if (mTranslucentActivityWaiting != top) {
1260 mUndrawnActivitiesBelowTopTranslucent.clear();
1261 if (mTranslucentActivityWaiting != null) {
1262 // Call the callback with a timeout indication.
1263 notifyActivityDrawnLocked(null);
1264 mTranslucentActivityWaiting = null;
1265 }
1266 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1267 }
1268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001269 // If the top activity is not fullscreen, then we need to
1270 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001271 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001272 boolean behindFullscreen = !isStackVisibleLocked();
Jose Lima7ba71252014-04-30 12:59:27 -07001273
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001274 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001275 final TaskRecord task = mTaskHistory.get(taskNdx);
1276 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001277 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1278 final ActivityRecord r = activities.get(activityNdx);
1279 if (r.finishing) {
1280 continue;
1281 }
1282 if (aboveTop && r != top) {
1283 continue;
1284 }
1285 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001286 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1287 // but must be drawn initially for the animation as though they were visible.
1288 if (!behindFullscreen || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001289 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1290 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001291 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001292
Craig Mautnerd44711d2013-02-23 11:24:36 -08001293 // First: if this is not the current activity being started, make
1294 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001295 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001296 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001297 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001298
1299 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001300 // This activity needs to be visible, but isn't even
1301 // running... get it started, but don't resume it
1302 // at this point.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001303 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1304 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001305 if (r != starting) {
1306 r.startFreezingScreenLocked(r.app, configChanges);
1307 }
1308 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001309 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1310 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001311 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001312 }
1313 if (r != starting) {
1314 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001315 }
1316
1317 } else if (r.visible) {
1318 // If this activity is already visible, then there is nothing
1319 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001320 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1321 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001322 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001323 try {
George Mount6ba042b2014-07-28 11:12:28 -07001324 if (r.returningOptions != null) {
1325 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1326 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001327 }
1328 } catch(RemoteException e) {
1329 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001330 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001331 // This activity is not currently visible, but is running.
1332 // Tell it to become visible.
1333 r.visible = true;
1334 if (r.state != ActivityState.RESUMED && r != starting) {
1335 // If this activity is paused, tell it
1336 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001337 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1338 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001339 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001340 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001341 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001342 mUndrawnActivitiesBelowTopTranslucent.add(r);
1343 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001344 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001345 r.sleeping = false;
1346 r.app.pendingUiClean = true;
1347 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1348 r.stopFreezingScreenLocked(false);
1349 } catch (Exception e) {
1350 // Just skip on any failure; we'll make it
1351 // visible when it next restarts.
1352 Slog.w(TAG, "Exception thrown making visibile: "
1353 + r.intent.getComponent(), e);
1354 }
1355 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001356 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357
Craig Mautnerd44711d2013-02-23 11:24:36 -08001358 // Aggregate current change flags.
1359 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360
Craig Mautnerd44711d2013-02-23 11:24:36 -08001361 if (r.fullscreen) {
1362 // At this point, nothing else needs to be shown
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001363 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r);
Craig Mautner580ea812013-04-25 12:58:38 -07001364 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001365 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001366 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001367 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001368 }
1369 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001370 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1371 "Make invisible? " + r + " finishing=" + r.finishing
1372 + " state=" + r.state + " behindFullscreen=" + behindFullscreen);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001373 // Now for any activities that aren't visible to the user, make
1374 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001375 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001376 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001377 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001378 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001379 switch (r.state) {
1380 case STOPPING:
1381 case STOPPED:
1382 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001383 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1384 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001385 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1386 }
1387 break;
1388
1389 case INITIALIZING:
1390 case RESUMED:
1391 case PAUSING:
1392 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001393 // This case created for transitioning activities from
1394 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001395 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001396 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001397 } else {
1398 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1399 mStackSupervisor.mStoppingActivities.add(r);
1400 }
1401 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001402 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001403 break;
1404
1405 default:
1406 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 }
1408 } catch (Exception e) {
1409 // Just skip on any failure; we'll make it
1410 // visible when it next restarts.
1411 Slog.w(TAG, "Exception thrown making hidden: "
1412 + r.intent.getComponent(), e);
1413 }
1414 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001415 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417 }
1418 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001420
1421 if (mTranslucentActivityWaiting != null &&
1422 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1423 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1424 notifyActivityDrawnLocked(null);
1425 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 }
Craig Mautner58547802013-03-05 08:23:53 -08001427
Todd Kennedyaab56db2015-01-30 09:39:53 -08001428 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001429 mTranslucentActivityWaiting = r;
1430 mUndrawnActivitiesBelowTopTranslucent.clear();
1431 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1432 }
1433
1434 /**
1435 * Called as activities below the top translucent activity are redrawn. When the last one is
1436 * redrawn notify the top activity by calling
1437 * {@link Activity#onTranslucentConversionComplete}.
1438 *
1439 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1440 * occurred and the activity will be notified immediately.
1441 */
1442 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001443 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001444 if ((r == null)
1445 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1446 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1447 // The last undrawn activity below the top has just been drawn. If there is an
1448 // opaque activity at the top, notify it that it can become translucent safely now.
1449 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1450 mTranslucentActivityWaiting = null;
1451 mUndrawnActivitiesBelowTopTranslucent.clear();
1452 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1453
Craig Mautner71dd1b62014-02-18 15:48:52 -08001454 if (waitingActivity != null) {
1455 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1456 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1457 try {
1458 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1459 waitingActivity.appToken, r != null);
1460 } catch (RemoteException e) {
1461 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001462 }
1463 }
1464 }
1465 }
1466
Craig Mautnera61bc652013-10-28 15:43:18 -07001467 /** If any activities below the top running one are in the INITIALIZING state and they have a
1468 * starting window displayed then remove that starting window. It is possible that the activity
1469 * in this state will never resumed in which case that starting window will be orphaned. */
1470 void cancelInitializingActivities() {
1471 final ActivityRecord topActivity = topRunningActivityLocked(null);
1472 boolean aboveTop = true;
1473 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1474 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1475 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1476 final ActivityRecord r = activities.get(activityNdx);
1477 if (aboveTop) {
1478 if (r == topActivity) {
1479 aboveTop = false;
1480 }
1481 continue;
1482 }
1483
1484 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001485 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1486 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001487 r.mStartingWindowShown = false;
1488 mWindowManager.removeAppStartingWindow(r.appToken);
1489 }
1490 }
1491 }
1492 }
1493
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494 /**
1495 * Ensure that the top activity in the stack is resumed.
1496 *
1497 * @param prev The previously resumed activity, for when in the process
1498 * of pausing; can be null to call from elsewhere.
1499 *
1500 * @return Returns true if something is being resumed, or false if
1501 * nothing happened.
1502 */
1503 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001504 return resumeTopActivityLocked(prev, null);
1505 }
1506
1507 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001508 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001509 // Don't even start recursing.
1510 return false;
1511 }
1512
1513 boolean result = false;
1514 try {
1515 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001516 mStackSupervisor.inResumeTopActivity = true;
1517 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1518 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001519 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001520 }
Craig Mautner544efa72014-09-04 16:41:20 -07001521 result = resumeTopActivityInnerLocked(prev, options);
1522 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001523 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001524 }
1525 return result;
1526 }
1527
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001528 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001529 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001530
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001531 if (!mService.mBooting && !mService.mBooted) {
1532 // Not ready yet!
1533 return false;
1534 }
1535
Craig Mautnerdf88d732014-01-27 09:21:32 -08001536 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001537 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001538 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001539 // Do not resume this stack if its parent is not resumed.
1540 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1541 return false;
1542 }
1543
Craig Mautnera61bc652013-10-28 15:43:18 -07001544 cancelInitializingActivities();
1545
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001547 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001548
1549 // Remember how we'll process this pause/resume situation, and ensure
1550 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001551 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1552 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553
Craig Mautner84984fa2014-06-19 11:19:20 -07001554 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001555 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001556 // There are no more activities!
1557 final String reason = "noMoreActivities";
1558 if (!mFullscreen) {
1559 // Try to move focus to the next visible stack with a running activity if this
1560 // stack is not covering the entire screen.
1561 final ActivityStack stack = getNextVisibleStackLocked();
1562 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1563 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1564 }
1565 }
1566 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001567 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001568 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001569 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001570 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001571 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1572 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1573 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001574 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 }
1576
1577 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001578
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001580 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1581 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 // Make sure we have executed any pending transitions, since there
1583 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001584 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001586 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001587 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001588 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 return false;
1590 }
1591
Craig Mautner525f3d92013-05-07 14:01:50 -07001592 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001593 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001594 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001595 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001596 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001597 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001598 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001599 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001600 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001601 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001602 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001603 } else if (!isOnHomeDisplay()) {
1604 return false;
1605 } else if (!isHomeStack()){
1606 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001607 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001608 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1609 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1610 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001611 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001612 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001613 }
1614
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 // If we are sleeping, and there is no resumed activity, and the top
1616 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001617 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001618 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001619 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 // Make sure we have executed any pending transitions, since there
1621 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001622 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001624 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001625 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001626 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001627 return false;
1628 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001629
1630 // Make sure that the user who owns this activity is started. If not,
1631 // we will just leave it as is because someone should be bringing
1632 // another user's activities to the top of the stack.
1633 if (mService.mStartedUsers.get(next.userId) == null) {
1634 Slog.w(TAG, "Skipping resume of top activity " + next
1635 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001636 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001637 return false;
1638 }
1639
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 // The activity may be waiting for stop, but that is no longer
1641 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001642 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001643 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001644 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001645 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001647 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001648
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001649 // If we are currently pausing an activity, then don't do anything
1650 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001651 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001652 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001653 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001654 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001655 return false;
1656 }
1657
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001658 // Okay we are now going to start a switch, to 'next'. We may first
1659 // have to pause the current activity, but this is an important point
1660 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001661 // XXX "App Redirected" dialog is getting too many false positives
1662 // at this point, so turn off for now.
1663 if (false) {
1664 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1665 long now = SystemClock.uptimeMillis();
1666 final boolean inTime = mLastStartedActivity.startTime != 0
1667 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1668 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1669 final int nextUid = next.info.applicationInfo.uid;
1670 if (inTime && lastUid != nextUid
1671 && lastUid != next.launchedFromUid
1672 && mService.checkPermission(
1673 android.Manifest.permission.STOP_APP_SWITCHES,
1674 -1, next.launchedFromUid)
1675 != PackageManager.PERMISSION_GRANTED) {
1676 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1677 } else {
1678 next.startTime = now;
1679 mLastStartedActivity = next;
1680 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001681 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001682 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001683 mLastStartedActivity = next;
1684 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001685 }
Craig Mautner58547802013-03-05 08:23:53 -08001686
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001687 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1688
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001689 // We need to start pausing the current activity so the top one
1690 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001691 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1692 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001693 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001694 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001695 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001696 }
1697 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001698 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1699 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001700 // At this point we want to put the upcoming activity's process
1701 // at the top of the LRU list, since we know we will be needing it
1702 // very soon and it would be a waste to let it get killed if it
1703 // happens to be sitting towards the end.
1704 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001705 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001706 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001707 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001708 return true;
1709 }
1710
Christopher Tated3f175c2012-06-14 14:16:54 -07001711 // If the most recent activity was noHistory but was only stopped rather
1712 // than stopped+finished because the device went to sleep, we need to make
1713 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001714 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001715 !mLastNoHistoryActivity.finishing) {
1716 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1717 " on new resume");
1718 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001719 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001720 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001721 }
1722
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001724 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1725 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001726 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001727 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1728 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001729 } else {
1730 // The next activity is already visible, so hide the previous
1731 // activity's windows right now so we can show the new one ASAP.
1732 // We only do this if the previous is finishing, which should mean
1733 // it is on top of the one being resumed so hiding it quickly
1734 // is good. Otherwise, we want to do the normal route of allowing
1735 // the resumed activity to be shown so we can decide if the
1736 // previous should actually be hidden depending on whether the
1737 // new one is found to be full-screen or not.
1738 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001739 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001740 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1741 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001742 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 + ", nowVisible=" + next.nowVisible);
1744 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001745 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001746 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001747 + ", waitingVisible="
1748 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1749 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 }
1751 }
1752 }
1753
Dianne Hackborne7f97212011-02-24 14:40:20 -08001754 // Launching this app's activity, make sure the app is no longer
1755 // considered stopped.
1756 try {
1757 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001758 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001759 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001760 } catch (IllegalArgumentException e) {
1761 Slog.w(TAG, "Failed trying to unstop package "
1762 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001763 }
1764
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765 // We are starting up the next activity, so tell the window manager
1766 // that the previous one will be hidden soon. This way it can know
1767 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001768 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 if (prev != null) {
1770 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001771 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 "Prepare close transition: prev=" + prev);
1773 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001774 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001775 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001777 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001778 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1779 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001781 mWindowManager.setAppWillBeHidden(prev.appToken);
1782 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001784 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1785 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001787 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001788 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001790 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001791 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001792 : next.mLaunchTaskBehind
1793 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1794 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795 }
1796 }
1797 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001798 mWindowManager.setAppWillBeHidden(prev.appToken);
1799 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001800 }
Craig Mautner967212c2013-04-13 21:10:58 -07001801 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001802 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001804 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001805 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001807 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 }
1809 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001810
1811 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001812 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001813 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1814 if (opts != null) {
1815 resumeAnimOptions = opts.toBundle();
1816 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001817 next.applyOptionsLocked();
1818 } else {
1819 next.clearOptionsLocked();
1820 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821
Craig Mautnercf910b02013-04-23 11:23:27 -07001822 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001824 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825
1826 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001827 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001829 // schedule launch ticks to collect information about slow apps.
1830 next.startLaunchTickingLocked();
1831
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001832 ActivityRecord lastResumedActivity =
1833 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001834 ActivityState lastState = next.state;
1835
1836 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001837
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001838 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 next.state = ActivityState.RESUMED;
1840 mResumedActivity = next;
1841 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001842 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001843 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001844 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001845 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001846
1847 // Have the window manager re-evaluate the orientation of
1848 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001849 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001850 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001851 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001852 mService.mConfiguration,
1853 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1854 if (config != null) {
1855 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001856 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001857 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001858 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001859
Craig Mautner525f3d92013-05-07 14:01:50 -07001860 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861 // The configuration update wasn't able to keep the existing
1862 // instance of the activity, and instead started a new one.
1863 // We should be all done, but let's just make sure our activity
1864 // is still at the top and schedule another run if something
1865 // weird happened.
1866 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001867 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 "Activity config changed during resume: " + next
1869 + ", new next: " + nextNext);
1870 if (nextNext != next) {
1871 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001872 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001873 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001874 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001875 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001876 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001877 return true;
1878 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001879 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001880 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 }
Craig Mautner58547802013-03-05 08:23:53 -08001882
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883 try {
1884 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001885 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001886 if (a != null) {
1887 final int N = a.size();
1888 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001889 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1890 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001891 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001892 }
1893 }
1894
1895 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001896 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 }
1898
Craig Mautner299f9602015-01-26 09:47:33 -08001899 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1900 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001901
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001902 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001903 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001904 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001905 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001906 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001907 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001908 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001909
Craig Mautner0eea92c2013-05-16 13:35:39 -07001910 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001911
Craig Mautnerac6f8432013-07-17 13:24:59 -07001912 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001913 } catch (Exception e) {
1914 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001915 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1916 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001918 if (lastStack != null) {
1919 lastStack.mResumedActivity = lastResumedActivity;
1920 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921 Slog.i(TAG, "Restarting because process died: " + next);
1922 if (!next.hasBeenLaunched) {
1923 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001924 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1925 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001926 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001927 next.appToken, next.packageName, next.theme,
1928 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001929 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1930 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001931 }
George Mount2c92c972014-03-20 09:38:23 -07001932 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001933 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001934 return true;
1935 }
1936
1937 // From this point on, if something goes wrong there is no way
1938 // to recover the activity.
1939 try {
1940 next.visible = true;
1941 completeResumeLocked(next);
1942 } catch (Exception e) {
1943 // If any exception gets thrown, toss away this
1944 // activity and try the next one.
1945 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001946 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001947 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001948 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 return true;
1950 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951 next.stopped = false;
1952
1953 } else {
1954 // Whoops, need to restart this activity!
1955 if (!next.hasBeenLaunched) {
1956 next.hasBeenLaunched = true;
1957 } else {
1958 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001959 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001960 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001961 mService.compatibilityInfoForPackageLocked(
1962 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001963 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001964 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001965 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001967 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001968 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001969 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001970 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001971 }
1972
Craig Mautnercf910b02013-04-23 11:23:27 -07001973 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 return true;
1975 }
1976
riddle_hsuc215a4f2014-12-27 12:10:45 +08001977 private TaskRecord getNextTask(TaskRecord targetTask) {
1978 final int index = mTaskHistory.indexOf(targetTask);
1979 if (index >= 0) {
1980 final int numTasks = mTaskHistory.size();
1981 for (int i = index + 1; i < numTasks; ++i) {
1982 TaskRecord task = mTaskHistory.get(i);
1983 if (task.userId == targetTask.userId) {
1984 return task;
1985 }
1986 }
1987 }
1988 return null;
1989 }
1990
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001991 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001992 // If the moving task is over home stack, transfer its return type to next task
1993 if (task.isOverHomeStack()) {
1994 final TaskRecord nextTask = getNextTask(task);
1995 if (nextTask != null) {
1996 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1997 }
1998 }
1999
Craig Mautner9c85c202013-10-04 20:11:26 -07002000 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002001 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002002 if (isOnHomeDisplay()) {
2003 ActivityStack lastStack = mStackSupervisor.getLastStack();
2004 final boolean fromHome = lastStack.isHomeStack();
2005 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002006 task.setTaskToReturnTo(fromHome
2007 ? lastStack.topTask() == null
2008 ? HOME_ACTIVITY_TYPE
2009 : lastStack.topTask().taskType
2010 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002011 }
2012 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002013 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002014 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002015
Craig Mautnerac6f8432013-07-17 13:24:59 -07002016 mTaskHistory.remove(task);
2017 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002018 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002019 final boolean notShownWhenLocked =
2020 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_ON_LOCK_SCREEN) == 0)
2021 || (newActivity == null && task.topRunningActivityLocked(null) == null);
2022 if (!isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002023 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002024 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002025 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
2026 if (!isCurrentProfileLocked(tmpTask.userId)
2027 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002028 break;
2029 }
2030 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002031 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002032 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002033 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002034 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002035 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002036
Craig Mautner8849a5e2013-04-02 16:41:03 -07002037 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002038 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002039 TaskRecord rTask = r.task;
2040 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002041 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2042 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002043 // Last activity in task had been removed or ActivityManagerService is reusing task.
2044 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002045 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002046 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002047 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002048 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002049 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002050 if (!newTask) {
2051 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002052 boolean startIt = true;
2053 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2054 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002055 if (task.getTopActivity() == null) {
2056 // All activities in task are finishing.
2057 continue;
2058 }
Craig Mautner70a86932013-02-28 22:37:44 -08002059 if (task == r.task) {
2060 // Here it is! Now, if this is not yet visible to the
2061 // user, then just add it without starting; it will
2062 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002063 if (!startIt) {
2064 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2065 + task, new RuntimeException("here").fillInStackTrace());
2066 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002067 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002068 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2069 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002070 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002071 r.userId, r.info.configChanges, task.voiceSession != null,
2072 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002073 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002074 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002076 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 return;
2078 }
2079 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002080 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 startIt = false;
2082 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 }
2084 }
2085
2086 // Place a new activity at top of stack, so it is next to interact
2087 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002088
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 // If we are not placing the new activity frontmost, we do not want
2090 // to deliver the onUserLeaving callback to the actual frontmost
2091 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002092 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002093 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002094 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002095 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 }
Craig Mautner70a86932013-02-28 22:37:44 -08002097
2098 task = r.task;
2099
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002100 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002101 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002102 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002103 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002104 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002105
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002106 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002107 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108 // We want to show the starting preview window if we are
2109 // switching to a new task, or the next activity's process is
2110 // not currently running.
2111 boolean showStartingIcon = newTask;
2112 ProcessRecord proc = r.app;
2113 if (proc == null) {
2114 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2115 }
2116 if (proc == null || proc.thread == null) {
2117 showStartingIcon = true;
2118 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002119 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002121 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002122 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002125 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002126 ? r.mLaunchTaskBehind
2127 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2128 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002129 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002130 mNoAnimActivities.remove(r);
2131 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002132 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002133 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002134 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002135 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 boolean doShow = true;
2137 if (newTask) {
2138 // Even though this activity is starting fresh, we still need
2139 // to reset it to make sure we apply affinities to move any
2140 // existing activities from other tasks in to it.
2141 // If the caller has requested that the target task be
2142 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002143 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 resetTaskIfNeededLocked(r, r);
2145 doShow = topRunningNonDelayedActivityLocked(null) == r;
2146 }
George Mount70778d72014-07-01 16:33:45 -07002147 } else if (options != null && new ActivityOptions(options).getAnimationType()
2148 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2149 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002151 if (r.mLaunchTaskBehind) {
2152 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2153 // tell WindowManager that r is visible even though it is at the back of the stack.
2154 mWindowManager.setAppVisibility(r.appToken, true);
2155 ensureActivitiesVisibleLocked(null, 0);
2156 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 // Figure out if we are transitioning from another activity that is
2158 // "has the same starting icon" as the next one. This allows the
2159 // window manager to keep the previous window it had previously
2160 // created, if it still had one.
2161 ActivityRecord prev = mResumedActivity;
2162 if (prev != null) {
2163 // We don't want to reuse the previous starting preview if:
2164 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002165 if (prev.task != r.task) {
2166 prev = null;
2167 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002168 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002169 else if (prev.nowVisible) {
2170 prev = null;
2171 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002173 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002174 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002175 mService.compatibilityInfoForPackageLocked(
2176 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002177 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002178 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002179 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 }
2181 } else {
2182 // If this is the first activity, don't do any fancy animations,
2183 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002184 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002185 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002186 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002187 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002188 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002189 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 }
2191 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002192 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002193 }
2194
2195 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002196 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 }
2198 }
2199
Dianne Hackbornbe707852011-11-11 14:32:10 -08002200 final void validateAppTokensLocked() {
2201 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002202 mValidateAppTokens.ensureCapacity(numActivities());
2203 final int numTasks = mTaskHistory.size();
2204 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2205 TaskRecord task = mTaskHistory.get(taskNdx);
2206 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002207 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002208 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002209 }
Craig Mautner000f0022013-02-26 15:04:29 -08002210 TaskGroup group = new TaskGroup();
2211 group.taskId = task.taskId;
2212 mValidateAppTokens.add(group);
2213 final int numActivities = activities.size();
2214 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2215 final ActivityRecord r = activities.get(activityNdx);
2216 group.tokens.add(r.appToken);
2217 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002218 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002219 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002220 }
2221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002222 /**
2223 * Perform a reset of the given task, if needed as part of launching it.
2224 * Returns the new HistoryRecord at the top of the task.
2225 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002226 /**
2227 * Helper method for #resetTaskIfNeededLocked.
2228 * We are inside of the task being reset... we'll either finish this activity, push it out
2229 * for another task, or leave it as-is.
2230 * @param task The task containing the Activity (taskTop) that might be reset.
2231 * @param forceReset
2232 * @return An ActivityOptions that needs to be processed.
2233 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002234 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002235 ActivityOptions topOptions = null;
2236
2237 int replyChainEnd = -1;
2238 boolean canMoveOptions = true;
2239
2240 // We only do this for activities that are not the root of the task (since if we finish
2241 // the root, we may no longer have the task!).
2242 final ArrayList<ActivityRecord> activities = task.mActivities;
2243 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002244 final int rootActivityNdx = task.findEffectiveRootIndex();
2245 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002246 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002247 if (target.frontOfTask)
2248 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002249
2250 final int flags = target.info.flags;
2251 final boolean finishOnTaskLaunch =
2252 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2253 final boolean allowTaskReparenting =
2254 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2255 final boolean clearWhenTaskReset =
2256 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2257
2258 if (!finishOnTaskLaunch
2259 && !clearWhenTaskReset
2260 && target.resultTo != null) {
2261 // If this activity is sending a reply to a previous
2262 // activity, we can't do anything with it now until
2263 // we reach the start of the reply chain.
2264 // XXX note that we are assuming the result is always
2265 // to the previous activity, which is almost always
2266 // the case but we really shouldn't count on.
2267 if (replyChainEnd < 0) {
2268 replyChainEnd = i;
2269 }
2270 } else if (!finishOnTaskLaunch
2271 && !clearWhenTaskReset
2272 && allowTaskReparenting
2273 && target.taskAffinity != null
2274 && !target.taskAffinity.equals(task.affinity)) {
2275 // If this activity has an affinity for another
2276 // task, then we need to move it out of here. We will
2277 // move it as far out of the way as possible, to the
2278 // bottom of the activity stack. This also keeps it
2279 // correctly ordered with any activities we previously
2280 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002281 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002282 final ActivityRecord bottom =
2283 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002284 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002285 if (bottom != null && target.taskAffinity != null
2286 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002287 // If the activity currently at the bottom has the
2288 // same task affinity as the one we are moving,
2289 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002290 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002291 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002292 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002293 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002294 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002295 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002296 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002297 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002298 + " out to new task " + target.task);
2299 }
2300
Craig Mautnere3a74d52013-02-22 14:14:58 -08002301 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002302 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002303
Craig Mautner525f3d92013-05-07 14:01:50 -07002304 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002305 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2306 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002307 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002308 if (p.finishing) {
2309 continue;
2310 }
2311
Craig Mautnere3a74d52013-02-22 14:14:58 -08002312 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002313 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002314 topOptions = p.takeOptionsLocked();
2315 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002316 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002317 }
2318 }
2319 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002320 + task + " adding to task=" + targetTask
2321 + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002322 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2323 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002324 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002325 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002326
Craig Mautner83162a92015-01-26 14:43:30 -08002327 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002328 }
2329
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002330 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002331 if (VALIDATE_TOKENS) {
2332 validateAppTokensLocked();
2333 }
2334
2335 replyChainEnd = -1;
2336 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2337 // If the activity should just be removed -- either
2338 // because it asks for it, or the task should be
2339 // cleared -- then finish it and anything that is
2340 // part of its reply chain.
2341 int end;
2342 if (clearWhenTaskReset) {
2343 // In this case, we want to finish this activity
2344 // and everything above it, so be sneaky and pretend
2345 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002346 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002347 } else if (replyChainEnd < 0) {
2348 end = i;
2349 } else {
2350 end = replyChainEnd;
2351 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002352 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002353 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002354 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002355 if (p.finishing) {
2356 continue;
2357 }
2358 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002359 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002360 topOptions = p.takeOptionsLocked();
2361 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002362 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002363 }
2364 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002365 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002366 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002367 if (finishActivityLocked(
2368 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002369 end--;
2370 srcPos--;
2371 }
2372 }
2373 replyChainEnd = -1;
2374 } else {
2375 // If we were in the middle of a chain, well the
2376 // activity that started it all doesn't want anything
2377 // special, so leave it all as-is.
2378 replyChainEnd = -1;
2379 }
2380 }
2381
2382 return topOptions;
2383 }
2384
2385 /**
2386 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2387 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2388 * @param affinityTask The task we are looking for an affinity to.
2389 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2390 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2391 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2392 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002393 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002394 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002395 int replyChainEnd = -1;
2396 final int taskId = task.taskId;
2397 final String taskAffinity = task.affinity;
2398
2399 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2400 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002401 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2402
2403 // Do not operate on or below the effective root Activity.
2404 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002405 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002406 if (target.frontOfTask)
2407 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002408
2409 final int flags = target.info.flags;
2410 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2411 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2412
2413 if (target.resultTo != null) {
2414 // If this activity is sending a reply to a previous
2415 // activity, we can't do anything with it now until
2416 // we reach the start of the reply chain.
2417 // XXX note that we are assuming the result is always
2418 // to the previous activity, which is almost always
2419 // the case but we really shouldn't count on.
2420 if (replyChainEnd < 0) {
2421 replyChainEnd = i;
2422 }
2423 } else if (topTaskIsHigher
2424 && allowTaskReparenting
2425 && taskAffinity != null
2426 && taskAffinity.equals(target.taskAffinity)) {
2427 // This activity has an affinity for our task. Either remove it if we are
2428 // clearing or move it over to our task. Note that
2429 // we currently punt on the case where we are resetting a
2430 // task that is not at the top but who has activities above
2431 // with an affinity to it... this is really not a normal
2432 // case, and we will need to later pull that task to the front
2433 // and usually at that point we will do the reset and pick
2434 // up those remaining activities. (This only happens if
2435 // someone starts an activity in a new task from an activity
2436 // in a task that is not currently on top.)
2437 if (forceReset || finishOnTaskLaunch) {
2438 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002439 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2440 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002441 for (int srcPos = start; srcPos >= i; --srcPos) {
2442 final ActivityRecord p = activities.get(srcPos);
2443 if (p.finishing) {
2444 continue;
2445 }
Todd Kennedy539db512014-12-15 09:57:55 -08002446 finishActivityLocked(
2447 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002448 }
2449 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002450 if (taskInsertionPoint < 0) {
2451 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002452
Craig Mautner77878772013-03-04 19:46:24 -08002453 }
Craig Mautner77878772013-03-04 19:46:24 -08002454
2455 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002456 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2457 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2458 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002459 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002460 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002461 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002462 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002463
Craig Mautnere3a74d52013-02-22 14:14:58 -08002464 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2465 + " to stack at " + task,
2466 new RuntimeException("here").fillInStackTrace());
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002467 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2468 + " from " + srcPos + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002469 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002470 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002471 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002472 if (VALIDATE_TOKENS) {
2473 validateAppTokensLocked();
2474 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002475
2476 // Now we've moved it in to place... but what if this is
2477 // a singleTop activity and we have put it on top of another
2478 // instance of the same activity? Then we drop the instance
2479 // below so it remains singleTop.
2480 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2481 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002482 int targetNdx = taskActivities.indexOf(target);
2483 if (targetNdx > 0) {
2484 ActivityRecord p = taskActivities.get(targetNdx - 1);
2485 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002486 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2487 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002488 }
2489 }
2490 }
2491 }
2492
2493 replyChainEnd = -1;
2494 }
2495 }
Craig Mautner77878772013-03-04 19:46:24 -08002496 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002497 }
2498
Craig Mautner8849a5e2013-04-02 16:41:03 -07002499 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002500 ActivityRecord newActivity) {
2501 boolean forceReset =
2502 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2503 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2504 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2505 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2506 forceReset = true;
2507 }
2508 }
2509
2510 final TaskRecord task = taskTop.task;
2511
2512 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2513 * for remaining tasks. Used for later tasks to reparent to task. */
2514 boolean taskFound = false;
2515
2516 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2517 ActivityOptions topOptions = null;
2518
Craig Mautner77878772013-03-04 19:46:24 -08002519 // Preserve the location for reparenting in the new task.
2520 int reparentInsertionPoint = -1;
2521
Craig Mautnere3a74d52013-02-22 14:14:58 -08002522 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2523 final TaskRecord targetTask = mTaskHistory.get(i);
2524
2525 if (targetTask == task) {
2526 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2527 taskFound = true;
2528 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002529 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2530 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002531 }
2532 }
2533
Craig Mautner70a86932013-02-28 22:37:44 -08002534 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002535 if (taskNdx >= 0) {
2536 do {
2537 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2538 } while (taskTop == null && taskNdx >= 0);
2539 }
Craig Mautner70a86932013-02-28 22:37:44 -08002540
Craig Mautnere3a74d52013-02-22 14:14:58 -08002541 if (topOptions != null) {
2542 // If we got some ActivityOptions from an activity on top that
2543 // was removed from the task, propagate them to the new real top.
2544 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002545 taskTop.updateOptionsLocked(topOptions);
2546 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002547 topOptions.abort();
2548 }
2549 }
2550
2551 return taskTop;
2552 }
2553
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002554 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2555 String resultWho, int requestCode, int resultCode, Intent data) {
2556
2557 if (callingUid > 0) {
2558 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002559 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 }
2561
2562 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2563 + " : who=" + resultWho + " req=" + requestCode
2564 + " res=" + resultCode + " data=" + data);
2565 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2566 try {
2567 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2568 list.add(new ResultInfo(resultWho, requestCode,
2569 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002570 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002571 return;
2572 } catch (Exception e) {
2573 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2574 }
2575 }
2576
2577 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2578 }
2579
Craig Mautner299f9602015-01-26 09:47:33 -08002580 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002581 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2582 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002583 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002584 if (next != r) {
2585 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002586 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002587 // For non-fullscreen stack, we want to move the focus to the next visible
2588 // stack to prevent the home screen from moving to the top and obscuring
2589 // other visible stacks.
2590 if (!mFullscreen
2591 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2592 return;
2593 }
2594 // Move the home stack to the top if this stack is fullscreen or there is no
2595 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002596 if (mStackSupervisor.moveHomeStackTaskToTop(
2597 task.getTaskToReturnTo(), myReason)) {
2598 // Activity focus was already adjusted. Nothing else to do...
2599 return;
2600 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002601 }
2602 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002603
2604 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002605 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002606 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002607 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002608 }
2609 }
2610
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002611 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2612 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2613 final String myReason = reason + " adjustFocusToNextVisibleStack";
2614 if (stack == null) {
2615 return false;
2616 }
2617 final ActivityRecord top = stack.topRunningActivityLocked(null);
2618 if (top == null) {
2619 return false;
2620 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002621 mService.setFocusedActivityLocked(top, myReason);
2622 return true;
2623 }
2624
Craig Mautnerf3333272013-04-22 10:55:53 -07002625 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002626 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2628 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2629 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002630 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002631 if (DEBUG_STATES) {
2632 Slog.d(TAG, "no-history finish of " + r);
2633 }
2634 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002635 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002636 } else {
2637 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2638 + " on stop because we're just sleeping");
2639 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002641 }
2642
2643 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002644 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002645 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 try {
2647 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002648 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2649 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002650 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002651 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2652 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002654 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002656 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002657 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002658 r.setSleeping(true);
2659 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002660 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002661 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 } catch (Exception e) {
2663 // Maybe just ignore exceptions here... if the process
2664 // has crashed, our death notification will clean things
2665 // up.
2666 Slog.w(TAG, "Exception thrown during pause", e);
2667 // Just in case, assume it to be stopped.
2668 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002669 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002670 r.state = ActivityState.STOPPED;
2671 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002672 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 }
2674 }
2675 }
2676 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002677
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002678 /**
2679 * @return Returns true if the activity is being finished, false if for
2680 * some reason it is being left as-is.
2681 */
2682 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002683 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002684 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002685 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002686 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002687 + ", result=" + resultCode + ", data=" + resultData
2688 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002689 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 return false;
2691 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002692
Craig Mautnerd44711d2013-02-23 11:24:36 -08002693 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694 return true;
2695 }
2696
Craig Mautnerd2328952013-03-05 12:46:26 -08002697 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002698 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2699 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2700 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2701 ActivityRecord r = activities.get(activityNdx);
2702 if (r.resultTo == self && r.requestCode == requestCode) {
2703 if ((r.resultWho == null && resultWho == null) ||
2704 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2705 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2706 false);
2707 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002708 }
2709 }
2710 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002711 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002712 }
2713
Todd Kennedy539db512014-12-15 09:57:55 -08002714 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002715 ActivityRecord r = topRunningActivityLocked(null);
2716 if (r != null && r.app == app) {
2717 // If the top running activity is from this crashing
2718 // process, then terminate it to avoid getting in a loop.
2719 Slog.w(TAG, " Force finishing activity "
2720 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002721 int taskNdx = mTaskHistory.indexOf(r.task);
2722 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002723 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002724 // Also terminate any activities below it that aren't yet
2725 // stopped, to avoid a situation where one will get
2726 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002727 --activityNdx;
2728 if (activityNdx < 0) {
2729 do {
2730 --taskNdx;
2731 if (taskNdx < 0) {
2732 break;
2733 }
2734 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2735 } while (activityNdx < 0);
2736 }
2737 if (activityNdx >= 0) {
2738 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002739 if (r.state == ActivityState.RESUMED
2740 || r.state == ActivityState.PAUSING
2741 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002742 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002743 Slog.w(TAG, " Force finishing activity "
2744 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002745 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002746 }
2747 }
2748 }
2749 }
2750 }
2751
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002752 final void finishVoiceTask(IVoiceInteractionSession session) {
2753 IBinder sessionBinder = session.asBinder();
2754 boolean didOne = false;
2755 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2756 TaskRecord tr = mTaskHistory.get(taskNdx);
2757 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2758 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2759 ActivityRecord r = tr.mActivities.get(activityNdx);
2760 if (!r.finishing) {
2761 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2762 false);
2763 didOne = true;
2764 }
2765 }
2766 }
2767 }
2768 if (didOne) {
2769 mService.updateOomAdjLocked();
2770 }
2771 }
2772
Craig Mautnerd2328952013-03-05 12:46:26 -08002773 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002774 ArrayList<ActivityRecord> activities = r.task.mActivities;
2775 for (int index = activities.indexOf(r); index >= 0; --index) {
2776 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002777 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002778 break;
2779 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002780 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002781 }
2782 return true;
2783 }
2784
Dianne Hackborn5c607432012-02-28 14:44:19 -08002785 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2786 // send the result
2787 ActivityRecord resultTo = r.resultTo;
2788 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002789 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002790 + " who=" + r.resultWho + " req=" + r.requestCode
2791 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002792 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002793 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002794 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002795 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002796 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002797 if (r.info.applicationInfo.uid > 0) {
2798 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2799 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002800 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002801 }
2802 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2803 resultData);
2804 r.resultTo = null;
2805 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002806 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002807
2808 // Make sure this HistoryRecord is not holding on to other resources,
2809 // because clients have remote IPC references to this object so we
2810 // can't assume that will go away and want to avoid circular IPC refs.
2811 r.results = null;
2812 r.pendingResults = null;
2813 r.newIntents = null;
2814 r.icicle = null;
2815 }
2816
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002817 /**
2818 * @return Returns true if this activity has been removed from the history
2819 * list, or false if it is still in the list and will be removed later.
2820 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002821 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2822 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 if (r.finishing) {
2824 Slog.w(TAG, "Duplicate finish request for " + r);
2825 return false;
2826 }
2827
Wale Ogunwale7d701172015-03-11 15:36:30 -07002828 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002829 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002830 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002831 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002832 task.taskId, r.shortComponentName, reason);
2833 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002834 final int index = activities.indexOf(r);
2835 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002836 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002837 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002838 // If the caller asked that this activity (and all above it)
2839 // be cleared when the task is reset, don't lose that information,
2840 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002841 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002842 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 }
2844 }
2845
2846 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002847
Craig Mautner299f9602015-01-26 09:47:33 -08002848 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849
Dianne Hackborn5c607432012-02-28 14:44:19 -08002850 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002851
Craig Mautnerde4ef022013-04-07 19:01:33 -07002852 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002853 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002854 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002855 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002856 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002857 ? AppTransition.TRANSIT_TASK_CLOSE
2858 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002861 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002862
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002863 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002864 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
2865 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2866 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002867 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 }
2869
Craig Mautneraea74a52014-03-08 14:23:10 -08002870 if (endTask) {
2871 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2872 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 } else if (r.state != ActivityState.PAUSING) {
2874 // If the activity is PAUSING, we will complete the finish once
2875 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002876 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002877 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002879 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002880 }
2881
2882 return false;
2883 }
2884
Craig Mautnerf3333272013-04-22 10:55:53 -07002885 static final int FINISH_IMMEDIATELY = 0;
2886 static final int FINISH_AFTER_PAUSE = 1;
2887 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002888
Craig Mautnerf3333272013-04-22 10:55:53 -07002889 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890 // First things first: if this activity is currently visible,
2891 // and the resumed activity is not yet visible, then hold off on
2892 // finishing until the resumed one becomes visible.
2893 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002894 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2895 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002896 if (mStackSupervisor.mStoppingActivities.size() > 3
2897 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 // If we already have a few activities waiting to stop,
2899 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002900 // them out. Or if r is the last of activity of the last task the stack
2901 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002902 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002903 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002904 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 }
2906 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002907 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2908 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002909 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002910 if (oomAdj) {
2911 mService.updateOomAdjLocked();
2912 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 return r;
2914 }
2915
2916 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002917 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002918 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002919 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002920 if (mResumedActivity == r) {
2921 mResumedActivity = null;
2922 }
2923 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002924 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 r.state = ActivityState.FINISHING;
2926
2927 if (mode == FINISH_IMMEDIATELY
2928 || prevState == ActivityState.STOPPED
2929 || prevState == ActivityState.INITIALIZING) {
2930 // If this activity is already stopped, we can just finish
2931 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002932 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002933 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002934 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002935 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002936 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002937 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002938 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2939 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002940 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002942
2943 // Need to go through the full pause cycle to get this
2944 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002945 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002946 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002947 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002948 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 return r;
2950 }
2951
Craig Mautneree36c772014-07-16 14:56:05 -07002952 void finishAllActivitiesLocked(boolean immediately) {
2953 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002954 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2955 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2956 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2957 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002958 noActivitiesInStack = false;
2959 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002960 continue;
2961 }
Craig Mautneree36c772014-07-16 14:56:05 -07002962 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002963 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2964 }
2965 }
Craig Mautneree36c772014-07-16 14:56:05 -07002966 if (noActivitiesInStack) {
2967 mActivityContainer.onTaskListEmptyLocked();
2968 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002969 }
2970
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002971 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2972 // Basic case: for simple app-centric recents, we need to recreate
2973 // the task if the affinity has changed.
2974 if (srec == null || srec.task.affinity == null ||
2975 !srec.task.affinity.equals(destAffinity)) {
2976 return true;
2977 }
2978 // Document-centric case: an app may be split in to multiple documents;
2979 // they need to re-create their task if this current activity is the root
2980 // of a document, unless simply finishing it will return them to the the
2981 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002982 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2983 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002984 // Okay, this activity is at the root of its task. What to do, what to do...
2985 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2986 // Finishing won't return to an application, so we need to recreate.
2987 return true;
2988 }
2989 // We now need to get the task below it to determine what to do.
2990 int taskIdx = mTaskHistory.indexOf(srec.task);
2991 if (taskIdx <= 0) {
2992 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2993 return false;
2994 }
2995 if (taskIdx == 0) {
2996 // At the bottom of the stack, nothing to go back to.
2997 return true;
2998 }
2999 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3000 if (!srec.task.affinity.equals(prevTask.affinity)) {
3001 // These are different apps, so need to recreate.
3002 return true;
3003 }
3004 }
3005 return false;
3006 }
3007
Wale Ogunwale7d701172015-03-11 15:36:30 -07003008 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003009 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003010 final TaskRecord task = srec.task;
3011 final ArrayList<ActivityRecord> activities = task.mActivities;
3012 final int start = activities.indexOf(srec);
3013 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003014 return false;
3015 }
3016 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003017 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003018 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003019 final ComponentName dest = destIntent.getComponent();
3020 if (start > 0 && dest != null) {
3021 for (int i = finishTo; i >= 0; i--) {
3022 ActivityRecord r = activities.get(i);
3023 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003024 r.info.name.equals(dest.getClassName())) {
3025 finishTo = i;
3026 parent = r;
3027 foundParentInTask = true;
3028 break;
3029 }
3030 }
3031 }
3032
3033 IActivityController controller = mService.mController;
3034 if (controller != null) {
3035 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3036 if (next != null) {
3037 // ask watcher if this is allowed
3038 boolean resumeOK = true;
3039 try {
3040 resumeOK = controller.activityResuming(next.packageName);
3041 } catch (RemoteException e) {
3042 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003043 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003044 }
3045
3046 if (!resumeOK) {
3047 return false;
3048 }
3049 }
3050 }
3051 final long origId = Binder.clearCallingIdentity();
3052 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003053 ActivityRecord r = activities.get(i);
3054 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003055 // Only return the supplied result for the first activity finished
3056 resultCode = Activity.RESULT_CANCELED;
3057 resultData = null;
3058 }
3059
3060 if (parent != null && foundParentInTask) {
3061 final int parentLaunchMode = parent.info.launchMode;
3062 final int destIntentFlags = destIntent.getFlags();
3063 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3064 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3065 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3066 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003067 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3068 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003069 } else {
3070 try {
3071 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3072 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003073 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003074 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003075 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003076 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003077 foundParentInTask = res == ActivityManager.START_SUCCESS;
3078 } catch (RemoteException e) {
3079 foundParentInTask = false;
3080 }
3081 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003082 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003083 }
3084 }
3085 Binder.restoreCallingIdentity(origId);
3086 return foundParentInTask;
3087 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003088 /**
3089 * Perform the common clean-up of an activity record. This is called both
3090 * as part of destroyActivityLocked() (when destroying the client-side
3091 * representation) and cleaning things up as a result of its hosting
3092 * processing going away, in which case there is no remaining client-side
3093 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003094 *
3095 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003096 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003097 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3098 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 if (mResumedActivity == r) {
3100 mResumedActivity = null;
3101 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003102 if (mPausingActivity == r) {
3103 mPausingActivity = null;
3104 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003105 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003106
3107 r.configDestroy = false;
3108 r.frozenBeforeDestroy = false;
3109
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003110 if (setState) {
3111 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3112 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003113 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003114 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003115 }
3116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003117 // Make sure this record is no longer in the pending finishes list.
3118 // This could happen, for example, if we are trimming activities
3119 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003120 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003121 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003123 // Remove any pending results.
3124 if (r.finishing && r.pendingResults != null) {
3125 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3126 PendingIntentRecord rec = apr.get();
3127 if (rec != null) {
3128 mService.cancelIntentSenderLocked(rec, false);
3129 }
3130 }
3131 r.pendingResults = null;
3132 }
3133
3134 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003135 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003136 }
3137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003139 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003140 if (getVisibleBehindActivity() == r) {
3141 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003142 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003143 }
3144
3145 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003146 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003148 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003149 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003150 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 }
3152
Craig Mautner299f9602015-01-26 09:47:33 -08003153 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003154 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003155 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003156 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003157 if (DEBUG_ADD_REMOVE) {
3158 RuntimeException here = new RuntimeException("here");
3159 here.fillInStackTrace();
3160 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003161 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003162 r.takeFromHistory();
3163 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003164 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003165 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003166 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003167 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003168 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003169 if (VALIDATE_TOKENS) {
3170 validateAppTokensLocked();
3171 }
Craig Mautner312ba862014-02-10 17:55:01 -08003172 final TaskRecord task = r.task;
3173 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003174 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003175 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003176 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3177 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003178 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003179 }
Craig Mautner299f9602015-01-26 09:47:33 -08003180 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003181 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003182 cleanUpActivityServicesLocked(r);
3183 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003185
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003186 /**
3187 * Perform clean-up of service connections in an activity record.
3188 */
3189 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3190 // Throw away any services that have been bound by this activity.
3191 if (r.connections != null) {
3192 Iterator<ConnectionRecord> it = r.connections.iterator();
3193 while (it.hasNext()) {
3194 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003195 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 }
3197 r.connections = null;
3198 }
3199 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003200
Craig Mautneree2e45a2014-06-27 12:10:03 -07003201 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003202 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003203 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003204 mHandler.sendMessage(msg);
3205 }
3206
Craig Mautneree2e45a2014-06-27 12:10:03 -07003207 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003208 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003209 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003210 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3211 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3212 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3213 final ActivityRecord r = activities.get(activityNdx);
3214 if (r.finishing) {
3215 continue;
3216 }
3217 if (r.fullscreen) {
3218 lastIsOpaque = true;
3219 }
3220 if (owner != null && r.app != owner) {
3221 continue;
3222 }
3223 if (!lastIsOpaque) {
3224 continue;
3225 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003226 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003227 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003228 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003229 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003230 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003231 activityRemoved = true;
3232 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003233 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003234 }
3235 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003236 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003237 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003238 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003239 }
3240
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003241 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3242 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003243 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3244 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003245 + " pausing=" + mPausingActivity + " for reason " + reason);
3246 return destroyActivityLocked(r, true, reason);
3247 }
3248 return false;
3249 }
3250
3251 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3252 String reason) {
3253 // Iterate over tasks starting at the back (oldest) first.
3254 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3255 int maxTasks = tasks.size() / 4;
3256 if (maxTasks < 1) {
3257 maxTasks = 1;
3258 }
3259 int numReleased = 0;
3260 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3261 final TaskRecord task = mTaskHistory.get(taskNdx);
3262 if (!tasks.contains(task)) {
3263 continue;
3264 }
3265 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3266 int curNum = 0;
3267 final ArrayList<ActivityRecord> activities = task.mActivities;
3268 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3269 final ActivityRecord activity = activities.get(actNdx);
3270 if (activity.app == app && activity.isDestroyable()) {
3271 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3272 + " in state " + activity.state + " resumed=" + mResumedActivity
3273 + " pausing=" + mPausingActivity + " for reason " + reason);
3274 destroyActivityLocked(activity, true, reason);
3275 if (activities.get(actNdx) != activity) {
3276 // Was removed from list, back up so we don't miss the next one.
3277 actNdx--;
3278 }
3279 curNum++;
3280 }
3281 }
3282 if (curNum > 0) {
3283 numReleased += curNum;
3284 maxTasks--;
3285 if (mTaskHistory.get(taskNdx) != task) {
3286 // The entire task got removed, back up so we don't miss the next one.
3287 taskNdx--;
3288 }
3289 }
3290 }
3291 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3292 return numReleased;
3293 }
3294
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 /**
3296 * Destroy the current CLIENT SIDE instance of an activity. This may be
3297 * called both when actually finishing an activity, or when performing
3298 * a configuration switch where we destroy the current client-side object
3299 * but then create a new client-side object for this same HistoryRecord.
3300 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003301 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003302 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3303 "Removing activity from " + reason + ": token=" + r
3304 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003306 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003307 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308
3309 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003310
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003311 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312
3313 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003315 if (hadApp) {
3316 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003317 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3319 mService.mHeavyWeightProcess = null;
3320 mService.mHandler.sendEmptyMessage(
3321 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3322 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003323 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003324 // Update any services we are bound to that might care about whether
3325 // their client may have activities.
3326 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003327 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003328 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003329 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 }
3331 }
3332
3333 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003334
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003336 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003337 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 r.configChangeFlags);
3339 } catch (Exception e) {
3340 // We can just ignore exceptions here... if the process
3341 // has crashed, our death notification will clean things
3342 // up.
3343 //Slog.w(TAG, "Exception thrown during finish", e);
3344 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003345 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 removedFromHistory = true;
3347 skipDestroy = true;
3348 }
3349 }
3350
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003351 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003352
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003353 // If the activity is finishing, we need to wait on removing it
3354 // from the list to give it a chance to do its cleanup. During
3355 // that time it may make calls back with its token so we need to
3356 // be able to find it on the list and so we don't want to remove
3357 // it from the list yet. Otherwise, we can just immediately put
3358 // it in the destroyed state since we are not removing it from the
3359 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003361 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3362 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003363 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003364 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3366 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003367 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003368 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003369 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003370 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003371 }
3372 } else {
3373 // remove this record from the history.
3374 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003375 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003376 removedFromHistory = true;
3377 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003378 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003379 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003380 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003381 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 }
3383 }
3384
3385 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003386
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003387 if (!mLRUActivities.remove(r) && hadApp) {
3388 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3389 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003390
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003391 return removedFromHistory;
3392 }
3393
Craig Mautner299f9602015-01-26 09:47:33 -08003394 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003395 final long origId = Binder.clearCallingIdentity();
3396 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003397 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003398 if (r != null) {
3399 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003400 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003401 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003402
Wale Ogunwale60454db2015-01-23 16:05:07 -08003403 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003404 if (r.state == ActivityState.DESTROYING) {
3405 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003406 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003407 }
3408 }
Craig Mautner05d29032013-05-03 13:40:13 -07003409 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003410 } finally {
3411 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003412 }
3413 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003414
Todd Kennedyaab56db2015-01-30 09:39:53 -08003415 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003416 if (hasVisibleBehindActivity() &&
3417 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003418 if (r == topRunningActivityLocked(null)) {
3419 // Don't release the top activity if it has requested to run behind the next
3420 // activity.
3421 return;
3422 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003423 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3424 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003425 " thread=" + r.app.thread);
3426 if (r != null && r.app != null && r.app.thread != null) {
3427 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003428 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003429 } catch (RemoteException e) {
3430 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003431 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003432 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003433 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003434 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003435 }
3436 }
3437 }
3438
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003439 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003440 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3441 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003442 if (r != null) {
3443 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003444 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003445 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003446 }
3447 mStackSupervisor.resumeTopActivitiesLocked();
3448 }
3449
Jose Lima4b6c6692014-08-12 17:41:12 -07003450 boolean hasVisibleBehindActivity() {
3451 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003452 }
3453
Jose Lima4b6c6692014-08-12 17:41:12 -07003454 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003455 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003456 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003457 }
3458 }
3459
Jose Lima4b6c6692014-08-12 17:41:12 -07003460 ActivityRecord getVisibleBehindActivity() {
3461 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003462 }
3463
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003464 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3465 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003466 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003467 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3468 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003469 while (i > 0) {
3470 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003471 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003472 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003474 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003475 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003476 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003477 }
3478 }
3479 }
3480
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003481 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3482 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003483 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3484 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003485 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3486 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003487 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003488 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003489 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3490 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003491
3492 boolean hasVisibleActivities = false;
3493
3494 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003495 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003496 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3497 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003498 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3499 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3500 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3501 final ActivityRecord r = activities.get(activityNdx);
3502 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003503 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3504 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003505 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003506 if (r.visible) {
3507 hasVisibleActivities = true;
3508 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003509 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003510 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3511 // Don't currently have state for the activity, or
3512 // it is finishing -- always remove it.
3513 remove = true;
3514 } else if (r.launchCount > 2 &&
3515 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3516 // We have launched this activity too many times since it was
3517 // able to run, so give up and remove it.
3518 remove = true;
3519 } else {
3520 // The process may be gone, but the activity lives on!
3521 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003522 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003523 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003524 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003525 RuntimeException here = new RuntimeException("here");
3526 here.fillInStackTrace();
3527 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3528 + ": haveState=" + r.haveState
3529 + " stateNotNeeded=" + r.stateNotNeeded
3530 + " finishing=" + r.finishing
3531 + " state=" + r.state, here);
3532 }
3533 if (!r.finishing) {
3534 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3535 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3536 r.userId, System.identityHashCode(r),
3537 r.task.taskId, r.shortComponentName,
3538 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003539 if (r.state == ActivityState.RESUMED) {
3540 mService.updateUsageStats(r, false);
3541 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003542 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003543 } else {
3544 // We have the current state for this activity, so
3545 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003546 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003547 TAG, "Keeping entry, setting app to null");
Craig Mautner0247fc82013-02-28 14:32:06 -08003548 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3549 + r);
3550 r.app = null;
3551 r.nowVisible = false;
3552 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003553 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003554 "App died, clearing saved state of " + r);
3555 r.icicle = null;
3556 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003557 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003558 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003559 if (remove) {
3560 removeActivityFromHistoryLocked(r, "appDied");
3561 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003562 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003563 }
3564 }
3565
3566 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003567 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003568
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003569 final void updateTransitLocked(int transit, Bundle options) {
3570 if (options != null) {
3571 ActivityRecord r = topRunningActivityLocked(null);
3572 if (r != null && r.state != ActivityState.RESUMED) {
3573 r.updateOptionsLocked(options);
3574 } else {
3575 ActivityOptions.abort(options);
3576 }
3577 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003578 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003579 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003580
Craig Mautner21d24a22014-04-23 11:45:37 -07003581 void updateTaskMovement(TaskRecord task, boolean toFront) {
3582 if (task.isPersistable) {
3583 task.mLastTimeMoved = System.currentTimeMillis();
3584 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3585 // recently will be most negative, tasks sent to the bottom before that will be less
3586 // negative. Similarly for recent tasks moved to the top which will be most positive.
3587 if (!toFront) {
3588 task.mLastTimeMoved *= -1;
3589 }
3590 }
3591 }
3592
Craig Mautner84984fa2014-06-19 11:19:20 -07003593 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003594 final int top = mTaskHistory.size() - 1;
3595 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3596 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003597 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003598 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3599 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003600 mTaskHistory.remove(taskNdx);
3601 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003602 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003603 return;
3604 }
3605 }
3606 }
3607
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003608 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003609 String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003610 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003611
Craig Mautner11bf9a52013-02-19 14:08:51 -08003612 final int numTasks = mTaskHistory.size();
3613 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003614 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003615 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003616 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003617 ActivityOptions.abort(options);
3618 } else {
3619 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3620 }
3621 return;
3622 }
3623
3624 // Shift all activities with this task up to the top
3625 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003626 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003627
3628 // Set focus to the top running activity of this stack.
3629 ActivityRecord r = topRunningActivityLocked(null);
3630 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003631
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003632 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003633 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003634 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003635 if (r != null) {
3636 mNoAnimActivities.add(r);
3637 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003638 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003639 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003640 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003642
Craig Mautner05d29032013-05-03 13:40:13 -07003643 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003644 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003645
3646 if (VALIDATE_TOKENS) {
3647 validateAppTokensLocked();
3648 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003649 }
3650
3651 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003652 * Worker method for rearranging history stack. Implements the function of moving all
3653 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003655 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003656 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3657 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003658 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003659 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003660 * @return Returns true if the move completed, false if not.
3661 */
Craig Mautner299f9602015-01-26 09:47:33 -08003662 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003663 final TaskRecord tr = taskForIdLocked(taskId);
3664 if (tr == null) {
3665 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3666 return false;
3667 }
3668
3669 Slog.i(TAG, "moveTaskToBack: " + tr);
3670
3671 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003673 // If we have a watcher, preflight the move before committing to it. First check
3674 // for *other* available tasks, but if none are available, then try again allowing the
3675 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003676 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003677 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003678 if (next == null) {
3679 next = topRunningActivityLocked(null, 0);
3680 }
3681 if (next != null) {
3682 // ask watcher if this is allowed
3683 boolean moveOK = true;
3684 try {
3685 moveOK = mService.mController.activityResuming(next.packageName);
3686 } catch (RemoteException e) {
3687 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003688 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 }
3690 if (!moveOK) {
3691 return false;
3692 }
3693 }
3694 }
3695
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003696 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003697
riddle_hsuc215a4f2014-12-27 12:10:45 +08003698 boolean prevIsHome = false;
3699 if (tr.isOverHomeStack()) {
3700 final TaskRecord nextTask = getNextTask(tr);
3701 if (nextTask != null) {
3702 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3703 } else {
3704 prevIsHome = true;
3705 }
3706 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003707 mTaskHistory.remove(tr);
3708 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003709 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710
Craig Mautnerc8143c62013-09-03 12:15:57 -07003711 // There is an assumption that moving a task to the back moves it behind the home activity.
3712 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003713 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003714 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3715 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003716 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003717 break;
3718 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003719 if (taskNdx == 1) {
3720 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003721 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003722 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003723 }
3724
Craig Mautner299f9602015-01-26 09:47:33 -08003725 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003726 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003727
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003728 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003729 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003730 }
3731
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003732 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003733 if (prevIsHome || task == tr && tr.isOverHomeStack()
3734 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003735 if (!mService.mBooting && !mService.mBooted) {
3736 // Not ready yet!
3737 return false;
3738 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003739 final int taskToReturnTo = tr.getTaskToReturnTo();
3740 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003741 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003742 }
3743
Craig Mautner05d29032013-05-03 13:40:13 -07003744 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745 return true;
3746 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003747
Craig Mautner8849a5e2013-04-02 16:41:03 -07003748 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003749 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003750 final Uri data = r.intent.getData();
3751 final String strData = data != null ? data.toSafeString() : null;
3752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003753 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003754 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003756 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003757 }
3758
3759 /**
3760 * Make sure the given activity matches the current configuration. Returns
3761 * false if the activity had to be destroyed. Returns true if the
3762 * configuration is the same, or the activity will remain running as-is
3763 * for whatever reason. Ensures the HistoryRecord is updated with the
3764 * correct configuration and all other bookkeeping is handled.
3765 */
3766 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3767 int globalChanges) {
3768 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003769 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003770 "Skipping config check (will change): " + r);
3771 return true;
3772 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003773
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003774 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003775 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003776
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003777 // Make sure the current stack override configuration is supported by the top task
3778 // before continuing.
3779 final TaskRecord topTask = topTask();
3780 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3781 || (!topTask.mResizeable && !mFullscreen))) {
3782 final boolean prevFullscreen = mFullscreen;
3783 final Configuration newOverrideConfig =
3784 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3785 updateOverrideConfiguration(newOverrideConfig);
3786 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003787 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003788 "Updated stack config to support task=" + topTask
3789 + " resizeable=" + topTask.mResizeable
3790 + " mForcedFullscreen=" + mForcedFullscreen
3791 + " prevFullscreen=" + prevFullscreen
3792 + " mFullscreen=" + mFullscreen);
3793 }
3794
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003795 // Short circuit: if the two configurations are the exact same
3796 // object (the common case), then there is nothing to do.
3797 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003798 if (r.configuration == newConfig
3799 && r.stackConfigOverride == mOverrideConfig
3800 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003801 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003802 "Configuration unchanged in " + r);
3803 return true;
3804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003805
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003806 // We don't worry about activities that are finishing.
3807 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003808 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003809 "Configuration doesn't matter in finishing " + r);
3810 r.stopFreezingScreenLocked(false);
3811 return true;
3812 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003813
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003814 // Okay we now are going to make this activity have the new config.
3815 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003816 final Configuration oldConfig = r.configuration;
3817 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003818 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003819 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003820
3821 // Determine what has changed. May be nothing, if this is a config
3822 // that has come back from the app after going idle. In that case
3823 // we just want to leave the official config object now in the
3824 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003825 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003826 if (stackChanges == 0) {
3827 // {@link Configuration#diff} doesn't catch changes from unset values.
3828 // Check for changes we care about.
3829 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3830 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3831 }
3832 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3833 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3834 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3835 }
3836 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3837 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3838 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003839 }
3840 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003841 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003842 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003843 "Configuration no differences in " + r);
3844 return true;
3845 }
3846
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003847 // If the activity isn't currently running, just leave the new
3848 // configuration and it will pick that up next time it starts.
3849 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003850 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003851 "Configuration doesn't matter not running " + r);
3852 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003853 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003854 return true;
3855 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003856
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003857 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003858 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3859 "Checking to restart " + r.info.name + ": changed=0x"
3860 + Integer.toHexString(changes) + ", handles=0x"
3861 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3862
Dianne Hackborne6676352011-06-01 16:51:20 -07003863 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003864 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3865 r.configChangeFlags |= changes;
3866 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003867 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003868 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003869 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003870 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003871 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003872 } else if (r.state == ActivityState.PAUSING) {
3873 // A little annoying: we are waiting for this activity to
3874 // finish pausing. Let's not do anything now, but just
3875 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003876 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003877 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003878 r.configDestroy = true;
3879 return true;
3880 } else if (r.state == ActivityState.RESUMED) {
3881 // Try to optimize this case: the configuration is changing
3882 // and we need to restart the top, resumed activity.
3883 // Instead of doing the normal handshaking, just say
3884 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003885 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003886 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003887 relaunchActivityLocked(r, r.configChangeFlags, true);
3888 r.configChangeFlags = 0;
3889 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003890 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003891 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003892 relaunchActivityLocked(r, r.configChangeFlags, false);
3893 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003894 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003895
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003896 // All done... tell the caller we weren't able to keep this
3897 // activity around.
3898 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003899 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003900
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003901 // Default case: the activity can handle this new configuration, so hand it over.
3902 // NOTE: We only forward the stack override configuration as the system level configuration
3903 // changes is always sent to all processes when they happen so it can just use whatever
3904 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003905 if (r.app != null && r.app.thread != null) {
3906 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003907 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003908 r.app.thread.scheduleActivityConfigurationChanged(
3909 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003910 } catch (RemoteException e) {
3911 // If process died, whatever.
3912 }
3913 }
3914 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003915
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003916 return true;
3917 }
3918
Craig Mautner2568c3a2015-03-26 14:22:34 -07003919 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003920 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003921 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003922 if (andResume) {
3923 results = r.results;
3924 newIntents = r.newIntents;
3925 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003926 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3927 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003928 + " andResume=" + andResume);
3929 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003930 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003931 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003932
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003933 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003934
Craig Mautner34b73df2014-01-12 21:11:08 -08003935 mStackSupervisor.removeChildActivityContainers(r);
3936
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003937 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003938 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
3939 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003940 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003941 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3942 !andResume, new Configuration(mService.mConfiguration),
3943 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003944 // Note: don't need to call pauseIfSleepingLocked() here, because
3945 // the caller will only pass in 'andResume' if this activity is
3946 // currently resumed, which implies we aren't sleeping.
3947 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003948 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949 }
3950
3951 if (andResume) {
3952 r.results = null;
3953 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003954 r.state = ActivityState.RESUMED;
3955 } else {
3956 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3957 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003958 }
3959
3960 return true;
3961 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003962
3963 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003964 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3965 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3966 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3967 final ActivityRecord r = activities.get(activityNdx);
3968 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003969 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003970 }
3971 if (r.fullscreen && !r.finishing) {
3972 return false;
3973 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003974 }
3975 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003976 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003977 if (r == null) {
3978 return false;
3979 }
3980 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3981 + " would have returned true for r=" + r);
3982 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003983 }
3984
3985 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003986 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3987 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3988 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3989 final ActivityRecord r = activities.get(activityNdx);
3990 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003991 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003992 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003993 }
3994 }
3995 }
3996
3997 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3998 boolean didSomething = false;
3999 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004000 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004001 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4002 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4003 int numActivities = activities.size();
4004 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4005 ActivityRecord r = activities.get(activityNdx);
4006 final boolean samePackage = r.packageName.equals(name)
4007 || (name == null && r.userId == userId);
4008 if ((userId == UserHandle.USER_ALL || r.userId == userId)
4009 && (samePackage || r.task == lastTask)
4010 && (r.app == null || evenPersistent || !r.app.persistent)) {
4011 if (!doit) {
4012 if (r.finishing) {
4013 // If this activity is just finishing, then it is not
4014 // interesting as far as something to stop.
4015 continue;
4016 }
4017 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004018 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004019 if (r.isHomeActivity()) {
4020 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4021 Slog.i(TAG, "Skip force-stop again " + r);
4022 continue;
4023 } else {
4024 homeActivity = r.realActivity;
4025 }
4026 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004027 didSomething = true;
4028 Slog.i(TAG, " Force finishing activity " + r);
4029 if (samePackage) {
4030 if (r.app != null) {
4031 r.app.removed = true;
4032 }
4033 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004034 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004035 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004036 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4037 true)) {
4038 // r has been deleted from mActivities, accommodate.
4039 --numActivities;
4040 --activityNdx;
4041 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004042 }
4043 }
4044 }
4045 return didSomething;
4046 }
4047
Dianne Hackborn09233282014-04-30 11:33:59 -07004048 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004049 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004050 final TaskRecord task = mTaskHistory.get(taskNdx);
4051 ActivityRecord r = null;
4052 ActivityRecord top = null;
4053 int numActivities = 0;
4054 int numRunning = 0;
4055 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07004056 if (activities.isEmpty()) {
4057 continue;
4058 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004059 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004060 continue;
4061 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004062 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4063 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004064
Craig Mautneraab647e2013-02-28 16:31:36 -08004065 // Initialize state for next task if needed.
4066 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4067 top = r;
4068 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004069 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004070
4071 // Add 'r' into the current task.
4072 numActivities++;
4073 if (r.app != null && r.app.thread != null) {
4074 numRunning++;
4075 }
4076
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004077 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004078 TAG, r.intent.getComponent().flattenToShortString()
4079 + ": task=" + r.task);
4080 }
4081
4082 RunningTaskInfo ci = new RunningTaskInfo();
4083 ci.id = task.taskId;
4084 ci.baseActivity = r.intent.getComponent();
4085 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004086 ci.lastActiveTime = task.lastActiveTime;
4087
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004088 if (top.task != null) {
4089 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004090 }
4091 ci.numActivities = numActivities;
4092 ci.numRunning = numRunning;
4093 //System.out.println(
4094 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08004095 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004096 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004097 }
4098
4099 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004100 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004101 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004102 if (top >= 0) {
4103 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4104 int activityTop = activities.size() - 1;
4105 if (activityTop > 0) {
4106 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4107 "unhandled-back", true);
4108 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004109 }
4110 }
4111
Craig Mautner6b74cb52013-09-27 17:02:21 -07004112 /**
4113 * Reset local parameters because an app's activity died.
4114 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004115 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004116 */
4117 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004118 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004119 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004120 "App died while pausing: " + mPausingActivity);
4121 mPausingActivity = null;
4122 }
4123 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4124 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004125 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004126 }
4127
Craig Mautner19091252013-10-05 00:03:53 -07004128 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004129 }
4130
Craig Mautnercae015f2013-02-08 14:31:27 -08004131 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004132 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4133 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4134 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4135 final ActivityRecord r = activities.get(activityNdx);
4136 if (r.app == app) {
4137 Slog.w(TAG, " Force finishing activity "
4138 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004139 // Force the destroy to skip right to removal.
4140 r.app = null;
4141 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004142 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004143 }
4144 }
4145 }
4146
Dianne Hackborn390517b2013-05-30 15:03:32 -07004147 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004148 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004149 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004150 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4151 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004152 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4153 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004154 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004155 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004156 if (printed) {
4157 header = null;
4158 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004159 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004160 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004161 }
4162
4163 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4164 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4165
4166 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004167 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4168 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004169 }
4170 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004171 final int top = mTaskHistory.size() - 1;
4172 if (top >= 0) {
4173 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4174 int listTop = list.size() - 1;
4175 if (listTop >= 0) {
4176 activities.add(list.get(listTop));
4177 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004178 }
4179 } else {
4180 ItemMatcher matcher = new ItemMatcher();
4181 matcher.build(name);
4182
Craig Mautneraab647e2013-02-28 16:31:36 -08004183 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4184 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4185 if (matcher.match(r1, r1.intent.getComponent())) {
4186 activities.add(r1);
4187 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004188 }
4189 }
4190 }
4191
4192 return activities;
4193 }
4194
4195 ActivityRecord restartPackage(String packageName) {
4196 ActivityRecord starting = topRunningActivityLocked(null);
4197
4198 // All activities that came from the package must be
4199 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004200 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4201 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4202 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4203 final ActivityRecord a = activities.get(activityNdx);
4204 if (a.info.packageName.equals(packageName)) {
4205 a.forceNewConfig = true;
4206 if (starting != null && a == starting && a.visible) {
4207 a.startFreezingScreenLocked(starting.app,
4208 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4209 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004210 }
4211 }
4212 }
4213
4214 return starting;
4215 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004216
Craig Mautner299f9602015-01-26 09:47:33 -08004217 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07004218 removeTask(task, reason, true /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004219 }
4220
Wale Ogunwale000957c2015-04-03 08:19:12 -07004221 /**
4222 * Removes the input task from this stack.
4223 * @param task to remove.
4224 * @param reason for removal.
4225 * @param notMoving task to another stack. In the case we are moving we don't want to perform
4226 * some operations on the task like removing it from window manager or recents.
4227 */
4228 void removeTask(TaskRecord task, String reason, boolean notMoving) {
4229 if (notMoving) {
4230 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004231 mWindowManager.removeTask(task.taskId);
4232 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004233
Craig Mautner04a0ea62014-01-13 12:51:26 -08004234 final ActivityRecord r = mResumedActivity;
4235 if (r != null && r.task == task) {
4236 mResumedActivity = null;
4237 }
4238
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004239 final int taskNdx = mTaskHistory.indexOf(task);
4240 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004241 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4242 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4243 if (!nextTask.isOverHomeStack()) {
4244 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4245 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004246 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004247 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004248 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004249
Wale Ogunwale000957c2015-04-03 08:19:12 -07004250 if (notMoving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004251 final boolean isVoiceSession = task.voiceSession != null;
4252 if (isVoiceSession) {
4253 try {
4254 task.voiceSession.taskFinished(task.intent, task.taskId);
4255 } catch (RemoteException e) {
4256 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004257 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004258 if (task.autoRemoveFromRecents() || isVoiceSession) {
4259 // Task creator asked to remove this when done, or this task was a voice
4260 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004261 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004262 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004263 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004264 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004265
4266 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004267 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004268 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004269 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004270 String myReason = reason + " leftTaskHistoryEmpty";
4271 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4272 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4273 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004274 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004275 if (mStacks != null) {
4276 mStacks.remove(this);
4277 mStacks.add(0, this);
4278 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004279 if (notHomeStack) {
4280 mActivityContainer.onTaskListEmptyLocked();
4281 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004282 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004283
4284 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004285 }
4286
Dianne Hackborn91097de2014-04-04 18:02:06 -07004287 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4288 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4289 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004290 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4291 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004292 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004293 return task;
4294 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004295
4296 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004297 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004298 }
4299
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004300 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004301 task.stack = this;
4302 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004303 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004304 } else {
4305 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004306 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004307 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004308 if (!moving && task.voiceSession != null) {
4309 try {
4310 task.voiceSession.taskStarted(task.intent, task.taskId);
4311 } catch (RemoteException e) {
4312 }
4313 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004314 }
4315
4316 public int getStackId() {
4317 return mStackId;
4318 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004319
4320 @Override
4321 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004322 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4323 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004324 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004325
4326 boolean updateOverrideConfiguration(Configuration newConfig) {
4327 Configuration oldConfig = mOverrideConfig;
4328 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004329 // We override the configuration only when the stack's dimensions are different from
4330 // the display. In this manner, we know that if the override configuration is empty,
4331 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004332 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004333 return !mOverrideConfig.equals(oldConfig);
4334 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004335}