blob: 33f915f245618caf8f07c9f944d2639535323c24 [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
Craig Mautner0eea92c2013-05-16 13:35:39 -0700750 return false;
751 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800752
Craig Mautner0eea92c2013-05-16 13:35:39 -0700753 void goToSleep() {
754 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800755
Craig Mautner0eea92c2013-05-16 13:35:39 -0700756 // Make sure any stopped but visible activities are now sleeping.
757 // This ensures that the activity's onStop() is called.
758 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
759 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
760 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
761 final ActivityRecord r = activities.get(activityNdx);
762 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
763 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800764 }
765 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800766 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700767 }
Craig Mautner59c00972012-07-30 12:10:24 -0700768
Dianne Hackbornd2835932010-12-13 16:28:46 -0800769 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700770 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800771 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700772 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800773 return null;
774 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800775
Winson Chung083baf92014-07-11 10:32:42 -0700776 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700777 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800778 // we can just go ahead and skip taking the screenshot if this is the home stack.
779 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700780 return null;
781 }
782
Winson Chung48a10a52014-08-27 14:36:51 -0700783 int w = mService.mThumbnailWidth;
784 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800785 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800786 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
787 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700788 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800789 }
Winson Chung376543b2014-05-21 17:43:28 -0700790 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800791 return null;
792 }
793
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700794 /**
795 * Start pausing the currently resumed activity. It is an error to call this if there
796 * is already an activity being paused or there is no resumed activity.
797 *
798 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
799 * @param uiSleeping True if this is happening with the user interface going to sleep (the
800 * screen turning off).
801 * @param resuming True if this is being called as part of resuming the top activity, so
802 * we shouldn't try to instigate a resume here.
803 * @param dontWait True if the caller does not want to wait for the pause to complete. If
804 * set to true, we will immediately complete the pause here before returning.
805 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
806 * it to tell us when it is done.
807 */
808 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
809 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800810 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800811 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
812 + " state=" + mPausingActivity.state);
813 if (!mService.isSleeping()) {
814 // Avoid recursion among check for sleep and complete pause during sleeping.
815 // Because activity will be paused immediately after resume, just let pause
816 // be completed by the order of activity paused from clients.
817 completePauseLocked(false);
818 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800819 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700820 ActivityRecord prev = mResumedActivity;
821 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700822 if (!resuming) {
823 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
824 mStackSupervisor.resumeTopActivitiesLocked();
825 }
826 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800828
829 if (mActivityContainer.mParentActivity == null) {
830 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700831 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800832 }
833
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700834 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700835 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800837 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700839 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
840 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700841 prev.state = ActivityState.PAUSING;
842 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700843 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700844 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800845 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800846 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700847 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700848 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849
850 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800851
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700852 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700853 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 try {
855 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700856 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700858 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800859 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700860 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700861 } catch (Exception e) {
862 // Ignore exception, if process died other code will cleanup.
863 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800864 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700865 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700866 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700867 }
868 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800869 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700871 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700872 }
873
874 // If we are not going to sleep, we want to ensure the device is
875 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700876 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700877 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700878 }
879
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800880 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700881 // Have the window manager pause its key dispatching until the new
882 // activity has started. If we're pausing the activity just because
883 // the screen is being turned off and the UI is sleeping, don't interrupt
884 // key dispatch; the same activity will pick it up again on wakeup.
885 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800886 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700887 } else if (DEBUG_PAUSE) {
888 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700889 }
890
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700891 if (dontWait) {
892 // If the caller said they don't want to wait for the pause, then complete
893 // the pause now.
894 completePauseLocked(false);
895 return false;
896
897 } else {
898 // Schedule a pause timeout in case the app doesn't respond.
899 // We don't give it much time because this directly impacts the
900 // responsiveness seen by the user.
901 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
902 msg.obj = prev;
903 prev.pauseTime = SystemClock.uptimeMillis();
904 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700905 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700906 return true;
907 }
908
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 } else {
910 // This activity failed to schedule the
911 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700912 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700913 if (!resuming) {
914 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
915 }
916 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 }
918 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700919
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700920 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700921 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
922 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923
Craig Mautnerd2328952013-03-05 12:46:26 -0800924 final ActivityRecord r = isInStackLocked(token);
925 if (r != null) {
926 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
927 if (mPausingActivity == r) {
928 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
929 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700930 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800931 } else {
932 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
933 r.userId, System.identityHashCode(r), r.shortComponentName,
934 mPausingActivity != null
935 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +0800936 if (r.finishing && r.state == ActivityState.PAUSING) {
937 if (DEBUG_PAUSE) Slog.v(TAG,
938 "Executing finish of failed to pause activity: " + r);
939 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
940 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 }
942 }
943 }
944
Craig Mautnera0026042014-04-23 11:45:37 -0700945 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
946 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700947 if (r.state != ActivityState.STOPPING) {
948 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
949 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
950 return;
951 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700952 if (persistentState != null) {
953 r.persistentState = persistentState;
954 mService.notifyTaskPersisterLocked(r.task, false);
955 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700956 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700957 if (icicle != null) {
958 // If icicle is null, this is happening due to a timeout, so we
959 // haven't really saved the state.
960 r.icicle = icicle;
961 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800962 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800963 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700964 }
965 if (!r.stopped) {
966 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
967 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
968 r.stopped = true;
969 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700970 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
971 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700972 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700973 if (r.finishing) {
974 r.clearOptionsLocked();
975 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700976 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700977 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700978 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700979 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700980 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800981 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700982 }
983 }
984 }
985
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700986 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800987 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700988 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800989
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800990 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700991 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800992 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700993 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700994 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800995 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700996 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800997 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700998 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
999 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001000 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 if (prev.configDestroy) {
1002 // The previous is being paused because the configuration
1003 // is changing, which means it is actually stopping...
1004 // To juggle the fact that we are also starting a new
1005 // instance right now, we need to first completely stop
1006 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001007 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001008 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -07001009 } else if (!hasVisibleBehindActivity()) {
1010 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001011 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001013 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1014 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001015 // If we already have a few activities waiting to stop,
1016 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001017 // them out. Or if r is the last of activity of the last task the stack
1018 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001019 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001020 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001021 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001022 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 }
1024 }
1025 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001026 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001027 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001028 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001029 // It is possible the activity was freezing the screen before it was paused.
1030 // In that case go ahead and remove the freeze this activity has on the screen
1031 // since it is no longer visible.
1032 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001033 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001034 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001035
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001036 if (resumeNext) {
1037 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1038 if (!mService.isSleepingOrShuttingDown()) {
1039 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1040 } else {
1041 mStackSupervisor.checkReadyForSleepLocked();
1042 ActivityRecord top = topStack.topRunningActivityLocked(null);
1043 if (top == null || (prev != null && top != prev)) {
1044 // If there are no more activities available to run,
1045 // do resume anyway to start something. Also if the top
1046 // activity on the stack is not the just paused activity,
1047 // we need to go ahead and resume it to ensure we complete
1048 // an in-flight app switch.
1049 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1050 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001051 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001052 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001053
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001054 if (prev != null) {
1055 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001056
Craig Mautner525f3d92013-05-07 14:01:50 -07001057 if (prev.app != null && prev.cpuTimeAtResume > 0
1058 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001059 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1060 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001061 if (diff > 0) {
1062 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1063 synchronized (bsi) {
1064 BatteryStatsImpl.Uid.Proc ps =
1065 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001066 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001067 if (ps != null) {
1068 ps.addForegroundTimeLocked(diff);
1069 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070 }
1071 }
1072 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001073 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001074 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001075
1076 // Notfiy when the task stack has changed
1077 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 }
1079
1080 /**
1081 * Once we know that we have asked an application to put an activity in
1082 * the resumed state (either by launching it or explicitly telling it),
1083 * this function updates the rest of our state to match that fact.
1084 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001085 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001086 next.idle = false;
1087 next.results = null;
1088 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001089
1090 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1091 ProcessRecord app = next.task.mActivities.get(0).app;
1092 if (app != null && app != mService.mHomeProcess) {
1093 mService.mHomeProcess = app;
1094 }
1095 }
1096
Craig Mautner07566322013-09-26 16:42:55 -07001097 if (next.nowVisible) {
1098 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001099 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001100 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001101
1102 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001103 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001104
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001105 mStackSupervisor.reportResumedActivityLocked(next);
1106
1107 next.resumeKeyDispatchingLocked();
1108 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001109
1110 // Mark the point when the activity is resuming
1111 // TODO: To be more accurate, the mark should be before the onCreate,
1112 // not after the onResume. But for subsequent starts, onResume is fine.
1113 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001114 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115 } else {
1116 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1117 }
Winson Chung376543b2014-05-21 17:43:28 -07001118
George Mount6ba042b2014-07-28 11:12:28 -07001119 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001120
1121 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1122 // When resuming an activity, require it to call requestVisibleBehind() again.
1123 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1124 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001125 }
1126
Craig Mautner2568c3a2015-03-26 14:22:34 -07001127 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001128 r.visible = visible;
1129 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001130 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001131 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001132 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001133 container.setVisible(visible);
1134 }
1135 }
1136
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001137 // Find the first visible activity above the passed activity and if it is translucent return it
1138 // otherwise return null;
1139 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1140 TaskRecord task = r.task;
1141 if (task == null) {
1142 return null;
1143 }
1144
1145 ActivityStack stack = task.stack;
1146 if (stack == null) {
1147 return null;
1148 }
1149
1150 int stackNdx = mStacks.indexOf(stack);
1151
1152 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1153 int taskNdx = tasks.indexOf(task);
1154
1155 ArrayList<ActivityRecord> activities = task.mActivities;
1156 int activityNdx = activities.indexOf(r) + 1;
1157
1158 final int numStacks = mStacks.size();
1159 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001160 ActivityStack historyStack = mStacks.get(stackNdx);
1161 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001162 final int numTasks = tasks.size();
1163 while (taskNdx < numTasks) {
1164 activities = tasks.get(taskNdx).mActivities;
1165 final int numActivities = activities.size();
1166 while (activityNdx < numActivities) {
1167 final ActivityRecord activity = activities.get(activityNdx);
1168 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001169 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001170 }
1171 ++activityNdx;
1172 }
1173 activityNdx = 0;
1174 ++taskNdx;
1175 }
1176 taskNdx = 0;
1177 ++stackNdx;
1178 }
1179
1180 return null;
1181 }
1182
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001183 private ActivityStack getNextVisibleStackLocked() {
1184 ArrayList<ActivityStack> stacks = mStacks;
1185 final ActivityRecord parent = mActivityContainer.mParentActivity;
1186 if (parent != null) {
1187 stacks = parent.task.stack.mStacks;
1188 }
1189 if (stacks != null) {
1190 for (int i = stacks.size() - 1; i >= 0; --i) {
1191 ActivityStack stack = stacks.get(i);
1192 if (stack != this && stack.isStackVisibleLocked()) {
1193 return stack;
1194 }
1195 }
1196 }
1197 return null;
1198 }
1199
Jose Lima7ba71252014-04-30 12:59:27 -07001200 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1201 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001202 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001203 if (!isAttached()) {
1204 return false;
1205 }
1206
1207 if (mStackSupervisor.isFrontStack(this)) {
1208 return true;
1209 }
1210
Jose Lima729cb232014-05-05 15:59:40 -07001211 /**
1212 * Start at the task above this one and go up, looking for a visible
1213 * fullscreen activity, or a translucent activity that requested the
1214 * wallpaper to be shown behind it.
1215 */
Jose Lima7ba71252014-04-30 12:59:27 -07001216 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001217 ActivityStack stack = mStacks.get(i);
1218 // stack above isn't full screen, so, we assume we're still visible. at some point
1219 // we should look at the stack bounds to see if we're occluded even if the stack
1220 // isn't fullscreen
1221 if (!stack.mFullscreen) {
1222 continue;
1223 }
1224 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1225 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001226 final TaskRecord task = tasks.get(taskNdx);
1227 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001228 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001229 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001230
1231 // Conditions for an activity to obscure the stack we're
1232 // examining:
1233 // 1. Not Finishing AND Visible AND:
1234 // 2. Either:
1235 // - Full Screen Activity OR
1236 // - On top of Home and our stack is NOT home
1237 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001238 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001239 return false;
1240 }
1241 }
1242 }
1243 }
1244
1245 return true;
1246 }
1247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248 /**
1249 * Make sure that all activities that need to be visible (that is, they
1250 * currently can be seen by the user) actually are.
1251 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001252 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1253 ActivityRecord top = topRunningActivityLocked(null);
1254 if (top == null) {
1255 return;
1256 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001257 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1258 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 + " configChanges=0x" + Integer.toHexString(configChanges));
1260
Craig Mautner5eda9b32013-07-02 11:58:16 -07001261 if (mTranslucentActivityWaiting != top) {
1262 mUndrawnActivitiesBelowTopTranslucent.clear();
1263 if (mTranslucentActivityWaiting != null) {
1264 // Call the callback with a timeout indication.
1265 notifyActivityDrawnLocked(null);
1266 mTranslucentActivityWaiting = null;
1267 }
1268 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1269 }
1270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001271 // If the top activity is not fullscreen, then we need to
1272 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001273 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001274 boolean behindFullscreen = !isStackVisibleLocked();
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001275 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001276
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001277 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001278 final TaskRecord task = mTaskHistory.get(taskNdx);
1279 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001280 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1281 final ActivityRecord r = activities.get(activityNdx);
1282 if (r.finishing) {
1283 continue;
1284 }
1285 if (aboveTop && r != top) {
1286 continue;
1287 }
1288 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001289 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1290 // but must be drawn initially for the animation as though they were visible.
1291 if (!behindFullscreen || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001292 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1293 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001294 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001295
Craig Mautnerd44711d2013-02-23 11:24:36 -08001296 // First: if this is not the current activity being started, make
1297 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001298 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001299 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001300 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001301
1302 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001303 // This activity needs to be visible, but isn't even running...
1304 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001305 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1306 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001307 if (r != starting) {
1308 r.startFreezingScreenLocked(r.app, configChanges);
1309 }
1310 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001311 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1312 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001313 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001314 }
1315 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001316 mStackSupervisor.startSpecificActivityLocked(
1317 r, noStackActivityResumed, false);
1318 noStackActivityResumed = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001319 }
1320
1321 } else if (r.visible) {
1322 // If this activity is already visible, then there is nothing
1323 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001324 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1325 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001326 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001327 try {
George Mount6ba042b2014-07-28 11:12:28 -07001328 if (r.returningOptions != null) {
1329 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1330 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001331 }
1332 } catch(RemoteException e) {
1333 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001334 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001335 // This activity is not currently visible, but is running.
1336 // Tell it to become visible.
1337 r.visible = true;
1338 if (r.state != ActivityState.RESUMED && r != starting) {
1339 // If this activity is paused, tell it
1340 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001341 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1342 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001343 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001344 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001345 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001346 mUndrawnActivitiesBelowTopTranslucent.add(r);
1347 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001348 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001349 r.sleeping = false;
1350 r.app.pendingUiClean = true;
1351 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1352 r.stopFreezingScreenLocked(false);
1353 } catch (Exception e) {
1354 // Just skip on any failure; we'll make it
1355 // visible when it next restarts.
1356 Slog.w(TAG, "Exception thrown making visibile: "
1357 + r.intent.getComponent(), e);
1358 }
1359 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361
Craig Mautnerd44711d2013-02-23 11:24:36 -08001362 // Aggregate current change flags.
1363 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364
Craig Mautnerd44711d2013-02-23 11:24:36 -08001365 if (r.fullscreen) {
1366 // At this point, nothing else needs to be shown
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001367 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r);
Craig Mautner580ea812013-04-25 12:58:38 -07001368 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001369 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001370 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001371 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001372 }
1373 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001374 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1375 "Make invisible? " + r + " finishing=" + r.finishing
1376 + " state=" + r.state + " behindFullscreen=" + behindFullscreen);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001377 // Now for any activities that aren't visible to the user, make
1378 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001379 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001380 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001382 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001383 switch (r.state) {
1384 case STOPPING:
1385 case STOPPED:
1386 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001387 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1388 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001389 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1390 }
1391 break;
1392
1393 case INITIALIZING:
1394 case RESUMED:
1395 case PAUSING:
1396 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001397 // This case created for transitioning activities from
1398 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001399 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001400 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001401 } else {
1402 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1403 mStackSupervisor.mStoppingActivities.add(r);
1404 }
1405 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001406 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001407 break;
1408
1409 default:
1410 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001411 }
1412 } catch (Exception e) {
1413 // Just skip on any failure; we'll make it
1414 // visible when it next restarts.
1415 Slog.w(TAG, "Exception thrown making hidden: "
1416 + r.intent.getComponent(), e);
1417 }
1418 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001419 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001420 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 }
1422 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001423 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001424
1425 if (mTranslucentActivityWaiting != null &&
1426 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1427 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1428 notifyActivityDrawnLocked(null);
1429 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 }
Craig Mautner58547802013-03-05 08:23:53 -08001431
Todd Kennedyaab56db2015-01-30 09:39:53 -08001432 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001433 mTranslucentActivityWaiting = r;
1434 mUndrawnActivitiesBelowTopTranslucent.clear();
1435 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1436 }
1437
1438 /**
1439 * Called as activities below the top translucent activity are redrawn. When the last one is
1440 * redrawn notify the top activity by calling
1441 * {@link Activity#onTranslucentConversionComplete}.
1442 *
1443 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1444 * occurred and the activity will be notified immediately.
1445 */
1446 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001447 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001448 if ((r == null)
1449 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1450 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1451 // The last undrawn activity below the top has just been drawn. If there is an
1452 // opaque activity at the top, notify it that it can become translucent safely now.
1453 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1454 mTranslucentActivityWaiting = null;
1455 mUndrawnActivitiesBelowTopTranslucent.clear();
1456 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1457
Craig Mautner71dd1b62014-02-18 15:48:52 -08001458 if (waitingActivity != null) {
1459 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1460 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1461 try {
1462 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1463 waitingActivity.appToken, r != null);
1464 } catch (RemoteException e) {
1465 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001466 }
1467 }
1468 }
1469 }
1470
Craig Mautnera61bc652013-10-28 15:43:18 -07001471 /** If any activities below the top running one are in the INITIALIZING state and they have a
1472 * starting window displayed then remove that starting window. It is possible that the activity
1473 * in this state will never resumed in which case that starting window will be orphaned. */
1474 void cancelInitializingActivities() {
1475 final ActivityRecord topActivity = topRunningActivityLocked(null);
1476 boolean aboveTop = true;
1477 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1478 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1479 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1480 final ActivityRecord r = activities.get(activityNdx);
1481 if (aboveTop) {
1482 if (r == topActivity) {
1483 aboveTop = false;
1484 }
1485 continue;
1486 }
1487
1488 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001489 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1490 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001491 r.mStartingWindowShown = false;
1492 mWindowManager.removeAppStartingWindow(r.appToken);
1493 }
1494 }
1495 }
1496 }
1497
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001498 /**
1499 * Ensure that the top activity in the stack is resumed.
1500 *
1501 * @param prev The previously resumed activity, for when in the process
1502 * of pausing; can be null to call from elsewhere.
1503 *
1504 * @return Returns true if something is being resumed, or false if
1505 * nothing happened.
1506 */
1507 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001508 return resumeTopActivityLocked(prev, null);
1509 }
1510
1511 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001512 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001513 // Don't even start recursing.
1514 return false;
1515 }
1516
1517 boolean result = false;
1518 try {
1519 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001520 mStackSupervisor.inResumeTopActivity = true;
1521 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1522 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001523 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001524 }
Craig Mautner544efa72014-09-04 16:41:20 -07001525 result = resumeTopActivityInnerLocked(prev, options);
1526 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001527 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001528 }
1529 return result;
1530 }
1531
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001532 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001533 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001534
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001535 if (!mService.mBooting && !mService.mBooted) {
1536 // Not ready yet!
1537 return false;
1538 }
1539
Craig Mautnerdf88d732014-01-27 09:21:32 -08001540 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001541 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001542 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001543 // Do not resume this stack if its parent is not resumed.
1544 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1545 return false;
1546 }
1547
Craig Mautnera61bc652013-10-28 15:43:18 -07001548 cancelInitializingActivities();
1549
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001550 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001551 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001552
1553 // Remember how we'll process this pause/resume situation, and ensure
1554 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001555 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1556 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001557
Craig Mautner84984fa2014-06-19 11:19:20 -07001558 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001560 // There are no more activities!
1561 final String reason = "noMoreActivities";
1562 if (!mFullscreen) {
1563 // Try to move focus to the next visible stack with a running activity if this
1564 // stack is not covering the entire screen.
1565 final ActivityStack stack = getNextVisibleStackLocked();
1566 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1567 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1568 }
1569 }
1570 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001571 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001572 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001573 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001574 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001575 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1576 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1577 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001578 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 }
1580
1581 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001582
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001584 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1585 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001586 // Make sure we have executed any pending transitions, since there
1587 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001588 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001590 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001591 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001592 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 return false;
1594 }
1595
Craig Mautner525f3d92013-05-07 14:01:50 -07001596 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001597 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001598 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001599 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001600 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001601 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001602 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001603 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001604 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001605 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001606 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001607 } else if (!isOnHomeDisplay()) {
1608 return false;
1609 } else if (!isHomeStack()){
1610 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001611 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001612 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1613 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1614 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001615 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001616 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001617 }
1618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 // If we are sleeping, and there is no resumed activity, and the top
1620 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001621 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001622 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001623 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 // Make sure we have executed any pending transitions, since there
1625 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001626 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001627 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001628 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001629 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001630 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001631 return false;
1632 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001633
1634 // Make sure that the user who owns this activity is started. If not,
1635 // we will just leave it as is because someone should be bringing
1636 // another user's activities to the top of the stack.
1637 if (mService.mStartedUsers.get(next.userId) == null) {
1638 Slog.w(TAG, "Skipping resume of top activity " + next
1639 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001640 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001641 return false;
1642 }
1643
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 // The activity may be waiting for stop, but that is no longer
1645 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001646 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001647 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001648 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001649 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001650
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001651 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001653 // If we are currently pausing an activity, then don't do anything
1654 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001655 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001656 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001657 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001658 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001659 return false;
1660 }
1661
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001662 // Okay we are now going to start a switch, to 'next'. We may first
1663 // have to pause the current activity, but this is an important point
1664 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001665 // XXX "App Redirected" dialog is getting too many false positives
1666 // at this point, so turn off for now.
1667 if (false) {
1668 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1669 long now = SystemClock.uptimeMillis();
1670 final boolean inTime = mLastStartedActivity.startTime != 0
1671 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1672 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1673 final int nextUid = next.info.applicationInfo.uid;
1674 if (inTime && lastUid != nextUid
1675 && lastUid != next.launchedFromUid
1676 && mService.checkPermission(
1677 android.Manifest.permission.STOP_APP_SWITCHES,
1678 -1, next.launchedFromUid)
1679 != PackageManager.PERMISSION_GRANTED) {
1680 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1681 } else {
1682 next.startTime = now;
1683 mLastStartedActivity = next;
1684 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001685 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001686 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001687 mLastStartedActivity = next;
1688 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001689 }
Craig Mautner58547802013-03-05 08:23:53 -08001690
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001691 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1692
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 // We need to start pausing the current activity so the top one
1694 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001695 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1696 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001697 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001698 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001699 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001700 }
1701 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001702 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1703 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001704 // At this point we want to put the upcoming activity's process
1705 // at the top of the LRU list, since we know we will be needing it
1706 // very soon and it would be a waste to let it get killed if it
1707 // happens to be sitting towards the end.
1708 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001709 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001710 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001711 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001712 return true;
1713 }
1714
Christopher Tated3f175c2012-06-14 14:16:54 -07001715 // If the most recent activity was noHistory but was only stopped rather
1716 // than stopped+finished because the device went to sleep, we need to make
1717 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001718 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001719 !mLastNoHistoryActivity.finishing) {
1720 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1721 " on new resume");
1722 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001723 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001724 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001725 }
1726
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001728 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1729 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001730 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001731 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1732 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 } else {
1734 // The next activity is already visible, so hide the previous
1735 // activity's windows right now so we can show the new one ASAP.
1736 // We only do this if the previous is finishing, which should mean
1737 // it is on top of the one being resumed so hiding it quickly
1738 // is good. Otherwise, we want to do the normal route of allowing
1739 // the resumed activity to be shown so we can decide if the
1740 // previous should actually be hidden depending on whether the
1741 // new one is found to be full-screen or not.
1742 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001743 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001744 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1745 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001746 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 + ", nowVisible=" + next.nowVisible);
1748 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001749 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001750 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001751 + ", waitingVisible="
1752 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1753 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 }
1755 }
1756 }
1757
Dianne Hackborne7f97212011-02-24 14:40:20 -08001758 // Launching this app's activity, make sure the app is no longer
1759 // considered stopped.
1760 try {
1761 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001762 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001763 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001764 } catch (IllegalArgumentException e) {
1765 Slog.w(TAG, "Failed trying to unstop package "
1766 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001767 }
1768
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 // We are starting up the next activity, so tell the window manager
1770 // that the previous one will be hidden soon. This way it can know
1771 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001772 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 if (prev != null) {
1774 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001775 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 "Prepare close transition: prev=" + prev);
1777 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001778 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001781 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001782 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1783 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001785 mWindowManager.setAppWillBeHidden(prev.appToken);
1786 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001788 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1789 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001791 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001792 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001794 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001795 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001796 : next.mLaunchTaskBehind
1797 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1798 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 }
1800 }
1801 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001802 mWindowManager.setAppWillBeHidden(prev.appToken);
1803 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 }
Craig Mautner967212c2013-04-13 21:10:58 -07001805 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001806 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001808 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001809 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001811 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 }
1813 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001814
1815 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001816 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001817 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1818 if (opts != null) {
1819 resumeAnimOptions = opts.toBundle();
1820 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001821 next.applyOptionsLocked();
1822 } else {
1823 next.clearOptionsLocked();
1824 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825
Craig Mautnercf910b02013-04-23 11:23:27 -07001826 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001827 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001828 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829
1830 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001831 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001832
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001833 // schedule launch ticks to collect information about slow apps.
1834 next.startLaunchTickingLocked();
1835
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001836 ActivityRecord lastResumedActivity =
1837 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001838 ActivityState lastState = next.state;
1839
1840 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001841
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001842 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 next.state = ActivityState.RESUMED;
1844 mResumedActivity = next;
1845 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001846 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001847 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001848 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001849 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001850
1851 // Have the window manager re-evaluate the orientation of
1852 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001853 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001854 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001855 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001856 mService.mConfiguration,
1857 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1858 if (config != null) {
1859 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001861 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001863
Craig Mautner525f3d92013-05-07 14:01:50 -07001864 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001865 // The configuration update wasn't able to keep the existing
1866 // instance of the activity, and instead started a new one.
1867 // We should be all done, but let's just make sure our activity
1868 // is still at the top and schedule another run if something
1869 // weird happened.
1870 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001871 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 "Activity config changed during resume: " + next
1873 + ", new next: " + nextNext);
1874 if (nextNext != next) {
1875 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001876 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001878 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001879 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001880 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001881 return true;
1882 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001883 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001884 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 }
Craig Mautner58547802013-03-05 08:23:53 -08001886
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001887 try {
1888 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001889 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001890 if (a != null) {
1891 final int N = a.size();
1892 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001893 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1894 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001895 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001896 }
1897 }
1898
1899 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001900 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 }
1902
Craig Mautner299f9602015-01-26 09:47:33 -08001903 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1904 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001905
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001906 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001907 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001908 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001909 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07001910 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001911 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001912 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001913
Craig Mautner0eea92c2013-05-16 13:35:39 -07001914 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915
Craig Mautnerac6f8432013-07-17 13:24:59 -07001916 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 } catch (Exception e) {
1918 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001919 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1920 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001922 if (lastStack != null) {
1923 lastStack.mResumedActivity = lastResumedActivity;
1924 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001925 Slog.i(TAG, "Restarting because process died: " + next);
1926 if (!next.hasBeenLaunched) {
1927 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001928 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1929 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001930 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001931 next.appToken, next.packageName, next.theme,
1932 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001933 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1934 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001935 }
George Mount2c92c972014-03-20 09:38:23 -07001936 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001937 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001938 return true;
1939 }
1940
1941 // From this point on, if something goes wrong there is no way
1942 // to recover the activity.
1943 try {
1944 next.visible = true;
1945 completeResumeLocked(next);
1946 } catch (Exception e) {
1947 // If any exception gets thrown, toss away this
1948 // activity and try the next one.
1949 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001950 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001951 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001952 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001953 return true;
1954 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 next.stopped = false;
1956
1957 } else {
1958 // Whoops, need to restart this activity!
1959 if (!next.hasBeenLaunched) {
1960 next.hasBeenLaunched = true;
1961 } else {
1962 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001963 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001964 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001965 mService.compatibilityInfoForPackageLocked(
1966 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001968 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001969 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001971 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001973 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001974 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001975 }
1976
Craig Mautnercf910b02013-04-23 11:23:27 -07001977 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 return true;
1979 }
1980
riddle_hsuc215a4f2014-12-27 12:10:45 +08001981 private TaskRecord getNextTask(TaskRecord targetTask) {
1982 final int index = mTaskHistory.indexOf(targetTask);
1983 if (index >= 0) {
1984 final int numTasks = mTaskHistory.size();
1985 for (int i = index + 1; i < numTasks; ++i) {
1986 TaskRecord task = mTaskHistory.get(i);
1987 if (task.userId == targetTask.userId) {
1988 return task;
1989 }
1990 }
1991 }
1992 return null;
1993 }
1994
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001995 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001996 // If the moving task is over home stack, transfer its return type to next task
1997 if (task.isOverHomeStack()) {
1998 final TaskRecord nextTask = getNextTask(task);
1999 if (nextTask != null) {
2000 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2001 }
2002 }
2003
Craig Mautner9c85c202013-10-04 20:11:26 -07002004 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002005 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002006 if (isOnHomeDisplay()) {
2007 ActivityStack lastStack = mStackSupervisor.getLastStack();
2008 final boolean fromHome = lastStack.isHomeStack();
2009 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002010 task.setTaskToReturnTo(fromHome
2011 ? lastStack.topTask() == null
2012 ? HOME_ACTIVITY_TYPE
2013 : lastStack.topTask().taskType
2014 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002015 }
2016 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002017 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002018 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002019
Craig Mautnerac6f8432013-07-17 13:24:59 -07002020 mTaskHistory.remove(task);
2021 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002022 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002023 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002024 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002025 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002026 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002027 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002028 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002029 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002030 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002031 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002032 break;
2033 }
2034 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002035 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002036 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002037 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002038 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002039 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002040
Craig Mautner8849a5e2013-04-02 16:41:03 -07002041 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002042 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002043 TaskRecord rTask = r.task;
2044 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002045 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2046 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002047 // Last activity in task had been removed or ActivityManagerService is reusing task.
2048 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002049 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002050 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002051 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002052 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002053 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 if (!newTask) {
2055 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002056 boolean startIt = true;
2057 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2058 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002059 if (task.getTopActivity() == null) {
2060 // All activities in task are finishing.
2061 continue;
2062 }
Craig Mautner70a86932013-02-28 22:37:44 -08002063 if (task == r.task) {
2064 // Here it is! Now, if this is not yet visible to the
2065 // user, then just add it without starting; it will
2066 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002067 if (!startIt) {
2068 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2069 + task, new RuntimeException("here").fillInStackTrace());
2070 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002071 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002072 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2073 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002074 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002075 r.userId, r.info.configChanges, task.voiceSession != null,
2076 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002078 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002080 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 return;
2082 }
2083 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002084 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002085 startIt = false;
2086 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 }
2088 }
2089
2090 // Place a new activity at top of stack, so it is next to interact
2091 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002092
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 // If we are not placing the new activity frontmost, we do not want
2094 // to deliver the onUserLeaving callback to the actual frontmost
2095 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002096 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002097 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002098 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002099 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002100 }
Craig Mautner70a86932013-02-28 22:37:44 -08002101
2102 task = r.task;
2103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002105 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002106 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002107 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002108 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002109
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002110 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002111 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 // We want to show the starting preview window if we are
2113 // switching to a new task, or the next activity's process is
2114 // not currently running.
2115 boolean showStartingIcon = newTask;
2116 ProcessRecord proc = r.app;
2117 if (proc == null) {
2118 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2119 }
2120 if (proc == null || proc.thread == null) {
2121 showStartingIcon = true;
2122 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002123 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002125 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002126 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002129 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002130 ? r.mLaunchTaskBehind
2131 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2132 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002133 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 mNoAnimActivities.remove(r);
2135 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002136 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002137 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002138 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002139 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 boolean doShow = true;
2141 if (newTask) {
2142 // Even though this activity is starting fresh, we still need
2143 // to reset it to make sure we apply affinities to move any
2144 // existing activities from other tasks in to it.
2145 // If the caller has requested that the target task be
2146 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002147 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 resetTaskIfNeededLocked(r, r);
2149 doShow = topRunningNonDelayedActivityLocked(null) == r;
2150 }
George Mount70778d72014-07-01 16:33:45 -07002151 } else if (options != null && new ActivityOptions(options).getAnimationType()
2152 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2153 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002155 if (r.mLaunchTaskBehind) {
2156 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2157 // tell WindowManager that r is visible even though it is at the back of the stack.
2158 mWindowManager.setAppVisibility(r.appToken, true);
2159 ensureActivitiesVisibleLocked(null, 0);
2160 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 // Figure out if we are transitioning from another activity that is
2162 // "has the same starting icon" as the next one. This allows the
2163 // window manager to keep the previous window it had previously
2164 // created, if it still had one.
2165 ActivityRecord prev = mResumedActivity;
2166 if (prev != null) {
2167 // We don't want to reuse the previous starting preview if:
2168 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002169 if (prev.task != r.task) {
2170 prev = null;
2171 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002173 else if (prev.nowVisible) {
2174 prev = null;
2175 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002177 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002178 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002179 mService.compatibilityInfoForPackageLocked(
2180 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002181 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002182 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002183 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184 }
2185 } else {
2186 // If this is the first activity, don't do any fancy animations,
2187 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002188 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002189 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002190 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002191 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002192 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002193 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 }
2195 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002196 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 }
2198
2199 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002200 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 }
2202 }
2203
Dianne Hackbornbe707852011-11-11 14:32:10 -08002204 final void validateAppTokensLocked() {
2205 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002206 mValidateAppTokens.ensureCapacity(numActivities());
2207 final int numTasks = mTaskHistory.size();
2208 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2209 TaskRecord task = mTaskHistory.get(taskNdx);
2210 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002211 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002212 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002213 }
Craig Mautner000f0022013-02-26 15:04:29 -08002214 TaskGroup group = new TaskGroup();
2215 group.taskId = task.taskId;
2216 mValidateAppTokens.add(group);
2217 final int numActivities = activities.size();
2218 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2219 final ActivityRecord r = activities.get(activityNdx);
2220 group.tokens.add(r.appToken);
2221 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002222 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002223 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002224 }
2225
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 /**
2227 * Perform a reset of the given task, if needed as part of launching it.
2228 * Returns the new HistoryRecord at the top of the task.
2229 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230 /**
2231 * Helper method for #resetTaskIfNeededLocked.
2232 * We are inside of the task being reset... we'll either finish this activity, push it out
2233 * for another task, or leave it as-is.
2234 * @param task The task containing the Activity (taskTop) that might be reset.
2235 * @param forceReset
2236 * @return An ActivityOptions that needs to be processed.
2237 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002238 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002239 ActivityOptions topOptions = null;
2240
2241 int replyChainEnd = -1;
2242 boolean canMoveOptions = true;
2243
2244 // We only do this for activities that are not the root of the task (since if we finish
2245 // the root, we may no longer have the task!).
2246 final ArrayList<ActivityRecord> activities = task.mActivities;
2247 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002248 final int rootActivityNdx = task.findEffectiveRootIndex();
2249 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002250 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002251 if (target.frontOfTask)
2252 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002253
2254 final int flags = target.info.flags;
2255 final boolean finishOnTaskLaunch =
2256 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2257 final boolean allowTaskReparenting =
2258 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2259 final boolean clearWhenTaskReset =
2260 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2261
2262 if (!finishOnTaskLaunch
2263 && !clearWhenTaskReset
2264 && target.resultTo != null) {
2265 // If this activity is sending a reply to a previous
2266 // activity, we can't do anything with it now until
2267 // we reach the start of the reply chain.
2268 // XXX note that we are assuming the result is always
2269 // to the previous activity, which is almost always
2270 // the case but we really shouldn't count on.
2271 if (replyChainEnd < 0) {
2272 replyChainEnd = i;
2273 }
2274 } else if (!finishOnTaskLaunch
2275 && !clearWhenTaskReset
2276 && allowTaskReparenting
2277 && target.taskAffinity != null
2278 && !target.taskAffinity.equals(task.affinity)) {
2279 // If this activity has an affinity for another
2280 // task, then we need to move it out of here. We will
2281 // move it as far out of the way as possible, to the
2282 // bottom of the activity stack. This also keeps it
2283 // correctly ordered with any activities we previously
2284 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002285 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002286 final ActivityRecord bottom =
2287 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002288 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002289 if (bottom != null && target.taskAffinity != null
2290 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002291 // If the activity currently at the bottom has the
2292 // same task affinity as the one we are moving,
2293 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002294 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002295 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002296 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002297 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002298 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002299 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002300 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002301 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002302 + " out to new task " + target.task);
2303 }
2304
Craig Mautnere3a74d52013-02-22 14:14:58 -08002305 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002306 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002307
Craig Mautner525f3d92013-05-07 14:01:50 -07002308 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002309 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2310 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002311 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002312 if (p.finishing) {
2313 continue;
2314 }
2315
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002317 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002318 topOptions = p.takeOptionsLocked();
2319 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002320 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002321 }
2322 }
2323 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002324 + task + " adding to task=" + targetTask
2325 + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002326 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2327 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002328 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002329 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002330
Craig Mautner83162a92015-01-26 14:43:30 -08002331 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002332 }
2333
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002334 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002335 if (VALIDATE_TOKENS) {
2336 validateAppTokensLocked();
2337 }
2338
2339 replyChainEnd = -1;
2340 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2341 // If the activity should just be removed -- either
2342 // because it asks for it, or the task should be
2343 // cleared -- then finish it and anything that is
2344 // part of its reply chain.
2345 int end;
2346 if (clearWhenTaskReset) {
2347 // In this case, we want to finish this activity
2348 // and everything above it, so be sneaky and pretend
2349 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002350 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002351 } else if (replyChainEnd < 0) {
2352 end = i;
2353 } else {
2354 end = replyChainEnd;
2355 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002356 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002357 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002358 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002359 if (p.finishing) {
2360 continue;
2361 }
2362 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002363 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002364 topOptions = p.takeOptionsLocked();
2365 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002366 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002367 }
2368 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002369 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002370 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002371 if (finishActivityLocked(
2372 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002373 end--;
2374 srcPos--;
2375 }
2376 }
2377 replyChainEnd = -1;
2378 } else {
2379 // If we were in the middle of a chain, well the
2380 // activity that started it all doesn't want anything
2381 // special, so leave it all as-is.
2382 replyChainEnd = -1;
2383 }
2384 }
2385
2386 return topOptions;
2387 }
2388
2389 /**
2390 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2391 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2392 * @param affinityTask The task we are looking for an affinity to.
2393 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2394 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2395 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2396 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002397 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002398 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002399 int replyChainEnd = -1;
2400 final int taskId = task.taskId;
2401 final String taskAffinity = task.affinity;
2402
2403 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2404 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002405 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2406
2407 // Do not operate on or below the effective root Activity.
2408 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002409 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002410 if (target.frontOfTask)
2411 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002412
2413 final int flags = target.info.flags;
2414 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2415 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2416
2417 if (target.resultTo != null) {
2418 // If this activity is sending a reply to a previous
2419 // activity, we can't do anything with it now until
2420 // we reach the start of the reply chain.
2421 // XXX note that we are assuming the result is always
2422 // to the previous activity, which is almost always
2423 // the case but we really shouldn't count on.
2424 if (replyChainEnd < 0) {
2425 replyChainEnd = i;
2426 }
2427 } else if (topTaskIsHigher
2428 && allowTaskReparenting
2429 && taskAffinity != null
2430 && taskAffinity.equals(target.taskAffinity)) {
2431 // This activity has an affinity for our task. Either remove it if we are
2432 // clearing or move it over to our task. Note that
2433 // we currently punt on the case where we are resetting a
2434 // task that is not at the top but who has activities above
2435 // with an affinity to it... this is really not a normal
2436 // case, and we will need to later pull that task to the front
2437 // and usually at that point we will do the reset and pick
2438 // up those remaining activities. (This only happens if
2439 // someone starts an activity in a new task from an activity
2440 // in a task that is not currently on top.)
2441 if (forceReset || finishOnTaskLaunch) {
2442 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002443 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2444 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002445 for (int srcPos = start; srcPos >= i; --srcPos) {
2446 final ActivityRecord p = activities.get(srcPos);
2447 if (p.finishing) {
2448 continue;
2449 }
Todd Kennedy539db512014-12-15 09:57:55 -08002450 finishActivityLocked(
2451 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002452 }
2453 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002454 if (taskInsertionPoint < 0) {
2455 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002456
Craig Mautner77878772013-03-04 19:46:24 -08002457 }
Craig Mautner77878772013-03-04 19:46:24 -08002458
2459 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002460 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2461 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2462 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002463 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002464 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002465 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002466 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002467
Craig Mautnere3a74d52013-02-22 14:14:58 -08002468 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2469 + " to stack at " + task,
2470 new RuntimeException("here").fillInStackTrace());
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002471 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2472 + " from " + srcPos + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002473 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002474 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002475 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002476 if (VALIDATE_TOKENS) {
2477 validateAppTokensLocked();
2478 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002479
2480 // Now we've moved it in to place... but what if this is
2481 // a singleTop activity and we have put it on top of another
2482 // instance of the same activity? Then we drop the instance
2483 // below so it remains singleTop.
2484 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2485 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002486 int targetNdx = taskActivities.indexOf(target);
2487 if (targetNdx > 0) {
2488 ActivityRecord p = taskActivities.get(targetNdx - 1);
2489 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002490 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2491 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002492 }
2493 }
2494 }
2495 }
2496
2497 replyChainEnd = -1;
2498 }
2499 }
Craig Mautner77878772013-03-04 19:46:24 -08002500 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002501 }
2502
Craig Mautner8849a5e2013-04-02 16:41:03 -07002503 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002504 ActivityRecord newActivity) {
2505 boolean forceReset =
2506 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2507 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2508 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2509 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2510 forceReset = true;
2511 }
2512 }
2513
2514 final TaskRecord task = taskTop.task;
2515
2516 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2517 * for remaining tasks. Used for later tasks to reparent to task. */
2518 boolean taskFound = false;
2519
2520 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2521 ActivityOptions topOptions = null;
2522
Craig Mautner77878772013-03-04 19:46:24 -08002523 // Preserve the location for reparenting in the new task.
2524 int reparentInsertionPoint = -1;
2525
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2527 final TaskRecord targetTask = mTaskHistory.get(i);
2528
2529 if (targetTask == task) {
2530 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2531 taskFound = true;
2532 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002533 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2534 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002535 }
2536 }
2537
Craig Mautner70a86932013-02-28 22:37:44 -08002538 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002539 if (taskNdx >= 0) {
2540 do {
2541 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2542 } while (taskTop == null && taskNdx >= 0);
2543 }
Craig Mautner70a86932013-02-28 22:37:44 -08002544
Craig Mautnere3a74d52013-02-22 14:14:58 -08002545 if (topOptions != null) {
2546 // If we got some ActivityOptions from an activity on top that
2547 // was removed from the task, propagate them to the new real top.
2548 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002549 taskTop.updateOptionsLocked(topOptions);
2550 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002551 topOptions.abort();
2552 }
2553 }
2554
2555 return taskTop;
2556 }
2557
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2559 String resultWho, int requestCode, int resultCode, Intent data) {
2560
2561 if (callingUid > 0) {
2562 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002563 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002564 }
2565
2566 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2567 + " : who=" + resultWho + " req=" + requestCode
2568 + " res=" + resultCode + " data=" + data);
2569 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2570 try {
2571 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2572 list.add(new ResultInfo(resultWho, requestCode,
2573 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002574 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575 return;
2576 } catch (Exception e) {
2577 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2578 }
2579 }
2580
2581 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2582 }
2583
Craig Mautner299f9602015-01-26 09:47:33 -08002584 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002585 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2586 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002587 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002588 if (next != r) {
2589 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002590 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002591 // For non-fullscreen stack, we want to move the focus to the next visible
2592 // stack to prevent the home screen from moving to the top and obscuring
2593 // other visible stacks.
2594 if (!mFullscreen
2595 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2596 return;
2597 }
2598 // Move the home stack to the top if this stack is fullscreen or there is no
2599 // other visible stack.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002600 if (mStackSupervisor.moveHomeStackTaskToTop(
2601 task.getTaskToReturnTo(), myReason)) {
2602 // Activity focus was already adjusted. Nothing else to do...
2603 return;
2604 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002605 }
2606 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002607
2608 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002609 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002610 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002611 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002612 }
2613 }
2614
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002615 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2616 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2617 final String myReason = reason + " adjustFocusToNextVisibleStack";
2618 if (stack == null) {
2619 return false;
2620 }
2621 final ActivityRecord top = stack.topRunningActivityLocked(null);
2622 if (top == null) {
2623 return false;
2624 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002625 mService.setFocusedActivityLocked(top, myReason);
2626 return true;
2627 }
2628
Craig Mautnerf3333272013-04-22 10:55:53 -07002629 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002630 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002631 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2632 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2633 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002634 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002635 if (DEBUG_STATES) {
2636 Slog.d(TAG, "no-history finish of " + r);
2637 }
2638 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002639 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002640 } else {
2641 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2642 + " on stop because we're just sleeping");
2643 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002644 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002645 }
2646
2647 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002648 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002649 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002650 try {
2651 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002652 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2653 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002655 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2656 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002657 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002658 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002659 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002660 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002661 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002662 r.setSleeping(true);
2663 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002664 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002665 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 } catch (Exception e) {
2667 // Maybe just ignore exceptions here... if the process
2668 // has crashed, our death notification will clean things
2669 // up.
2670 Slog.w(TAG, "Exception thrown during pause", e);
2671 // Just in case, assume it to be stopped.
2672 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002673 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 r.state = ActivityState.STOPPED;
2675 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002676 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677 }
2678 }
2679 }
2680 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 /**
2683 * @return Returns true if the activity is being finished, false if for
2684 * some reason it is being left as-is.
2685 */
2686 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002687 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002688 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002689 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002690 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002691 + ", result=" + resultCode + ", data=" + resultData
2692 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002693 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694 return false;
2695 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696
Craig Mautnerd44711d2013-02-23 11:24:36 -08002697 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698 return true;
2699 }
2700
Craig Mautnerd2328952013-03-05 12:46:26 -08002701 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002702 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2703 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2704 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2705 ActivityRecord r = activities.get(activityNdx);
2706 if (r.resultTo == self && r.requestCode == requestCode) {
2707 if ((r.resultWho == null && resultWho == null) ||
2708 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2709 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2710 false);
2711 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002712 }
2713 }
2714 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002715 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002716 }
2717
Todd Kennedy539db512014-12-15 09:57:55 -08002718 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002719 ActivityRecord r = topRunningActivityLocked(null);
2720 if (r != null && r.app == app) {
2721 // If the top running activity is from this crashing
2722 // process, then terminate it to avoid getting in a loop.
2723 Slog.w(TAG, " Force finishing activity "
2724 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002725 int taskNdx = mTaskHistory.indexOf(r.task);
2726 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002727 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002728 // Also terminate any activities below it that aren't yet
2729 // stopped, to avoid a situation where one will get
2730 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002731 --activityNdx;
2732 if (activityNdx < 0) {
2733 do {
2734 --taskNdx;
2735 if (taskNdx < 0) {
2736 break;
2737 }
2738 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2739 } while (activityNdx < 0);
2740 }
2741 if (activityNdx >= 0) {
2742 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002743 if (r.state == ActivityState.RESUMED
2744 || r.state == ActivityState.PAUSING
2745 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002746 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002747 Slog.w(TAG, " Force finishing activity "
2748 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002749 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002750 }
2751 }
2752 }
2753 }
2754 }
2755
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002756 final void finishVoiceTask(IVoiceInteractionSession session) {
2757 IBinder sessionBinder = session.asBinder();
2758 boolean didOne = false;
2759 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2760 TaskRecord tr = mTaskHistory.get(taskNdx);
2761 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2762 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2763 ActivityRecord r = tr.mActivities.get(activityNdx);
2764 if (!r.finishing) {
2765 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2766 false);
2767 didOne = true;
2768 }
2769 }
2770 }
2771 }
2772 if (didOne) {
2773 mService.updateOomAdjLocked();
2774 }
2775 }
2776
Craig Mautnerd2328952013-03-05 12:46:26 -08002777 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002778 ArrayList<ActivityRecord> activities = r.task.mActivities;
2779 for (int index = activities.indexOf(r); index >= 0; --index) {
2780 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002781 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002782 break;
2783 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002784 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002785 }
2786 return true;
2787 }
2788
Dianne Hackborn5c607432012-02-28 14:44:19 -08002789 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2790 // send the result
2791 ActivityRecord resultTo = r.resultTo;
2792 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002793 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002794 + " who=" + r.resultWho + " req=" + r.requestCode
2795 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002796 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002797 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002798 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002799 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002800 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002801 if (r.info.applicationInfo.uid > 0) {
2802 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2803 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002804 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002805 }
2806 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2807 resultData);
2808 r.resultTo = null;
2809 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002810 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002811
2812 // Make sure this HistoryRecord is not holding on to other resources,
2813 // because clients have remote IPC references to this object so we
2814 // can't assume that will go away and want to avoid circular IPC refs.
2815 r.results = null;
2816 r.pendingResults = null;
2817 r.newIntents = null;
2818 r.icicle = null;
2819 }
2820
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002821 /**
2822 * @return Returns true if this activity has been removed from the history
2823 * list, or false if it is still in the list and will be removed later.
2824 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002825 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2826 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 if (r.finishing) {
2828 Slog.w(TAG, "Duplicate finish request for " + r);
2829 return false;
2830 }
2831
Wale Ogunwale7d701172015-03-11 15:36:30 -07002832 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002833 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002835 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002836 task.taskId, r.shortComponentName, reason);
2837 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002838 final int index = activities.indexOf(r);
2839 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002840 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002841 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002842 // If the caller asked that this activity (and all above it)
2843 // be cleared when the task is reset, don't lose that information,
2844 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002845 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002846 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 }
2848 }
2849
2850 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002851
Craig Mautner299f9602015-01-26 09:47:33 -08002852 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002853
Dianne Hackborn5c607432012-02-28 14:44:19 -08002854 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002855
Craig Mautnerde4ef022013-04-07 19:01:33 -07002856 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002857 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002858 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002859 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002860 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002861 ? AppTransition.TRANSIT_TASK_CLOSE
2862 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002863
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002864 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002865 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002866
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002867 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002868 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
2869 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2870 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002871 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872 }
2873
Craig Mautneraea74a52014-03-08 14:23:10 -08002874 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07002875 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002876 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002877 } else if (r.state != ActivityState.PAUSING) {
2878 // If the activity is PAUSING, we will complete the finish once
2879 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002880 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002881 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002882 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002883 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 }
2885
2886 return false;
2887 }
2888
Craig Mautnerf3333272013-04-22 10:55:53 -07002889 static final int FINISH_IMMEDIATELY = 0;
2890 static final int FINISH_AFTER_PAUSE = 1;
2891 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002892
Craig Mautnerf3333272013-04-22 10:55:53 -07002893 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 // First things first: if this activity is currently visible,
2895 // and the resumed activity is not yet visible, then hold off on
2896 // finishing until the resumed one becomes visible.
2897 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002898 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2899 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002900 if (mStackSupervisor.mStoppingActivities.size() > 3
2901 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 // If we already have a few activities waiting to stop,
2903 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002904 // them out. Or if r is the last of activity of the last task the stack
2905 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002906 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002907 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002908 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002909 }
2910 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002911 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2912 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002914 if (oomAdj) {
2915 mService.updateOomAdjLocked();
2916 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 return r;
2918 }
2919
2920 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002921 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002922 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002923 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 if (mResumedActivity == r) {
2925 mResumedActivity = null;
2926 }
2927 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002928 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 r.state = ActivityState.FINISHING;
2930
2931 if (mode == FINISH_IMMEDIATELY
2932 || prevState == ActivityState.STOPPED
2933 || prevState == ActivityState.INITIALIZING) {
2934 // If this activity is already stopped, we can just finish
2935 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07002936 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002937 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002938 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002939 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002940 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002941 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002942 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2943 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002944 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002946
2947 // Need to go through the full pause cycle to get this
2948 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002949 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002950 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002951 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002952 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002953 return r;
2954 }
2955
Craig Mautneree36c772014-07-16 14:56:05 -07002956 void finishAllActivitiesLocked(boolean immediately) {
2957 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002958 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2959 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2960 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2961 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002962 noActivitiesInStack = false;
2963 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002964 continue;
2965 }
Craig Mautneree36c772014-07-16 14:56:05 -07002966 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002967 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2968 }
2969 }
Craig Mautneree36c772014-07-16 14:56:05 -07002970 if (noActivitiesInStack) {
2971 mActivityContainer.onTaskListEmptyLocked();
2972 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002973 }
2974
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002975 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2976 // Basic case: for simple app-centric recents, we need to recreate
2977 // the task if the affinity has changed.
2978 if (srec == null || srec.task.affinity == null ||
2979 !srec.task.affinity.equals(destAffinity)) {
2980 return true;
2981 }
2982 // Document-centric case: an app may be split in to multiple documents;
2983 // they need to re-create their task if this current activity is the root
2984 // of a document, unless simply finishing it will return them to the the
2985 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002986 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2987 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002988 // Okay, this activity is at the root of its task. What to do, what to do...
2989 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2990 // Finishing won't return to an application, so we need to recreate.
2991 return true;
2992 }
2993 // We now need to get the task below it to determine what to do.
2994 int taskIdx = mTaskHistory.indexOf(srec.task);
2995 if (taskIdx <= 0) {
2996 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2997 return false;
2998 }
2999 if (taskIdx == 0) {
3000 // At the bottom of the stack, nothing to go back to.
3001 return true;
3002 }
3003 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3004 if (!srec.task.affinity.equals(prevTask.affinity)) {
3005 // These are different apps, so need to recreate.
3006 return true;
3007 }
3008 }
3009 return false;
3010 }
3011
Wale Ogunwale7d701172015-03-11 15:36:30 -07003012 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003013 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003014 final TaskRecord task = srec.task;
3015 final ArrayList<ActivityRecord> activities = task.mActivities;
3016 final int start = activities.indexOf(srec);
3017 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003018 return false;
3019 }
3020 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003021 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003022 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003023 final ComponentName dest = destIntent.getComponent();
3024 if (start > 0 && dest != null) {
3025 for (int i = finishTo; i >= 0; i--) {
3026 ActivityRecord r = activities.get(i);
3027 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003028 r.info.name.equals(dest.getClassName())) {
3029 finishTo = i;
3030 parent = r;
3031 foundParentInTask = true;
3032 break;
3033 }
3034 }
3035 }
3036
3037 IActivityController controller = mService.mController;
3038 if (controller != null) {
3039 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3040 if (next != null) {
3041 // ask watcher if this is allowed
3042 boolean resumeOK = true;
3043 try {
3044 resumeOK = controller.activityResuming(next.packageName);
3045 } catch (RemoteException e) {
3046 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003047 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003048 }
3049
3050 if (!resumeOK) {
3051 return false;
3052 }
3053 }
3054 }
3055 final long origId = Binder.clearCallingIdentity();
3056 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003057 ActivityRecord r = activities.get(i);
3058 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003059 // Only return the supplied result for the first activity finished
3060 resultCode = Activity.RESULT_CANCELED;
3061 resultData = null;
3062 }
3063
3064 if (parent != null && foundParentInTask) {
3065 final int parentLaunchMode = parent.info.launchMode;
3066 final int destIntentFlags = destIntent.getFlags();
3067 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3068 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3069 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3070 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003071 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3072 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003073 } else {
3074 try {
3075 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3076 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003077 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003078 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003079 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003080 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003081 foundParentInTask = res == ActivityManager.START_SUCCESS;
3082 } catch (RemoteException e) {
3083 foundParentInTask = false;
3084 }
3085 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003086 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003087 }
3088 }
3089 Binder.restoreCallingIdentity(origId);
3090 return foundParentInTask;
3091 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003092 /**
3093 * Perform the common clean-up of an activity record. This is called both
3094 * as part of destroyActivityLocked() (when destroying the client-side
3095 * representation) and cleaning things up as a result of its hosting
3096 * processing going away, in which case there is no remaining client-side
3097 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003098 *
3099 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003101 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3102 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103 if (mResumedActivity == r) {
3104 mResumedActivity = null;
3105 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003106 if (mPausingActivity == r) {
3107 mPausingActivity = null;
3108 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003109 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003110
3111 r.configDestroy = false;
3112 r.frozenBeforeDestroy = false;
3113
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003114 if (setState) {
3115 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3116 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003117 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003118 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003119 }
3120
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121 // Make sure this record is no longer in the pending finishes list.
3122 // This could happen, for example, if we are trimming activities
3123 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003124 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003125 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 // Remove any pending results.
3128 if (r.finishing && r.pendingResults != null) {
3129 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3130 PendingIntentRecord rec = apr.get();
3131 if (rec != null) {
3132 mService.cancelIntentSenderLocked(rec, false);
3133 }
3134 }
3135 r.pendingResults = null;
3136 }
3137
3138 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003139 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 }
3141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003142 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003143 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003144 if (getVisibleBehindActivity() == r) {
3145 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003146 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003147 }
3148
3149 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003150 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003152 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003153 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003154 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 }
3156
Craig Mautner299f9602015-01-26 09:47:33 -08003157 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003158 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003159 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003160 r.makeFinishingLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003161 if (DEBUG_ADD_REMOVE) {
3162 RuntimeException here = new RuntimeException("here");
3163 here.fillInStackTrace();
3164 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003166 r.takeFromHistory();
3167 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003168 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003169 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003170 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003171 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003172 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003173 if (VALIDATE_TOKENS) {
3174 validateAppTokensLocked();
3175 }
Craig Mautner312ba862014-02-10 17:55:01 -08003176 final TaskRecord task = r.task;
3177 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003178 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003179 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003180 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3181 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003182 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003183 }
Craig Mautner299f9602015-01-26 09:47:33 -08003184 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003185 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003186 cleanUpActivityServicesLocked(r);
3187 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003189
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003190 /**
3191 * Perform clean-up of service connections in an activity record.
3192 */
3193 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3194 // Throw away any services that have been bound by this activity.
3195 if (r.connections != null) {
3196 Iterator<ConnectionRecord> it = r.connections.iterator();
3197 while (it.hasNext()) {
3198 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003199 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 }
3201 r.connections = null;
3202 }
3203 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003204
Craig Mautneree2e45a2014-06-27 12:10:03 -07003205 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003206 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003207 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003208 mHandler.sendMessage(msg);
3209 }
3210
Craig Mautneree2e45a2014-06-27 12:10:03 -07003211 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003212 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003213 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003214 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3215 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3216 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3217 final ActivityRecord r = activities.get(activityNdx);
3218 if (r.finishing) {
3219 continue;
3220 }
3221 if (r.fullscreen) {
3222 lastIsOpaque = true;
3223 }
3224 if (owner != null && r.app != owner) {
3225 continue;
3226 }
3227 if (!lastIsOpaque) {
3228 continue;
3229 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003230 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003231 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003232 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003233 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003234 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003235 activityRemoved = true;
3236 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003237 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003238 }
3239 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003240 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003241 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003242 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003243 }
3244
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003245 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3246 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003247 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3248 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003249 + " pausing=" + mPausingActivity + " for reason " + reason);
3250 return destroyActivityLocked(r, true, reason);
3251 }
3252 return false;
3253 }
3254
3255 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3256 String reason) {
3257 // Iterate over tasks starting at the back (oldest) first.
3258 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3259 int maxTasks = tasks.size() / 4;
3260 if (maxTasks < 1) {
3261 maxTasks = 1;
3262 }
3263 int numReleased = 0;
3264 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3265 final TaskRecord task = mTaskHistory.get(taskNdx);
3266 if (!tasks.contains(task)) {
3267 continue;
3268 }
3269 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3270 int curNum = 0;
3271 final ArrayList<ActivityRecord> activities = task.mActivities;
3272 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3273 final ActivityRecord activity = activities.get(actNdx);
3274 if (activity.app == app && activity.isDestroyable()) {
3275 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3276 + " in state " + activity.state + " resumed=" + mResumedActivity
3277 + " pausing=" + mPausingActivity + " for reason " + reason);
3278 destroyActivityLocked(activity, true, reason);
3279 if (activities.get(actNdx) != activity) {
3280 // Was removed from list, back up so we don't miss the next one.
3281 actNdx--;
3282 }
3283 curNum++;
3284 }
3285 }
3286 if (curNum > 0) {
3287 numReleased += curNum;
3288 maxTasks--;
3289 if (mTaskHistory.get(taskNdx) != task) {
3290 // The entire task got removed, back up so we don't miss the next one.
3291 taskNdx--;
3292 }
3293 }
3294 }
3295 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3296 return numReleased;
3297 }
3298
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 /**
3300 * Destroy the current CLIENT SIDE instance of an activity. This may be
3301 * called both when actually finishing an activity, or when performing
3302 * a configuration switch where we destroy the current client-side object
3303 * but then create a new client-side object for this same HistoryRecord.
3304 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003305 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003306 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3307 "Removing activity from " + reason + ": token=" + r
3308 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003309 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003310 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003311 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312
3313 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003314
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003315 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003316
3317 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003318
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003319 if (hadApp) {
3320 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003321 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3323 mService.mHeavyWeightProcess = null;
3324 mService.mHandler.sendEmptyMessage(
3325 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3326 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003327 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003328 // Update any services we are bound to that might care about whether
3329 // their client may have activities.
3330 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003331 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003332 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003333 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 }
3335 }
3336
3337 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003338
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003339 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003340 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003341 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003342 r.configChangeFlags);
3343 } catch (Exception e) {
3344 // We can just ignore exceptions here... if the process
3345 // has crashed, our death notification will clean things
3346 // up.
3347 //Slog.w(TAG, "Exception thrown during finish", e);
3348 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003349 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003350 removedFromHistory = true;
3351 skipDestroy = true;
3352 }
3353 }
3354
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003356
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003357 // If the activity is finishing, we need to wait on removing it
3358 // from the list to give it a chance to do its cleanup. During
3359 // that time it may make calls back with its token so we need to
3360 // be able to find it on the list and so we don't want to remove
3361 // it from the list yet. Otherwise, we can just immediately put
3362 // it in the destroyed state since we are not removing it from the
3363 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003365 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3366 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003368 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3370 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003371 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003372 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003373 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003374 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 }
3376 } else {
3377 // remove this record from the history.
3378 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003379 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003380 removedFromHistory = true;
3381 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003382 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
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 }
3388
3389 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003390
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003391 if (!mLRUActivities.remove(r) && hadApp) {
3392 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3393 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003394
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003395 return removedFromHistory;
3396 }
3397
Craig Mautner299f9602015-01-26 09:47:33 -08003398 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003399 final long origId = Binder.clearCallingIdentity();
3400 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003401 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003402 if (r != null) {
3403 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003404 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003405 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003406
Wale Ogunwale60454db2015-01-23 16:05:07 -08003407 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003408 if (r.state == ActivityState.DESTROYING) {
3409 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003410 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003411 }
3412 }
Craig Mautner05d29032013-05-03 13:40:13 -07003413 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003414 } finally {
3415 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003416 }
3417 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003418
Todd Kennedyaab56db2015-01-30 09:39:53 -08003419 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003420 if (hasVisibleBehindActivity() &&
3421 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003422 if (r == topRunningActivityLocked(null)) {
3423 // Don't release the top activity if it has requested to run behind the next
3424 // activity.
3425 return;
3426 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003427 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3428 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003429 " thread=" + r.app.thread);
3430 if (r != null && r.app != null && r.app.thread != null) {
3431 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003432 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003433 } catch (RemoteException e) {
3434 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003435 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003436 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003437 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003438 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003439 }
3440 }
3441 }
3442
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003443 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003444 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3445 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003446 if (r != null) {
3447 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003448 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003449 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003450 }
3451 mStackSupervisor.resumeTopActivitiesLocked();
3452 }
3453
Jose Lima4b6c6692014-08-12 17:41:12 -07003454 boolean hasVisibleBehindActivity() {
3455 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003456 }
3457
Jose Lima4b6c6692014-08-12 17:41:12 -07003458 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003459 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003460 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003461 }
3462 }
3463
Jose Lima4b6c6692014-08-12 17:41:12 -07003464 ActivityRecord getVisibleBehindActivity() {
3465 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003466 }
3467
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003468 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3469 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003470 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003471 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3472 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 while (i > 0) {
3474 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003475 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003476 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003477 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003478 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003480 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003481 }
3482 }
3483 }
3484
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003485 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3486 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003487 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3488 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003489 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3490 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003491 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003492 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003493 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3494 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003495
3496 boolean hasVisibleActivities = false;
3497
3498 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003499 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003500 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3501 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003502 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3503 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3504 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3505 final ActivityRecord r = activities.get(activityNdx);
3506 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003507 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3508 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003509 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003510 if (r.visible) {
3511 hasVisibleActivities = true;
3512 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003513 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003514 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3515 // Don't currently have state for the activity, or
3516 // it is finishing -- always remove it.
3517 remove = true;
3518 } else if (r.launchCount > 2 &&
3519 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3520 // We have launched this activity too many times since it was
3521 // able to run, so give up and remove it.
3522 remove = true;
3523 } else {
3524 // The process may be gone, but the activity lives on!
3525 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003526 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003527 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003528 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003529 RuntimeException here = new RuntimeException("here");
3530 here.fillInStackTrace();
3531 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3532 + ": haveState=" + r.haveState
3533 + " stateNotNeeded=" + r.stateNotNeeded
3534 + " finishing=" + r.finishing
3535 + " state=" + r.state, here);
3536 }
3537 if (!r.finishing) {
3538 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3539 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3540 r.userId, System.identityHashCode(r),
3541 r.task.taskId, r.shortComponentName,
3542 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003543 if (r.state == ActivityState.RESUMED) {
3544 mService.updateUsageStats(r, false);
3545 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003546 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003547 } else {
3548 // We have the current state for this activity, so
3549 // it can be restarted later when needed.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003550 if (DEBUG_ALL) Slog.v(
Craig Mautner0247fc82013-02-28 14:32:06 -08003551 TAG, "Keeping entry, setting app to null");
Craig Mautner0247fc82013-02-28 14:32:06 -08003552 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3553 + r);
3554 r.app = null;
3555 r.nowVisible = false;
3556 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003557 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003558 "App died, clearing saved state of " + r);
3559 r.icicle = null;
3560 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003561 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003562 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003563 if (remove) {
3564 removeActivityFromHistoryLocked(r, "appDied");
3565 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003566 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003567 }
3568 }
3569
3570 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003571 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003572
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003573 final void updateTransitLocked(int transit, Bundle options) {
3574 if (options != null) {
3575 ActivityRecord r = topRunningActivityLocked(null);
3576 if (r != null && r.state != ActivityState.RESUMED) {
3577 r.updateOptionsLocked(options);
3578 } else {
3579 ActivityOptions.abort(options);
3580 }
3581 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003582 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003583 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003584
Craig Mautner21d24a22014-04-23 11:45:37 -07003585 void updateTaskMovement(TaskRecord task, boolean toFront) {
3586 if (task.isPersistable) {
3587 task.mLastTimeMoved = System.currentTimeMillis();
3588 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3589 // recently will be most negative, tasks sent to the bottom before that will be less
3590 // negative. Similarly for recent tasks moved to the top which will be most positive.
3591 if (!toFront) {
3592 task.mLastTimeMoved *= -1;
3593 }
3594 }
3595 }
3596
Craig Mautner84984fa2014-06-19 11:19:20 -07003597 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003598 final int top = mTaskHistory.size() - 1;
3599 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3600 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003601 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003602 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3603 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003604 mTaskHistory.remove(taskNdx);
3605 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003606 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003607 return;
3608 }
3609 }
3610 }
3611
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003612 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003613 String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003614 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615
Craig Mautner11bf9a52013-02-19 14:08:51 -08003616 final int numTasks = mTaskHistory.size();
3617 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003618 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003619 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003620 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003621 ActivityOptions.abort(options);
3622 } else {
3623 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3624 }
3625 return;
3626 }
3627
3628 // Shift all activities with this task up to the top
3629 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003630 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003631
3632 // Set focus to the top running activity of this stack.
3633 ActivityRecord r = topRunningActivityLocked(null);
3634 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003635
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003636 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003637 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003638 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003639 if (r != null) {
3640 mNoAnimActivities.add(r);
3641 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003642 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003643 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003644 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003645 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003646
Craig Mautner05d29032013-05-03 13:40:13 -07003647 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003648 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003649
3650 if (VALIDATE_TOKENS) {
3651 validateAppTokensLocked();
3652 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003653 }
3654
3655 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003656 * Worker method for rearranging history stack. Implements the function of moving all
3657 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003659 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003660 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3661 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003662 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003663 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 * @return Returns true if the move completed, false if not.
3665 */
Craig Mautner299f9602015-01-26 09:47:33 -08003666 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003667 final TaskRecord tr = taskForIdLocked(taskId);
3668 if (tr == null) {
3669 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3670 return false;
3671 }
3672
3673 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003674 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003675
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 // If we have a watcher, preflight the move before committing to it. First check
3677 // for *other* available tasks, but if none are available, then try again allowing the
3678 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003679 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003680 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003681 if (next == null) {
3682 next = topRunningActivityLocked(null, 0);
3683 }
3684 if (next != null) {
3685 // ask watcher if this is allowed
3686 boolean moveOK = true;
3687 try {
3688 moveOK = mService.mController.activityResuming(next.packageName);
3689 } catch (RemoteException e) {
3690 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003691 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 }
3693 if (!moveOK) {
3694 return false;
3695 }
3696 }
3697 }
3698
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003699 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003700
riddle_hsuc215a4f2014-12-27 12:10:45 +08003701 boolean prevIsHome = false;
3702 if (tr.isOverHomeStack()) {
3703 final TaskRecord nextTask = getNextTask(tr);
3704 if (nextTask != null) {
3705 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3706 } else {
3707 prevIsHome = true;
3708 }
3709 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003710 mTaskHistory.remove(tr);
3711 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003712 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713
Craig Mautnerc8143c62013-09-03 12:15:57 -07003714 // There is an assumption that moving a task to the back moves it behind the home activity.
3715 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003716 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003717 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3718 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003719 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003720 break;
3721 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003722 if (taskNdx == 1) {
3723 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003724 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003725 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003726 }
3727
Craig Mautner299f9602015-01-26 09:47:33 -08003728 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003729 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003730
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003732 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 }
3734
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003735 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003736 if (prevIsHome || task == tr && tr.isOverHomeStack()
3737 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003738 if (!mService.mBooting && !mService.mBooted) {
3739 // Not ready yet!
3740 return false;
3741 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003742 final int taskToReturnTo = tr.getTaskToReturnTo();
3743 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003744 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003745 }
3746
Craig Mautner05d29032013-05-03 13:40:13 -07003747 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003748 return true;
3749 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003750
Craig Mautner8849a5e2013-04-02 16:41:03 -07003751 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003753 final Uri data = r.intent.getData();
3754 final String strData = data != null ? data.toSafeString() : null;
3755
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003756 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003757 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003758 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003759 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003760 }
3761
3762 /**
3763 * Make sure the given activity matches the current configuration. Returns
3764 * false if the activity had to be destroyed. Returns true if the
3765 * configuration is the same, or the activity will remain running as-is
3766 * for whatever reason. Ensures the HistoryRecord is updated with the
3767 * correct configuration and all other bookkeeping is handled.
3768 */
3769 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3770 int globalChanges) {
3771 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003772 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 "Skipping config check (will change): " + r);
3774 return true;
3775 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003776
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003777 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003778 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003779
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003780 // Make sure the current stack override configuration is supported by the top task
3781 // before continuing.
3782 final TaskRecord topTask = topTask();
3783 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3784 || (!topTask.mResizeable && !mFullscreen))) {
3785 final boolean prevFullscreen = mFullscreen;
3786 final Configuration newOverrideConfig =
3787 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3788 updateOverrideConfiguration(newOverrideConfig);
3789 mForcedFullscreen = !prevFullscreen && mFullscreen;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003790 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003791 "Updated stack config to support task=" + topTask
3792 + " resizeable=" + topTask.mResizeable
3793 + " mForcedFullscreen=" + mForcedFullscreen
3794 + " prevFullscreen=" + prevFullscreen
3795 + " mFullscreen=" + mFullscreen);
3796 }
3797
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798 // Short circuit: if the two configurations are the exact same
3799 // object (the common case), then there is nothing to do.
3800 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003801 if (r.configuration == newConfig
3802 && r.stackConfigOverride == mOverrideConfig
3803 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003804 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003805 "Configuration unchanged in " + r);
3806 return true;
3807 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003808
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003809 // We don't worry about activities that are finishing.
3810 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003811 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003812 "Configuration doesn't matter in finishing " + r);
3813 r.stopFreezingScreenLocked(false);
3814 return true;
3815 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003816
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003817 // Okay we now are going to make this activity have the new config.
3818 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003819 final Configuration oldConfig = r.configuration;
3820 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003821 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003822 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003823
3824 // Determine what has changed. May be nothing, if this is a config
3825 // that has come back from the app after going idle. In that case
3826 // we just want to leave the official config object now in the
3827 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003828 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003829 if (stackChanges == 0) {
3830 // {@link Configuration#diff} doesn't catch changes from unset values.
3831 // Check for changes we care about.
3832 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3833 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3834 }
3835 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3836 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3837 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3838 }
3839 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3840 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3841 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003842 }
3843 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003844 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003845 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003846 "Configuration no differences in " + r);
3847 return true;
3848 }
3849
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003850 // If the activity isn't currently running, just leave the new
3851 // configuration and it will pick that up next time it starts.
3852 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003853 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003854 "Configuration doesn't matter not running " + r);
3855 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003856 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003857 return true;
3858 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003859
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003860 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003861 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
3862 "Checking to restart " + r.info.name + ": changed=0x"
3863 + Integer.toHexString(changes) + ", handles=0x"
3864 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
3865
Dianne Hackborne6676352011-06-01 16:51:20 -07003866 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003867 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3868 r.configChangeFlags |= changes;
3869 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003870 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003871 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003872 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003873 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003874 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003875 } else if (r.state == ActivityState.PAUSING) {
3876 // A little annoying: we are waiting for this activity to
3877 // finish pausing. Let's not do anything now, but just
3878 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003879 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003880 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003881 r.configDestroy = true;
3882 return true;
3883 } else if (r.state == ActivityState.RESUMED) {
3884 // Try to optimize this case: the configuration is changing
3885 // and we need to restart the top, resumed activity.
3886 // Instead of doing the normal handshaking, just say
3887 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003888 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003889 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003890 relaunchActivityLocked(r, r.configChangeFlags, true);
3891 r.configChangeFlags = 0;
3892 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003893 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003894 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003895 relaunchActivityLocked(r, r.configChangeFlags, false);
3896 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003897 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003898
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003899 // All done... tell the caller we weren't able to keep this
3900 // activity around.
3901 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003903
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003904 // Default case: the activity can handle this new configuration, so hand it over.
3905 // NOTE: We only forward the stack override configuration as the system level configuration
3906 // changes is always sent to all processes when they happen so it can just use whatever
3907 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003908 if (r.app != null && r.app.thread != null) {
3909 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003910 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003911 r.app.thread.scheduleActivityConfigurationChanged(
3912 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 } catch (RemoteException e) {
3914 // If process died, whatever.
3915 }
3916 }
3917 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003918
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 return true;
3920 }
3921
Craig Mautner2568c3a2015-03-26 14:22:34 -07003922 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003923 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003924 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003925 if (andResume) {
3926 results = r.results;
3927 newIntents = r.newIntents;
3928 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003929 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3930 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003931 + " andResume=" + andResume);
3932 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003933 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003934 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003935
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003936 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003937
Craig Mautner34b73df2014-01-12 21:11:08 -08003938 mStackSupervisor.removeChildActivityContainers(r);
3939
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003940 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003941 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
3942 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003943 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003944 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3945 !andResume, new Configuration(mService.mConfiguration),
3946 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003947 // Note: don't need to call pauseIfSleepingLocked() here, because
3948 // the caller will only pass in 'andResume' if this activity is
3949 // currently resumed, which implies we aren't sleeping.
3950 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003951 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952 }
3953
3954 if (andResume) {
3955 r.results = null;
3956 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003957 r.state = ActivityState.RESUMED;
3958 } else {
3959 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3960 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003961 }
3962
3963 return true;
3964 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003965
3966 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003967 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3968 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3969 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3970 final ActivityRecord r = activities.get(activityNdx);
3971 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003972 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003973 }
3974 if (r.fullscreen && !r.finishing) {
3975 return false;
3976 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003977 }
3978 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003979 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08003980 if (r == null) {
3981 return false;
3982 }
3983 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3984 + " would have returned true for r=" + r);
3985 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003986 }
3987
3988 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003989 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3990 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3991 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3992 final ActivityRecord r = activities.get(activityNdx);
3993 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003994 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003995 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003996 }
3997 }
3998 }
3999
4000 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
4001 boolean didSomething = false;
4002 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004003 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004004 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4005 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4006 int numActivities = activities.size();
4007 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4008 ActivityRecord r = activities.get(activityNdx);
4009 final boolean samePackage = r.packageName.equals(name)
4010 || (name == null && r.userId == userId);
4011 if ((userId == UserHandle.USER_ALL || r.userId == userId)
4012 && (samePackage || r.task == lastTask)
4013 && (r.app == null || evenPersistent || !r.app.persistent)) {
4014 if (!doit) {
4015 if (r.finishing) {
4016 // If this activity is just finishing, then it is not
4017 // interesting as far as something to stop.
4018 continue;
4019 }
4020 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004021 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004022 if (r.isHomeActivity()) {
4023 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4024 Slog.i(TAG, "Skip force-stop again " + r);
4025 continue;
4026 } else {
4027 homeActivity = r.realActivity;
4028 }
4029 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004030 didSomething = true;
4031 Slog.i(TAG, " Force finishing activity " + r);
4032 if (samePackage) {
4033 if (r.app != null) {
4034 r.app.removed = true;
4035 }
4036 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004037 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004038 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004039 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4040 true)) {
4041 // r has been deleted from mActivities, accommodate.
4042 --numActivities;
4043 --activityNdx;
4044 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004045 }
4046 }
4047 }
4048 return didSomething;
4049 }
4050
Dianne Hackborn09233282014-04-30 11:33:59 -07004051 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004052 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4053 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004054 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004055 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004056 if (task.getTopActivity() == null) {
4057 continue;
4058 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004059 ActivityRecord r = null;
4060 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004061 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004062 int numActivities = 0;
4063 int numRunning = 0;
4064 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004065 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004066 continue;
4067 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004068 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004069 tmp = activities.get(activityNdx);
4070 if (tmp.finishing) {
4071 continue;
4072 }
4073 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004074
Craig Mautneraab647e2013-02-28 16:31:36 -08004075 // Initialize state for next task if needed.
4076 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4077 top = r;
4078 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004079 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004080
4081 // Add 'r' into the current task.
4082 numActivities++;
4083 if (r.app != null && r.app.thread != null) {
4084 numRunning++;
4085 }
4086
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004087 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004088 TAG, r.intent.getComponent().flattenToShortString()
4089 + ": task=" + r.task);
4090 }
4091
4092 RunningTaskInfo ci = new RunningTaskInfo();
4093 ci.id = task.taskId;
4094 ci.baseActivity = r.intent.getComponent();
4095 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004096 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004097 if (focusedStack && topTask) {
4098 // Give the latest time to ensure foreground task can be sorted
4099 // at the first, because lastActiveTime of creating task is 0.
4100 ci.lastActiveTime = System.currentTimeMillis();
4101 topTask = false;
4102 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004103
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004104 if (top.task != null) {
4105 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004106 }
4107 ci.numActivities = numActivities;
4108 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004109 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004110 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004111 }
4112
4113 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004114 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004115 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004116 if (top >= 0) {
4117 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4118 int activityTop = activities.size() - 1;
4119 if (activityTop > 0) {
4120 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4121 "unhandled-back", true);
4122 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004123 }
4124 }
4125
Craig Mautner6b74cb52013-09-27 17:02:21 -07004126 /**
4127 * Reset local parameters because an app's activity died.
4128 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004129 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004130 */
4131 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004132 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004133 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004134 "App died while pausing: " + mPausingActivity);
4135 mPausingActivity = null;
4136 }
4137 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4138 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004139 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004140 }
4141
Craig Mautner19091252013-10-05 00:03:53 -07004142 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004143 }
4144
Craig Mautnercae015f2013-02-08 14:31:27 -08004145 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004146 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4147 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4148 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4149 final ActivityRecord r = activities.get(activityNdx);
4150 if (r.app == app) {
4151 Slog.w(TAG, " Force finishing activity "
4152 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004153 // Force the destroy to skip right to removal.
4154 r.app = null;
4155 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004156 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004157 }
4158 }
4159 }
4160
Dianne Hackborn390517b2013-05-30 15:03:32 -07004161 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004162 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004163 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004164 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4165 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004166 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4167 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004168 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004169 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004170 if (printed) {
4171 header = null;
4172 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004173 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004174 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004175 }
4176
4177 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4178 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4179
4180 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004181 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4182 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004183 }
4184 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004185 final int top = mTaskHistory.size() - 1;
4186 if (top >= 0) {
4187 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4188 int listTop = list.size() - 1;
4189 if (listTop >= 0) {
4190 activities.add(list.get(listTop));
4191 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004192 }
4193 } else {
4194 ItemMatcher matcher = new ItemMatcher();
4195 matcher.build(name);
4196
Craig Mautneraab647e2013-02-28 16:31:36 -08004197 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4198 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4199 if (matcher.match(r1, r1.intent.getComponent())) {
4200 activities.add(r1);
4201 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004202 }
4203 }
4204 }
4205
4206 return activities;
4207 }
4208
4209 ActivityRecord restartPackage(String packageName) {
4210 ActivityRecord starting = topRunningActivityLocked(null);
4211
4212 // All activities that came from the package must be
4213 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004214 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4215 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4216 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4217 final ActivityRecord a = activities.get(activityNdx);
4218 if (a.info.packageName.equals(packageName)) {
4219 a.forceNewConfig = true;
4220 if (starting != null && a == starting && a.visible) {
4221 a.startFreezingScreenLocked(starting.app,
4222 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4223 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004224 }
4225 }
4226 }
4227
4228 return starting;
4229 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004230
Craig Mautner299f9602015-01-26 09:47:33 -08004231 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07004232 removeTask(task, reason, true /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004233 }
4234
Wale Ogunwale000957c2015-04-03 08:19:12 -07004235 /**
4236 * Removes the input task from this stack.
4237 * @param task to remove.
4238 * @param reason for removal.
4239 * @param notMoving task to another stack. In the case we are moving we don't want to perform
4240 * some operations on the task like removing it from window manager or recents.
4241 */
4242 void removeTask(TaskRecord task, String reason, boolean notMoving) {
4243 if (notMoving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004244 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004245 mWindowManager.removeTask(task.taskId);
4246 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004247
Craig Mautner04a0ea62014-01-13 12:51:26 -08004248 final ActivityRecord r = mResumedActivity;
4249 if (r != null && r.task == task) {
4250 mResumedActivity = null;
4251 }
4252
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004253 final int taskNdx = mTaskHistory.indexOf(task);
4254 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004255 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4256 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4257 if (!nextTask.isOverHomeStack()) {
4258 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4259 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004260 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004261 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004262 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004263
Wale Ogunwale000957c2015-04-03 08:19:12 -07004264 if (notMoving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004265 final boolean isVoiceSession = task.voiceSession != null;
4266 if (isVoiceSession) {
4267 try {
4268 task.voiceSession.taskFinished(task.intent, task.taskId);
4269 } catch (RemoteException e) {
4270 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004271 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004272 if (task.autoRemoveFromRecents() || isVoiceSession) {
4273 // Task creator asked to remove this when done, or this task was a voice
4274 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004275 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004276 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004277 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004278 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004279
4280 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004281 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004282 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004283 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004284 String myReason = reason + " leftTaskHistoryEmpty";
4285 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4286 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4287 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004288 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004289 if (mStacks != null) {
4290 mStacks.remove(this);
4291 mStacks.add(0, this);
4292 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004293 if (notHomeStack) {
4294 mActivityContainer.onTaskListEmptyLocked();
4295 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004296 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004297
4298 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004299 }
4300
Dianne Hackborn91097de2014-04-04 18:02:06 -07004301 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4302 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4303 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004304 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4305 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004306 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004307 return task;
4308 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004309
4310 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004311 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004312 }
4313
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004314 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004315 task.stack = this;
4316 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004317 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004318 } else {
4319 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004320 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004321 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004322 if (!moving && task.voiceSession != null) {
4323 try {
4324 task.voiceSession.taskStarted(task.intent, task.taskId);
4325 } catch (RemoteException e) {
4326 }
4327 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004328 }
4329
4330 public int getStackId() {
4331 return mStackId;
4332 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004333
4334 @Override
4335 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004336 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4337 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004338 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004339
4340 boolean updateOverrideConfiguration(Configuration newConfig) {
4341 Configuration oldConfig = mOverrideConfig;
4342 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004343 // We override the configuration only when the stack's dimensions are different from
4344 // the display. In this manner, we know that if the override configuration is empty,
4345 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004346 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004347 return !mOverrideConfig.equals(oldConfig);
4348 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004349
4350 void onLockTaskPackagesUpdatedLocked() {
4351 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4352 mTaskHistory.get(taskNdx).setLockTaskAuth();
4353 }
4354 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004355}