blob: c4f363406eb977a165395256bfd2a07d434eeee7 [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 Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070024import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070025
Wale Ogunwalee23149f2015-03-06 15:39:44 -080026import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070027
Craig Mautner84984fa2014-06-19 11:19:20 -070028import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070030
Wale Ogunwale040b4702015-08-06 18:10:50 -070031import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070032import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070033
Winsonc809cbb2015-11-02 12:06:15 -080034import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070035import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070036import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070037
Dianne Hackborn91097de2014-04-04 18:02:06 -070038import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080039import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070050import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070075import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080086import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070087import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
89/**
90 * State and management of a single stack of activities.
91 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070092final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Wale Ogunwalee23149f2015-03-06 15:39:44 -080094 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070095 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
96 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080097 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070098 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070099 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700100 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700101 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700102 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700103 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
104 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700105 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700106 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700107 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
108 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
109 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
110 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
111 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700112
113 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800114
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700115 // Ticks during which we check progress while waiting for an app to launch.
116 static final int LAUNCH_TICK = 500;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we wait until giving up on the last activity to pause. This
119 // is short because it directly impacts the responsiveness of starting the
120 // next activity.
121 static final int PAUSE_TIMEOUT = 500;
122
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700123 // How long we wait for the activity to tell us it has stopped before
124 // giving up. This is a good amount of time because we really need this
125 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700126 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long we wait until giving up on an activity telling us it has
129 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700130 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800133 // disabled.
134 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800135
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700136 // How long between activity launches that we consider safe to not warn
137 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700138 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 // Set to false to disable the preview that is shown while a new activity
141 // is being started.
142 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Craig Mautner5eda9b32013-07-02 11:58:16 -0700144 // How long to wait for all background Activities to redraw following a call to
145 // convertToTranslucent().
146 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 enum ActivityState {
149 INITIALIZING,
150 RESUMED,
151 PAUSING,
152 PAUSED,
153 STOPPING,
154 STOPPED,
155 FINISHING,
156 DESTROYING,
157 DESTROYED
158 }
159
160 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700161 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800162 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 /**
165 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800166 * running) activities. It contains #TaskRecord objects.
167 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800168 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169
170 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800171 * Used for validating app tokens with window manager.
172 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800173 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 /**
176 * List of running activities, sorted by recent usage.
177 * The first entry in the list is the least recently used.
178 * It contains HistoryRecord objects.
179 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800180 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181
182 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183 * Animations that for the current transition have requested not to
184 * be considered for the transition animation.
185 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800186 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * When we are in the process of pausing an activity, before starting the
190 * next one, this variable holds the activity that is currently being paused.
191 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800192 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193
194 /**
195 * This is the last activity that we put into the paused state. This is
196 * used to determine if we need to do an activity transition while sleeping,
197 * when we normally hold the top activity paused.
198 */
199 ActivityRecord mLastPausedActivity = null;
200
201 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700202 * Activities that specify No History must be removed once the user navigates away from them.
203 * If the device goes to sleep with such an activity in the paused state then we save it here
204 * and finish it later if another activity replaces it on wakeup.
205 */
206 ActivityRecord mLastNoHistoryActivity = null;
207
208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Current activity that is resumed, or null if there is none.
210 */
211 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700213 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 * This is the last activity that has been started. It is only used to
215 * identify when multiple activities are started at once so that the user
216 * can be warned they may not be in the activity they think they are.
217 */
218 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800219
Craig Mautner5eda9b32013-07-02 11:58:16 -0700220 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
221 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
222 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
223 // Activity in mTranslucentActivityWaiting is notified via
224 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
225 // background activity being drawn then the same call will be made with a true value.
226 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700228
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700229 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 * Set when we know we are going to be calling updateConfiguration()
231 * soon, so want to skip intermediate config checks.
232 */
233 boolean mConfigWillChange;
234
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700235 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800236 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700237 // Current bounds of the stack or null if fullscreen.
238 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800239
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700240 long mLaunchStartTime = 0;
241 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800242
Craig Mautner858d8a62013-04-23 17:08:34 -0700243 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700244
Craig Mautnerc00204b2013-03-05 15:02:14 -0800245 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800246 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800247 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
248 ArrayList<ActivityStack> mStacks;
249 /** The attached Display's unique identifier, or -1 if detached */
250 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800251
Craig Mautner27084302013-03-25 08:05:25 -0700252 /** Run all ActivityStacks through this */
253 final ActivityStackSupervisor mStackSupervisor;
254
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700255 private final LaunchingTaskPositioner mTaskPositioner;
256
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700257 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700258 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
259 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
260 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
261 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700262 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700263 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700264 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700265
266 static class ScheduleDestroyArgs {
267 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700268 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700269 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700270 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700271 mReason = reason;
272 }
273 }
274
Zoran Marcetaf958b322012-08-09 20:27:12 +0900275 final Handler mHandler;
276
277 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800278
Craig Mautnerc8143c62013-09-03 12:15:57 -0700279 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900280 super(looper);
281 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282
Zoran Marcetaf958b322012-08-09 20:27:12 +0900283 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700284 public void handleMessage(Message msg) {
285 switch (msg.what) {
286 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800287 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 // We don't at this point know if the activity is fullscreen,
289 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700291 synchronized (mService) {
292 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700293 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700295 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700298 case LAUNCH_TICK_MSG: {
299 ActivityRecord r = (ActivityRecord)msg.obj;
300 synchronized (mService) {
301 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700302 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700303 }
304 }
305 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800307 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800311 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800312 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800313 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700314 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700315 case STOP_TIMEOUT_MSG: {
316 ActivityRecord r = (ActivityRecord)msg.obj;
317 // We don't at this point know if the activity is fullscreen,
318 // so we need to be conservative and assume it isn't.
319 Slog.w(TAG, "Activity stop timeout for " + r);
320 synchronized (mService) {
321 if (r.isInHistory()) {
322 activityStoppedLocked(r, null, null, null);
323 }
324 }
325 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700326 case DESTROY_ACTIVITIES_MSG: {
327 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
328 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700329 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700330 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700331 } break;
332 case TRANSLUCENT_TIMEOUT_MSG: {
333 synchronized (mService) {
334 notifyActivityDrawnLocked(null);
335 }
336 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700337 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700338 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700339 final ActivityRecord r = getVisibleBehindActivity();
340 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700341 if (r != null) {
342 mService.killAppAtUsersRequest(r.app, null);
343 }
344 }
345 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700346 }
347 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800348 }
349
Craig Mautner34b73df2014-01-12 21:11:08 -0800350 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800351 int count = 0;
352 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
353 count += mTaskHistory.get(taskNdx).mActivities.size();
354 }
355 return count;
356 }
357
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
359 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800360 mActivityContainer = activityContainer;
361 mStackSupervisor = activityContainer.getOuter();
362 mService = mStackSupervisor.mService;
363 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
364 mWindowManager = mService.mWindowManager;
365 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700366 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800367 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700368 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
369 ? new LaunchingTaskPositioner() : null;
370 }
371
372 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
373 mDisplayId = activityDisplay.mDisplayId;
374 mStacks = activityDisplay.mStacks;
375 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
376 mFullscreen = mBounds == null;
377 if (mTaskPositioner != null) {
378 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
379 mTaskPositioner.configure(mBounds);
380 }
381 }
382
383 void detachDisplay() {
384 mDisplayId = Display.INVALID_DISPLAY;
385 mStacks = null;
386 if (mTaskPositioner != null) {
387 mTaskPositioner.reset();
388 }
389 mWindowManager.detachStack(mStackId);
390 }
391
Winsonc809cbb2015-11-02 12:06:15 -0800392 public void getDisplaySize(Point out) {
393 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
394 }
395
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700396 void setBounds(Rect bounds) {
397 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700398 if (mTaskPositioner != null) {
399 mTaskPositioner.configure(bounds);
400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 }
Craig Mautner5962b122012-10-05 14:45:52 -0700402
Amith Yamasani734983f2014-03-04 16:48:05 -0800403 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700404 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700405 }
406
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700407 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700409 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700410 if (r != null) {
411 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800412 }
413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 return null;
415 }
416
417 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800420 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800421 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
422 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800423 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 return r;
425 }
426 }
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 return null;
429 }
430
431 /**
432 * This is a simplified version of topRunningActivityLocked that provides a number of
433 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800434 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 * @param token If non-null, any history records matching this token will be skipped.
436 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800437 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 * @return Returns the HistoryRecord of the next activity on the stack.
439 */
440 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 TaskRecord task = mTaskHistory.get(taskNdx);
443 if (task.taskId == taskId) {
444 continue;
445 }
446 ArrayList<ActivityRecord> activities = task.mActivities;
447 for (int i = activities.size() - 1; i >= 0; --i) {
448 final ActivityRecord r = activities.get(i);
449 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
Craig Mautner8849a5e2013-04-02 16:41:03 -0700458 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
460 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700461 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
462 final ActivityRecord r = activities.get(activityNdx);
463 if (!r.finishing) {
464 return r;
465 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700466 }
467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700468 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700469 }
470
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700471 final TaskRecord topTask() {
472 final int size = mTaskHistory.size();
473 if (size > 0) {
474 return mTaskHistory.get(size - 1);
475 }
476 return null;
477 }
478
Craig Mautnerd2328952013-03-05 12:46:26 -0800479 TaskRecord taskForIdLocked(int id) {
480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
481 final TaskRecord task = mTaskHistory.get(taskNdx);
482 if (task.taskId == id) {
483 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800484 }
485 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700486 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700487 }
488
Craig Mautnerd2328952013-03-05 12:46:26 -0800489 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700490 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800491 return isInStackLocked(r);
492 }
493
494 ActivityRecord isInStackLocked(ActivityRecord r) {
495 if (r == null) {
496 return null;
497 }
498 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700499 if (task != null && task.stack != null
500 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800501 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800502 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800503 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800504 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800505 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800506 }
507
Craig Mautner2420ead2013-04-01 17:13:20 -0700508 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700509 final boolean hadit = mLRUActivities.remove(r);
510 mLRUActivities.add(r);
511 return hadit;
512 }
513
Craig Mautnerde4ef022013-04-07 19:01:33 -0700514 final boolean isHomeStack() {
515 return mStackId == HOME_STACK_ID;
516 }
517
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 final boolean isOnHomeDisplay() {
519 return isAttached() &&
520 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
521 }
522
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700523 void moveToFront(String reason) {
524 moveToFront(reason, null);
525 }
526
527 /**
528 * @param reason The reason for moving the stack to the front.
529 * @param task If non-null, the task will be moved to the top of the stack.
530 * */
531 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700532 if (!isAttached()) {
533 return;
534 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700535
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700536 mStacks.remove(this);
537 int addIndex = mStacks.size();
538
539 if (addIndex > 0) {
540 final ActivityStack topStack = mStacks.get(addIndex - 1);
541 if (topStack.mStackId == PINNED_STACK_ID && topStack != this) {
542 // The pinned stack is always the top most stack (always-on-top).
543 // So, stack is moved just below the pinned stack.
544 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800545 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700546 }
547
548 mStacks.add(addIndex, this);
549
550 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
551 if (isOnHomeDisplay()) {
552 mStackSupervisor.setFocusStack(reason, this);
553 }
554 if (task != null) {
555 insertTaskAtTop(task, null);
556 } else {
557 task = topTask();
558 }
559 if (task != null) {
560 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800562 }
563
564 final boolean isAttached() {
565 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800566 }
567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700568 /**
569 * Returns the top activity in any existing task matching the given
570 * Intent. Returns null if no such task is found.
571 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700572 ActivityRecord findTaskLocked(ActivityRecord target) {
573 Intent intent = target.intent;
574 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 ComponentName cls = intent.getComponent();
576 if (info.targetActivity != null) {
577 cls = new ComponentName(info.packageName, info.targetActivity);
578 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700579 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700580 boolean isDocument = intent != null & intent.isDocument();
581 // If documentData is non-null then it must match the existing task data.
582 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800583
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700584 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800585 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
586 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700587 if (task.voiceSession != null) {
588 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700590 continue;
591 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700592 if (task.userId != userId) {
593 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700594 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700595 continue;
596 }
Craig Mautner000f0022013-02-26 15:04:29 -0800597 final ActivityRecord r = task.getTopActivity();
598 if (r == null || r.finishing || r.userId != userId ||
599 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700600 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800601 continue;
602 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700603 if (r.mActivityType != target.mActivityType) {
604 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
605 continue;
606 }
Craig Mautner000f0022013-02-26 15:04:29 -0800607
Craig Mautnerd00f4742014-03-12 14:17:26 -0700608 final Intent taskIntent = task.intent;
609 final Intent affinityIntent = task.affinityIntent;
610 final boolean taskIsDocument;
611 final Uri taskDocumentData;
612 if (taskIntent != null && taskIntent.isDocument()) {
613 taskIsDocument = true;
614 taskDocumentData = taskIntent.getData();
615 } else if (affinityIntent != null && affinityIntent.isDocument()) {
616 taskIsDocument = true;
617 taskDocumentData = affinityIntent.getData();
618 } else {
619 taskIsDocument = false;
620 taskDocumentData = null;
621 }
622
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700623 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700624 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700625 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700626 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700627 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
628 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700629 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700630 return r;
631 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700632 } else if (taskIntent != null && taskIntent.getComponent() != null &&
633 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700634 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700635 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800636 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700637 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
638 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800639 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700640 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
641 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700642 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700643 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800644 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
646 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800647 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700648 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700649 }
650
651 return null;
652 }
653
654 /**
655 * Returns the first activity (starting from the top of the stack) that
656 * is the same as the given activity. Returns null if no such activity
657 * is found.
658 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700659 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700660 ComponentName cls = intent.getComponent();
661 if (info.targetActivity != null) {
662 cls = new ComponentName(info.packageName, info.targetActivity);
663 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700664 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700665
Craig Mautner000f0022013-02-26 15:04:29 -0800666 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700667 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700668 final boolean notCurrentUserTask =
669 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700670 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700671
Craig Mautner000f0022013-02-26 15:04:29 -0800672 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
673 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700674 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700675 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700676 }
Craig Mautner000f0022013-02-26 15:04:29 -0800677 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700678 return r;
679 }
680 }
681 }
682
683 return null;
684 }
685
Amith Yamasani742a6712011-05-04 14:49:28 -0700686 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700687 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700688 */
Craig Mautner93529a42013-10-04 15:03:13 -0700689 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800690 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700691 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800692 }
693 mCurrentUser = userId;
694
695 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800696 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800697 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700698 final TaskRecord task = mTaskHistory.get(i);
699
700 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
701 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700702 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700703 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700704 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700705 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800706 mTaskHistory.remove(i);
707 mTaskHistory.add(task);
708 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800709 // Use same value for i.
710 } else {
711 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800712 }
713 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700714 if (VALIDATE_TOKENS) {
715 validateAppTokensLocked();
716 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700717 }
718
Craig Mautner2420ead2013-04-01 17:13:20 -0700719 void minimalResumeActivityLocked(ActivityRecord r) {
720 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700721 if (DEBUG_STATES) Slog.v(TAG_STATES,
722 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700723 r.stopped = false;
724 mResumedActivity = r;
725 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800726 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700727 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700728 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700729 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700730 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
731 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700732 }
733
Chong Zhang45c25ce2015-08-10 22:18:26 -0700734 private void addRecentActivityLocked(ActivityRecord r) {
735 if (r != null) {
736 mRecentTasks.addLocked(r.task);
737 r.task.touchActiveTime();
738 }
739 }
740
Narayan Kamath7829c812015-06-08 17:39:43 +0100741 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700742 if (mFullyDrawnStartTime != 0) {
743 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
744 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100745 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700746 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
747 }
748
749 private void stopFullyDrawnTraceIfNeeded() {
750 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
751 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
752 mFullyDrawnStartTime = 0;
753 }
754 }
755
Craig Mautnere79d42682013-04-01 19:01:53 -0700756 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700757 if (r.displayStartTime == 0) {
758 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
759 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100760 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700761 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700763 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100764 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700765 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700767 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800768
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700769 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700770 // Make sure that there is no activity waiting for this to launch.
771 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
772 r.displayStartTime = r.fullyDrawnStartTime = 0;
773 } else {
774 mStackSupervisor.removeTimeoutsForActivityLocked(r);
775 mStackSupervisor.scheduleIdleTimeoutLocked(r);
776 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700777 }
778
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800779 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800780 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800781 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
782 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
783 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
784 activities.get(activityNdx).setSleeping(false);
785 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800786 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800787 if (mPausingActivity != null) {
788 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
789 activityPausedLocked(mPausingActivity.appToken, true);
790 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800791 }
792
Craig Mautner0eea92c2013-05-16 13:35:39 -0700793 /**
794 * @return true if something must be done before going to sleep.
795 */
796 boolean checkReadyForSleepLocked() {
797 if (mResumedActivity != null) {
798 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700799 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
800 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
801 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700802 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700803 return true;
804 }
805 if (mPausingActivity != null) {
806 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700807 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700808 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800809 }
810
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700811 if (hasVisibleBehindActivity()) {
812 // Stop visible behind activity before going to sleep.
813 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
814 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700815 if (DEBUG_STATES) Slog.v(TAG_STATES,
816 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700817 return true;
818 }
819
Craig Mautner0eea92c2013-05-16 13:35:39 -0700820 return false;
821 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800822
Craig Mautner0eea92c2013-05-16 13:35:39 -0700823 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700824 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800825
Craig Mautner0eea92c2013-05-16 13:35:39 -0700826 // Make sure any stopped but visible activities are now sleeping.
827 // This ensures that the activity's onStop() is called.
828 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
829 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
830 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
831 final ActivityRecord r = activities.get(activityNdx);
832 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
833 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800834 }
835 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800836 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 }
Craig Mautner59c00972012-07-30 12:10:24 -0700838
Winson8b1871d2015-11-20 09:56:20 -0800839 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
840 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800841 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700842 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800843 return null;
844 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800845
Winson Chung083baf92014-07-11 10:32:42 -0700846 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700847 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800848 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700849 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700850 return null;
851 }
852
Winson Chung48a10a52014-08-27 14:36:51 -0700853 int w = mService.mThumbnailWidth;
854 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800855 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800856 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700857 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800858
859 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800860 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
861 // use within SystemUI while keeping memory usage low.
Winson8b1871d2015-11-20 09:56:20 -0800862 if (mService.mTakeFullscreenScreenshots) {
Winson8b1871d2015-11-20 09:56:20 -0800863 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800864 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800865 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800866 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800867 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800868 }
Winson Chung376543b2014-05-21 17:43:28 -0700869 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800870 return null;
871 }
872
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700873 /**
874 * Start pausing the currently resumed activity. It is an error to call this if there
875 * is already an activity being paused or there is no resumed activity.
876 *
877 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
878 * @param uiSleeping True if this is happening with the user interface going to sleep (the
879 * screen turning off).
880 * @param resuming True if this is being called as part of resuming the top activity, so
881 * we shouldn't try to instigate a resume here.
882 * @param dontWait True if the caller does not want to wait for the pause to complete. If
883 * set to true, we will immediately complete the pause here before returning.
884 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
885 * it to tell us when it is done.
886 */
887 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
888 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800889 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800890 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
891 + " state=" + mPausingActivity.state);
892 if (!mService.isSleeping()) {
893 // Avoid recursion among check for sleep and complete pause during sleeping.
894 // Because activity will be paused immediately after resume, just let pause
895 // be completed by the order of activity paused from clients.
896 completePauseLocked(false);
897 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800898 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700899 ActivityRecord prev = mResumedActivity;
900 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700901 if (!resuming) {
902 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
903 mStackSupervisor.resumeTopActivitiesLocked();
904 }
905 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800907
908 if (mActivityContainer.mParentActivity == null) {
909 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700910 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800911 }
912
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700913 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700914 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800916 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700918 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
919 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 prev.state = ActivityState.PAUSING;
921 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700922 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700923 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700924 if (mService.mHasRecents
925 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson8b1871d2015-11-20 09:56:20 -0800926 prev.updateThumbnailLocked(screenshotActivitiesLocked(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700927 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700928 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700929
930 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800931
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700933 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700934 try {
935 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700936 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700938 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800939 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700940 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 } catch (Exception e) {
942 // Ignore exception, if process died other code will cleanup.
943 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800944 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700945 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700946 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947 }
948 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800949 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700951 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952 }
953
954 // If we are not going to sleep, we want to ensure the device is
955 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700956 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700957 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700958 }
959
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800960 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700961 // Have the window manager pause its key dispatching until the new
962 // activity has started. If we're pausing the activity just because
963 // the screen is being turned off and the UI is sleeping, don't interrupt
964 // key dispatch; the same activity will pick it up again on wakeup.
965 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800966 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700967 } else if (DEBUG_PAUSE) {
968 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700969 }
970
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700971 if (dontWait) {
972 // If the caller said they don't want to wait for the pause, then complete
973 // the pause now.
974 completePauseLocked(false);
975 return false;
976
977 } else {
978 // Schedule a pause timeout in case the app doesn't respond.
979 // We don't give it much time because this directly impacts the
980 // responsiveness seen by the user.
981 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
982 msg.obj = prev;
983 prev.pauseTime = SystemClock.uptimeMillis();
984 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700985 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700986 return true;
987 }
988
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 } else {
990 // This activity failed to schedule the
991 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700992 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700993 if (!resuming) {
994 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
995 }
996 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700997 }
998 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700999
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001000 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001001 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1002 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001003
Craig Mautnerd2328952013-03-05 12:46:26 -08001004 final ActivityRecord r = isInStackLocked(token);
1005 if (r != null) {
1006 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1007 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001008 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001009 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001010 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001011 } else {
1012 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1013 r.userId, System.identityHashCode(r), r.shortComponentName,
1014 mPausingActivity != null
1015 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001016 if (r.state == ActivityState.PAUSING) {
1017 r.state = ActivityState.PAUSED;
1018 if (r.finishing) {
1019 if (DEBUG_PAUSE) Slog.v(TAG,
1020 "Executing finish of failed to pause activity: " + r);
1021 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1022 }
louis_chang047dfd42015-04-08 16:35:55 +08001023 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024 }
1025 }
1026 }
1027
Craig Mautnera0026042014-04-23 11:45:37 -07001028 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1029 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001030 if (r.state != ActivityState.STOPPING) {
1031 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1032 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1033 return;
1034 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001035 if (persistentState != null) {
1036 r.persistentState = persistentState;
1037 mService.notifyTaskPersisterLocked(r.task, false);
1038 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001039 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001040 if (icicle != null) {
1041 // If icicle is null, this is happening due to a timeout, so we
1042 // haven't really saved the state.
1043 r.icicle = icicle;
1044 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001045 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001046 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001047 }
1048 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001049 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001050 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1051 r.stopped = true;
1052 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001053 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1054 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001055 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001056 if (r.finishing) {
1057 r.clearOptionsLocked();
1058 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001059 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001060 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001061 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001062 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001063 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001064 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001065 }
1066 }
1067 }
1068
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001069 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001070 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001071 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001072
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001073 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001074 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001075 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001076 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001077 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001078 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001080 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001081 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1082 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001083 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001084 if (prev.configDestroy) {
1085 // The previous is being paused because the configuration
1086 // is changing, which means it is actually stopping...
1087 // To juggle the fact that we are also starting a new
1088 // instance right now, we need to first completely stop
1089 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001090 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001091 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001092 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001093 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001094 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001095 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001096 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1097 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001098 // If we already have a few activities waiting to stop,
1099 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001100 // them out. Or if r is the last of activity of the last task the stack
1101 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001102 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001103 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001104 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001105 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001106 }
1107 }
1108 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001109 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001110 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001111 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001112 // It is possible the activity was freezing the screen before it was paused.
1113 // In that case go ahead and remove the freeze this activity has on the screen
1114 // since it is no longer visible.
1115 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001116 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001117 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001118
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001119 if (resumeNext) {
1120 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1121 if (!mService.isSleepingOrShuttingDown()) {
1122 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1123 } else {
1124 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001125 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001126 if (top == null || (prev != null && top != prev)) {
1127 // If there are no more activities available to run,
1128 // do resume anyway to start something. Also if the top
1129 // activity on the stack is not the just paused activity,
1130 // we need to go ahead and resume it to ensure we complete
1131 // an in-flight app switch.
1132 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1133 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001134 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001136
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001137 if (prev != null) {
1138 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001139
Craig Mautner525f3d92013-05-07 14:01:50 -07001140 if (prev.app != null && prev.cpuTimeAtResume > 0
1141 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001142 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1143 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001144 if (diff > 0) {
1145 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1146 synchronized (bsi) {
1147 BatteryStatsImpl.Uid.Proc ps =
1148 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001149 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001150 if (ps != null) {
1151 ps.addForegroundTimeLocked(diff);
1152 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 }
1154 }
1155 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001156 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001157 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001158
1159 // Notfiy when the task stack has changed
1160 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001161 }
1162
1163 /**
1164 * Once we know that we have asked an application to put an activity in
1165 * the resumed state (either by launching it or explicitly telling it),
1166 * this function updates the rest of our state to match that fact.
1167 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001168 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001169 next.idle = false;
1170 next.results = null;
1171 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001172
Chong Zhang85ee6542015-10-02 13:36:38 -07001173 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001174 ProcessRecord app = next.task.mActivities.get(0).app;
1175 if (app != null && app != mService.mHomeProcess) {
1176 mService.mHomeProcess = app;
1177 }
1178 }
1179
Craig Mautner07566322013-09-26 16:42:55 -07001180 if (next.nowVisible) {
1181 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001182 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001183 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184
1185 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001186 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001188 mStackSupervisor.reportResumedActivityLocked(next);
1189
1190 next.resumeKeyDispatchingLocked();
1191 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001192
1193 // Mark the point when the activity is resuming
1194 // TODO: To be more accurate, the mark should be before the onCreate,
1195 // not after the onResume. But for subsequent starts, onResume is fine.
1196 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001197 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198 } else {
1199 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1200 }
Winson Chung376543b2014-05-21 17:43:28 -07001201
George Mount6ba042b2014-07-28 11:12:28 -07001202 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001203
1204 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1205 // When resuming an activity, require it to call requestVisibleBehind() again.
1206 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1207 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 }
1209
Craig Mautner2568c3a2015-03-26 14:22:34 -07001210 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001211 r.visible = visible;
1212 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001213 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001214 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001215 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001216 container.setVisible(visible);
1217 }
1218 }
1219
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001220 // Find the first visible activity above the passed activity and if it is translucent return it
1221 // otherwise return null;
1222 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1223 TaskRecord task = r.task;
1224 if (task == null) {
1225 return null;
1226 }
1227
1228 ActivityStack stack = task.stack;
1229 if (stack == null) {
1230 return null;
1231 }
1232
1233 int stackNdx = mStacks.indexOf(stack);
1234
1235 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1236 int taskNdx = tasks.indexOf(task);
1237
1238 ArrayList<ActivityRecord> activities = task.mActivities;
1239 int activityNdx = activities.indexOf(r) + 1;
1240
1241 final int numStacks = mStacks.size();
1242 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001243 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001244 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001245 final int numTasks = tasks.size();
1246 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001247 final TaskRecord currentTask = tasks.get(taskNdx);
1248 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001249 final int numActivities = activities.size();
1250 while (activityNdx < numActivities) {
1251 final ActivityRecord activity = activities.get(activityNdx);
1252 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001253 return historyStack.mFullscreen
1254 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001255 }
1256 ++activityNdx;
1257 }
1258 activityNdx = 0;
1259 ++taskNdx;
1260 }
1261 taskNdx = 0;
1262 ++stackNdx;
1263 }
1264
1265 return null;
1266 }
1267
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001268 private ActivityStack getNextVisibleStackLocked() {
1269 ArrayList<ActivityStack> stacks = mStacks;
1270 final ActivityRecord parent = mActivityContainer.mParentActivity;
1271 if (parent != null) {
1272 stacks = parent.task.stack.mStacks;
1273 }
1274 if (stacks != null) {
1275 for (int i = stacks.size() - 1; i >= 0; --i) {
1276 ActivityStack stack = stacks.get(i);
1277 if (stack != this && stack.isStackVisibleLocked()) {
1278 return stack;
1279 }
1280 }
1281 }
1282 return null;
1283 }
1284
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001285 /** Returns true if the stack contains a fullscreen task. */
1286 private boolean hasFullscreenTask() {
1287 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1288 final TaskRecord task = mTaskHistory.get(i);
1289 if (task.mFullscreen) {
1290 return true;
1291 }
1292 }
1293 return false;
1294 }
1295
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001296 private boolean hasTranslucentActivity(ActivityStack stack) {
1297 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1298 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1299 final TaskRecord task = tasks.get(taskNdx);
1300 final ArrayList<ActivityRecord> activities = task.mActivities;
1301 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1302 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001303
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001304 // Conditions for an activity to obscure the stack we're
1305 // examining:
1306 // 1. Not Finishing AND Visible AND:
1307 // 2. Either:
1308 // - Full Screen Activity OR
1309 // - On top of Home and our stack is NOT home
1310 if (!r.finishing && r.visible && (r.fullscreen ||
1311 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1312 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001313 }
1314 }
1315 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001316 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001317 }
1318
1319 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001320 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001321 if (!isAttached()) {
1322 return false;
1323 }
1324
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001325 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001326 return true;
1327 }
1328
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001329 final int stackIndex = mStacks.indexOf(this);
1330
1331 if (stackIndex == mStacks.size() - 1) {
1332 Slog.wtf(TAG,
1333 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1334 return false;
1335 }
1336
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001337 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1338 final int focusedStackId = focusedStack.mStackId;
1339
1340 if (mStackId == DOCKED_STACK_ID) {
1341 // Docked stack is always visible, except in the case where the home activity
1342 // is the top running activity in the focused home stack.
1343 if (focusedStackId != HOME_STACK_ID) {
1344 return true;
1345 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001346 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001347 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1348 }
1349
Chong Zhangb16cf342015-11-12 17:22:40 -08001350 // Find the first stack below focused stack that actually got something visible.
1351 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1352 while (belowFocusedIndex >= 0 &&
1353 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1354 belowFocusedIndex--;
1355 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001356 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1357 && stackIndex == belowFocusedIndex) {
1358 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001359 return true;
1360 }
1361
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001362 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1363 && hasTranslucentActivity(focusedStack)) {
1364 // Stacks behind the fullscreen stack with a translucent activity are always
1365 // visible so they can act as a backdrop to the translucent activity.
1366 // For example, dialog activities
1367 if (stackIndex == belowFocusedIndex) {
1368 return true;
1369 }
1370 if (belowFocusedIndex >= 0) {
1371 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001372 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1373 && stackIndex == (belowFocusedIndex - 1)) {
1374 // The stack behind the docked or pinned stack is also visible so we can have a
1375 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001376 return true;
1377 }
1378 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001379 }
1380
Wale Ogunwale3797c222015-10-27 14:21:58 -07001381 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001382 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001383 return false;
1384 }
1385
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001386 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001387 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001388
1389 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1390 continue;
1391 }
1392
Wale Ogunwale3797c222015-10-27 14:21:58 -07001393 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001394 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001395 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001396 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001397
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001398 if (!hasTranslucentActivity(stack)) {
1399 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001400 }
1401 }
1402
1403 return true;
1404 }
1405
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001406 final int rankTaskLayers(int baseLayer) {
1407 int layer = 0;
1408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1409 final TaskRecord task = mTaskHistory.get(taskNdx);
1410 ActivityRecord r = task.topRunningActivityLocked();
1411 if (r == null || r.finishing || !r.visible) {
1412 task.mLayerRank = -1;
1413 } else {
1414 task.mLayerRank = baseLayer + layer++;
1415 }
1416 }
1417 return layer;
1418 }
1419
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001420 /**
1421 * Make sure that all activities that need to be visible (that is, they
1422 * currently can be seen by the user) actually are.
1423 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001424 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1425 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001426 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001427 if (top == null) {
1428 return;
1429 }
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001430 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001432 checkTranslucentActivityWaiting(top);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001433
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001434 // If the top activity is not fullscreen, then we need to
1435 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001436 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001437 final boolean stackInvisible = !isStackVisibleLocked();
1438 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001439 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001440
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001442 final TaskRecord task = mTaskHistory.get(taskNdx);
1443 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001444
Craig Mautnerd44711d2013-02-23 11:24:36 -08001445 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1446 final ActivityRecord r = activities.get(activityNdx);
1447 if (r.finishing) {
1448 continue;
1449 }
1450 if (aboveTop && r != top) {
1451 continue;
1452 }
1453 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001454 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1455 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001456 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001457 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1458 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001459 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001460 // First: if this is not the current activity being started, make
1461 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001462 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001463 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001465
1466 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001467 if (makeVisibleAndRestartIfNeeded(starting, configChanges, top,
1468 noStackActivityResumed, r)) {
1469 if (activityNdx >= activities.size()) {
1470 // Record may be removed if its process needs to restart.
1471 activityNdx = activities.size() - 1;
1472 } else {
1473 noStackActivityResumed = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001474 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001475 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001476 } else if (r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001477 if (alreadyVisible(r)) {
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001478 noStackActivityResumed = false;
1479 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001480 } else {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001481 becomeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001483 // Aggregate current change flags.
1484 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001485 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1486 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001487 } else {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001488 becomeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489 }
1490 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001491 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1492 // The visibility of tasks and the activities they contain in freeform stack are
1493 // determined individually unlike other stacks where the visibility or fullscreen
1494 // status of an activity in a previous task affects other.
1495 behindFullscreenActivity = stackInvisible;
1496 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001498
1499 if (mTranslucentActivityWaiting != null &&
1500 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1501 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1502 notifyActivityDrawnLocked(null);
1503 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 }
Craig Mautner58547802013-03-05 08:23:53 -08001505
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001506 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1507 if (mTranslucentActivityWaiting != top) {
1508 mUndrawnActivitiesBelowTopTranslucent.clear();
1509 if (mTranslucentActivityWaiting != null) {
1510 // Call the callback with a timeout indication.
1511 notifyActivityDrawnLocked(null);
1512 mTranslucentActivityWaiting = null;
1513 }
1514 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1515 }
1516 }
1517
1518 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
1519 ActivityRecord top, boolean noStackActivityResumed, ActivityRecord r) {
1520 // We need to make sure the app is running if it's the top, or it is just made visible from
1521 // invisible. If the app is already visible, it must have died while it was visible. In this
1522 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1523 // thrashing.
1524 if (r == top || !r.visible) {
1525 // This activity needs to be visible, but isn't even running...
1526 // get it started and resume if no other stack in this stack is resumed.
1527 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1528 if (r != starting) {
1529 r.startFreezingScreenLocked(r.app, configChanges);
1530 }
1531 if (!r.visible || r.mLaunchTaskBehind) {
1532 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1533 setVisible(r, true);
1534 }
1535 if (r != starting) {
1536 mStackSupervisor.startSpecificActivityLocked(r, noStackActivityResumed, false);
1537 return true;
1538 }
1539 }
1540 return false;
1541 }
1542
1543 private void becomeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
1544 ActivityRecord r) {
1545 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1546 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1547 + " behindFullscreenActivity=" + behindFullscreenActivity);
1548 if (!r.visible) {
1549 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1550 return;
1551 }
1552 // Now for any activities that aren't visible to the user, make sure they no longer are
1553 // keeping the screen frozen.
1554 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1555 try {
1556 setVisible(r, false);
1557 switch (r.state) {
1558 case STOPPING:
1559 case STOPPED:
1560 if (r.app != null && r.app.thread != null) {
1561 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1562 "Scheduling invisibility: " + r);
1563 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1564 }
1565 break;
1566
1567 case INITIALIZING:
1568 case RESUMED:
1569 case PAUSING:
1570 case PAUSED:
1571 // This case created for transitioning activities from
1572 // translucent to opaque {@link Activity#convertToOpaque}.
1573 if (getVisibleBehindActivity() == r) {
1574 releaseBackgroundResources(r);
1575 } else {
1576 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1577 mStackSupervisor.mStoppingActivities.add(r);
1578 }
1579 mStackSupervisor.scheduleIdleLocked();
1580 }
1581 break;
1582
1583 default:
1584 break;
1585 }
1586 } catch (Exception e) {
1587 // Just skip on any failure; we'll make it visible when it next restarts.
1588 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1589 }
1590 }
1591
1592 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1593 TaskRecord task, ActivityRecord r) {
1594 if (r.fullscreen) {
1595 // At this point, nothing else needs to be shown in this task.
1596 behindFullscreenActivity = true;
1597 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1598 + " stackInvisible=" + stackInvisible
1599 + " behindFullscreenActivity=" + behindFullscreenActivity);
1600 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1601 behindFullscreenActivity = true;
1602 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1603 + " stackInvisible=" + stackInvisible
1604 + " behindFullscreenActivity=" + behindFullscreenActivity);
1605 }
1606 return behindFullscreenActivity;
1607 }
1608
1609 private void becomeVisible(ActivityRecord starting, ActivityRecord r) {
1610 // This activity is not currently visible, but is running. Tell it to become visible.
1611 r.visible = true;
1612 if (r.state != ActivityState.RESUMED && r != starting) {
1613 // If this activity is paused, tell it to now show its window.
1614 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1615 "Making visible and scheduling visibility: " + r);
1616 try {
1617 if (mTranslucentActivityWaiting != null) {
1618 r.updateOptionsLocked(r.returningOptions);
1619 mUndrawnActivitiesBelowTopTranslucent.add(r);
1620 }
1621 setVisible(r, true);
1622 r.sleeping = false;
1623 r.app.pendingUiClean = true;
1624 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1625 r.stopFreezingScreenLocked(false);
1626 } catch (Exception e) {
1627 // Just skip on any failure; we'll make it
1628 // visible when it next restarts.
1629 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1630 }
1631 }
1632 }
1633
1634 private boolean alreadyVisible(ActivityRecord r) {
1635 // If this activity is already visible, then there is nothing else to do here.
1636 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1637 r.stopFreezingScreenLocked(false);
1638 try {
1639 if (r.returningOptions != null) {
1640 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1641 }
1642 } catch(RemoteException e) {
1643 }
1644 return r.state == ActivityState.RESUMED;
1645 }
1646
Todd Kennedyaab56db2015-01-30 09:39:53 -08001647 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001648 mTranslucentActivityWaiting = r;
1649 mUndrawnActivitiesBelowTopTranslucent.clear();
1650 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1651 }
1652
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001653 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1654 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1655 final TaskRecord task = mTaskHistory.get(taskNdx);
1656 final ArrayList<ActivityRecord> activities = task.mActivities;
1657 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1658 final ActivityRecord r = activities.get(activityNdx);
1659 if ( r.appTimeTracker != except) {
1660 r.appTimeTracker = null;
1661 }
1662 }
1663 }
1664 }
1665
Craig Mautner5eda9b32013-07-02 11:58:16 -07001666 /**
1667 * Called as activities below the top translucent activity are redrawn. When the last one is
1668 * redrawn notify the top activity by calling
1669 * {@link Activity#onTranslucentConversionComplete}.
1670 *
1671 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1672 * occurred and the activity will be notified immediately.
1673 */
1674 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001675 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001676 if ((r == null)
1677 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1678 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1679 // The last undrawn activity below the top has just been drawn. If there is an
1680 // opaque activity at the top, notify it that it can become translucent safely now.
1681 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1682 mTranslucentActivityWaiting = null;
1683 mUndrawnActivitiesBelowTopTranslucent.clear();
1684 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1685
Craig Mautner71dd1b62014-02-18 15:48:52 -08001686 if (waitingActivity != null) {
1687 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1688 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1689 try {
1690 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1691 waitingActivity.appToken, r != null);
1692 } catch (RemoteException e) {
1693 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001694 }
1695 }
1696 }
1697 }
1698
Craig Mautnera61bc652013-10-28 15:43:18 -07001699 /** If any activities below the top running one are in the INITIALIZING state and they have a
1700 * starting window displayed then remove that starting window. It is possible that the activity
1701 * in this state will never resumed in which case that starting window will be orphaned. */
1702 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001703 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001704 boolean aboveTop = true;
1705 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1706 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1707 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1708 final ActivityRecord r = activities.get(activityNdx);
1709 if (aboveTop) {
1710 if (r == topActivity) {
1711 aboveTop = false;
1712 }
1713 continue;
1714 }
1715
1716 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001717 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1718 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001719 r.mStartingWindowShown = false;
1720 mWindowManager.removeAppStartingWindow(r.appToken);
1721 }
1722 }
1723 }
1724 }
1725
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 /**
1727 * Ensure that the top activity in the stack is resumed.
1728 *
1729 * @param prev The previously resumed activity, for when in the process
1730 * of pausing; can be null to call from elsewhere.
1731 *
1732 * @return Returns true if something is being resumed, or false if
1733 * nothing happened.
1734 */
1735 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001736 return resumeTopActivityLocked(prev, null);
1737 }
1738
Chong Zhang280d3322015-11-03 17:27:26 -08001739 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001740 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001741 // Don't even start recursing.
1742 return false;
1743 }
1744
1745 boolean result = false;
1746 try {
1747 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001748 mStackSupervisor.inResumeTopActivity = true;
1749 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1750 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001751 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001752 }
Craig Mautner544efa72014-09-04 16:41:20 -07001753 result = resumeTopActivityInnerLocked(prev, options);
1754 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001755 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001756 }
1757 return result;
1758 }
1759
Chong Zhang280d3322015-11-03 17:27:26 -08001760 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001761 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001762
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001763 if (!mService.mBooting && !mService.mBooted) {
1764 // Not ready yet!
1765 return false;
1766 }
1767
Craig Mautnerdf88d732014-01-27 09:21:32 -08001768 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001769 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001770 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001771 // Do not resume this stack if its parent is not resumed.
1772 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1773 return false;
1774 }
1775
Craig Mautnera61bc652013-10-28 15:43:18 -07001776 cancelInitializingActivities();
1777
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001779 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780
1781 // Remember how we'll process this pause/resume situation, and ensure
1782 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001783 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1784 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785
Craig Mautner84984fa2014-06-19 11:19:20 -07001786 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001788 // There are no more activities!
1789 final String reason = "noMoreActivities";
1790 if (!mFullscreen) {
1791 // Try to move focus to the next visible stack with a running activity if this
1792 // stack is not covering the entire screen.
1793 final ActivityStack stack = getNextVisibleStackLocked();
1794 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1795 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1796 }
1797 }
1798 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001799 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001800 if (DEBUG_STATES) Slog.d(TAG_STATES,
1801 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001802 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001803 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001804 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1805 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1806 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001807 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 }
1809
1810 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001811
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001813 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1814 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 // Make sure we have executed any pending transitions, since there
1816 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001817 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001819 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001820 if (DEBUG_STATES) Slog.d(TAG_STATES,
1821 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001822 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 return false;
1824 }
1825
Craig Mautner525f3d92013-05-07 14:01:50 -07001826 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001827 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001828 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001829 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001830 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001831 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001832 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001833 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001834 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001835 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001836 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001837 } else if (!isOnHomeDisplay()) {
1838 return false;
1839 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001840 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001841 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001842 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1843 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1844 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001845 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001846 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001847 }
1848
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 // If we are sleeping, and there is no resumed activity, and the top
1850 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001851 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001852 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001853 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 // Make sure we have executed any pending transitions, since there
1855 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001856 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001858 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001859 if (DEBUG_STATES) Slog.d(TAG_STATES,
1860 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001861 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 return false;
1863 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001864
1865 // Make sure that the user who owns this activity is started. If not,
1866 // we will just leave it as is because someone should be bringing
1867 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001868 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001869 Slog.w(TAG, "Skipping resume of top activity " + next
1870 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001871 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001872 return false;
1873 }
1874
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001875 // The activity may be waiting for stop, but that is no longer
1876 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001877 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001878 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001879 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001880 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001882 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001884 // If we are currently pausing an activity, then don't do anything
1885 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001886 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001887 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001888 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001889 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001890 return false;
1891 }
1892
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001893 // Okay we are now going to start a switch, to 'next'. We may first
1894 // have to pause the current activity, but this is an important point
1895 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001896 // XXX "App Redirected" dialog is getting too many false positives
1897 // at this point, so turn off for now.
1898 if (false) {
1899 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1900 long now = SystemClock.uptimeMillis();
1901 final boolean inTime = mLastStartedActivity.startTime != 0
1902 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1903 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1904 final int nextUid = next.info.applicationInfo.uid;
1905 if (inTime && lastUid != nextUid
1906 && lastUid != next.launchedFromUid
1907 && mService.checkPermission(
1908 android.Manifest.permission.STOP_APP_SWITCHES,
1909 -1, next.launchedFromUid)
1910 != PackageManager.PERMISSION_GRANTED) {
1911 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1912 } else {
1913 next.startTime = now;
1914 mLastStartedActivity = next;
1915 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001916 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001917 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001918 mLastStartedActivity = next;
1919 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001920 }
Craig Mautner58547802013-03-05 08:23:53 -08001921
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001922 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1923
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001924 // We need to start pausing the current activity so the top one
1925 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001926 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1927 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001928 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001929 if (DEBUG_STATES) Slog.d(TAG_STATES,
1930 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001931 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001932 }
1933 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001934 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001935 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001936 // At this point we want to put the upcoming activity's process
1937 // at the top of the LRU list, since we know we will be needing it
1938 // very soon and it would be a waste to let it get killed if it
1939 // happens to be sitting towards the end.
1940 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001941 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001942 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001943 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001944 return true;
1945 }
1946
Christopher Tated3f175c2012-06-14 14:16:54 -07001947 // If the most recent activity was noHistory but was only stopped rather
1948 // than stopped+finished because the device went to sleep, we need to make
1949 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001950 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001951 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001952 if (DEBUG_STATES) Slog.d(TAG_STATES,
1953 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001954 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001955 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001956 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001957 }
1958
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001960 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1961 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001962 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001963 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1964 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001965 } else {
1966 // The next activity is already visible, so hide the previous
1967 // activity's windows right now so we can show the new one ASAP.
1968 // We only do this if the previous is finishing, which should mean
1969 // it is on top of the one being resumed so hiding it quickly
1970 // is good. Otherwise, we want to do the normal route of allowing
1971 // the resumed activity to be shown so we can decide if the
1972 // previous should actually be hidden depending on whether the
1973 // new one is found to be full-screen or not.
1974 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001975 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001976 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1977 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001978 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001979 + ", nowVisible=" + next.nowVisible);
1980 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001981 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001982 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001983 + ", waitingVisible="
1984 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1985 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001986 }
1987 }
1988 }
1989
Dianne Hackborne7f97212011-02-24 14:40:20 -08001990 // Launching this app's activity, make sure the app is no longer
1991 // considered stopped.
1992 try {
1993 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001994 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001995 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001996 } catch (IllegalArgumentException e) {
1997 Slog.w(TAG, "Failed trying to unstop package "
1998 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001999 }
2000
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 // We are starting up the next activity, so tell the window manager
2002 // that the previous one will be hidden soon. This way it can know
2003 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002004 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002005 if (prev != null) {
2006 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002007 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008 "Prepare close transition: prev=" + prev);
2009 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002010 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002011 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002012 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002013 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002014 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2015 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002017 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002019 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2020 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002021 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002022 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002023 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002025 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002026 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002027 : next.mLaunchTaskBehind
2028 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2029 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 }
2031 }
Craig Mautner967212c2013-04-13 21:10:58 -07002032 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002033 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002034 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002035 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002036 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002037 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002038 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002039 }
2040 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002041
2042 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002043 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002044 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2045 if (opts != null) {
2046 resumeAnimOptions = opts.toBundle();
2047 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002048 next.applyOptionsLocked();
2049 } else {
2050 next.clearOptionsLocked();
2051 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002052
Craig Mautnercf910b02013-04-23 11:23:27 -07002053 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002055 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056
2057 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002058 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002059
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002060 // schedule launch ticks to collect information about slow apps.
2061 next.startLaunchTickingLocked();
2062
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002063 ActivityRecord lastResumedActivity =
2064 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 ActivityState lastState = next.state;
2066
2067 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002068
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002069 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 next.state = ActivityState.RESUMED;
2071 mResumedActivity = next;
2072 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002073 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002074 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002076 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077
2078 // Have the window manager re-evaluate the orientation of
2079 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002080 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002081 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002082 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002083 mService.mConfiguration,
2084 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2085 if (config != null) {
2086 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002088 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002090
Craig Mautner525f3d92013-05-07 14:01:50 -07002091 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 // The configuration update wasn't able to keep the existing
2093 // instance of the activity, and instead started a new one.
2094 // We should be all done, but let's just make sure our activity
2095 // is still at the top and schedule another run if something
2096 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002097 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002098 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 "Activity config changed during resume: " + next
2100 + ", new next: " + nextNext);
2101 if (nextNext != next) {
2102 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002103 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002105 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002106 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002107 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002108 return true;
2109 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002110 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002111 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 }
Craig Mautner58547802013-03-05 08:23:53 -08002113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 try {
2115 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002116 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 if (a != null) {
2118 final int N = a.size();
2119 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002120 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2121 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002122 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 }
2124 }
2125
2126 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002127 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 }
2129
Craig Mautner299f9602015-01-26 09:47:33 -08002130 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2131 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002132
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002133 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002134 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002135 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002136 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002137 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002138 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002139 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002140
Craig Mautner0eea92c2013-05-16 13:35:39 -07002141 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002143 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 } catch (Exception e) {
2145 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002146 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002147 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002149 if (lastStack != null) {
2150 lastStack.mResumedActivity = lastResumedActivity;
2151 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 Slog.i(TAG, "Restarting because process died: " + next);
2153 if (!next.hasBeenLaunched) {
2154 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002155 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2156 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002157 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002158 next.appToken, next.packageName, next.theme,
2159 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002160 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2161 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 }
George Mount2c92c972014-03-20 09:38:23 -07002163 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002164 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 return true;
2166 }
2167
2168 // From this point on, if something goes wrong there is no way
2169 // to recover the activity.
2170 try {
2171 next.visible = true;
2172 completeResumeLocked(next);
2173 } catch (Exception e) {
2174 // If any exception gets thrown, toss away this
2175 // activity and try the next one.
2176 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002177 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002178 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002179 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 return true;
2181 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 next.stopped = false;
2183
2184 } else {
2185 // Whoops, need to restart this activity!
2186 if (!next.hasBeenLaunched) {
2187 next.hasBeenLaunched = true;
2188 } else {
2189 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002190 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002191 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002192 mService.compatibilityInfoForPackageLocked(
2193 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002195 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002196 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002198 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002199 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002200 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002201 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202 }
2203
Craig Mautnercf910b02013-04-23 11:23:27 -07002204 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 return true;
2206 }
2207
riddle_hsuc215a4f2014-12-27 12:10:45 +08002208 private TaskRecord getNextTask(TaskRecord targetTask) {
2209 final int index = mTaskHistory.indexOf(targetTask);
2210 if (index >= 0) {
2211 final int numTasks = mTaskHistory.size();
2212 for (int i = index + 1; i < numTasks; ++i) {
2213 TaskRecord task = mTaskHistory.get(i);
2214 if (task.userId == targetTask.userId) {
2215 return task;
2216 }
2217 }
2218 }
2219 return null;
2220 }
2221
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002222 private void insertTaskAtPosition(TaskRecord task, int position) {
2223 if (position >= mTaskHistory.size()) {
2224 insertTaskAtTop(task, null);
2225 return;
2226 }
2227 // Calculate maximum possible position for this task.
2228 int maxPosition = mTaskHistory.size();
2229 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002230 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002231 // Put non-current user tasks below current user tasks.
2232 while (maxPosition > 0) {
2233 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2234 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002235 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002236 break;
2237 }
2238 maxPosition--;
2239 }
2240 }
2241 position = Math.min(position, maxPosition);
2242 mTaskHistory.remove(task);
2243 mTaskHistory.add(position, task);
2244 updateTaskMovement(task, true);
2245 }
2246
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002247 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002248 // If the moving task is over home stack, transfer its return type to next task
2249 if (task.isOverHomeStack()) {
2250 final TaskRecord nextTask = getNextTask(task);
2251 if (nextTask != null) {
2252 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2253 }
2254 }
2255
Craig Mautner9c85c202013-10-04 20:11:26 -07002256 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002257 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002258 if (isOnHomeDisplay()) {
2259 ActivityStack lastStack = mStackSupervisor.getLastStack();
2260 final boolean fromHome = lastStack.isHomeStack();
2261 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002262 task.setTaskToReturnTo(fromHome
2263 ? lastStack.topTask() == null
2264 ? HOME_ACTIVITY_TYPE
2265 : lastStack.topTask().taskType
2266 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002267 }
2268 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002269 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002270 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002271
Craig Mautnerac6f8432013-07-17 13:24:59 -07002272 mTaskHistory.remove(task);
2273 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002274 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002275 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002276 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002277 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002278 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002279 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002280 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002281 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002282 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002283 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002284 break;
2285 }
2286 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002287 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002288 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002289 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002290 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002291 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002292
Craig Mautner8849a5e2013-04-02 16:41:03 -07002293 final void startActivityLocked(ActivityRecord r, boolean newTask,
Chong Zhang280d3322015-11-03 17:27:26 -08002294 boolean doResume, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002295 TaskRecord rTask = r.task;
2296 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002297 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2298 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002299 // Last activity in task had been removed or ActivityManagerService is reusing task.
2300 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002301 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002302 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002303 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002304 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002305 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 if (!newTask) {
2307 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002308 boolean startIt = true;
2309 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2310 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002311 if (task.getTopActivity() == null) {
2312 // All activities in task are finishing.
2313 continue;
2314 }
Craig Mautner70a86932013-02-28 22:37:44 -08002315 if (task == r.task) {
2316 // Here it is! Now, if this is not yet visible to the
2317 // user, then just add it without starting; it will
2318 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002319 if (!startIt) {
2320 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2321 + task, new RuntimeException("here").fillInStackTrace());
2322 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002323 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002324 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002325 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002326 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002328 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 return;
2330 }
2331 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002332 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 startIt = false;
2334 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 }
2336 }
2337
2338 // Place a new activity at top of stack, so it is next to interact
2339 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002340
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002341 // If we are not placing the new activity frontmost, we do not want
2342 // to deliver the onUserLeaving callback to the actual frontmost
2343 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002344 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002345 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002346 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002347 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 }
Craig Mautner70a86932013-02-28 22:37:44 -08002349
2350 task = r.task;
2351
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002353 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002354 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002355 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002356 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002357
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002358 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002359 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // We want to show the starting preview window if we are
2361 // switching to a new task, or the next activity's process is
2362 // not currently running.
2363 boolean showStartingIcon = newTask;
2364 ProcessRecord proc = r.app;
2365 if (proc == null) {
2366 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2367 }
2368 if (proc == null || proc.thread == null) {
2369 showStartingIcon = true;
2370 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002371 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002373 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002374 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002377 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002378 ? r.mLaunchTaskBehind
2379 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2380 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002381 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 mNoAnimActivities.remove(r);
2383 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002384 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 boolean doShow = true;
2386 if (newTask) {
2387 // Even though this activity is starting fresh, we still need
2388 // to reset it to make sure we apply affinities to move any
2389 // existing activities from other tasks in to it.
2390 // If the caller has requested that the target task be
2391 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002392 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002393 resetTaskIfNeededLocked(r, r);
2394 doShow = topRunningNonDelayedActivityLocked(null) == r;
2395 }
Chong Zhang280d3322015-11-03 17:27:26 -08002396 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002397 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2398 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002400 if (r.mLaunchTaskBehind) {
2401 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2402 // tell WindowManager that r is visible even though it is at the back of the stack.
2403 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002404 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002405 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002406 // Figure out if we are transitioning from another activity that is
2407 // "has the same starting icon" as the next one. This allows the
2408 // window manager to keep the previous window it had previously
2409 // created, if it still had one.
2410 ActivityRecord prev = mResumedActivity;
2411 if (prev != null) {
2412 // We don't want to reuse the previous starting preview if:
2413 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002414 if (prev.task != r.task) {
2415 prev = null;
2416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002418 else if (prev.nowVisible) {
2419 prev = null;
2420 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002422 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002423 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002424 mService.compatibilityInfoForPackageLocked(
2425 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002426 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002427 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002428 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 }
2430 } else {
2431 // If this is the first activity, don't do any fancy animations,
2432 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002433 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002434 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002435 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 }
2437 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002438 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 }
2440
2441 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002442 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002443 } else {
2444 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002445 }
2446 }
2447
Dianne Hackbornbe707852011-11-11 14:32:10 -08002448 final void validateAppTokensLocked() {
2449 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002450 mValidateAppTokens.ensureCapacity(numActivities());
2451 final int numTasks = mTaskHistory.size();
2452 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2453 TaskRecord task = mTaskHistory.get(taskNdx);
2454 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002455 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002456 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002457 }
Craig Mautner000f0022013-02-26 15:04:29 -08002458 TaskGroup group = new TaskGroup();
2459 group.taskId = task.taskId;
2460 mValidateAppTokens.add(group);
2461 final int numActivities = activities.size();
2462 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2463 final ActivityRecord r = activities.get(activityNdx);
2464 group.tokens.add(r.appToken);
2465 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002466 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002467 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002468 }
2469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002470 /**
2471 * Perform a reset of the given task, if needed as part of launching it.
2472 * Returns the new HistoryRecord at the top of the task.
2473 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002474 /**
2475 * Helper method for #resetTaskIfNeededLocked.
2476 * We are inside of the task being reset... we'll either finish this activity, push it out
2477 * for another task, or leave it as-is.
2478 * @param task The task containing the Activity (taskTop) that might be reset.
2479 * @param forceReset
2480 * @return An ActivityOptions that needs to be processed.
2481 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002482 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002483 ActivityOptions topOptions = null;
2484
2485 int replyChainEnd = -1;
2486 boolean canMoveOptions = true;
2487
2488 // We only do this for activities that are not the root of the task (since if we finish
2489 // the root, we may no longer have the task!).
2490 final ArrayList<ActivityRecord> activities = task.mActivities;
2491 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002492 final int rootActivityNdx = task.findEffectiveRootIndex();
2493 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002494 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002495 if (target.frontOfTask)
2496 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002497
2498 final int flags = target.info.flags;
2499 final boolean finishOnTaskLaunch =
2500 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2501 final boolean allowTaskReparenting =
2502 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2503 final boolean clearWhenTaskReset =
2504 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2505
2506 if (!finishOnTaskLaunch
2507 && !clearWhenTaskReset
2508 && target.resultTo != null) {
2509 // If this activity is sending a reply to a previous
2510 // activity, we can't do anything with it now until
2511 // we reach the start of the reply chain.
2512 // XXX note that we are assuming the result is always
2513 // to the previous activity, which is almost always
2514 // the case but we really shouldn't count on.
2515 if (replyChainEnd < 0) {
2516 replyChainEnd = i;
2517 }
2518 } else if (!finishOnTaskLaunch
2519 && !clearWhenTaskReset
2520 && allowTaskReparenting
2521 && target.taskAffinity != null
2522 && !target.taskAffinity.equals(task.affinity)) {
2523 // If this activity has an affinity for another
2524 // task, then we need to move it out of here. We will
2525 // move it as far out of the way as possible, to the
2526 // bottom of the activity stack. This also keeps it
2527 // correctly ordered with any activities we previously
2528 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002529 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002530 final ActivityRecord bottom =
2531 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002532 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002533 if (bottom != null && target.taskAffinity != null
2534 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002535 // If the activity currently at the bottom has the
2536 // same task affinity as the one we are moving,
2537 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002538 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002539 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002540 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002541 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002542 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002543 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002544 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002545 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002546 + " out to new task " + target.task);
2547 }
2548
Wale Ogunwale706ed792015-08-02 10:29:44 -07002549 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002550
Craig Mautner525f3d92013-05-07 14:01:50 -07002551 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002552 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2553 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002554 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002555 if (p.finishing) {
2556 continue;
2557 }
2558
Craig Mautnere3a74d52013-02-22 14:14:58 -08002559 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002560 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002561 topOptions = p.takeOptionsLocked();
2562 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002563 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002564 }
2565 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002566 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2567 "Removing activity " + p + " from task=" + task + " adding to task="
2568 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002569 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2570 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002571 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002572 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002573
Wale Ogunwale706ed792015-08-02 10:29:44 -07002574 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002575 }
2576
Wale Ogunwale706ed792015-08-02 10:29:44 -07002577 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002578 if (VALIDATE_TOKENS) {
2579 validateAppTokensLocked();
2580 }
2581
2582 replyChainEnd = -1;
2583 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2584 // If the activity should just be removed -- either
2585 // because it asks for it, or the task should be
2586 // cleared -- then finish it and anything that is
2587 // part of its reply chain.
2588 int end;
2589 if (clearWhenTaskReset) {
2590 // In this case, we want to finish this activity
2591 // and everything above it, so be sneaky and pretend
2592 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002593 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002594 } else if (replyChainEnd < 0) {
2595 end = i;
2596 } else {
2597 end = replyChainEnd;
2598 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002599 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002600 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002601 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002602 if (p.finishing) {
2603 continue;
2604 }
2605 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002606 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002607 topOptions = p.takeOptionsLocked();
2608 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002609 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002610 }
2611 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002612 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002613 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002614 if (finishActivityLocked(
2615 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002616 end--;
2617 srcPos--;
2618 }
2619 }
2620 replyChainEnd = -1;
2621 } else {
2622 // If we were in the middle of a chain, well the
2623 // activity that started it all doesn't want anything
2624 // special, so leave it all as-is.
2625 replyChainEnd = -1;
2626 }
2627 }
2628
2629 return topOptions;
2630 }
2631
2632 /**
2633 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2634 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2635 * @param affinityTask The task we are looking for an affinity to.
2636 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2637 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2638 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2639 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002640 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002641 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002642 int replyChainEnd = -1;
2643 final int taskId = task.taskId;
2644 final String taskAffinity = task.affinity;
2645
2646 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2647 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002648 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2649
2650 // Do not operate on or below the effective root Activity.
2651 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002652 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002653 if (target.frontOfTask)
2654 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002655
2656 final int flags = target.info.flags;
2657 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2658 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2659
2660 if (target.resultTo != null) {
2661 // If this activity is sending a reply to a previous
2662 // activity, we can't do anything with it now until
2663 // we reach the start of the reply chain.
2664 // XXX note that we are assuming the result is always
2665 // to the previous activity, which is almost always
2666 // the case but we really shouldn't count on.
2667 if (replyChainEnd < 0) {
2668 replyChainEnd = i;
2669 }
2670 } else if (topTaskIsHigher
2671 && allowTaskReparenting
2672 && taskAffinity != null
2673 && taskAffinity.equals(target.taskAffinity)) {
2674 // This activity has an affinity for our task. Either remove it if we are
2675 // clearing or move it over to our task. Note that
2676 // we currently punt on the case where we are resetting a
2677 // task that is not at the top but who has activities above
2678 // with an affinity to it... this is really not a normal
2679 // case, and we will need to later pull that task to the front
2680 // and usually at that point we will do the reset and pick
2681 // up those remaining activities. (This only happens if
2682 // someone starts an activity in a new task from an activity
2683 // in a task that is not currently on top.)
2684 if (forceReset || finishOnTaskLaunch) {
2685 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002686 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2687 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002688 for (int srcPos = start; srcPos >= i; --srcPos) {
2689 final ActivityRecord p = activities.get(srcPos);
2690 if (p.finishing) {
2691 continue;
2692 }
Todd Kennedy539db512014-12-15 09:57:55 -08002693 finishActivityLocked(
2694 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002695 }
2696 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002697 if (taskInsertionPoint < 0) {
2698 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002699
Craig Mautner77878772013-03-04 19:46:24 -08002700 }
Craig Mautner77878772013-03-04 19:46:24 -08002701
2702 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002703 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2704 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2705 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002706 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002707 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002708 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002709 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002710
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002711 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2712 "Removing and adding activity " + p + " to stack at " + task
2713 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002714 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2715 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002716 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002717 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002718 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002719 if (VALIDATE_TOKENS) {
2720 validateAppTokensLocked();
2721 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002722
2723 // Now we've moved it in to place... but what if this is
2724 // a singleTop activity and we have put it on top of another
2725 // instance of the same activity? Then we drop the instance
2726 // below so it remains singleTop.
2727 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2728 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002729 int targetNdx = taskActivities.indexOf(target);
2730 if (targetNdx > 0) {
2731 ActivityRecord p = taskActivities.get(targetNdx - 1);
2732 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002733 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2734 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002735 }
2736 }
2737 }
2738 }
2739
2740 replyChainEnd = -1;
2741 }
2742 }
Craig Mautner77878772013-03-04 19:46:24 -08002743 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002744 }
2745
Craig Mautner8849a5e2013-04-02 16:41:03 -07002746 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002747 ActivityRecord newActivity) {
2748 boolean forceReset =
2749 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2750 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2751 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2752 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2753 forceReset = true;
2754 }
2755 }
2756
2757 final TaskRecord task = taskTop.task;
2758
2759 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2760 * for remaining tasks. Used for later tasks to reparent to task. */
2761 boolean taskFound = false;
2762
2763 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2764 ActivityOptions topOptions = null;
2765
Craig Mautner77878772013-03-04 19:46:24 -08002766 // Preserve the location for reparenting in the new task.
2767 int reparentInsertionPoint = -1;
2768
Craig Mautnere3a74d52013-02-22 14:14:58 -08002769 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2770 final TaskRecord targetTask = mTaskHistory.get(i);
2771
2772 if (targetTask == task) {
2773 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2774 taskFound = true;
2775 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002776 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2777 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002778 }
2779 }
2780
Craig Mautner70a86932013-02-28 22:37:44 -08002781 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002782 if (taskNdx >= 0) {
2783 do {
2784 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2785 } while (taskTop == null && taskNdx >= 0);
2786 }
Craig Mautner70a86932013-02-28 22:37:44 -08002787
Craig Mautnere3a74d52013-02-22 14:14:58 -08002788 if (topOptions != null) {
2789 // If we got some ActivityOptions from an activity on top that
2790 // was removed from the task, propagate them to the new real top.
2791 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002792 taskTop.updateOptionsLocked(topOptions);
2793 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002794 topOptions.abort();
2795 }
2796 }
2797
2798 return taskTop;
2799 }
2800
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2802 String resultWho, int requestCode, int resultCode, Intent data) {
2803
2804 if (callingUid > 0) {
2805 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002806 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 }
2808
2809 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2810 + " : who=" + resultWho + " req=" + requestCode
2811 + " res=" + resultCode + " data=" + data);
2812 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2813 try {
2814 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2815 list.add(new ResultInfo(resultWho, requestCode,
2816 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002817 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002818 return;
2819 } catch (Exception e) {
2820 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2821 }
2822 }
2823
2824 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2825 }
2826
Craig Mautner299f9602015-01-26 09:47:33 -08002827 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002828 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2829 return;
2830 }
2831
2832 final ActivityRecord next = topRunningActivityLocked();
2833 final String myReason = reason + " adjustFocus";
2834 if (next != r) {
2835 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
2836 // For freeform, docked, and pinned stacks we always keep the focus within the
2837 // stack as long as there is a running activity in the stack that we can adjust
2838 // focus to.
2839 mService.setFocusedActivityLocked(next, myReason);
2840 return;
2841 } else {
2842 final TaskRecord task = r.task;
2843 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2844 // For non-fullscreen stack, we want to move the focus to the next visible
2845 // stack to prevent the home screen from moving to the top and obscuring
2846 // other visible stacks.
2847 if (!mFullscreen
2848 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2849 return;
2850 }
2851 // Move the home stack to the top if this stack is fullscreen or there is no
2852 // other visible stack.
2853 if (mStackSupervisor.moveHomeStackTaskToTop(
2854 task.getTaskToReturnTo(), myReason)) {
2855 // Activity focus was already adjusted. Nothing else to do...
2856 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002857 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002858 }
2859 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002860 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002861
Wale Ogunwaled045c822015-12-02 09:14:28 -08002862 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2863 if (top != null) {
2864 mService.setFocusedActivityLocked(top, myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002865 }
2866 }
2867
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002868 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2869 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2870 final String myReason = reason + " adjustFocusToNextVisibleStack";
2871 if (stack == null) {
2872 return false;
2873 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002874 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002875 if (top == null) {
2876 return false;
2877 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002878 mService.setFocusedActivityLocked(top, myReason);
2879 return true;
2880 }
2881
Craig Mautnerf3333272013-04-22 10:55:53 -07002882 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002883 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2885 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2886 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002887 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002888 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002889 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2890 "stop-no-history", false)) {
2891 // Activity was finished, no need to continue trying to schedule stop.
2892 adjustFocusedActivityLocked(r, "stopActivityFinished");
2893 r.resumeKeyDispatchingLocked();
2894 return;
2895 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002896 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002897 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002898 + " on stop because we're just sleeping");
2899 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002900 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002901 }
2902
2903 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002904 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002905 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002906 try {
2907 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002908 if (DEBUG_STATES) Slog.v(TAG_STATES,
2909 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002910 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002911 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2912 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002914 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002915 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002916 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002917 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002918 r.setSleeping(true);
2919 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002920 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002921 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002922 } catch (Exception e) {
2923 // Maybe just ignore exceptions here... if the process
2924 // has crashed, our death notification will clean things
2925 // up.
2926 Slog.w(TAG, "Exception thrown during pause", e);
2927 // Just in case, assume it to be stopped.
2928 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002929 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002930 r.state = ActivityState.STOPPED;
2931 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002932 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002933 }
2934 }
2935 }
2936 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002937
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 /**
2939 * @return Returns true if the activity is being finished, false if for
2940 * some reason it is being left as-is.
2941 */
2942 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002943 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002944 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002945 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2946 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002947 + ", result=" + resultCode + ", data=" + resultData
2948 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002949 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002950 return false;
2951 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952
Craig Mautnerd44711d2013-02-23 11:24:36 -08002953 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002954 return true;
2955 }
2956
Craig Mautnerd2328952013-03-05 12:46:26 -08002957 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002958 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2959 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2960 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2961 ActivityRecord r = activities.get(activityNdx);
2962 if (r.resultTo == self && r.requestCode == requestCode) {
2963 if ((r.resultWho == null && resultWho == null) ||
2964 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2965 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2966 false);
2967 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002968 }
2969 }
2970 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002971 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002972 }
2973
Todd Kennedy539db512014-12-15 09:57:55 -08002974 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002975 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002976 if (r != null && r.app == app) {
2977 // If the top running activity is from this crashing
2978 // process, then terminate it to avoid getting in a loop.
2979 Slog.w(TAG, " Force finishing activity "
2980 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002981 int taskNdx = mTaskHistory.indexOf(r.task);
2982 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002983 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002984 // Also terminate any activities below it that aren't yet
2985 // stopped, to avoid a situation where one will get
2986 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002987 --activityNdx;
2988 if (activityNdx < 0) {
2989 do {
2990 --taskNdx;
2991 if (taskNdx < 0) {
2992 break;
2993 }
2994 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2995 } while (activityNdx < 0);
2996 }
2997 if (activityNdx >= 0) {
2998 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002999 if (r.state == ActivityState.RESUMED
3000 || r.state == ActivityState.PAUSING
3001 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003002 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003003 Slog.w(TAG, " Force finishing activity "
3004 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003005 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003006 }
3007 }
3008 }
3009 }
3010 }
3011
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003012 final void finishVoiceTask(IVoiceInteractionSession session) {
3013 IBinder sessionBinder = session.asBinder();
3014 boolean didOne = false;
3015 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3016 TaskRecord tr = mTaskHistory.get(taskNdx);
3017 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3018 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3019 ActivityRecord r = tr.mActivities.get(activityNdx);
3020 if (!r.finishing) {
3021 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3022 false);
3023 didOne = true;
3024 }
3025 }
3026 }
3027 }
3028 if (didOne) {
3029 mService.updateOomAdjLocked();
3030 }
3031 }
3032
Craig Mautnerd2328952013-03-05 12:46:26 -08003033 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003034 ArrayList<ActivityRecord> activities = r.task.mActivities;
3035 for (int index = activities.indexOf(r); index >= 0; --index) {
3036 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003037 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003038 break;
3039 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003040 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003041 }
3042 return true;
3043 }
3044
Dianne Hackborn5c607432012-02-28 14:44:19 -08003045 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3046 // send the result
3047 ActivityRecord resultTo = r.resultTo;
3048 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003049 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003050 + " who=" + r.resultWho + " req=" + r.requestCode
3051 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003052 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003053 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003054 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003055 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003056 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003057 if (r.info.applicationInfo.uid > 0) {
3058 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3059 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003060 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003061 }
3062 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3063 resultData);
3064 r.resultTo = null;
3065 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003066 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003067
3068 // Make sure this HistoryRecord is not holding on to other resources,
3069 // because clients have remote IPC references to this object so we
3070 // can't assume that will go away and want to avoid circular IPC refs.
3071 r.results = null;
3072 r.pendingResults = null;
3073 r.newIntents = null;
3074 r.icicle = null;
3075 }
3076
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077 /**
3078 * @return Returns true if this activity has been removed from the history
3079 * list, or false if it is still in the list and will be removed later.
3080 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003081 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3082 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 if (r.finishing) {
3084 Slog.w(TAG, "Duplicate finish request for " + r);
3085 return false;
3086 }
3087
Wale Ogunwale7d701172015-03-11 15:36:30 -07003088 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003089 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003090 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003091 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003092 task.taskId, r.shortComponentName, reason);
3093 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003094 final int index = activities.indexOf(r);
3095 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003096 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003097 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003098 // If the caller asked that this activity (and all above it)
3099 // be cleared when the task is reset, don't lose that information,
3100 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003101 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003102 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103 }
3104 }
3105
3106 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003107
Craig Mautner299f9602015-01-26 09:47:33 -08003108 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003109
Dianne Hackborn5c607432012-02-28 14:44:19 -08003110 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003111
Craig Mautnerde4ef022013-04-07 19:01:33 -07003112 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003113 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003114 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003115 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003116 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003117 ? AppTransition.TRANSIT_TASK_CLOSE
3118 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003121 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003122
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003123 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003124 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3125 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3126 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003127 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003128 }
3129
Craig Mautneraea74a52014-03-08 14:23:10 -08003130 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003131 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003132 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 } else if (r.state != ActivityState.PAUSING) {
3134 // If the activity is PAUSING, we will complete the finish once
3135 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003136 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003137 if (r.visible) {
3138 mWindowManager.setAppVisibility(r.appToken, false);
3139 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003140 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003142 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 }
3144
3145 return false;
3146 }
3147
Craig Mautnerf3333272013-04-22 10:55:53 -07003148 static final int FINISH_IMMEDIATELY = 0;
3149 static final int FINISH_AFTER_PAUSE = 1;
3150 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151
Craig Mautnerf3333272013-04-22 10:55:53 -07003152 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 // First things first: if this activity is currently visible,
3154 // and the resumed activity is not yet visible, then hold off on
3155 // finishing until the resumed one becomes visible.
3156 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003157 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3158 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003159 if (mStackSupervisor.mStoppingActivities.size() > 3
3160 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003161 // If we already have a few activities waiting to stop,
3162 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003163 // them out. Or if r is the last of activity of the last task the stack
3164 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003165 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003166 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003167 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 }
3169 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003170 if (DEBUG_STATES) Slog.v(TAG_STATES,
3171 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003173 if (oomAdj) {
3174 mService.updateOomAdjLocked();
3175 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 return r;
3177 }
3178
3179 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003180 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003181 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003182 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 if (mResumedActivity == r) {
3184 mResumedActivity = null;
3185 }
3186 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003187 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 r.state = ActivityState.FINISHING;
3189
3190 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003191 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003192 || prevState == ActivityState.STOPPED
3193 || prevState == ActivityState.INITIALIZING) {
3194 // If this activity is already stopped, we can just finish
3195 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003196 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003197 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003198 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003199 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003200 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003201 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003202 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3203 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003204 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003206
3207 // Need to go through the full pause cycle to get this
3208 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003209 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003210 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003211 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003212 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 return r;
3214 }
3215
Craig Mautneree36c772014-07-16 14:56:05 -07003216 void finishAllActivitiesLocked(boolean immediately) {
3217 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003218 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3219 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3220 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3221 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003222 noActivitiesInStack = false;
3223 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003224 continue;
3225 }
Craig Mautneree36c772014-07-16 14:56:05 -07003226 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003227 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3228 }
3229 }
Craig Mautneree36c772014-07-16 14:56:05 -07003230 if (noActivitiesInStack) {
3231 mActivityContainer.onTaskListEmptyLocked();
3232 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003233 }
3234
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003235 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3236 // Basic case: for simple app-centric recents, we need to recreate
3237 // the task if the affinity has changed.
3238 if (srec == null || srec.task.affinity == null ||
3239 !srec.task.affinity.equals(destAffinity)) {
3240 return true;
3241 }
3242 // Document-centric case: an app may be split in to multiple documents;
3243 // they need to re-create their task if this current activity is the root
3244 // of a document, unless simply finishing it will return them to the the
3245 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003246 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3247 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003248 // Okay, this activity is at the root of its task. What to do, what to do...
3249 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3250 // Finishing won't return to an application, so we need to recreate.
3251 return true;
3252 }
3253 // We now need to get the task below it to determine what to do.
3254 int taskIdx = mTaskHistory.indexOf(srec.task);
3255 if (taskIdx <= 0) {
3256 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3257 return false;
3258 }
3259 if (taskIdx == 0) {
3260 // At the bottom of the stack, nothing to go back to.
3261 return true;
3262 }
3263 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3264 if (!srec.task.affinity.equals(prevTask.affinity)) {
3265 // These are different apps, so need to recreate.
3266 return true;
3267 }
3268 }
3269 return false;
3270 }
3271
Wale Ogunwale7d701172015-03-11 15:36:30 -07003272 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003273 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003274 final TaskRecord task = srec.task;
3275 final ArrayList<ActivityRecord> activities = task.mActivities;
3276 final int start = activities.indexOf(srec);
3277 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003278 return false;
3279 }
3280 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003281 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003282 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003283 final ComponentName dest = destIntent.getComponent();
3284 if (start > 0 && dest != null) {
3285 for (int i = finishTo; i >= 0; i--) {
3286 ActivityRecord r = activities.get(i);
3287 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003288 r.info.name.equals(dest.getClassName())) {
3289 finishTo = i;
3290 parent = r;
3291 foundParentInTask = true;
3292 break;
3293 }
3294 }
3295 }
3296
3297 IActivityController controller = mService.mController;
3298 if (controller != null) {
3299 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3300 if (next != null) {
3301 // ask watcher if this is allowed
3302 boolean resumeOK = true;
3303 try {
3304 resumeOK = controller.activityResuming(next.packageName);
3305 } catch (RemoteException e) {
3306 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003307 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003308 }
3309
3310 if (!resumeOK) {
3311 return false;
3312 }
3313 }
3314 }
3315 final long origId = Binder.clearCallingIdentity();
3316 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003317 ActivityRecord r = activities.get(i);
3318 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003319 // Only return the supplied result for the first activity finished
3320 resultCode = Activity.RESULT_CANCELED;
3321 resultData = null;
3322 }
3323
3324 if (parent != null && foundParentInTask) {
3325 final int parentLaunchMode = parent.info.launchMode;
3326 final int destIntentFlags = destIntent.getFlags();
3327 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3328 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3329 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3330 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003331 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3332 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003333 } else {
3334 try {
3335 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3336 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003337 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003338 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003339 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003340 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003341 foundParentInTask = res == ActivityManager.START_SUCCESS;
3342 } catch (RemoteException e) {
3343 foundParentInTask = false;
3344 }
3345 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003346 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003347 }
3348 }
3349 Binder.restoreCallingIdentity(origId);
3350 return foundParentInTask;
3351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 /**
3353 * Perform the common clean-up of an activity record. This is called both
3354 * as part of destroyActivityLocked() (when destroying the client-side
3355 * representation) and cleaning things up as a result of its hosting
3356 * processing going away, in which case there is no remaining client-side
3357 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003358 *
3359 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003361 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3362 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003363 if (mResumedActivity == r) {
3364 mResumedActivity = null;
3365 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003366 if (mPausingActivity == r) {
3367 mPausingActivity = null;
3368 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003369 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003370
3371 r.configDestroy = false;
3372 r.frozenBeforeDestroy = false;
3373
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003374 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003375 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003376 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003377 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003378 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003379 }
3380
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003381 // Make sure this record is no longer in the pending finishes list.
3382 // This could happen, for example, if we are trimming activities
3383 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003384 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003385 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003386
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003387 // Remove any pending results.
3388 if (r.finishing && r.pendingResults != null) {
3389 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3390 PendingIntentRecord rec = apr.get();
3391 if (rec != null) {
3392 mService.cancelIntentSenderLocked(rec, false);
3393 }
3394 }
3395 r.pendingResults = null;
3396 }
3397
3398 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003399 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003400 }
3401
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003402 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003403 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003404 if (getVisibleBehindActivity() == r) {
3405 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003406 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003407 }
3408
3409 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003410 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003411 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003412 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003413 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003414 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003415 }
3416
Craig Mautner299f9602015-01-26 09:47:33 -08003417 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003418 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003419 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003420 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003421 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3422 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3423
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003424 r.takeFromHistory();
3425 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003426 if (DEBUG_STATES) Slog.v(TAG_STATES,
3427 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003428 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003429 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003430 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003431 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003432 if (VALIDATE_TOKENS) {
3433 validateAppTokensLocked();
3434 }
Craig Mautner312ba862014-02-10 17:55:01 -08003435 final TaskRecord task = r.task;
3436 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003437 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003438 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003439 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003440 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003441 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003442 }
Craig Mautner299f9602015-01-26 09:47:33 -08003443 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003444 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003445 cleanUpActivityServicesLocked(r);
3446 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003447 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003448
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003449 /**
3450 * Perform clean-up of service connections in an activity record.
3451 */
3452 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3453 // Throw away any services that have been bound by this activity.
3454 if (r.connections != null) {
3455 Iterator<ConnectionRecord> it = r.connections.iterator();
3456 while (it.hasNext()) {
3457 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003458 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 }
3460 r.connections = null;
3461 }
3462 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003463
Craig Mautneree2e45a2014-06-27 12:10:03 -07003464 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003465 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003466 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003467 mHandler.sendMessage(msg);
3468 }
3469
Craig Mautneree2e45a2014-06-27 12:10:03 -07003470 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003471 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003472 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003473 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3474 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3475 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3476 final ActivityRecord r = activities.get(activityNdx);
3477 if (r.finishing) {
3478 continue;
3479 }
3480 if (r.fullscreen) {
3481 lastIsOpaque = true;
3482 }
3483 if (owner != null && r.app != owner) {
3484 continue;
3485 }
3486 if (!lastIsOpaque) {
3487 continue;
3488 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003489 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003490 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003491 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003492 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003493 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003494 activityRemoved = true;
3495 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003496 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003497 }
3498 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003499 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003500 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003501 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003502 }
3503
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003504 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3505 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003506 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3507 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003508 + " pausing=" + mPausingActivity + " for reason " + reason);
3509 return destroyActivityLocked(r, true, reason);
3510 }
3511 return false;
3512 }
3513
3514 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3515 String reason) {
3516 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003517 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003518 int maxTasks = tasks.size() / 4;
3519 if (maxTasks < 1) {
3520 maxTasks = 1;
3521 }
3522 int numReleased = 0;
3523 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3524 final TaskRecord task = mTaskHistory.get(taskNdx);
3525 if (!tasks.contains(task)) {
3526 continue;
3527 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003528 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003529 int curNum = 0;
3530 final ArrayList<ActivityRecord> activities = task.mActivities;
3531 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3532 final ActivityRecord activity = activities.get(actNdx);
3533 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003534 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003535 + " in state " + activity.state + " resumed=" + mResumedActivity
3536 + " pausing=" + mPausingActivity + " for reason " + reason);
3537 destroyActivityLocked(activity, true, reason);
3538 if (activities.get(actNdx) != activity) {
3539 // Was removed from list, back up so we don't miss the next one.
3540 actNdx--;
3541 }
3542 curNum++;
3543 }
3544 }
3545 if (curNum > 0) {
3546 numReleased += curNum;
3547 maxTasks--;
3548 if (mTaskHistory.get(taskNdx) != task) {
3549 // The entire task got removed, back up so we don't miss the next one.
3550 taskNdx--;
3551 }
3552 }
3553 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003554 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3555 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003556 return numReleased;
3557 }
3558
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 /**
3560 * Destroy the current CLIENT SIDE instance of an activity. This may be
3561 * called both when actually finishing an activity, or when performing
3562 * a configuration switch where we destroy the current client-side object
3563 * but then create a new client-side object for this same HistoryRecord.
3564 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003565 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003566 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3567 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003568 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003569 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003570 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003571 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003572
3573 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003574
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003575 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576
3577 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003578
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003579 if (hadApp) {
3580 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003581 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003582 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3583 mService.mHeavyWeightProcess = null;
3584 mService.mHandler.sendEmptyMessage(
3585 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3586 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003587 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003588 // Update any services we are bound to that might care about whether
3589 // their client may have activities.
3590 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003591 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003592 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003593 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 }
3595 }
3596
3597 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003598
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003599 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003600 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003601 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003602 r.configChangeFlags);
3603 } catch (Exception e) {
3604 // We can just ignore exceptions here... if the process
3605 // has crashed, our death notification will clean things
3606 // up.
3607 //Slog.w(TAG, "Exception thrown during finish", e);
3608 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003609 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003610 removedFromHistory = true;
3611 skipDestroy = true;
3612 }
3613 }
3614
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003616
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003617 // If the activity is finishing, we need to wait on removing it
3618 // from the list to give it a chance to do its cleanup. During
3619 // that time it may make calls back with its token so we need to
3620 // be able to find it on the list and so we don't want to remove
3621 // it from the list yet. Otherwise, we can just immediately put
3622 // it in the destroyed state since we are not removing it from the
3623 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003624 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003625 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003626 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003627 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003628 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003629 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3630 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003631 if (DEBUG_STATES) Slog.v(TAG_STATES,
3632 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003633 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003634 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003635 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 }
3637 } else {
3638 // remove this record from the history.
3639 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003640 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 removedFromHistory = true;
3642 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003643 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003644 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003645 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003646 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003647 }
3648 }
3649
3650 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003651
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003652 if (!mLRUActivities.remove(r) && hadApp) {
3653 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3654 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003655
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003656 return removedFromHistory;
3657 }
3658
Craig Mautner299f9602015-01-26 09:47:33 -08003659 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003660 final long origId = Binder.clearCallingIdentity();
3661 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003662 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003663 if (r != null) {
3664 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003665 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003666 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003667
Wale Ogunwale60454db2015-01-23 16:05:07 -08003668 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003669 if (r.state == ActivityState.DESTROYING) {
3670 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003671 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003672 }
3673 }
Craig Mautner05d29032013-05-03 13:40:13 -07003674 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003675 } finally {
3676 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003677 }
3678 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003679
Todd Kennedyaab56db2015-01-30 09:39:53 -08003680 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003681 if (hasVisibleBehindActivity() &&
3682 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003683 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003684 // Don't release the top activity if it has requested to run behind the next
3685 // activity.
3686 return;
3687 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003688 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003689 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003690 " thread=" + r.app.thread);
3691 if (r != null && r.app != null && r.app.thread != null) {
3692 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003693 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003694 } catch (RemoteException e) {
3695 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003696 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003697 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003698 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003699 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003700 }
3701 }
3702 }
3703
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003704 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003705 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3706 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003707 if (r != null) {
3708 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003709 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003710 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003711 }
3712 mStackSupervisor.resumeTopActivitiesLocked();
3713 }
3714
Jose Lima4b6c6692014-08-12 17:41:12 -07003715 boolean hasVisibleBehindActivity() {
3716 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003717 }
3718
Jose Lima4b6c6692014-08-12 17:41:12 -07003719 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003720 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003721 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003722 }
3723 }
3724
Jose Lima4b6c6692014-08-12 17:41:12 -07003725 ActivityRecord getVisibleBehindActivity() {
3726 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003727 }
3728
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003729 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3730 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003732 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3733 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003734 while (i > 0) {
3735 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003736 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003737 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003738 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003739 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003740 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003741 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742 }
3743 }
3744 }
3745
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003746 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3747 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003748 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3749 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003750 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3751 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003752 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003753 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003754 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3755 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003756
3757 boolean hasVisibleActivities = false;
3758
3759 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003760 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003761 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3762 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003763 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3764 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3765 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3766 final ActivityRecord r = activities.get(activityNdx);
3767 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003768 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3769 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003770 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003771 if (r.visible) {
3772 hasVisibleActivities = true;
3773 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003774 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003775 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3776 // Don't currently have state for the activity, or
3777 // it is finishing -- always remove it.
3778 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003779 } else if (!r.visible && r.launchCount > 2 &&
3780 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003781 // We have launched this activity too many times since it was
3782 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003783 // (Note if the activity is visible, we don't remove the record.
3784 // We leave the dead window on the screen but the process will
3785 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003786 remove = true;
3787 } else {
3788 // The process may be gone, but the activity lives on!
3789 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003790 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003791 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003792 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3793 "Removing activity " + r + " from stack at " + i
3794 + ": haveState=" + r.haveState
3795 + " stateNotNeeded=" + r.stateNotNeeded
3796 + " finishing=" + r.finishing
3797 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003798 if (!r.finishing) {
3799 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3800 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3801 r.userId, System.identityHashCode(r),
3802 r.task.taskId, r.shortComponentName,
3803 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003804 if (r.state == ActivityState.RESUMED) {
3805 mService.updateUsageStats(r, false);
3806 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003807 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003808 } else {
3809 // We have the current state for this activity, so
3810 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003811 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3812 if (DEBUG_APP) Slog.v(TAG_APP,
3813 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003814 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003815 // Set nowVisible to previous visible state. If the app was visible while
3816 // it died, we leave the dead window on screen so it's basically visible.
3817 // This is needed when user later tap on the dead window, we need to stop
3818 // other apps when user transfers focus to the restarted activity.
3819 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003820 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003821 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003822 "App died, clearing saved state of " + r);
3823 r.icicle = null;
3824 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003825 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003826 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003827 if (remove) {
3828 removeActivityFromHistoryLocked(r, "appDied");
3829 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003830 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003831 }
3832 }
3833
3834 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003835 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003836
Chong Zhang280d3322015-11-03 17:27:26 -08003837 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003838 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003839 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003840 if (r != null && r.state != ActivityState.RESUMED) {
3841 r.updateOptionsLocked(options);
3842 } else {
3843 ActivityOptions.abort(options);
3844 }
3845 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003846 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003847 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003848
Craig Mautner21d24a22014-04-23 11:45:37 -07003849 void updateTaskMovement(TaskRecord task, boolean toFront) {
3850 if (task.isPersistable) {
3851 task.mLastTimeMoved = System.currentTimeMillis();
3852 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3853 // recently will be most negative, tasks sent to the bottom before that will be less
3854 // negative. Similarly for recent tasks moved to the top which will be most positive.
3855 if (!toFront) {
3856 task.mLastTimeMoved *= -1;
3857 }
3858 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003859 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003860 }
3861
Craig Mautner84984fa2014-06-19 11:19:20 -07003862 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003863 final int top = mTaskHistory.size() - 1;
3864 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3865 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003866 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003867 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3868 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003869 mTaskHistory.remove(taskNdx);
3870 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003871 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003872 return;
3873 }
3874 }
3875 }
3876
Chong Zhang280d3322015-11-03 17:27:26 -08003877 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003878 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003879 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880
Craig Mautner11bf9a52013-02-19 14:08:51 -08003881 final int numTasks = mTaskHistory.size();
3882 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003883 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003884 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003885 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003886 ActivityOptions.abort(options);
3887 } else {
3888 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3889 }
3890 return;
3891 }
3892
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003893 if (timeTracker != null) {
3894 // The caller wants a time tracker associated with this task.
3895 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3896 tr.mActivities.get(i).appTimeTracker = timeTracker;
3897 }
3898 }
3899
Craig Mautner11bf9a52013-02-19 14:08:51 -08003900 // Shift all activities with this task up to the top
3901 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003902 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003903
Chong Zhang45c25ce2015-08-10 22:18:26 -07003904 // Don't refocus if invisible to current user
3905 ActivityRecord top = tr.getTopActivity();
3906 if (!okToShowLocked(top)) {
3907 addRecentActivityLocked(top);
3908 ActivityOptions.abort(options);
3909 return;
3910 }
3911
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003912 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003913 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003914 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003915
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003916 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003917 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003918 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 if (r != null) {
3920 mNoAnimActivities.add(r);
3921 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003922 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003923 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003924 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003925 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003926
Craig Mautner05d29032013-05-03 13:40:13 -07003927 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003928 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003929
3930 if (VALIDATE_TOKENS) {
3931 validateAppTokensLocked();
3932 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003933 }
3934
3935 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003936 * Worker method for rearranging history stack. Implements the function of moving all
3937 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003938 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003939 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003940 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3941 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003942 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003943 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003944 * @return Returns true if the move completed, false if not.
3945 */
Craig Mautner299f9602015-01-26 09:47:33 -08003946 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003947 final TaskRecord tr = taskForIdLocked(taskId);
3948 if (tr == null) {
3949 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3950 return false;
3951 }
3952
3953 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003954 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003955
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 // If we have a watcher, preflight the move before committing to it. First check
3957 // for *other* available tasks, but if none are available, then try again allowing the
3958 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003959 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003960 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003961 if (next == null) {
3962 next = topRunningActivityLocked(null, 0);
3963 }
3964 if (next != null) {
3965 // ask watcher if this is allowed
3966 boolean moveOK = true;
3967 try {
3968 moveOK = mService.mController.activityResuming(next.packageName);
3969 } catch (RemoteException e) {
3970 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003971 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003972 }
3973 if (!moveOK) {
3974 return false;
3975 }
3976 }
3977 }
3978
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003979 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003980
riddle_hsuc215a4f2014-12-27 12:10:45 +08003981 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003982
3983 // If true, we should resume the home activity next if the task we are moving to the
3984 // back is over the home stack. We force to false if the task we are moving to back
3985 // is the home task and we don't want it resumed after moving to the back.
3986 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3987 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003988 final TaskRecord nextTask = getNextTask(tr);
3989 if (nextTask != null) {
3990 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3991 } else {
3992 prevIsHome = true;
3993 }
3994 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003995 mTaskHistory.remove(tr);
3996 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003997 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003998
Craig Mautnerc8143c62013-09-03 12:15:57 -07003999 // There is an assumption that moving a task to the back moves it behind the home activity.
4000 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004001 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004002 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4003 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004004 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004005 break;
4006 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004007 if (taskNdx == 1) {
4008 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004009 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004010 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004011 }
4012
Craig Mautner299f9602015-01-26 09:47:33 -08004013 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004014 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004015
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004016 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004017 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004018 }
4019
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004020 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004021 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004022 if (!mService.mBooting && !mService.mBooted) {
4023 // Not ready yet!
4024 return false;
4025 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004026 final int taskToReturnTo = tr.getTaskToReturnTo();
4027 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004028 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004029 }
4030
Craig Mautner05d29032013-05-03 13:40:13 -07004031 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004032 return true;
4033 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004034
Craig Mautner8849a5e2013-04-02 16:41:03 -07004035 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004036 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004037 final Uri data = r.intent.getData();
4038 final String strData = data != null ? data.toSafeString() : null;
4039
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004040 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004041 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004042 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004043 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004044 }
4045
4046 /**
4047 * Make sure the given activity matches the current configuration. Returns
4048 * false if the activity had to be destroyed. Returns true if the
4049 * configuration is the same, or the activity will remain running as-is
4050 * for whatever reason. Ensures the HistoryRecord is updated with the
4051 * correct configuration and all other bookkeeping is handled.
4052 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004053 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4054 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004055 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004056 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004057 "Skipping config check (will change): " + r);
4058 return true;
4059 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004060
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004061 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004062 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004063
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004064 // Short circuit: if the two configurations are the exact same
4065 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004066 final Configuration newConfig = mService.mConfiguration;
4067 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004068 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004069 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004070 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004071 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004072 "Configuration unchanged in " + r);
4073 return true;
4074 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004076 // We don't worry about activities that are finishing.
4077 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004078 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004079 "Configuration doesn't matter in finishing " + r);
4080 r.stopFreezingScreenLocked(false);
4081 return true;
4082 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004083
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004084 // Okay we now are going to make this activity have the new config.
4085 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004086 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004087 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004088 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004089 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004090
Filip Gruszczynski23493322015-07-29 17:02:59 -07004091 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004092 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004093 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004094 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004095 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004096 // There are no significant differences, so we won't relaunch but should still deliver
4097 // the new configuration to the client process.
4098 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004099 return true;
4100 }
4101
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004102 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4103 "Configuration changes for " + r + " ; taskChanges="
4104 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4105 + Configuration.configurationDiffToString(changes));
4106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004107 // If the activity isn't currently running, just leave the new
4108 // configuration and it will pick that up next time it starts.
4109 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004110 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004111 "Configuration doesn't matter not running " + r);
4112 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004113 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004114 return true;
4115 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004116
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004117 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004118 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4119 "Checking to restart " + r.info.name + ": changed=0x"
4120 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004121 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4122 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004123
Dianne Hackborne6676352011-06-01 16:51:20 -07004124 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004125 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4126 r.configChangeFlags |= changes;
4127 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004128 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004129 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004130 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004131 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004132 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004133 } else if (r.state == ActivityState.PAUSING) {
4134 // A little annoying: we are waiting for this activity to
4135 // finish pausing. Let's not do anything now, but just
4136 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004137 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004138 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004139 r.configDestroy = true;
4140 return true;
4141 } else if (r.state == ActivityState.RESUMED) {
4142 // Try to optimize this case: the configuration is changing
4143 // and we need to restart the top, resumed activity.
4144 // Instead of doing the normal handshaking, just say
4145 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004146 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004147 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004148 relaunchActivityLocked(r, r.configChangeFlags, true,
4149 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004150 r.configChangeFlags = 0;
4151 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004152 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004153 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004154 relaunchActivityLocked(r, r.configChangeFlags, false,
4155 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004156 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004157 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004158
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004159 // All done... tell the caller we weren't able to keep this
4160 // activity around.
4161 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004162 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004163
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004164 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004165 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004166 // changes is always sent to all processes when they happen so it can just use whatever
4167 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004168 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004169 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004170
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004171 return true;
4172 }
4173
Filip Gruszczynski23493322015-07-29 17:02:59 -07004174 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4175 Configuration oldTaskOverride) {
4176 // Determine what has changed. May be nothing, if this is a config
4177 // that has come back from the app after going idle. In that case
4178 // we just want to leave the official config object now in the
4179 // activity and do nothing else.
4180 int taskChanges = oldTaskOverride.diff(taskConfig);
4181 // We don't want to use size changes if they don't cross boundaries that are important to
4182 // the app.
4183 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4184 final boolean crosses = record.crossesHorizontalSizeThreshold(
4185 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4186 || record.crossesVerticalSizeThreshold(
4187 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4188 if (!crosses) {
4189 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4190 }
4191 }
4192 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4193 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4194 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004195 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004196 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4197 }
4198 }
4199 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4200 }
4201
4202 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4203 Configuration oldTaskOverride, int taskChanges) {
4204 if (taskChanges == 0) {
4205 // {@link Configuration#diff} doesn't catch changes from unset values.
4206 // Check for changes we care about.
4207 if (oldTaskOverride.orientation != taskConfig.orientation) {
4208 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4209 }
4210 // We want to explicitly track situations where the size configuration goes from
4211 // undefined to defined. We don't care about crossing the threshold in that case,
4212 // because there is no threshold.
4213 final int oldHeight = oldTaskOverride.screenHeightDp;
4214 final int newHeight = taskConfig.screenHeightDp;
4215 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4216 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4217 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4218 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4219 }
4220 final int oldWidth = oldTaskOverride.screenWidthDp;
4221 final int newWidth = taskConfig.screenWidthDp;
4222 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4223 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4224 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4225 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4226 }
4227 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4228 final int newSmallest = taskConfig.smallestScreenWidthDp;
4229 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4230 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4231 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4232 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4233 }
4234 }
4235 return taskChanges;
4236 }
4237
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004238 private static boolean isResizeOnlyChange(int change) {
4239 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4240 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4241 }
4242
Wale Ogunwale83301a92015-09-24 15:54:08 -07004243 private void relaunchActivityLocked(
4244 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4245 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4246 return;
4247 }
4248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004249 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004250 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004251 if (andResume) {
4252 results = r.results;
4253 newIntents = r.newIntents;
4254 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004255 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4256 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004257 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004258 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004259 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004260 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004261
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004262 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004263
Craig Mautner34b73df2014-01-12 21:11:08 -08004264 mStackSupervisor.removeChildActivityContainers(r);
4265
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004266 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004267 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4268 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004269 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004270 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4271 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004272 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004273 // Note: don't need to call pauseIfSleepingLocked() here, because
4274 // the caller will only pass in 'andResume' if this activity is
4275 // currently resumed, which implies we aren't sleeping.
4276 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004277 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004278 }
4279
4280 if (andResume) {
4281 r.results = null;
4282 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004283 r.state = ActivityState.RESUMED;
4284 } else {
4285 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4286 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004288 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004289
4290 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004291 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4292 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4293 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4294 final ActivityRecord r = activities.get(activityNdx);
4295 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004296 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004297 }
4298 if (r.fullscreen && !r.finishing) {
4299 return false;
4300 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004301 }
4302 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004303 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004304 if (r == null) {
4305 return false;
4306 }
4307 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4308 + " would have returned true for r=" + r);
4309 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004310 }
4311
4312 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004313 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4314 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4315 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4316 final ActivityRecord r = activities.get(activityNdx);
4317 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004318 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004319 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004320 }
4321 }
4322 }
4323
Wale Ogunwale540e1232015-05-01 15:35:39 -07004324 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4325 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004326 boolean didSomething = false;
4327 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004328 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004329 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4330 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4331 int numActivities = activities.size();
4332 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4333 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004334 final boolean sameComponent =
4335 (r.packageName.equals(packageName) && (filterByClasses == null
4336 || filterByClasses.contains(r.realActivity.getClassName())))
4337 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004338 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004339 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004340 && (r.app == null || evenPersistent || !r.app.persistent)) {
4341 if (!doit) {
4342 if (r.finishing) {
4343 // If this activity is just finishing, then it is not
4344 // interesting as far as something to stop.
4345 continue;
4346 }
4347 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004348 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004349 if (r.isHomeActivity()) {
4350 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4351 Slog.i(TAG, "Skip force-stop again " + r);
4352 continue;
4353 } else {
4354 homeActivity = r.realActivity;
4355 }
4356 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004357 didSomething = true;
4358 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004359 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004360 if (r.app != null) {
4361 r.app.removed = true;
4362 }
4363 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004364 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004365 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004366 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4367 true)) {
4368 // r has been deleted from mActivities, accommodate.
4369 --numActivities;
4370 --activityNdx;
4371 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004372 }
4373 }
4374 }
4375 return didSomething;
4376 }
4377
Dianne Hackborn09233282014-04-30 11:33:59 -07004378 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004379 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4380 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004382 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004383 if (task.getTopActivity() == null) {
4384 continue;
4385 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004386 ActivityRecord r = null;
4387 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004388 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004389 int numActivities = 0;
4390 int numRunning = 0;
4391 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004392 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004393 continue;
4394 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004395 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004396 tmp = activities.get(activityNdx);
4397 if (tmp.finishing) {
4398 continue;
4399 }
4400 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004401
Craig Mautneraab647e2013-02-28 16:31:36 -08004402 // Initialize state for next task if needed.
4403 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4404 top = r;
4405 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004406 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004407
4408 // Add 'r' into the current task.
4409 numActivities++;
4410 if (r.app != null && r.app.thread != null) {
4411 numRunning++;
4412 }
4413
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004414 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004415 TAG, r.intent.getComponent().flattenToShortString()
4416 + ": task=" + r.task);
4417 }
4418
4419 RunningTaskInfo ci = new RunningTaskInfo();
4420 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004421 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004422 ci.baseActivity = r.intent.getComponent();
4423 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004424 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004425 if (focusedStack && topTask) {
4426 // Give the latest time to ensure foreground task can be sorted
4427 // at the first, because lastActiveTime of creating task is 0.
4428 ci.lastActiveTime = System.currentTimeMillis();
4429 topTask = false;
4430 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004431
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004432 if (top.task != null) {
4433 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004434 }
4435 ci.numActivities = numActivities;
4436 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004437 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004438 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004439 }
4440
4441 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004442 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004443 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004444 if (top >= 0) {
4445 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4446 int activityTop = activities.size() - 1;
4447 if (activityTop > 0) {
4448 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4449 "unhandled-back", true);
4450 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004451 }
4452 }
4453
Craig Mautner6b74cb52013-09-27 17:02:21 -07004454 /**
4455 * Reset local parameters because an app's activity died.
4456 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004457 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004458 */
4459 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004460 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004461 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004462 "App died while pausing: " + mPausingActivity);
4463 mPausingActivity = null;
4464 }
4465 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4466 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004467 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004468 }
4469
Craig Mautner19091252013-10-05 00:03:53 -07004470 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004471 }
4472
Craig Mautnercae015f2013-02-08 14:31:27 -08004473 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004474 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4475 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4476 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4477 final ActivityRecord r = activities.get(activityNdx);
4478 if (r.app == app) {
4479 Slog.w(TAG, " Force finishing activity "
4480 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004481 // Force the destroy to skip right to removal.
4482 r.app = null;
4483 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004484 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004485 }
4486 }
4487 }
4488
Dianne Hackborn390517b2013-05-30 15:03:32 -07004489 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004490 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004491 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004492 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4493 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004494 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4495 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004496 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004497 " Task id #" + task.taskId + "\n" +
4498 " mFullscreen=" + task.mFullscreen + "\n" +
4499 " mBounds=" + task.mBounds + "\n" +
4500 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004501 if (printed) {
4502 header = null;
4503 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004504 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004505 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004506 }
4507
4508 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4509 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4510
4511 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004512 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4513 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004514 }
4515 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004516 final int top = mTaskHistory.size() - 1;
4517 if (top >= 0) {
4518 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4519 int listTop = list.size() - 1;
4520 if (listTop >= 0) {
4521 activities.add(list.get(listTop));
4522 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004523 }
4524 } else {
4525 ItemMatcher matcher = new ItemMatcher();
4526 matcher.build(name);
4527
Craig Mautneraab647e2013-02-28 16:31:36 -08004528 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4529 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4530 if (matcher.match(r1, r1.intent.getComponent())) {
4531 activities.add(r1);
4532 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004533 }
4534 }
4535 }
4536
4537 return activities;
4538 }
4539
4540 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004541 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004542
4543 // All activities that came from the package must be
4544 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004545 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4546 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4547 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4548 final ActivityRecord a = activities.get(activityNdx);
4549 if (a.info.packageName.equals(packageName)) {
4550 a.forceNewConfig = true;
4551 if (starting != null && a == starting && a.visible) {
4552 a.startFreezingScreenLocked(starting.app,
4553 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4554 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004555 }
4556 }
4557 }
4558
4559 return starting;
4560 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004561
Craig Mautner299f9602015-01-26 09:47:33 -08004562 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004563 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004564 }
4565
Wale Ogunwale000957c2015-04-03 08:19:12 -07004566 /**
4567 * Removes the input task from this stack.
4568 * @param task to remove.
4569 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004570 * @param moving task to another stack. In the case we are moving we don't want to perform
4571 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004572 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004573 void removeTask(TaskRecord task, String reason, boolean moving) {
4574 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004575 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004576 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004577 if (!StackId.persistTaskBounds(mStackId)) {
4578 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4579 // default configuration the next time it launches.
4580 task.updateOverrideConfiguration(null);
4581 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004582 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004583
Craig Mautner04a0ea62014-01-13 12:51:26 -08004584 final ActivityRecord r = mResumedActivity;
4585 if (r != null && r.task == task) {
4586 mResumedActivity = null;
4587 }
4588
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004589 final int taskNdx = mTaskHistory.indexOf(task);
4590 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004591 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4592 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4593 if (!nextTask.isOverHomeStack()) {
4594 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4595 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004596 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004597 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004598 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004599
Wale Ogunwale040b4702015-08-06 18:10:50 -07004600 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004601 final boolean isVoiceSession = task.voiceSession != null;
4602 if (isVoiceSession) {
4603 try {
4604 task.voiceSession.taskFinished(task.intent, task.taskId);
4605 } catch (RemoteException e) {
4606 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004607 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004608 if (task.autoRemoveFromRecents() || isVoiceSession) {
4609 // Task creator asked to remove this when done, or this task was a voice
4610 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004611 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004612 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004613 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004614 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004615
4616 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004617 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004618 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004619 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004620 String myReason = reason + " leftTaskHistoryEmpty";
4621 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004622 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004623 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004624 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004625 if (mStacks != null) {
4626 mStacks.remove(this);
4627 mStacks.add(0, this);
4628 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004629 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004630 mActivityContainer.onTaskListEmptyLocked();
4631 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004632 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004633
4634 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004635 }
4636
Dianne Hackborn91097de2014-04-04 18:02:06 -07004637 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4638 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4639 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004640 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4641 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004642 // add the task to stack first, mTaskPositioner might need the stack association
4643 addTask(task, toTop, false);
Wale Ogunwale935e5022015-11-10 12:36:10 -08004644 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004645 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004646 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004647 return task;
4648 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004649
Wale Ogunwale935e5022015-11-10 12:36:10 -08004650 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4651 if (mTaskPositioner == null) {
4652 return false;
4653 }
4654 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4655 return true;
4656 }
4657
Craig Mautnerc00204b2013-03-05 15:02:14 -08004658 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004659 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004660 }
4661
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004662 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004663 task.stack = this;
4664 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004665 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004666 } else {
4667 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004668 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004669 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004670 if (!moving && task.voiceSession != null) {
4671 try {
4672 task.voiceSession.taskStarted(task.intent, task.taskId);
4673 } catch (RemoteException e) {
4674 }
4675 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004676 }
4677
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004678 void positionTask(final TaskRecord task, int position, boolean moving) {
4679 task.stack = this;
4680 insertTaskAtPosition(task, position);
4681 if (!moving && task.voiceSession != null) {
4682 try {
4683 task.voiceSession.taskStarted(task.intent, task.taskId);
4684 } catch (RemoteException e) {
4685 }
4686 }
4687 }
4688
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004689 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004690 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004691 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004692 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4693 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4694 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4695 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004696 bounds, task.mOverrideConfig, !r.isHomeActivity());
Chong Zhangb15758a2015-11-17 12:12:03 -08004697 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004698 r.taskConfigOverride = task.mOverrideConfig;
4699 }
4700
Wale Ogunwale079a0042015-10-24 11:44:07 -07004701 void setFocusAndResumeStateIfNeeded(
4702 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4703 // If the activity had focus before move focus to this stack.
4704 if (setFocus) {
4705 // If the activity owns the last resumed activity, transfer that together,
4706 // so that we don't resume the same activity again in the new stack.
4707 // Apps may depend on onResume()/onPause() being called in pairs.
4708 if (setResume) {
4709 mResumedActivity = r;
4710 // Move the stack in which we are placing the activity to the front. We don't use
4711 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4712 // already focused, the call will short-circuit and do nothing.
4713 moveToFront(reason);
4714 } else {
4715 // We need to not only move the stack to the front, but also have the activity
4716 // focused. This will achieve both goals.
4717 mService.setFocusedActivityLocked(r, reason);
4718 }
4719 }
4720 }
4721
4722 /**
4723 * Moves the input activity from its current stack to this one.
4724 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4725 * created on this stack which the activity is added to.
4726 * */
4727 void moveActivityToStack(ActivityRecord r) {
4728 final ActivityStack prevStack = r.task.stack;
4729 if (prevStack.mStackId == mStackId) {
4730 // You are already in the right stack silly...
4731 return;
4732 }
4733
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004734 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004735 && (mStackSupervisor.topRunningActivityLocked() == r);
4736 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4737
4738 final TaskRecord task = createTaskRecord(
4739 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4740 r.setTask(task, null);
4741 task.addActivityToTop(r);
4742 setAppTask(r, task);
4743 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4744 }
4745
Wale Ogunwale706ed792015-08-02 10:29:44 -07004746 private void setAppTask(ActivityRecord r, TaskRecord task) {
4747 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004748 task.updateOverrideConfiguration(bounds);
4749 mWindowManager.setAppTask(
Wale Ogunwale2998eef2015-12-02 19:46:29 -08004750 r.appToken, task.taskId, mStackId, task.getLaunchBounds(), task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004751 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004752 r.taskConfigOverride = task.mOverrideConfig;
4753 }
4754
Craig Mautnerc00204b2013-03-05 15:02:14 -08004755 public int getStackId() {
4756 return mStackId;
4757 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004758
4759 @Override
4760 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004761 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4762 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004763 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004764
Craig Mautner15df08a2015-04-01 12:17:18 -07004765 void onLockTaskPackagesUpdatedLocked() {
4766 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4767 mTaskHistory.get(taskNdx).setLockTaskAuth();
4768 }
4769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004770}