blob: 951b9a94e4e5e058c50e8638475bc88d70b34e3e [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 Ogunwale6dfdfd62015-04-15 12:01:38 -070019import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070020
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 boolean okToShowLocked(ActivityRecord r) {
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700367 return mStackSupervisor.isCurrentProfileLocked(r.userId)
368 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0;
Craig Mautner5962b122012-10-05 14:45:52 -0700369 }
370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700371 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800372 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700373 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
374 if (r != null) {
375 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800376 }
377 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 return null;
379 }
380
381 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800382 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
383 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800384 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800385 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
386 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800387 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800388 return r;
389 }
390 }
391 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700392 return null;
393 }
394
395 /**
396 * This is a simplified version of topRunningActivityLocked that provides a number of
397 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800398 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700399 * @param token If non-null, any history records matching this token will be skipped.
400 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800401 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700402 * @return Returns the HistoryRecord of the next activity on the stack.
403 */
404 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800405 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
406 TaskRecord task = mTaskHistory.get(taskNdx);
407 if (task.taskId == taskId) {
408 continue;
409 }
410 ArrayList<ActivityRecord> activities = task.mActivities;
411 for (int i = activities.size() - 1; i >= 0; --i) {
412 final ActivityRecord r = activities.get(i);
413 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800414 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800415 return r;
416 }
417 }
418 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700419 return null;
420 }
421
Craig Mautner8849a5e2013-04-02 16:41:03 -0700422 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700423 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
424 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700425 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
426 final ActivityRecord r = activities.get(activityNdx);
427 if (!r.finishing) {
428 return r;
429 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700430 }
431 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700432 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700433 }
434
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700435 final TaskRecord topTask() {
436 final int size = mTaskHistory.size();
437 if (size > 0) {
438 return mTaskHistory.get(size - 1);
439 }
440 return null;
441 }
442
Craig Mautnerd2328952013-03-05 12:46:26 -0800443 TaskRecord taskForIdLocked(int id) {
444 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
445 final TaskRecord task = mTaskHistory.get(taskNdx);
446 if (task.taskId == id) {
447 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800448 }
449 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700450 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700451 }
452
Craig Mautnerd2328952013-03-05 12:46:26 -0800453 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700454 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800455 return isInStackLocked(r);
456 }
457
458 ActivityRecord isInStackLocked(ActivityRecord r) {
459 if (r == null) {
460 return null;
461 }
462 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700463 if (task != null && task.stack != null
464 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800465 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800466 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800467 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800468 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800469 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800470 }
471
Craig Mautner2420ead2013-04-01 17:13:20 -0700472 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700473 final boolean hadit = mLRUActivities.remove(r);
474 mLRUActivities.add(r);
475 return hadit;
476 }
477
Craig Mautnerde4ef022013-04-07 19:01:33 -0700478 final boolean isHomeStack() {
479 return mStackId == HOME_STACK_ID;
480 }
481
Craig Mautnere0a38842013-12-16 16:14:02 -0800482 final boolean isOnHomeDisplay() {
483 return isAttached() &&
484 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
485 }
486
Craig Mautner299f9602015-01-26 09:47:33 -0800487 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800488 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800489 final boolean homeStack = isHomeStack()
490 || (mActivityContainer.mParentActivity != null
491 && mActivityContainer.mParentActivity.isHomeActivity());
492 ActivityStack lastFocusStack = null;
493 if (!homeStack) {
494 // Need to move this stack to the front before calling
495 // {@link ActivityStackSupervisor#moveHomeStack} below.
496 lastFocusStack = mStacks.get(mStacks.size() - 1);
497 mStacks.remove(this);
498 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800499 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700500 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
501 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800502 if (isOnHomeDisplay()) {
503 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
504 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800505 final TaskRecord task = topTask();
506 if (task != null) {
507 mWindowManager.moveTaskToTop(task.taskId);
508 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 }
511
512 final boolean isAttached() {
513 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800514 }
515
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700516 /**
517 * Returns the top activity in any existing task matching the given
518 * Intent. Returns null if no such task is found.
519 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700520 ActivityRecord findTaskLocked(ActivityRecord target) {
521 Intent intent = target.intent;
522 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700523 ComponentName cls = intent.getComponent();
524 if (info.targetActivity != null) {
525 cls = new ComponentName(info.packageName, info.targetActivity);
526 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700527 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700528 boolean isDocument = intent != null & intent.isDocument();
529 // If documentData is non-null then it must match the existing task data.
530 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800531
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700532 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800533 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
534 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700535 if (task.voiceSession != null) {
536 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700537 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700538 continue;
539 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700540 if (task.userId != userId) {
541 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700542 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700543 continue;
544 }
Craig Mautner000f0022013-02-26 15:04:29 -0800545 final ActivityRecord r = task.getTopActivity();
546 if (r == null || r.finishing || r.userId != userId ||
547 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700548 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800549 continue;
550 }
551
Craig Mautnerd00f4742014-03-12 14:17:26 -0700552 final Intent taskIntent = task.intent;
553 final Intent affinityIntent = task.affinityIntent;
554 final boolean taskIsDocument;
555 final Uri taskDocumentData;
556 if (taskIntent != null && taskIntent.isDocument()) {
557 taskIsDocument = true;
558 taskDocumentData = taskIntent.getData();
559 } else if (affinityIntent != null && affinityIntent.isDocument()) {
560 taskIsDocument = true;
561 taskDocumentData = affinityIntent.getData();
562 } else {
563 taskIsDocument = false;
564 taskDocumentData = null;
565 }
566
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700567 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700568 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700569 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700570 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700571 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
572 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700573 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700574 return r;
575 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700576 } else if (taskIntent != null && taskIntent.getComponent() != null &&
577 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700578 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700579 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800580 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700581 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
582 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800583 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700584 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
585 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700586 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700587 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800588 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
590 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800591 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700592 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700593 }
594
595 return null;
596 }
597
598 /**
599 * Returns the first activity (starting from the top of the stack) that
600 * is the same as the given activity. Returns null if no such activity
601 * is found.
602 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700603 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700604 ComponentName cls = intent.getComponent();
605 if (info.targetActivity != null) {
606 cls = new ComponentName(info.packageName, info.targetActivity);
607 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700608 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700609
Craig Mautner000f0022013-02-26 15:04:29 -0800610 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700611 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700612 final boolean notCurrentUserTask =
613 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700614 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700615
Craig Mautner000f0022013-02-26 15:04:29 -0800616 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
617 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700618 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700619 return null;
620 }
Craig Mautner000f0022013-02-26 15:04:29 -0800621 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700622 //Slog.i(TAG, "Found matching class!");
623 //dump();
624 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
625 return r;
626 }
627 }
628 }
629
630 return null;
631 }
632
Amith Yamasani742a6712011-05-04 14:49:28 -0700633 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700634 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700635 */
Craig Mautner93529a42013-10-04 15:03:13 -0700636 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800637 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700638 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800639 }
640 mCurrentUser = userId;
641
642 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800643 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800644 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700645 final TaskRecord task = mTaskHistory.get(i);
646
647 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
648 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700649 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700650 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700651 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700652 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800653 mTaskHistory.remove(i);
654 mTaskHistory.add(task);
655 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800656 // Use same value for i.
657 } else {
658 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800659 }
660 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700661 if (VALIDATE_TOKENS) {
662 validateAppTokensLocked();
663 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700664 }
665
Craig Mautner2420ead2013-04-01 17:13:20 -0700666 void minimalResumeActivityLocked(ActivityRecord r) {
667 r.state = ActivityState.RESUMED;
668 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
669 + " (starting new instance)");
670 r.stopped = false;
671 mResumedActivity = r;
672 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800673 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700674 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700675 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700676 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700677 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700678 }
679
Dianne Hackborncee04b52013-07-03 17:01:28 -0700680 private void startLaunchTraces() {
681 if (mFullyDrawnStartTime != 0) {
682 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
683 }
684 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
685 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
686 }
687
688 private void stopFullyDrawnTraceIfNeeded() {
689 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
690 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
691 mFullyDrawnStartTime = 0;
692 }
693 }
694
Craig Mautnere79d42682013-04-01 19:01:53 -0700695 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700696 if (r.displayStartTime == 0) {
697 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
698 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700699 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700700 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700701 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700702 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700703 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700704 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700705 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700706 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800707
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700708 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700709 // Make sure that there is no activity waiting for this to launch.
710 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
711 r.displayStartTime = r.fullyDrawnStartTime = 0;
712 } else {
713 mStackSupervisor.removeTimeoutsForActivityLocked(r);
714 mStackSupervisor.scheduleIdleTimeoutLocked(r);
715 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700716 }
717
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800718 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800719 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800720 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
721 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
722 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
723 activities.get(activityNdx).setSleeping(false);
724 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800725 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800726 if (mPausingActivity != null) {
727 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
728 activityPausedLocked(mPausingActivity.appToken, true);
729 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800730 }
731
Craig Mautner0eea92c2013-05-16 13:35:39 -0700732 /**
733 * @return true if something must be done before going to sleep.
734 */
735 boolean checkReadyForSleepLocked() {
736 if (mResumedActivity != null) {
737 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700738 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
739 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
740 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700741 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700742 return true;
743 }
744 if (mPausingActivity != null) {
745 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700746 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700747 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800748 }
749
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700750 if (hasVisibleBehindActivity()) {
751 // Stop visible behind activity before going to sleep.
752 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
753 mStackSupervisor.mStoppingActivities.add(r);
754 if (DEBUG_STATES) Slog.v(TAG, "Sleep still waiting to stop visible behind " + r);
755 return true;
756 }
757
Craig Mautner0eea92c2013-05-16 13:35:39 -0700758 return false;
759 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800760
Craig Mautner0eea92c2013-05-16 13:35:39 -0700761 void goToSleep() {
762 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800763
Craig Mautner0eea92c2013-05-16 13:35:39 -0700764 // Make sure any stopped but visible activities are now sleeping.
765 // This ensures that the activity's onStop() is called.
766 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
767 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
768 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
769 final ActivityRecord r = activities.get(activityNdx);
770 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
771 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800772 }
773 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800774 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700775 }
Craig Mautner59c00972012-07-30 12:10:24 -0700776
Dianne Hackbornd2835932010-12-13 16:28:46 -0800777 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700778 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800779 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700780 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800781 return null;
782 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800783
Winson Chung083baf92014-07-11 10:32:42 -0700784 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700785 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800786 // we can just go ahead and skip taking the screenshot if this is the home stack.
787 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700788 return null;
789 }
790
Winson Chung48a10a52014-08-27 14:36:51 -0700791 int w = mService.mThumbnailWidth;
792 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800793 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800794 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
795 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700796 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800797 }
Winson Chung376543b2014-05-21 17:43:28 -0700798 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800799 return null;
800 }
801
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700802 /**
803 * Start pausing the currently resumed activity. It is an error to call this if there
804 * is already an activity being paused or there is no resumed activity.
805 *
806 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
807 * @param uiSleeping True if this is happening with the user interface going to sleep (the
808 * screen turning off).
809 * @param resuming True if this is being called as part of resuming the top activity, so
810 * we shouldn't try to instigate a resume here.
811 * @param dontWait True if the caller does not want to wait for the pause to complete. If
812 * set to true, we will immediately complete the pause here before returning.
813 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
814 * it to tell us when it is done.
815 */
816 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
817 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800818 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800819 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
820 + " state=" + mPausingActivity.state);
821 if (!mService.isSleeping()) {
822 // Avoid recursion among check for sleep and complete pause during sleeping.
823 // Because activity will be paused immediately after resume, just let pause
824 // be completed by the order of activity paused from clients.
825 completePauseLocked(false);
826 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800827 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828 ActivityRecord prev = mResumedActivity;
829 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700830 if (!resuming) {
831 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
832 mStackSupervisor.resumeTopActivitiesLocked();
833 }
834 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800836
837 if (mActivityContainer.mParentActivity == null) {
838 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700839 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800840 }
841
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700842 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700843 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800845 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700847 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
848 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 prev.state = ActivityState.PAUSING;
850 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700851 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700852 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800853 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800854 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700855 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700856 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857
858 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700861 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 try {
863 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700864 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700865 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700866 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800867 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700868 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 } catch (Exception e) {
870 // Ignore exception, if process died other code will cleanup.
871 Slog.w(TAG, "Exception thrown during pause", e);
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 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700878 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700879 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 }
881
882 // If we are not going to sleep, we want to ensure the device is
883 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700884 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700885 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886 }
887
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700889 // Have the window manager pause its key dispatching until the new
890 // activity has started. If we're pausing the activity just because
891 // the screen is being turned off and the UI is sleeping, don't interrupt
892 // key dispatch; the same activity will pick it up again on wakeup.
893 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800894 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700895 } else if (DEBUG_PAUSE) {
896 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700897 }
898
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700899 if (dontWait) {
900 // If the caller said they don't want to wait for the pause, then complete
901 // the pause now.
902 completePauseLocked(false);
903 return false;
904
905 } else {
906 // Schedule a pause timeout in case the app doesn't respond.
907 // We don't give it much time because this directly impacts the
908 // responsiveness seen by the user.
909 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
910 msg.obj = prev;
911 prev.pauseTime = SystemClock.uptimeMillis();
912 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700914 return true;
915 }
916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 } else {
918 // This activity failed to schedule the
919 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700920 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 if (!resuming) {
922 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
923 }
924 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 }
926 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700927
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700928 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700929 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
930 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931
Craig Mautnerd2328952013-03-05 12:46:26 -0800932 final ActivityRecord r = isInStackLocked(token);
933 if (r != null) {
934 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
935 if (mPausingActivity == r) {
936 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
937 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700938 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800939 } else {
940 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
941 r.userId, System.identityHashCode(r), r.shortComponentName,
942 mPausingActivity != null
943 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +0800944 if (r.finishing && r.state == ActivityState.PAUSING) {
945 if (DEBUG_PAUSE) Slog.v(TAG,
946 "Executing finish of failed to pause activity: " + r);
947 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
948 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 }
950 }
951 }
952
Craig Mautnera0026042014-04-23 11:45:37 -0700953 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
954 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700955 if (r.state != ActivityState.STOPPING) {
956 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
957 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
958 return;
959 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700960 if (persistentState != null) {
961 r.persistentState = persistentState;
962 mService.notifyTaskPersisterLocked(r.task, false);
963 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700964 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700965 if (icicle != null) {
966 // If icicle is null, this is happening due to a timeout, so we
967 // haven't really saved the state.
968 r.icicle = icicle;
969 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800970 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800971 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700972 }
973 if (!r.stopped) {
974 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
975 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
976 r.stopped = true;
977 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700978 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
979 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700980 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700981 if (r.finishing) {
982 r.clearOptionsLocked();
983 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700984 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700985 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700986 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700987 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700988 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800989 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700990 }
991 }
992 }
993
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700994 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800995 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700996 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800997
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800998 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700999 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001000 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001001 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001002 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001003 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001004 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001005 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001006 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1007 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001008 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001009 if (prev.configDestroy) {
1010 // The previous is being paused because the configuration
1011 // is changing, which means it is actually stopping...
1012 // To juggle the fact that we are also starting a new
1013 // instance right now, we need to first completely stop
1014 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001015 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001016 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001017 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001018 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001019 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001020 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001021 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1022 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 // If we already have a few activities waiting to stop,
1024 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001025 // them out. Or if r is the last of activity of the last task the stack
1026 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001027 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001028 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001029 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001030 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001031 }
1032 }
1033 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001034 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001035 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001036 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001037 // It is possible the activity was freezing the screen before it was paused.
1038 // In that case go ahead and remove the freeze this activity has on the screen
1039 // since it is no longer visible.
1040 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001041 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001043
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001044 if (resumeNext) {
1045 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1046 if (!mService.isSleepingOrShuttingDown()) {
1047 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1048 } else {
1049 mStackSupervisor.checkReadyForSleepLocked();
1050 ActivityRecord top = topStack.topRunningActivityLocked(null);
1051 if (top == null || (prev != null && top != prev)) {
1052 // If there are no more activities available to run,
1053 // do resume anyway to start something. Also if the top
1054 // activity on the stack is not the just paused activity,
1055 // we need to go ahead and resume it to ensure we complete
1056 // an in-flight app switch.
1057 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1058 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001059 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001060 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001061
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001062 if (prev != null) {
1063 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064
Craig Mautner525f3d92013-05-07 14:01:50 -07001065 if (prev.app != null && prev.cpuTimeAtResume > 0
1066 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001067 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1068 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001069 if (diff > 0) {
1070 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1071 synchronized (bsi) {
1072 BatteryStatsImpl.Uid.Proc ps =
1073 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001074 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001075 if (ps != null) {
1076 ps.addForegroundTimeLocked(diff);
1077 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 }
1079 }
1080 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001081 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001082 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001083
1084 // Notfiy when the task stack has changed
1085 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001086 }
1087
1088 /**
1089 * Once we know that we have asked an application to put an activity in
1090 * the resumed state (either by launching it or explicitly telling it),
1091 * this function updates the rest of our state to match that fact.
1092 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001093 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094 next.idle = false;
1095 next.results = null;
1096 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001097
1098 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1099 ProcessRecord app = next.task.mActivities.get(0).app;
1100 if (app != null && app != mService.mHomeProcess) {
1101 mService.mHomeProcess = app;
1102 }
1103 }
1104
Craig Mautner07566322013-09-26 16:42:55 -07001105 if (next.nowVisible) {
1106 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001107 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001108 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001109
1110 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001111 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001112
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001113 mStackSupervisor.reportResumedActivityLocked(next);
1114
1115 next.resumeKeyDispatchingLocked();
1116 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001117
1118 // Mark the point when the activity is resuming
1119 // TODO: To be more accurate, the mark should be before the onCreate,
1120 // not after the onResume. But for subsequent starts, onResume is fine.
1121 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001122 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 } else {
1124 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1125 }
Winson Chung376543b2014-05-21 17:43:28 -07001126
George Mount6ba042b2014-07-28 11:12:28 -07001127 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001128
1129 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1130 // When resuming an activity, require it to call requestVisibleBehind() again.
1131 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1132 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001133 }
1134
Craig Mautner2568c3a2015-03-26 14:22:34 -07001135 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001136 r.visible = visible;
1137 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001138 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001139 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001140 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001141 container.setVisible(visible);
1142 }
1143 }
1144
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001145 // Find the first visible activity above the passed activity and if it is translucent return it
1146 // otherwise return null;
1147 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1148 TaskRecord task = r.task;
1149 if (task == null) {
1150 return null;
1151 }
1152
1153 ActivityStack stack = task.stack;
1154 if (stack == null) {
1155 return null;
1156 }
1157
1158 int stackNdx = mStacks.indexOf(stack);
1159
1160 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1161 int taskNdx = tasks.indexOf(task);
1162
1163 ArrayList<ActivityRecord> activities = task.mActivities;
1164 int activityNdx = activities.indexOf(r) + 1;
1165
1166 final int numStacks = mStacks.size();
1167 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001168 ActivityStack historyStack = mStacks.get(stackNdx);
1169 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001170 final int numTasks = tasks.size();
1171 while (taskNdx < numTasks) {
1172 activities = tasks.get(taskNdx).mActivities;
1173 final int numActivities = activities.size();
1174 while (activityNdx < numActivities) {
1175 final ActivityRecord activity = activities.get(activityNdx);
1176 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001177 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001178 }
1179 ++activityNdx;
1180 }
1181 activityNdx = 0;
1182 ++taskNdx;
1183 }
1184 taskNdx = 0;
1185 ++stackNdx;
1186 }
1187
1188 return null;
1189 }
1190
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001191 private ActivityStack getNextVisibleStackLocked() {
1192 ArrayList<ActivityStack> stacks = mStacks;
1193 final ActivityRecord parent = mActivityContainer.mParentActivity;
1194 if (parent != null) {
1195 stacks = parent.task.stack.mStacks;
1196 }
1197 if (stacks != null) {
1198 for (int i = stacks.size() - 1; i >= 0; --i) {
1199 ActivityStack stack = stacks.get(i);
1200 if (stack != this && stack.isStackVisibleLocked()) {
1201 return stack;
1202 }
1203 }
1204 }
1205 return null;
1206 }
1207
Jose Lima7ba71252014-04-30 12:59:27 -07001208 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1209 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001210 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001211 if (!isAttached()) {
1212 return false;
1213 }
1214
1215 if (mStackSupervisor.isFrontStack(this)) {
1216 return true;
1217 }
1218
Jose Lima729cb232014-05-05 15:59:40 -07001219 /**
1220 * Start at the task above this one and go up, looking for a visible
1221 * fullscreen activity, or a translucent activity that requested the
1222 * wallpaper to be shown behind it.
1223 */
Jose Lima7ba71252014-04-30 12:59:27 -07001224 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001225 ActivityStack stack = mStacks.get(i);
1226 // stack above isn't full screen, so, we assume we're still visible. at some point
1227 // we should look at the stack bounds to see if we're occluded even if the stack
1228 // isn't fullscreen
1229 if (!stack.mFullscreen) {
1230 continue;
1231 }
1232 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1233 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001234 final TaskRecord task = tasks.get(taskNdx);
1235 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001236 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001237 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001238
1239 // Conditions for an activity to obscure the stack we're
1240 // examining:
1241 // 1. Not Finishing AND Visible AND:
1242 // 2. Either:
1243 // - Full Screen Activity OR
1244 // - On top of Home and our stack is NOT home
1245 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001246 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001247 return false;
1248 }
1249 }
1250 }
1251 }
1252
1253 return true;
1254 }
1255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 /**
1257 * Make sure that all activities that need to be visible (that is, they
1258 * currently can be seen by the user) actually are.
1259 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001260 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1261 ActivityRecord top = topRunningActivityLocked(null);
1262 if (top == null) {
1263 return;
1264 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001265 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1266 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001267 + " configChanges=0x" + Integer.toHexString(configChanges));
1268
Craig Mautner5eda9b32013-07-02 11:58:16 -07001269 if (mTranslucentActivityWaiting != top) {
1270 mUndrawnActivitiesBelowTopTranslucent.clear();
1271 if (mTranslucentActivityWaiting != null) {
1272 // Call the callback with a timeout indication.
1273 notifyActivityDrawnLocked(null);
1274 mTranslucentActivityWaiting = null;
1275 }
1276 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1277 }
1278
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001279 // If the top activity is not fullscreen, then we need to
1280 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001281 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001282 boolean behindFullscreen = !isStackVisibleLocked();
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001283 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001284
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001285 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001286 final TaskRecord task = mTaskHistory.get(taskNdx);
1287 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001288 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1289 final ActivityRecord r = activities.get(activityNdx);
1290 if (r.finishing) {
1291 continue;
1292 }
1293 if (aboveTop && r != top) {
1294 continue;
1295 }
1296 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001297 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1298 // but must be drawn initially for the animation as though they were visible.
1299 if (!behindFullscreen || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001300 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1301 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001302 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001303
Craig Mautnerd44711d2013-02-23 11:24:36 -08001304 // First: if this is not the current activity being started, make
1305 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001306 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001307 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001308 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001309
1310 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001311 // This activity needs to be visible, but isn't even running...
1312 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001313 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1314 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001315 if (r != starting) {
1316 r.startFreezingScreenLocked(r.app, configChanges);
1317 }
1318 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001319 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1320 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001321 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001322 }
1323 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001324 mStackSupervisor.startSpecificActivityLocked(
1325 r, noStackActivityResumed, false);
1326 noStackActivityResumed = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001327 }
1328
1329 } else if (r.visible) {
1330 // If this activity is already visible, then there is nothing
1331 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001332 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1333 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001334 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001335 try {
George Mount6ba042b2014-07-28 11:12:28 -07001336 if (r.returningOptions != null) {
1337 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1338 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001339 }
1340 } catch(RemoteException e) {
1341 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001342 if (r.state == ActivityState.RESUMED) {
1343 noStackActivityResumed = false;
1344 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001345 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001346 // This activity is not currently visible, but is running.
1347 // Tell it to become visible.
1348 r.visible = true;
1349 if (r.state != ActivityState.RESUMED && r != starting) {
1350 // If this activity is paused, tell it
1351 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001352 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1353 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001354 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001355 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001356 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001357 mUndrawnActivitiesBelowTopTranslucent.add(r);
1358 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001359 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001360 r.sleeping = false;
1361 r.app.pendingUiClean = true;
1362 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1363 r.stopFreezingScreenLocked(false);
1364 } catch (Exception e) {
1365 // Just skip on any failure; we'll make it
1366 // visible when it next restarts.
1367 Slog.w(TAG, "Exception thrown making visibile: "
1368 + r.intent.getComponent(), e);
1369 }
1370 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001371 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001372
Craig Mautnerd44711d2013-02-23 11:24:36 -08001373 // Aggregate current change flags.
1374 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001375
Craig Mautnerd44711d2013-02-23 11:24:36 -08001376 if (r.fullscreen) {
1377 // At this point, nothing else needs to be shown
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001378 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r);
Craig Mautner580ea812013-04-25 12:58:38 -07001379 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001380 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001381 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001382 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001383 }
1384 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001385 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1386 "Make invisible? " + r + " finishing=" + r.finishing
1387 + " state=" + r.state + " behindFullscreen=" + behindFullscreen);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001388 // Now for any activities that aren't visible to the user, make
1389 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001391 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001392 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001393 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001394 switch (r.state) {
1395 case STOPPING:
1396 case STOPPED:
1397 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001398 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1399 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001400 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1401 }
1402 break;
1403
1404 case INITIALIZING:
1405 case RESUMED:
1406 case PAUSING:
1407 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001408 // This case created for transitioning activities from
1409 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001410 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001411 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001412 } else {
1413 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1414 mStackSupervisor.mStoppingActivities.add(r);
1415 }
1416 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001417 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001418 break;
1419
1420 default:
1421 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 }
1423 } catch (Exception e) {
1424 // Just skip on any failure; we'll make it
1425 // visible when it next restarts.
1426 Slog.w(TAG, "Exception thrown making hidden: "
1427 + r.intent.getComponent(), e);
1428 }
1429 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001430 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432 }
1433 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001434 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001435
1436 if (mTranslucentActivityWaiting != null &&
1437 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1438 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1439 notifyActivityDrawnLocked(null);
1440 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 }
Craig Mautner58547802013-03-05 08:23:53 -08001442
Todd Kennedyaab56db2015-01-30 09:39:53 -08001443 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001444 mTranslucentActivityWaiting = r;
1445 mUndrawnActivitiesBelowTopTranslucent.clear();
1446 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1447 }
1448
1449 /**
1450 * Called as activities below the top translucent activity are redrawn. When the last one is
1451 * redrawn notify the top activity by calling
1452 * {@link Activity#onTranslucentConversionComplete}.
1453 *
1454 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1455 * occurred and the activity will be notified immediately.
1456 */
1457 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001458 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001459 if ((r == null)
1460 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1461 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1462 // The last undrawn activity below the top has just been drawn. If there is an
1463 // opaque activity at the top, notify it that it can become translucent safely now.
1464 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1465 mTranslucentActivityWaiting = null;
1466 mUndrawnActivitiesBelowTopTranslucent.clear();
1467 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1468
Craig Mautner71dd1b62014-02-18 15:48:52 -08001469 if (waitingActivity != null) {
1470 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1471 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1472 try {
1473 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1474 waitingActivity.appToken, r != null);
1475 } catch (RemoteException e) {
1476 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001477 }
1478 }
1479 }
1480 }
1481
Craig Mautnera61bc652013-10-28 15:43:18 -07001482 /** If any activities below the top running one are in the INITIALIZING state and they have a
1483 * starting window displayed then remove that starting window. It is possible that the activity
1484 * in this state will never resumed in which case that starting window will be orphaned. */
1485 void cancelInitializingActivities() {
1486 final ActivityRecord topActivity = topRunningActivityLocked(null);
1487 boolean aboveTop = true;
1488 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1489 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1490 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1491 final ActivityRecord r = activities.get(activityNdx);
1492 if (aboveTop) {
1493 if (r == topActivity) {
1494 aboveTop = false;
1495 }
1496 continue;
1497 }
1498
1499 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001500 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1501 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001502 r.mStartingWindowShown = false;
1503 mWindowManager.removeAppStartingWindow(r.appToken);
1504 }
1505 }
1506 }
1507 }
1508
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 /**
1510 * Ensure that the top activity in the stack is resumed.
1511 *
1512 * @param prev The previously resumed activity, for when in the process
1513 * of pausing; can be null to call from elsewhere.
1514 *
1515 * @return Returns true if something is being resumed, or false if
1516 * nothing happened.
1517 */
1518 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001519 return resumeTopActivityLocked(prev, null);
1520 }
1521
1522 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001523 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001524 // Don't even start recursing.
1525 return false;
1526 }
1527
1528 boolean result = false;
1529 try {
1530 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001531 mStackSupervisor.inResumeTopActivity = true;
1532 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1533 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001534 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001535 }
Craig Mautner544efa72014-09-04 16:41:20 -07001536 result = resumeTopActivityInnerLocked(prev, options);
1537 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001538 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001539 }
1540 return result;
1541 }
1542
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001543 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001544 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001545
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001546 if (!mService.mBooting && !mService.mBooted) {
1547 // Not ready yet!
1548 return false;
1549 }
1550
Craig Mautnerdf88d732014-01-27 09:21:32 -08001551 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001552 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001553 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001554 // Do not resume this stack if its parent is not resumed.
1555 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1556 return false;
1557 }
1558
Craig Mautnera61bc652013-10-28 15:43:18 -07001559 cancelInitializingActivities();
1560
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001562 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563
1564 // Remember how we'll process this pause/resume situation, and ensure
1565 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001566 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1567 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568
Craig Mautner84984fa2014-06-19 11:19:20 -07001569 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001570 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001571 // There are no more activities!
1572 final String reason = "noMoreActivities";
1573 if (!mFullscreen) {
1574 // Try to move focus to the next visible stack with a running activity if this
1575 // stack is not covering the entire screen.
1576 final ActivityStack stack = getNextVisibleStackLocked();
1577 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1578 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1579 }
1580 }
1581 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001582 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001583 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001584 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001585 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001586 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1587 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1588 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001589 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001590 }
1591
1592 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001593
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001594 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001595 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1596 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 // Make sure we have executed any pending transitions, since there
1598 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001599 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001600 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001601 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001602 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001603 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001604 return false;
1605 }
1606
Craig Mautner525f3d92013-05-07 14:01:50 -07001607 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001608 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001609 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001610 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001611 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001612 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001613 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001614 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001615 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001616 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001617 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001618 } else if (!isOnHomeDisplay()) {
1619 return false;
1620 } else if (!isHomeStack()){
1621 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001622 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001623 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1624 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1625 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001626 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001627 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001628 }
1629
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 // If we are sleeping, and there is no resumed activity, and the top
1631 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001632 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001633 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001634 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 // Make sure we have executed any pending transitions, since there
1636 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001637 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001639 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001640 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001641 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001642 return false;
1643 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001644
1645 // Make sure that the user who owns this activity is started. If not,
1646 // we will just leave it as is because someone should be bringing
1647 // another user's activities to the top of the stack.
1648 if (mService.mStartedUsers.get(next.userId) == null) {
1649 Slog.w(TAG, "Skipping resume of top activity " + next
1650 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001651 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001652 return false;
1653 }
1654
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 // The activity may be waiting for stop, but that is no longer
1656 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001657 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001658 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001659 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001660 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001662 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001664 // If we are currently pausing an activity, then don't do anything
1665 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001666 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001667 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001668 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001669 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001670 return false;
1671 }
1672
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001673 // Okay we are now going to start a switch, to 'next'. We may first
1674 // have to pause the current activity, but this is an important point
1675 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001676 // XXX "App Redirected" dialog is getting too many false positives
1677 // at this point, so turn off for now.
1678 if (false) {
1679 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1680 long now = SystemClock.uptimeMillis();
1681 final boolean inTime = mLastStartedActivity.startTime != 0
1682 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1683 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1684 final int nextUid = next.info.applicationInfo.uid;
1685 if (inTime && lastUid != nextUid
1686 && lastUid != next.launchedFromUid
1687 && mService.checkPermission(
1688 android.Manifest.permission.STOP_APP_SWITCHES,
1689 -1, next.launchedFromUid)
1690 != PackageManager.PERMISSION_GRANTED) {
1691 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1692 } else {
1693 next.startTime = now;
1694 mLastStartedActivity = next;
1695 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001696 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001697 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001698 mLastStartedActivity = next;
1699 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001700 }
Craig Mautner58547802013-03-05 08:23:53 -08001701
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001702 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1703
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 // We need to start pausing the current activity so the top one
1705 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001706 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1707 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001708 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001709 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001710 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001711 }
1712 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001713 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1714 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001715 // At this point we want to put the upcoming activity's process
1716 // at the top of the LRU list, since we know we will be needing it
1717 // very soon and it would be a waste to let it get killed if it
1718 // happens to be sitting towards the end.
1719 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001720 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001721 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001722 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 return true;
1724 }
1725
Christopher Tated3f175c2012-06-14 14:16:54 -07001726 // If the most recent activity was noHistory but was only stopped rather
1727 // than stopped+finished because the device went to sleep, we need to make
1728 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001729 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001730 !mLastNoHistoryActivity.finishing) {
1731 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1732 " on new resume");
1733 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001734 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001735 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001736 }
1737
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001739 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1740 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001741 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001742 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1743 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 } else {
1745 // The next activity is already visible, so hide the previous
1746 // activity's windows right now so we can show the new one ASAP.
1747 // We only do this if the previous is finishing, which should mean
1748 // it is on top of the one being resumed so hiding it quickly
1749 // is good. Otherwise, we want to do the normal route of allowing
1750 // the resumed activity to be shown so we can decide if the
1751 // previous should actually be hidden depending on whether the
1752 // new one is found to be full-screen or not.
1753 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001754 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001755 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1756 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001757 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758 + ", nowVisible=" + next.nowVisible);
1759 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001760 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001761 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001762 + ", waitingVisible="
1763 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1764 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765 }
1766 }
1767 }
1768
Dianne Hackborne7f97212011-02-24 14:40:20 -08001769 // Launching this app's activity, make sure the app is no longer
1770 // considered stopped.
1771 try {
1772 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001773 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001774 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001775 } catch (IllegalArgumentException e) {
1776 Slog.w(TAG, "Failed trying to unstop package "
1777 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001778 }
1779
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 // We are starting up the next activity, so tell the window manager
1781 // that the previous one will be hidden soon. This way it can know
1782 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001783 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 if (prev != null) {
1785 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001786 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 "Prepare close transition: prev=" + prev);
1788 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001789 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001790 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001792 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001793 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1794 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001796 mWindowManager.setAppWillBeHidden(prev.appToken);
1797 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001798 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001799 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1800 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001802 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001803 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001805 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001806 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001807 : next.mLaunchTaskBehind
1808 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1809 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 }
1811 }
1812 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001813 mWindowManager.setAppWillBeHidden(prev.appToken);
1814 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 }
Craig Mautner967212c2013-04-13 21:10:58 -07001816 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001817 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001819 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001820 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001822 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 }
1824 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001825
1826 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001827 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001828 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1829 if (opts != null) {
1830 resumeAnimOptions = opts.toBundle();
1831 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001832 next.applyOptionsLocked();
1833 } else {
1834 next.clearOptionsLocked();
1835 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001836
Craig Mautnercf910b02013-04-23 11:23:27 -07001837 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001838 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001839 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001840
1841 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001842 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001844 // schedule launch ticks to collect information about slow apps.
1845 next.startLaunchTickingLocked();
1846
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001847 ActivityRecord lastResumedActivity =
1848 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 ActivityState lastState = next.state;
1850
1851 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001852
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001853 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 next.state = ActivityState.RESUMED;
1855 mResumedActivity = next;
1856 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001857 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001858 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001859 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001860 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861
1862 // Have the window manager re-evaluate the orientation of
1863 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001864 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001865 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001866 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001867 mService.mConfiguration,
1868 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1869 if (config != null) {
1870 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001872 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001873 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001874
Craig Mautner525f3d92013-05-07 14:01:50 -07001875 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001876 // The configuration update wasn't able to keep the existing
1877 // instance of the activity, and instead started a new one.
1878 // We should be all done, but let's just make sure our activity
1879 // is still at the top and schedule another run if something
1880 // weird happened.
1881 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001882 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883 "Activity config changed during resume: " + next
1884 + ", new next: " + nextNext);
1885 if (nextNext != next) {
1886 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001887 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001888 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001889 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001890 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001891 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001892 return true;
1893 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001894 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001895 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001896 }
Craig Mautner58547802013-03-05 08:23:53 -08001897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 try {
1899 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001900 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 if (a != null) {
1902 final int N = a.size();
1903 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001904 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1905 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001906 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001907 }
1908 }
1909
1910 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001911 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 }
1913
Craig Mautner299f9602015-01-26 09:47:33 -08001914 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1915 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001916
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001917 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001918 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001919 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001920 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07001921 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001922 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001923 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001924
Craig Mautner0eea92c2013-05-16 13:35:39 -07001925 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926
Craig Mautnerac6f8432013-07-17 13:24:59 -07001927 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001928 } catch (Exception e) {
1929 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001930 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1931 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001932 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001933 if (lastStack != null) {
1934 lastStack.mResumedActivity = lastResumedActivity;
1935 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001936 Slog.i(TAG, "Restarting because process died: " + next);
1937 if (!next.hasBeenLaunched) {
1938 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001939 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1940 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001941 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001942 next.appToken, next.packageName, next.theme,
1943 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001944 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1945 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001946 }
George Mount2c92c972014-03-20 09:38:23 -07001947 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001948 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 return true;
1950 }
1951
1952 // From this point on, if something goes wrong there is no way
1953 // to recover the activity.
1954 try {
1955 next.visible = true;
1956 completeResumeLocked(next);
1957 } catch (Exception e) {
1958 // If any exception gets thrown, toss away this
1959 // activity and try the next one.
1960 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001961 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001962 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001963 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001964 return true;
1965 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 next.stopped = false;
1967
1968 } else {
1969 // Whoops, need to restart this activity!
1970 if (!next.hasBeenLaunched) {
1971 next.hasBeenLaunched = true;
1972 } else {
1973 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001974 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001975 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001976 mService.compatibilityInfoForPackageLocked(
1977 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001979 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001980 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001981 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001982 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001983 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001984 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001985 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001986 }
1987
Craig Mautnercf910b02013-04-23 11:23:27 -07001988 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989 return true;
1990 }
1991
riddle_hsuc215a4f2014-12-27 12:10:45 +08001992 private TaskRecord getNextTask(TaskRecord targetTask) {
1993 final int index = mTaskHistory.indexOf(targetTask);
1994 if (index >= 0) {
1995 final int numTasks = mTaskHistory.size();
1996 for (int i = index + 1; i < numTasks; ++i) {
1997 TaskRecord task = mTaskHistory.get(i);
1998 if (task.userId == targetTask.userId) {
1999 return task;
2000 }
2001 }
2002 }
2003 return null;
2004 }
2005
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002006 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002007 // If the moving task is over home stack, transfer its return type to next task
2008 if (task.isOverHomeStack()) {
2009 final TaskRecord nextTask = getNextTask(task);
2010 if (nextTask != null) {
2011 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2012 }
2013 }
2014
Craig Mautner9c85c202013-10-04 20:11:26 -07002015 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002016 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002017 if (isOnHomeDisplay()) {
2018 ActivityStack lastStack = mStackSupervisor.getLastStack();
2019 final boolean fromHome = lastStack.isHomeStack();
2020 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002021 task.setTaskToReturnTo(fromHome
2022 ? lastStack.topTask() == null
2023 ? HOME_ACTIVITY_TYPE
2024 : lastStack.topTask().taskType
2025 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002026 }
2027 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002028 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002029 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002030
Craig Mautnerac6f8432013-07-17 13:24:59 -07002031 mTaskHistory.remove(task);
2032 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002033 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002034 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002035 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002036 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002037 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002038 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002039 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002040 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002041 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002042 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002043 break;
2044 }
2045 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002046 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002047 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002048 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002049 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002050 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002051
Craig Mautner8849a5e2013-04-02 16:41:03 -07002052 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002053 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002054 TaskRecord rTask = r.task;
2055 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002056 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2057 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002058 // Last activity in task had been removed or ActivityManagerService is reusing task.
2059 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002060 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002061 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002062 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002063 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002064 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 if (!newTask) {
2066 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002067 boolean startIt = true;
2068 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2069 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002070 if (task.getTopActivity() == null) {
2071 // All activities in task are finishing.
2072 continue;
2073 }
Craig Mautner70a86932013-02-28 22:37:44 -08002074 if (task == r.task) {
2075 // Here it is! Now, if this is not yet visible to the
2076 // user, then just add it without starting; it will
2077 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002078 if (!startIt) {
2079 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2080 + task, new RuntimeException("here").fillInStackTrace());
2081 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002082 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002083 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2084 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002085 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002086 r.userId, r.info.configChanges, task.voiceSession != null,
2087 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002089 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002091 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 return;
2093 }
2094 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002095 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 startIt = false;
2097 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002098 }
2099 }
2100
2101 // Place a new activity at top of stack, so it is next to interact
2102 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 // If we are not placing the new activity frontmost, we do not want
2105 // to deliver the onUserLeaving callback to the actual frontmost
2106 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002107 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002108 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002109 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002110 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 }
Craig Mautner70a86932013-02-28 22:37:44 -08002112
2113 task = r.task;
2114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002116 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002117 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002118 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002119 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002120
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002121 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002122 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 // We want to show the starting preview window if we are
2124 // switching to a new task, or the next activity's process is
2125 // not currently running.
2126 boolean showStartingIcon = newTask;
2127 ProcessRecord proc = r.app;
2128 if (proc == null) {
2129 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2130 }
2131 if (proc == null || proc.thread == null) {
2132 showStartingIcon = true;
2133 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002134 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002136 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002137 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002140 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002141 ? r.mLaunchTaskBehind
2142 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2143 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002144 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 mNoAnimActivities.remove(r);
2146 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002147 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002148 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002149 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002150 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 boolean doShow = true;
2152 if (newTask) {
2153 // Even though this activity is starting fresh, we still need
2154 // to reset it to make sure we apply affinities to move any
2155 // existing activities from other tasks in to it.
2156 // If the caller has requested that the target task be
2157 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002158 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 resetTaskIfNeededLocked(r, r);
2160 doShow = topRunningNonDelayedActivityLocked(null) == r;
2161 }
George Mount70778d72014-07-01 16:33:45 -07002162 } else if (options != null && new ActivityOptions(options).getAnimationType()
2163 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2164 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002166 if (r.mLaunchTaskBehind) {
2167 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2168 // tell WindowManager that r is visible even though it is at the back of the stack.
2169 mWindowManager.setAppVisibility(r.appToken, true);
2170 ensureActivitiesVisibleLocked(null, 0);
2171 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 // Figure out if we are transitioning from another activity that is
2173 // "has the same starting icon" as the next one. This allows the
2174 // window manager to keep the previous window it had previously
2175 // created, if it still had one.
2176 ActivityRecord prev = mResumedActivity;
2177 if (prev != null) {
2178 // We don't want to reuse the previous starting preview if:
2179 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002180 if (prev.task != r.task) {
2181 prev = null;
2182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002183 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002184 else if (prev.nowVisible) {
2185 prev = null;
2186 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002188 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002189 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002190 mService.compatibilityInfoForPackageLocked(
2191 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002192 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002193 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002194 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 }
2196 } else {
2197 // If this is the first activity, don't do any fancy animations,
2198 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002199 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002200 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002201 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002202 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002203 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002204 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 }
2206 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002207 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 }
2209
2210 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002211 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 }
2213 }
2214
Dianne Hackbornbe707852011-11-11 14:32:10 -08002215 final void validateAppTokensLocked() {
2216 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002217 mValidateAppTokens.ensureCapacity(numActivities());
2218 final int numTasks = mTaskHistory.size();
2219 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2220 TaskRecord task = mTaskHistory.get(taskNdx);
2221 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002222 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002223 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002224 }
Craig Mautner000f0022013-02-26 15:04:29 -08002225 TaskGroup group = new TaskGroup();
2226 group.taskId = task.taskId;
2227 mValidateAppTokens.add(group);
2228 final int numActivities = activities.size();
2229 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2230 final ActivityRecord r = activities.get(activityNdx);
2231 group.tokens.add(r.appToken);
2232 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002233 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002234 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002235 }
2236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 /**
2238 * Perform a reset of the given task, if needed as part of launching it.
2239 * Returns the new HistoryRecord at the top of the task.
2240 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002241 /**
2242 * Helper method for #resetTaskIfNeededLocked.
2243 * We are inside of the task being reset... we'll either finish this activity, push it out
2244 * for another task, or leave it as-is.
2245 * @param task The task containing the Activity (taskTop) that might be reset.
2246 * @param forceReset
2247 * @return An ActivityOptions that needs to be processed.
2248 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002249 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002250 ActivityOptions topOptions = null;
2251
2252 int replyChainEnd = -1;
2253 boolean canMoveOptions = true;
2254
2255 // We only do this for activities that are not the root of the task (since if we finish
2256 // the root, we may no longer have the task!).
2257 final ArrayList<ActivityRecord> activities = task.mActivities;
2258 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002259 final int rootActivityNdx = task.findEffectiveRootIndex();
2260 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002261 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002262 if (target.frontOfTask)
2263 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002264
2265 final int flags = target.info.flags;
2266 final boolean finishOnTaskLaunch =
2267 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2268 final boolean allowTaskReparenting =
2269 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2270 final boolean clearWhenTaskReset =
2271 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2272
2273 if (!finishOnTaskLaunch
2274 && !clearWhenTaskReset
2275 && target.resultTo != null) {
2276 // If this activity is sending a reply to a previous
2277 // activity, we can't do anything with it now until
2278 // we reach the start of the reply chain.
2279 // XXX note that we are assuming the result is always
2280 // to the previous activity, which is almost always
2281 // the case but we really shouldn't count on.
2282 if (replyChainEnd < 0) {
2283 replyChainEnd = i;
2284 }
2285 } else if (!finishOnTaskLaunch
2286 && !clearWhenTaskReset
2287 && allowTaskReparenting
2288 && target.taskAffinity != null
2289 && !target.taskAffinity.equals(task.affinity)) {
2290 // If this activity has an affinity for another
2291 // task, then we need to move it out of here. We will
2292 // move it as far out of the way as possible, to the
2293 // bottom of the activity stack. This also keeps it
2294 // correctly ordered with any activities we previously
2295 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002296 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002297 final ActivityRecord bottom =
2298 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002299 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002300 if (bottom != null && target.taskAffinity != null
2301 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002302 // If the activity currently at the bottom has the
2303 // same task affinity as the one we are moving,
2304 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002305 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002306 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002307 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002308 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002309 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002310 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002311 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002312 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002313 + " out to new task " + target.task);
2314 }
2315
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002317 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002318
Craig Mautner525f3d92013-05-07 14:01:50 -07002319 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002320 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2321 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002322 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002323 if (p.finishing) {
2324 continue;
2325 }
2326
Craig Mautnere3a74d52013-02-22 14:14:58 -08002327 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002328 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002329 topOptions = p.takeOptionsLocked();
2330 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002331 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002332 }
2333 }
2334 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002335 + task + " adding to task=" + targetTask
2336 + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002337 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2338 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002339 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002340 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002341
Craig Mautner83162a92015-01-26 14:43:30 -08002342 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002343 }
2344
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002345 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002346 if (VALIDATE_TOKENS) {
2347 validateAppTokensLocked();
2348 }
2349
2350 replyChainEnd = -1;
2351 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2352 // If the activity should just be removed -- either
2353 // because it asks for it, or the task should be
2354 // cleared -- then finish it and anything that is
2355 // part of its reply chain.
2356 int end;
2357 if (clearWhenTaskReset) {
2358 // In this case, we want to finish this activity
2359 // and everything above it, so be sneaky and pretend
2360 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002361 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002362 } else if (replyChainEnd < 0) {
2363 end = i;
2364 } else {
2365 end = replyChainEnd;
2366 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002367 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002368 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002369 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002370 if (p.finishing) {
2371 continue;
2372 }
2373 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002374 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002375 topOptions = p.takeOptionsLocked();
2376 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002377 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002378 }
2379 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002380 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002381 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002382 if (finishActivityLocked(
2383 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002384 end--;
2385 srcPos--;
2386 }
2387 }
2388 replyChainEnd = -1;
2389 } else {
2390 // If we were in the middle of a chain, well the
2391 // activity that started it all doesn't want anything
2392 // special, so leave it all as-is.
2393 replyChainEnd = -1;
2394 }
2395 }
2396
2397 return topOptions;
2398 }
2399
2400 /**
2401 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2402 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2403 * @param affinityTask The task we are looking for an affinity to.
2404 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2405 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2406 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2407 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002408 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002409 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002410 int replyChainEnd = -1;
2411 final int taskId = task.taskId;
2412 final String taskAffinity = task.affinity;
2413
2414 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2415 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002416 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2417
2418 // Do not operate on or below the effective root Activity.
2419 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002420 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002421 if (target.frontOfTask)
2422 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002423
2424 final int flags = target.info.flags;
2425 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2426 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2427
2428 if (target.resultTo != null) {
2429 // If this activity is sending a reply to a previous
2430 // activity, we can't do anything with it now until
2431 // we reach the start of the reply chain.
2432 // XXX note that we are assuming the result is always
2433 // to the previous activity, which is almost always
2434 // the case but we really shouldn't count on.
2435 if (replyChainEnd < 0) {
2436 replyChainEnd = i;
2437 }
2438 } else if (topTaskIsHigher
2439 && allowTaskReparenting
2440 && taskAffinity != null
2441 && taskAffinity.equals(target.taskAffinity)) {
2442 // This activity has an affinity for our task. Either remove it if we are
2443 // clearing or move it over to our task. Note that
2444 // we currently punt on the case where we are resetting a
2445 // task that is not at the top but who has activities above
2446 // with an affinity to it... this is really not a normal
2447 // case, and we will need to later pull that task to the front
2448 // and usually at that point we will do the reset and pick
2449 // up those remaining activities. (This only happens if
2450 // someone starts an activity in a new task from an activity
2451 // in a task that is not currently on top.)
2452 if (forceReset || finishOnTaskLaunch) {
2453 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002454 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2455 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002456 for (int srcPos = start; srcPos >= i; --srcPos) {
2457 final ActivityRecord p = activities.get(srcPos);
2458 if (p.finishing) {
2459 continue;
2460 }
Todd Kennedy539db512014-12-15 09:57:55 -08002461 finishActivityLocked(
2462 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002463 }
2464 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002465 if (taskInsertionPoint < 0) {
2466 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002467
Craig Mautner77878772013-03-04 19:46:24 -08002468 }
Craig Mautner77878772013-03-04 19:46:24 -08002469
2470 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002471 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2472 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2473 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002474 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002475 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002476 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002477 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002478
Craig Mautnere3a74d52013-02-22 14:14:58 -08002479 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2480 + " to stack at " + task,
2481 new RuntimeException("here").fillInStackTrace());
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002482 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2483 + " from " + srcPos + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002484 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002485 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002486 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002487 if (VALIDATE_TOKENS) {
2488 validateAppTokensLocked();
2489 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002490
2491 // Now we've moved it in to place... but what if this is
2492 // a singleTop activity and we have put it on top of another
2493 // instance of the same activity? Then we drop the instance
2494 // below so it remains singleTop.
2495 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2496 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002497 int targetNdx = taskActivities.indexOf(target);
2498 if (targetNdx > 0) {
2499 ActivityRecord p = taskActivities.get(targetNdx - 1);
2500 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002501 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2502 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002503 }
2504 }
2505 }
2506 }
2507
2508 replyChainEnd = -1;
2509 }
2510 }
Craig Mautner77878772013-03-04 19:46:24 -08002511 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002512 }
2513
Craig Mautner8849a5e2013-04-02 16:41:03 -07002514 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002515 ActivityRecord newActivity) {
2516 boolean forceReset =
2517 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2518 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2519 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2520 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2521 forceReset = true;
2522 }
2523 }
2524
2525 final TaskRecord task = taskTop.task;
2526
2527 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2528 * for remaining tasks. Used for later tasks to reparent to task. */
2529 boolean taskFound = false;
2530
2531 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2532 ActivityOptions topOptions = null;
2533
Craig Mautner77878772013-03-04 19:46:24 -08002534 // Preserve the location for reparenting in the new task.
2535 int reparentInsertionPoint = -1;
2536
Craig Mautnere3a74d52013-02-22 14:14:58 -08002537 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2538 final TaskRecord targetTask = mTaskHistory.get(i);
2539
2540 if (targetTask == task) {
2541 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2542 taskFound = true;
2543 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002544 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2545 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002546 }
2547 }
2548
Craig Mautner70a86932013-02-28 22:37:44 -08002549 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002550 if (taskNdx >= 0) {
2551 do {
2552 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2553 } while (taskTop == null && taskNdx >= 0);
2554 }
Craig Mautner70a86932013-02-28 22:37:44 -08002555
Craig Mautnere3a74d52013-02-22 14:14:58 -08002556 if (topOptions != null) {
2557 // If we got some ActivityOptions from an activity on top that
2558 // was removed from the task, propagate them to the new real top.
2559 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002560 taskTop.updateOptionsLocked(topOptions);
2561 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002562 topOptions.abort();
2563 }
2564 }
2565
2566 return taskTop;
2567 }
2568
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2570 String resultWho, int requestCode, int resultCode, Intent data) {
2571
2572 if (callingUid > 0) {
2573 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002574 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575 }
2576
2577 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2578 + " : who=" + resultWho + " req=" + requestCode
2579 + " res=" + resultCode + " data=" + data);
2580 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2581 try {
2582 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2583 list.add(new ResultInfo(resultWho, requestCode,
2584 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002585 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 return;
2587 } catch (Exception e) {
2588 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2589 }
2590 }
2591
2592 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2593 }
2594
Craig Mautner299f9602015-01-26 09:47:33 -08002595 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002596 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2597 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002598 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002599 if (next != r) {
2600 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002601 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002602 // For non-fullscreen stack, we want to move the focus to the next visible
2603 // stack to prevent the home screen from moving to the top and obscuring
2604 // other visible stacks.
2605 if (!mFullscreen
2606 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2607 return;
2608 }
2609 // Move the home stack to the top if this stack is fullscreen or there is no
2610 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002611 if (mStackSupervisor.moveHomeStackTaskToTop(
2612 task.getTaskToReturnTo(), myReason)) {
2613 // Activity focus was already adjusted. Nothing else to do...
2614 return;
2615 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002616 }
2617 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002618
2619 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002620 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002621 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002622 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002623 }
2624 }
2625
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002626 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2627 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2628 final String myReason = reason + " adjustFocusToNextVisibleStack";
2629 if (stack == null) {
2630 return false;
2631 }
2632 final ActivityRecord top = stack.topRunningActivityLocked(null);
2633 if (top == null) {
2634 return false;
2635 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002636 mService.setFocusedActivityLocked(top, myReason);
2637 return true;
2638 }
2639
Craig Mautnerf3333272013-04-22 10:55:53 -07002640 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002641 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002642 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2643 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2644 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002645 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002646 if (DEBUG_STATES) {
2647 Slog.d(TAG, "no-history finish of " + r);
2648 }
2649 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002650 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002651 } else {
2652 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2653 + " on stop because we're just sleeping");
2654 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002656 }
2657
2658 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002659 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002660 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 try {
2662 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002663 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2664 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002665 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002666 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2667 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002668 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002669 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002670 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002671 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002672 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002673 r.setSleeping(true);
2674 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002675 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002676 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677 } catch (Exception e) {
2678 // Maybe just ignore exceptions here... if the process
2679 // has crashed, our death notification will clean things
2680 // up.
2681 Slog.w(TAG, "Exception thrown during pause", e);
2682 // Just in case, assume it to be stopped.
2683 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002684 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002685 r.state = ActivityState.STOPPED;
2686 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002687 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688 }
2689 }
2690 }
2691 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002692
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002693 /**
2694 * @return Returns true if the activity is being finished, false if for
2695 * some reason it is being left as-is.
2696 */
2697 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002698 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002699 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002700 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002701 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002702 + ", result=" + resultCode + ", data=" + resultData
2703 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002704 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002705 return false;
2706 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707
Craig Mautnerd44711d2013-02-23 11:24:36 -08002708 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 return true;
2710 }
2711
Craig Mautnerd2328952013-03-05 12:46:26 -08002712 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002713 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2714 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2715 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2716 ActivityRecord r = activities.get(activityNdx);
2717 if (r.resultTo == self && r.requestCode == requestCode) {
2718 if ((r.resultWho == null && resultWho == null) ||
2719 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2720 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2721 false);
2722 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002723 }
2724 }
2725 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002726 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002727 }
2728
Todd Kennedy539db512014-12-15 09:57:55 -08002729 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002730 ActivityRecord r = topRunningActivityLocked(null);
2731 if (r != null && r.app == app) {
2732 // If the top running activity is from this crashing
2733 // process, then terminate it to avoid getting in a loop.
2734 Slog.w(TAG, " Force finishing activity "
2735 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002736 int taskNdx = mTaskHistory.indexOf(r.task);
2737 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002738 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002739 // Also terminate any activities below it that aren't yet
2740 // stopped, to avoid a situation where one will get
2741 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002742 --activityNdx;
2743 if (activityNdx < 0) {
2744 do {
2745 --taskNdx;
2746 if (taskNdx < 0) {
2747 break;
2748 }
2749 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2750 } while (activityNdx < 0);
2751 }
2752 if (activityNdx >= 0) {
2753 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002754 if (r.state == ActivityState.RESUMED
2755 || r.state == ActivityState.PAUSING
2756 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002757 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002758 Slog.w(TAG, " Force finishing activity "
2759 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002760 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002761 }
2762 }
2763 }
2764 }
2765 }
2766
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002767 final void finishVoiceTask(IVoiceInteractionSession session) {
2768 IBinder sessionBinder = session.asBinder();
2769 boolean didOne = false;
2770 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2771 TaskRecord tr = mTaskHistory.get(taskNdx);
2772 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2773 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2774 ActivityRecord r = tr.mActivities.get(activityNdx);
2775 if (!r.finishing) {
2776 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2777 false);
2778 didOne = true;
2779 }
2780 }
2781 }
2782 }
2783 if (didOne) {
2784 mService.updateOomAdjLocked();
2785 }
2786 }
2787
Craig Mautnerd2328952013-03-05 12:46:26 -08002788 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002789 ArrayList<ActivityRecord> activities = r.task.mActivities;
2790 for (int index = activities.indexOf(r); index >= 0; --index) {
2791 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002792 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002793 break;
2794 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002795 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002796 }
2797 return true;
2798 }
2799
Dianne Hackborn5c607432012-02-28 14:44:19 -08002800 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2801 // send the result
2802 ActivityRecord resultTo = r.resultTo;
2803 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002804 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002805 + " who=" + r.resultWho + " req=" + r.requestCode
2806 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002807 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002808 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002809 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002810 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002811 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002812 if (r.info.applicationInfo.uid > 0) {
2813 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2814 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002815 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002816 }
2817 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2818 resultData);
2819 r.resultTo = null;
2820 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002821 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002822
2823 // Make sure this HistoryRecord is not holding on to other resources,
2824 // because clients have remote IPC references to this object so we
2825 // can't assume that will go away and want to avoid circular IPC refs.
2826 r.results = null;
2827 r.pendingResults = null;
2828 r.newIntents = null;
2829 r.icicle = null;
2830 }
2831
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002832 /**
2833 * @return Returns true if this activity has been removed from the history
2834 * list, or false if it is still in the list and will be removed later.
2835 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002836 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2837 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 if (r.finishing) {
2839 Slog.w(TAG, "Duplicate finish request for " + r);
2840 return false;
2841 }
2842
Wale Ogunwale7d701172015-03-11 15:36:30 -07002843 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002844 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002846 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002847 task.taskId, r.shortComponentName, reason);
2848 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002849 final int index = activities.indexOf(r);
2850 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002851 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002852 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002853 // If the caller asked that this activity (and all above it)
2854 // be cleared when the task is reset, don't lose that information,
2855 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002856 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002857 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858 }
2859 }
2860
2861 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002862
Craig Mautner299f9602015-01-26 09:47:33 -08002863 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002864
Dianne Hackborn5c607432012-02-28 14:44:19 -08002865 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002866
Craig Mautnerde4ef022013-04-07 19:01:33 -07002867 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002868 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002869 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002870 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002871 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002872 ? AppTransition.TRANSIT_TASK_CLOSE
2873 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002874
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002876 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002877
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002878 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002879 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
2880 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2881 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002882 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 }
2884
Craig Mautneraea74a52014-03-08 14:23:10 -08002885 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07002886 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002887 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002888 } else if (r.state != ActivityState.PAUSING) {
2889 // If the activity is PAUSING, we will complete the finish once
2890 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002891 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002892 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002893 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002894 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895 }
2896
2897 return false;
2898 }
2899
Craig Mautnerf3333272013-04-22 10:55:53 -07002900 static final int FINISH_IMMEDIATELY = 0;
2901 static final int FINISH_AFTER_PAUSE = 1;
2902 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002903
Craig Mautnerf3333272013-04-22 10:55:53 -07002904 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 // First things first: if this activity is currently visible,
2906 // and the resumed activity is not yet visible, then hold off on
2907 // finishing until the resumed one becomes visible.
2908 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002909 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2910 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002911 if (mStackSupervisor.mStoppingActivities.size() > 3
2912 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 // If we already have a few activities waiting to stop,
2914 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002915 // them out. Or if r is the last of activity of the last task the stack
2916 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002917 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002918 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002919 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002920 }
2921 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002922 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2923 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002925 if (oomAdj) {
2926 mService.updateOomAdjLocked();
2927 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 return r;
2929 }
2930
2931 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002932 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002933 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002934 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 if (mResumedActivity == r) {
2936 mResumedActivity = null;
2937 }
2938 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002939 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002940 r.state = ActivityState.FINISHING;
2941
2942 if (mode == FINISH_IMMEDIATELY
2943 || prevState == ActivityState.STOPPED
2944 || prevState == ActivityState.INITIALIZING) {
2945 // If this activity is already stopped, we can just finish
2946 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002947 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002948 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002949 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002950 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002951 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002952 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002953 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2954 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002955 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002957
2958 // Need to go through the full pause cycle to get this
2959 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002960 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002961 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002962 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002963 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964 return r;
2965 }
2966
Craig Mautneree36c772014-07-16 14:56:05 -07002967 void finishAllActivitiesLocked(boolean immediately) {
2968 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002969 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2970 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2971 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2972 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002973 noActivitiesInStack = false;
2974 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002975 continue;
2976 }
Craig Mautneree36c772014-07-16 14:56:05 -07002977 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002978 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2979 }
2980 }
Craig Mautneree36c772014-07-16 14:56:05 -07002981 if (noActivitiesInStack) {
2982 mActivityContainer.onTaskListEmptyLocked();
2983 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002984 }
2985
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002986 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2987 // Basic case: for simple app-centric recents, we need to recreate
2988 // the task if the affinity has changed.
2989 if (srec == null || srec.task.affinity == null ||
2990 !srec.task.affinity.equals(destAffinity)) {
2991 return true;
2992 }
2993 // Document-centric case: an app may be split in to multiple documents;
2994 // they need to re-create their task if this current activity is the root
2995 // of a document, unless simply finishing it will return them to the the
2996 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002997 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2998 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002999 // Okay, this activity is at the root of its task. What to do, what to do...
3000 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3001 // Finishing won't return to an application, so we need to recreate.
3002 return true;
3003 }
3004 // We now need to get the task below it to determine what to do.
3005 int taskIdx = mTaskHistory.indexOf(srec.task);
3006 if (taskIdx <= 0) {
3007 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3008 return false;
3009 }
3010 if (taskIdx == 0) {
3011 // At the bottom of the stack, nothing to go back to.
3012 return true;
3013 }
3014 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3015 if (!srec.task.affinity.equals(prevTask.affinity)) {
3016 // These are different apps, so need to recreate.
3017 return true;
3018 }
3019 }
3020 return false;
3021 }
3022
Wale Ogunwale7d701172015-03-11 15:36:30 -07003023 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003024 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003025 final TaskRecord task = srec.task;
3026 final ArrayList<ActivityRecord> activities = task.mActivities;
3027 final int start = activities.indexOf(srec);
3028 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003029 return false;
3030 }
3031 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003032 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003033 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003034 final ComponentName dest = destIntent.getComponent();
3035 if (start > 0 && dest != null) {
3036 for (int i = finishTo; i >= 0; i--) {
3037 ActivityRecord r = activities.get(i);
3038 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003039 r.info.name.equals(dest.getClassName())) {
3040 finishTo = i;
3041 parent = r;
3042 foundParentInTask = true;
3043 break;
3044 }
3045 }
3046 }
3047
3048 IActivityController controller = mService.mController;
3049 if (controller != null) {
3050 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3051 if (next != null) {
3052 // ask watcher if this is allowed
3053 boolean resumeOK = true;
3054 try {
3055 resumeOK = controller.activityResuming(next.packageName);
3056 } catch (RemoteException e) {
3057 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003058 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003059 }
3060
3061 if (!resumeOK) {
3062 return false;
3063 }
3064 }
3065 }
3066 final long origId = Binder.clearCallingIdentity();
3067 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003068 ActivityRecord r = activities.get(i);
3069 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003070 // Only return the supplied result for the first activity finished
3071 resultCode = Activity.RESULT_CANCELED;
3072 resultData = null;
3073 }
3074
3075 if (parent != null && foundParentInTask) {
3076 final int parentLaunchMode = parent.info.launchMode;
3077 final int destIntentFlags = destIntent.getFlags();
3078 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3079 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3080 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3081 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003082 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3083 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003084 } else {
3085 try {
3086 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3087 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003088 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003089 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003090 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003091 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003092 foundParentInTask = res == ActivityManager.START_SUCCESS;
3093 } catch (RemoteException e) {
3094 foundParentInTask = false;
3095 }
3096 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003097 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003098 }
3099 }
3100 Binder.restoreCallingIdentity(origId);
3101 return foundParentInTask;
3102 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103 /**
3104 * Perform the common clean-up of an activity record. This is called both
3105 * as part of destroyActivityLocked() (when destroying the client-side
3106 * representation) and cleaning things up as a result of its hosting
3107 * processing going away, in which case there is no remaining client-side
3108 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003109 *
3110 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003112 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3113 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114 if (mResumedActivity == r) {
3115 mResumedActivity = null;
3116 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003117 if (mPausingActivity == r) {
3118 mPausingActivity = null;
3119 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003120 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121
3122 r.configDestroy = false;
3123 r.frozenBeforeDestroy = false;
3124
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003125 if (setState) {
3126 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3127 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003128 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003129 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003130 }
3131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 // Make sure this record is no longer in the pending finishes list.
3133 // This could happen, for example, if we are trimming activities
3134 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003135 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003136 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 // Remove any pending results.
3139 if (r.finishing && r.pendingResults != null) {
3140 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3141 PendingIntentRecord rec = apr.get();
3142 if (rec != null) {
3143 mService.cancelIntentSenderLocked(rec, false);
3144 }
3145 }
3146 r.pendingResults = null;
3147 }
3148
3149 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003150 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 }
3152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003154 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003155 if (getVisibleBehindActivity() == r) {
3156 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003157 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003158 }
3159
3160 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003161 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003163 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003164 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003165 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 }
3167
Craig Mautner299f9602015-01-26 09:47:33 -08003168 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003169 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003170 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003171 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003172 if (DEBUG_ADD_REMOVE) {
3173 RuntimeException here = new RuntimeException("here");
3174 here.fillInStackTrace();
3175 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003177 r.takeFromHistory();
3178 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003179 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003180 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003181 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003182 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003183 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003184 if (VALIDATE_TOKENS) {
3185 validateAppTokensLocked();
3186 }
Craig Mautner312ba862014-02-10 17:55:01 -08003187 final TaskRecord task = r.task;
3188 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003189 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003190 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003191 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3192 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003193 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003194 }
Craig Mautner299f9602015-01-26 09:47:33 -08003195 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003196 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003197 cleanUpActivityServicesLocked(r);
3198 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003199 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 /**
3202 * Perform clean-up of service connections in an activity record.
3203 */
3204 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3205 // Throw away any services that have been bound by this activity.
3206 if (r.connections != null) {
3207 Iterator<ConnectionRecord> it = r.connections.iterator();
3208 while (it.hasNext()) {
3209 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003210 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 }
3212 r.connections = null;
3213 }
3214 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003215
Craig Mautneree2e45a2014-06-27 12:10:03 -07003216 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003217 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003218 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003219 mHandler.sendMessage(msg);
3220 }
3221
Craig Mautneree2e45a2014-06-27 12:10:03 -07003222 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003223 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003224 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003225 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3226 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3227 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3228 final ActivityRecord r = activities.get(activityNdx);
3229 if (r.finishing) {
3230 continue;
3231 }
3232 if (r.fullscreen) {
3233 lastIsOpaque = true;
3234 }
3235 if (owner != null && r.app != owner) {
3236 continue;
3237 }
3238 if (!lastIsOpaque) {
3239 continue;
3240 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003241 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003242 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003243 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003244 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003245 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003246 activityRemoved = true;
3247 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003248 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003249 }
3250 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003251 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003252 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003253 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003254 }
3255
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003256 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3257 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003258 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3259 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003260 + " pausing=" + mPausingActivity + " for reason " + reason);
3261 return destroyActivityLocked(r, true, reason);
3262 }
3263 return false;
3264 }
3265
3266 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3267 String reason) {
3268 // Iterate over tasks starting at the back (oldest) first.
3269 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3270 int maxTasks = tasks.size() / 4;
3271 if (maxTasks < 1) {
3272 maxTasks = 1;
3273 }
3274 int numReleased = 0;
3275 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3276 final TaskRecord task = mTaskHistory.get(taskNdx);
3277 if (!tasks.contains(task)) {
3278 continue;
3279 }
3280 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3281 int curNum = 0;
3282 final ArrayList<ActivityRecord> activities = task.mActivities;
3283 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3284 final ActivityRecord activity = activities.get(actNdx);
3285 if (activity.app == app && activity.isDestroyable()) {
3286 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3287 + " in state " + activity.state + " resumed=" + mResumedActivity
3288 + " pausing=" + mPausingActivity + " for reason " + reason);
3289 destroyActivityLocked(activity, true, reason);
3290 if (activities.get(actNdx) != activity) {
3291 // Was removed from list, back up so we don't miss the next one.
3292 actNdx--;
3293 }
3294 curNum++;
3295 }
3296 }
3297 if (curNum > 0) {
3298 numReleased += curNum;
3299 maxTasks--;
3300 if (mTaskHistory.get(taskNdx) != task) {
3301 // The entire task got removed, back up so we don't miss the next one.
3302 taskNdx--;
3303 }
3304 }
3305 }
3306 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3307 return numReleased;
3308 }
3309
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003310 /**
3311 * Destroy the current CLIENT SIDE instance of an activity. This may be
3312 * called both when actually finishing an activity, or when performing
3313 * a configuration switch where we destroy the current client-side object
3314 * but then create a new client-side object for this same HistoryRecord.
3315 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003316 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003317 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3318 "Removing activity from " + reason + ": token=" + r
3319 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003321 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003322 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003323
3324 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003325
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003326 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003327
3328 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003329
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 if (hadApp) {
3331 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003332 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3334 mService.mHeavyWeightProcess = null;
3335 mService.mHandler.sendEmptyMessage(
3336 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3337 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003338 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003339 // Update any services we are bound to that might care about whether
3340 // their client may have activities.
3341 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003342 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003343 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003344 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 }
3346 }
3347
3348 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003349
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003350 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003351 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003352 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 r.configChangeFlags);
3354 } catch (Exception e) {
3355 // We can just ignore exceptions here... if the process
3356 // has crashed, our death notification will clean things
3357 // up.
3358 //Slog.w(TAG, "Exception thrown during finish", e);
3359 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003360 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 removedFromHistory = true;
3362 skipDestroy = true;
3363 }
3364 }
3365
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003366 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003367
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003368 // If the activity is finishing, we need to wait on removing it
3369 // from the list to give it a chance to do its cleanup. During
3370 // that time it may make calls back with its token so we need to
3371 // be able to find it on the list and so we don't want to remove
3372 // it from the list yet. Otherwise, we can just immediately put
3373 // it in the destroyed state since we are not removing it from the
3374 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003376 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3377 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003378 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003379 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003380 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3381 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003382 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003383 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003384 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003385 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 }
3387 } else {
3388 // remove this record from the history.
3389 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003390 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003391 removedFromHistory = true;
3392 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003393 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003394 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003395 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003396 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003397 }
3398 }
3399
3400 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003401
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003402 if (!mLRUActivities.remove(r) && hadApp) {
3403 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3404 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003405
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003406 return removedFromHistory;
3407 }
3408
Craig Mautner299f9602015-01-26 09:47:33 -08003409 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003410 final long origId = Binder.clearCallingIdentity();
3411 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003412 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003413 if (r != null) {
3414 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003415 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003416 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003417
Wale Ogunwale60454db2015-01-23 16:05:07 -08003418 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003419 if (r.state == ActivityState.DESTROYING) {
3420 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003421 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003422 }
3423 }
Craig Mautner05d29032013-05-03 13:40:13 -07003424 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003425 } finally {
3426 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 }
3428 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003429
Todd Kennedyaab56db2015-01-30 09:39:53 -08003430 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003431 if (hasVisibleBehindActivity() &&
3432 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003433 if (r == topRunningActivityLocked(null)) {
3434 // Don't release the top activity if it has requested to run behind the next
3435 // activity.
3436 return;
3437 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003438 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3439 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003440 " thread=" + r.app.thread);
3441 if (r != null && r.app != null && r.app.thread != null) {
3442 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003443 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003444 } catch (RemoteException e) {
3445 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003446 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003447 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003448 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003449 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003450 }
3451 }
3452 }
3453
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003454 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003455 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3456 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003457 if (r != null) {
3458 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003459 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003460 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003461 }
3462 mStackSupervisor.resumeTopActivitiesLocked();
3463 }
3464
Jose Lima4b6c6692014-08-12 17:41:12 -07003465 boolean hasVisibleBehindActivity() {
3466 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003467 }
3468
Jose Lima4b6c6692014-08-12 17:41:12 -07003469 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003470 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003471 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003472 }
3473 }
3474
Jose Lima4b6c6692014-08-12 17:41:12 -07003475 ActivityRecord getVisibleBehindActivity() {
3476 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003477 }
3478
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003479 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3480 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003481 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003482 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3483 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003484 while (i > 0) {
3485 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003486 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003487 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003488 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003489 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003490 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003491 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003492 }
3493 }
3494 }
3495
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003496 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3497 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003498 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3499 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003500 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3501 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003502 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003503 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003504 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3505 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003506
3507 boolean hasVisibleActivities = false;
3508
3509 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003510 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003511 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3512 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003513 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3514 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3515 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3516 final ActivityRecord r = activities.get(activityNdx);
3517 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003518 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3519 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003520 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003521 if (r.visible) {
3522 hasVisibleActivities = true;
3523 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003524 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003525 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3526 // Don't currently have state for the activity, or
3527 // it is finishing -- always remove it.
3528 remove = true;
3529 } else if (r.launchCount > 2 &&
3530 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3531 // We have launched this activity too many times since it was
3532 // able to run, so give up and remove it.
3533 remove = true;
3534 } else {
3535 // The process may be gone, but the activity lives on!
3536 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003537 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003538 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003539 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003540 RuntimeException here = new RuntimeException("here");
3541 here.fillInStackTrace();
3542 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3543 + ": haveState=" + r.haveState
3544 + " stateNotNeeded=" + r.stateNotNeeded
3545 + " finishing=" + r.finishing
3546 + " state=" + r.state, here);
3547 }
3548 if (!r.finishing) {
3549 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3550 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3551 r.userId, System.identityHashCode(r),
3552 r.task.taskId, r.shortComponentName,
3553 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003554 if (r.state == ActivityState.RESUMED) {
3555 mService.updateUsageStats(r, false);
3556 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003557 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003558 } else {
3559 // We have the current state for this activity, so
3560 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003561 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003562 TAG, "Keeping entry, setting app to null");
Craig Mautner0247fc82013-02-28 14:32:06 -08003563 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3564 + r);
3565 r.app = null;
3566 r.nowVisible = false;
3567 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003568 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003569 "App died, clearing saved state of " + r);
3570 r.icicle = null;
3571 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003572 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003573 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003574 if (remove) {
3575 removeActivityFromHistoryLocked(r, "appDied");
3576 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003577 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003578 }
3579 }
3580
3581 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003582 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003583
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003584 final void updateTransitLocked(int transit, Bundle options) {
3585 if (options != null) {
3586 ActivityRecord r = topRunningActivityLocked(null);
3587 if (r != null && r.state != ActivityState.RESUMED) {
3588 r.updateOptionsLocked(options);
3589 } else {
3590 ActivityOptions.abort(options);
3591 }
3592 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003593 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003594 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003595
Craig Mautner21d24a22014-04-23 11:45:37 -07003596 void updateTaskMovement(TaskRecord task, boolean toFront) {
3597 if (task.isPersistable) {
3598 task.mLastTimeMoved = System.currentTimeMillis();
3599 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3600 // recently will be most negative, tasks sent to the bottom before that will be less
3601 // negative. Similarly for recent tasks moved to the top which will be most positive.
3602 if (!toFront) {
3603 task.mLastTimeMoved *= -1;
3604 }
3605 }
3606 }
3607
Craig Mautner84984fa2014-06-19 11:19:20 -07003608 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003609 final int top = mTaskHistory.size() - 1;
3610 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3611 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003612 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003613 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3614 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003615 mTaskHistory.remove(taskNdx);
3616 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003617 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003618 return;
3619 }
3620 }
3621 }
3622
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003623 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003624 String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003625 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003626
Craig Mautner11bf9a52013-02-19 14:08:51 -08003627 final int numTasks = mTaskHistory.size();
3628 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003629 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003630 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003631 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003632 ActivityOptions.abort(options);
3633 } else {
3634 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3635 }
3636 return;
3637 }
3638
3639 // Shift all activities with this task up to the top
3640 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003641 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003642
3643 // Set focus to the top running activity of this stack.
3644 ActivityRecord r = topRunningActivityLocked(null);
3645 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003646
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003647 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003648 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003649 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003650 if (r != null) {
3651 mNoAnimActivities.add(r);
3652 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003653 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003655 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003656 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003657
Craig Mautner05d29032013-05-03 13:40:13 -07003658 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003659 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003660
3661 if (VALIDATE_TOKENS) {
3662 validateAppTokensLocked();
3663 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 }
3665
3666 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003667 * Worker method for rearranging history stack. Implements the function of moving all
3668 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003669 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003670 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003671 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3672 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003673 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003674 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003675 * @return Returns true if the move completed, false if not.
3676 */
Craig Mautner299f9602015-01-26 09:47:33 -08003677 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003678 final TaskRecord tr = taskForIdLocked(taskId);
3679 if (tr == null) {
3680 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3681 return false;
3682 }
3683
3684 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003685 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003686
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003687 // If we have a watcher, preflight the move before committing to it. First check
3688 // for *other* available tasks, but if none are available, then try again allowing the
3689 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003690 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003691 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 if (next == null) {
3693 next = topRunningActivityLocked(null, 0);
3694 }
3695 if (next != null) {
3696 // ask watcher if this is allowed
3697 boolean moveOK = true;
3698 try {
3699 moveOK = mService.mController.activityResuming(next.packageName);
3700 } catch (RemoteException e) {
3701 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003702 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703 }
3704 if (!moveOK) {
3705 return false;
3706 }
3707 }
3708 }
3709
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003710 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003711
riddle_hsuc215a4f2014-12-27 12:10:45 +08003712 boolean prevIsHome = false;
3713 if (tr.isOverHomeStack()) {
3714 final TaskRecord nextTask = getNextTask(tr);
3715 if (nextTask != null) {
3716 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3717 } else {
3718 prevIsHome = true;
3719 }
3720 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003721 mTaskHistory.remove(tr);
3722 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003723 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003724
Craig Mautnerc8143c62013-09-03 12:15:57 -07003725 // There is an assumption that moving a task to the back moves it behind the home activity.
3726 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003727 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003728 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3729 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003730 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003731 break;
3732 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003733 if (taskNdx == 1) {
3734 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003735 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003736 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003737 }
3738
Craig Mautner299f9602015-01-26 09:47:33 -08003739 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003740 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003741
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003743 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003744 }
3745
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003746 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003747 if (prevIsHome || task == tr && tr.isOverHomeStack()
3748 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003749 if (!mService.mBooting && !mService.mBooted) {
3750 // Not ready yet!
3751 return false;
3752 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003753 final int taskToReturnTo = tr.getTaskToReturnTo();
3754 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003755 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003756 }
3757
Craig Mautner05d29032013-05-03 13:40:13 -07003758 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003759 return true;
3760 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003761
Craig Mautner8849a5e2013-04-02 16:41:03 -07003762 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003763 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003764 final Uri data = r.intent.getData();
3765 final String strData = data != null ? data.toSafeString() : null;
3766
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003767 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003768 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003769 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003770 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003771 }
3772
3773 /**
3774 * Make sure the given activity matches the current configuration. Returns
3775 * false if the activity had to be destroyed. Returns true if the
3776 * configuration is the same, or the activity will remain running as-is
3777 * for whatever reason. Ensures the HistoryRecord is updated with the
3778 * correct configuration and all other bookkeeping is handled.
3779 */
3780 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3781 int globalChanges) {
3782 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003783 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 "Skipping config check (will change): " + r);
3785 return true;
3786 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003787
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003788 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003789 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003790
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003791 // Make sure the current stack override configuration is supported by the top task
3792 // before continuing.
3793 final TaskRecord topTask = topTask();
3794 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3795 || (!topTask.mResizeable && !mFullscreen))) {
3796 final boolean prevFullscreen = mFullscreen;
3797 final Configuration newOverrideConfig =
3798 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3799 updateOverrideConfiguration(newOverrideConfig);
3800 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003801 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003802 "Updated stack config to support task=" + topTask
3803 + " resizeable=" + topTask.mResizeable
3804 + " mForcedFullscreen=" + mForcedFullscreen
3805 + " prevFullscreen=" + prevFullscreen
3806 + " mFullscreen=" + mFullscreen);
3807 }
3808
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003809 // Short circuit: if the two configurations are the exact same
3810 // object (the common case), then there is nothing to do.
3811 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003812 if (r.configuration == newConfig
3813 && r.stackConfigOverride == mOverrideConfig
3814 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003815 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003816 "Configuration unchanged in " + r);
3817 return true;
3818 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003819
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003820 // We don't worry about activities that are finishing.
3821 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003822 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003823 "Configuration doesn't matter in finishing " + r);
3824 r.stopFreezingScreenLocked(false);
3825 return true;
3826 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003827
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003828 // Okay we now are going to make this activity have the new config.
3829 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003830 final Configuration oldConfig = r.configuration;
3831 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003832 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003833 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003834
3835 // Determine what has changed. May be nothing, if this is a config
3836 // that has come back from the app after going idle. In that case
3837 // we just want to leave the official config object now in the
3838 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003839 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003840 if (stackChanges == 0) {
3841 // {@link Configuration#diff} doesn't catch changes from unset values.
3842 // Check for changes we care about.
3843 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3844 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3845 }
3846 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3847 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3848 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3849 }
3850 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3851 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3852 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003853 }
3854 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003855 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003856 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003857 "Configuration no differences in " + r);
3858 return true;
3859 }
3860
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 // If the activity isn't currently running, just leave the new
3862 // configuration and it will pick that up next time it starts.
3863 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003864 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003865 "Configuration doesn't matter not running " + r);
3866 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003867 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003868 return true;
3869 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003870
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003871 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003872 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3873 "Checking to restart " + r.info.name + ": changed=0x"
3874 + Integer.toHexString(changes) + ", handles=0x"
3875 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3876
Dianne Hackborne6676352011-06-01 16:51:20 -07003877 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003878 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3879 r.configChangeFlags |= changes;
3880 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003881 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003882 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003883 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003884 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003885 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003886 } else if (r.state == ActivityState.PAUSING) {
3887 // A little annoying: we are waiting for this activity to
3888 // finish pausing. Let's not do anything now, but just
3889 // flag that it needs to be restarted when done pausing.
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 skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003892 r.configDestroy = true;
3893 return true;
3894 } else if (r.state == ActivityState.RESUMED) {
3895 // Try to optimize this case: the configuration is changing
3896 // and we need to restart the top, resumed activity.
3897 // Instead of doing the normal handshaking, just say
3898 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003899 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003900 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003901 relaunchActivityLocked(r, r.configChangeFlags, true);
3902 r.configChangeFlags = 0;
3903 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003904 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003905 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003906 relaunchActivityLocked(r, r.configChangeFlags, false);
3907 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003908 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003909
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003910 // All done... tell the caller we weren't able to keep this
3911 // activity around.
3912 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003914
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003915 // Default case: the activity can handle this new configuration, so hand it over.
3916 // NOTE: We only forward the stack override configuration as the system level configuration
3917 // changes is always sent to all processes when they happen so it can just use whatever
3918 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 if (r.app != null && r.app.thread != null) {
3920 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003921 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003922 r.app.thread.scheduleActivityConfigurationChanged(
3923 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003924 } catch (RemoteException e) {
3925 // If process died, whatever.
3926 }
3927 }
3928 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003929
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 return true;
3931 }
3932
Craig Mautner2568c3a2015-03-26 14:22:34 -07003933 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003934 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003935 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003936 if (andResume) {
3937 results = r.results;
3938 newIntents = r.newIntents;
3939 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003940 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3941 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003942 + " andResume=" + andResume);
3943 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003944 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003946
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003947 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003948
Craig Mautner34b73df2014-01-12 21:11:08 -08003949 mStackSupervisor.removeChildActivityContainers(r);
3950
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003951 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003952 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
3953 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003954 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003955 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3956 !andResume, new Configuration(mService.mConfiguration),
3957 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003958 // Note: don't need to call pauseIfSleepingLocked() here, because
3959 // the caller will only pass in 'andResume' if this activity is
3960 // currently resumed, which implies we aren't sleeping.
3961 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003962 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003963 }
3964
3965 if (andResume) {
3966 r.results = null;
3967 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003968 r.state = ActivityState.RESUMED;
3969 } else {
3970 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3971 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003972 }
3973
3974 return true;
3975 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003976
3977 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003978 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3979 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3980 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3981 final ActivityRecord r = activities.get(activityNdx);
3982 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003983 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003984 }
3985 if (r.fullscreen && !r.finishing) {
3986 return false;
3987 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003988 }
3989 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003990 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003991 if (r == null) {
3992 return false;
3993 }
3994 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3995 + " would have returned true for r=" + r);
3996 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003997 }
3998
3999 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004000 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4001 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4002 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4003 final ActivityRecord r = activities.get(activityNdx);
4004 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004005 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004006 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004007 }
4008 }
4009 }
4010
4011 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
4012 boolean didSomething = false;
4013 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004014 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004015 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4016 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4017 int numActivities = activities.size();
4018 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4019 ActivityRecord r = activities.get(activityNdx);
4020 final boolean samePackage = r.packageName.equals(name)
4021 || (name == null && r.userId == userId);
4022 if ((userId == UserHandle.USER_ALL || r.userId == userId)
4023 && (samePackage || r.task == lastTask)
4024 && (r.app == null || evenPersistent || !r.app.persistent)) {
4025 if (!doit) {
4026 if (r.finishing) {
4027 // If this activity is just finishing, then it is not
4028 // interesting as far as something to stop.
4029 continue;
4030 }
4031 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004032 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004033 if (r.isHomeActivity()) {
4034 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4035 Slog.i(TAG, "Skip force-stop again " + r);
4036 continue;
4037 } else {
4038 homeActivity = r.realActivity;
4039 }
4040 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004041 didSomething = true;
4042 Slog.i(TAG, " Force finishing activity " + r);
4043 if (samePackage) {
4044 if (r.app != null) {
4045 r.app.removed = true;
4046 }
4047 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004048 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004049 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004050 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4051 true)) {
4052 // r has been deleted from mActivities, accommodate.
4053 --numActivities;
4054 --activityNdx;
4055 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004056 }
4057 }
4058 }
4059 return didSomething;
4060 }
4061
Dianne Hackborn09233282014-04-30 11:33:59 -07004062 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004063 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4064 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004065 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004066 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004067 if (task.getTopActivity() == null) {
4068 continue;
4069 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004070 ActivityRecord r = null;
4071 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004072 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004073 int numActivities = 0;
4074 int numRunning = 0;
4075 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004076 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004077 continue;
4078 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004079 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004080 tmp = activities.get(activityNdx);
4081 if (tmp.finishing) {
4082 continue;
4083 }
4084 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004085
Craig Mautneraab647e2013-02-28 16:31:36 -08004086 // Initialize state for next task if needed.
4087 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4088 top = r;
4089 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004090 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004091
4092 // Add 'r' into the current task.
4093 numActivities++;
4094 if (r.app != null && r.app.thread != null) {
4095 numRunning++;
4096 }
4097
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004098 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004099 TAG, r.intent.getComponent().flattenToShortString()
4100 + ": task=" + r.task);
4101 }
4102
4103 RunningTaskInfo ci = new RunningTaskInfo();
4104 ci.id = task.taskId;
4105 ci.baseActivity = r.intent.getComponent();
4106 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004107 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004108 if (focusedStack && topTask) {
4109 // Give the latest time to ensure foreground task can be sorted
4110 // at the first, because lastActiveTime of creating task is 0.
4111 ci.lastActiveTime = System.currentTimeMillis();
4112 topTask = false;
4113 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004114
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004115 if (top.task != null) {
4116 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004117 }
4118 ci.numActivities = numActivities;
4119 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004120 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004121 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004122 }
4123
4124 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004125 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004126 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004127 if (top >= 0) {
4128 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4129 int activityTop = activities.size() - 1;
4130 if (activityTop > 0) {
4131 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4132 "unhandled-back", true);
4133 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004134 }
4135 }
4136
Craig Mautner6b74cb52013-09-27 17:02:21 -07004137 /**
4138 * Reset local parameters because an app's activity died.
4139 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004140 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004141 */
4142 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004143 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004144 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004145 "App died while pausing: " + mPausingActivity);
4146 mPausingActivity = null;
4147 }
4148 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4149 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004150 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004151 }
4152
Craig Mautner19091252013-10-05 00:03:53 -07004153 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004154 }
4155
Craig Mautnercae015f2013-02-08 14:31:27 -08004156 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004157 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4158 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4159 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4160 final ActivityRecord r = activities.get(activityNdx);
4161 if (r.app == app) {
4162 Slog.w(TAG, " Force finishing activity "
4163 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004164 // Force the destroy to skip right to removal.
4165 r.app = null;
4166 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004167 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004168 }
4169 }
4170 }
4171
Dianne Hackborn390517b2013-05-30 15:03:32 -07004172 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004173 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004174 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004175 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4176 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004177 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4178 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004179 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004180 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004181 if (printed) {
4182 header = null;
4183 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004184 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004185 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004186 }
4187
4188 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4189 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4190
4191 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004192 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4193 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004194 }
4195 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004196 final int top = mTaskHistory.size() - 1;
4197 if (top >= 0) {
4198 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4199 int listTop = list.size() - 1;
4200 if (listTop >= 0) {
4201 activities.add(list.get(listTop));
4202 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004203 }
4204 } else {
4205 ItemMatcher matcher = new ItemMatcher();
4206 matcher.build(name);
4207
Craig Mautneraab647e2013-02-28 16:31:36 -08004208 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4209 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4210 if (matcher.match(r1, r1.intent.getComponent())) {
4211 activities.add(r1);
4212 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004213 }
4214 }
4215 }
4216
4217 return activities;
4218 }
4219
4220 ActivityRecord restartPackage(String packageName) {
4221 ActivityRecord starting = topRunningActivityLocked(null);
4222
4223 // All activities that came from the package must be
4224 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004225 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4226 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4227 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4228 final ActivityRecord a = activities.get(activityNdx);
4229 if (a.info.packageName.equals(packageName)) {
4230 a.forceNewConfig = true;
4231 if (starting != null && a == starting && a.visible) {
4232 a.startFreezingScreenLocked(starting.app,
4233 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4234 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004235 }
4236 }
4237 }
4238
4239 return starting;
4240 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004241
Craig Mautner299f9602015-01-26 09:47:33 -08004242 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07004243 removeTask(task, reason, true /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004244 }
4245
Wale Ogunwale000957c2015-04-03 08:19:12 -07004246 /**
4247 * Removes the input task from this stack.
4248 * @param task to remove.
4249 * @param reason for removal.
4250 * @param notMoving task to another stack. In the case we are moving we don't want to perform
4251 * some operations on the task like removing it from window manager or recents.
4252 */
4253 void removeTask(TaskRecord task, String reason, boolean notMoving) {
4254 if (notMoving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004255 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004256 mWindowManager.removeTask(task.taskId);
4257 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004258
Craig Mautner04a0ea62014-01-13 12:51:26 -08004259 final ActivityRecord r = mResumedActivity;
4260 if (r != null && r.task == task) {
4261 mResumedActivity = null;
4262 }
4263
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004264 final int taskNdx = mTaskHistory.indexOf(task);
4265 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004266 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4267 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4268 if (!nextTask.isOverHomeStack()) {
4269 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4270 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004271 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004272 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004273 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004274
Wale Ogunwale000957c2015-04-03 08:19:12 -07004275 if (notMoving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004276 final boolean isVoiceSession = task.voiceSession != null;
4277 if (isVoiceSession) {
4278 try {
4279 task.voiceSession.taskFinished(task.intent, task.taskId);
4280 } catch (RemoteException e) {
4281 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004282 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004283 if (task.autoRemoveFromRecents() || isVoiceSession) {
4284 // Task creator asked to remove this when done, or this task was a voice
4285 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004286 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004287 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004288 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004289 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004290
4291 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004292 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004293 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004294 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004295 String myReason = reason + " leftTaskHistoryEmpty";
4296 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4297 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4298 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004299 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004300 if (mStacks != null) {
4301 mStacks.remove(this);
4302 mStacks.add(0, this);
4303 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004304 if (notHomeStack) {
4305 mActivityContainer.onTaskListEmptyLocked();
4306 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004307 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004308
4309 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004310 }
4311
Dianne Hackborn91097de2014-04-04 18:02:06 -07004312 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4313 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4314 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004315 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4316 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004317 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004318 return task;
4319 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004320
4321 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004322 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004323 }
4324
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004325 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004326 task.stack = this;
4327 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004328 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004329 } else {
4330 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004331 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004332 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004333 if (!moving && task.voiceSession != null) {
4334 try {
4335 task.voiceSession.taskStarted(task.intent, task.taskId);
4336 } catch (RemoteException e) {
4337 }
4338 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004339 }
4340
4341 public int getStackId() {
4342 return mStackId;
4343 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004344
4345 @Override
4346 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004347 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4348 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004349 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004350
4351 boolean updateOverrideConfiguration(Configuration newConfig) {
4352 Configuration oldConfig = mOverrideConfig;
4353 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004354 // We override the configuration only when the stack's dimensions are different from
4355 // the display. In this manner, we know that if the override configuration is empty,
4356 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004357 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004358 return !mOverrideConfig.equals(oldConfig);
4359 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004360
4361 void onLockTaskPackagesUpdatedLocked() {
4362 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4363 mTaskHistory.get(taskNdx).setLockTaskAuth();
4364 }
4365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004366}